Changeset 1615946 in tol for tol_pkg/GraphTools
- Timestamp:
- Nov 12, 2009, 9:26:11 PM (17 years ago)
- Branches:
- 1941-docker-image-to-test, devel, master
- Children:
- af628fe
- Parents:
- 789dc8a
- File:
-
- 1 edited
-
tol_pkg/GraphTools/GraphTools.tol (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
tol_pkg/GraphTools/GraphTools.tol
r789dc8a r1615946 1 1 /* -*- mode: C++ -*- */ 2 2 ////////////////////////////////////////////////////////////////////////////// 3 // FILE : graphtools.tol4 // PURPOSE: Defines NameBlockGraphTools3 // FILE : GraphTools.tol 4 // PURPOSE: Defines Package GraphTools 5 5 ////////////////////////////////////////////////////////////////////////////// 6 6 … … 9 9 ////////////////////////////////////////////////////////////////////////////// 10 10 [[ 11 #Require MatEncode; 12 #Require MatQuery; 11 13 12 14 Text _.autodoc.description = "Functions and classes related with Graph " … … 22 24 23 25 ////////////////////////////////////////////////////////////////////////////// 24 Text _.autodoc.member.Append.Indexed = "Adds and index a new element to a "25 "set and returns 0 if itŽs all right. If an element with given name exists "26 "already then returns its index, else returns 0";27 Real Append.Indexed(Set set, Anything newElement, Text name)28 //////////////////////////////////////////////////////////////////////////////29 {30 Real idx = FindIndexByName(set, name );31 If(idx != 0,32 {33 WriteLn("[GraphTools::Append.Indexed] Cannot add an existant element "+34 Name(set)+"["<<idx+"] with name "+ name,"E");35 036 },37 {38 Set aux = [[ Copy(newElement) ]];39 Anything PutName(name, aux[1]);40 Set Append(set, aux, True);41 idx42 })43 };44 45 //////////////////////////////////////////////////////////////////////////////46 Class @Feature.Abstract47 //////////////////////////////////////////////////////////////////////////////48 {49 Static Text _.autodoc.description = "Class for handling with abstract "50 "features of arbitrary entities, numerical or not, that will be stored in "51 "an external matrix with a row for each entity.";52 //The name of the feature53 Text _.name;54 //The number of the feature in the full set of features55 Real _.position;56 57 ////////////////////////////////////////////////////////////////////////////58 Static Text _.autodoc.member.get = "Returns the stored value corresponding "59 "to this feature related to an individual entity.";60 Anything get(Matrix store, Real entity)61 ////////////////////////////////////////////////////////////////////////////62 };63 64 //////////////////////////////////////////////////////////////////////////////65 Class @Feature.Numeric : @Feature.Abstract66 //////////////////////////////////////////////////////////////////////////////67 {68 Static Text _.autodoc.description = "Class for handling with numerical "69 "numerical features of arbitrary entities that will be stored in an "70 "external matrix with a row for each entity.";71 72 ////////////////////////////////////////////////////////////////////////////73 Static Text _.autodoc.member.New = "Creates a new instance of "74 "Class @Feature.Numeric";75 Static @Feature.Numeric New(Text name, Real position)76 ////////////////////////////////////////////////////////////////////////////77 {78 @Feature.Numeric aux =79 [[80 Text _.name = name;81 Real _.position = position82 ]]83 };84 85 ////////////////////////////////////////////////////////////////////////////86 Anything get(Matrix store, Real entity)87 ////////////////////////////////////////////////////////////////////////////88 {89 MatDat(store, entity, _.position)90 }91 };92 93 //////////////////////////////////////////////////////////////////////////////94 Class @Feature.NonNumeric : @Feature.Abstract95 //////////////////////////////////////////////////////////////////////////////96 {97 Static Text _.autodoc.description = "Class for handling with numerical "98 "numerical features of arbitrary entities that will be stored in an "99 "external matrix with a row for each entity. Here the stored value is the "100 "index of the feature stored in an arbitrary domain.";101 102 //The set of valid values for this feature indexed by name103 Set _.domain;104 105 ////////////////////////////////////////////////////////////////////////////106 Static Text _.autodoc.member.New = "Creates a new instance of "107 "Class @Feature.NonNumeric";108 Static @Feature.NonNumeric New(Text name, Real position, Set domain)109 ////////////////////////////////////////////////////////////////////////////110 {111 @Feature.NonNumeric aux =112 [[113 Text _.name = name;114 Real _.position = position;115 Set _.domain = domain116 ]];117 Real SetIndexByName(aux::_.domain);118 aux119 };120 121 ////////////////////////////////////////////////////////////////////////////122 Anything get(Matrix store, Real entity)123 ////////////////////////////////////////////////////////////////////////////124 {125 Real idx = MatDat(store, entity, _.position);126 _.domain[idx]127 }128 129 };130 131 //////////////////////////////////////////////////////////////////////////////132 Class @FeatureSet133 //////////////////////////////////////////////////////////////////////////////134 {135 //Stores and index by name a set of instances od @Feature.Abstract136 Set _.feature = Copy(Empty);137 //In each row of this matrix are stored the values or indexes of features138 //related to an individual entity139 Matrix _.data = Rand(0,0,0,0);140 141 ////////////////////////////////////////////////////////////////////////////142 Static Text _.autodoc.member.New = "Creates a new instance of "143 "Class @FeatureSet";144 Static @FeatureSet New(Real void)145 ////////////////////////////////////////////////////////////////////////////146 {147 @FeatureSet aux148 };149 150 ////////////////////////////////////////////////////////////////////////////151 Static Text _.autodoc.member.add.feature.numeric =152 "Adds a new numerical feature";153 Real add.feature.numeric(Text name)154 ////////////////////////////////////////////////////////////////////////////155 {156 GraphTools::Append.Indexed(157 _.feature,158 @Feature.Numeric::New(name, Card(_.feature)+1),159 name)160 };161 162 ////////////////////////////////////////////////////////////////////////////163 Static Text _.autodoc.member.add.feature.nonnumeric=164 "Adds a new non numerical feature";165 Real add.feature.nonnumeric(Text name, Set domain)166 ////////////////////////////////////////////////////////////////////////////167 {168 GraphTools::Append.Indexed(169 _.feature,170 @Feature.NonNumeric::New(name, Card(_.feature)+1, domain),171 name)172 };173 174 ////////////////////////////////////////////////////////////////////////////175 Text _.autodoc.member.feature.index = "Returns the index of a feature or 0 "176 "if it not exists";177 Real feature.index(Text name)178 ////////////////////////////////////////////////////////////////////////////179 {180 FindIndexByName(_.feature, name )181 };182 183 ////////////////////////////////////////////////////////////////////////////184 Text _.autodoc.member.get = "Returns the value of a feature for an entity";185 Anything get(Real entity,186 Anything feature) //Real position or Text name187 ////////////////////////////////////////////////////////////////////////////188 {189 @Feature.Abstract f = _.feature[feature];190 f::get(_.data, entity)191 };192 193 ////////////////////////////////////////////////////////////////////////////194 Text _.autodoc.member.get.all = "Returns a set with the values of all "195 "features for an entity";196 Set get.all(Real entity)197 ////////////////////////////////////////////////////////////////////////////198 {199 EvalSet(_.feature, Anything get.all.cycle(@FeatureSet f)200 {201 f::get(_.data, entity)202 })203 }204 205 };206 207 208 //////////////////////////////////////////////////////////////////////////////209 26 Class @Graph 210 27 ////////////////////////////////////////////////////////////////////////////// … … 222 39 Real _.directed; 223 40 //Features of vertices. First feature must be the identifier of vertices 224 @FeatureSet _.vertex =@FeatureSet::New(?);41 MatEncode::@FeatureSet _.vertex = MatEncode::@FeatureSet::New(?); 225 42 //Number of vertices 226 43 Real _.vertex.num = 0; 227 44 //Features of edges. First two features must be input and output vertex 228 @FeatureSet _.edge =@FeatureSet::New(?);45 MatEncode::@FeatureSet _.edge = MatEncode::@FeatureSet::New(?); 229 46 //Number of edges 230 47 Real _.edge.num = 0; 231 48 //Sparse matrix storing index of edge for each cell (out,in) 232 49 VMatrix _.edge.map = Rand(0,0,0,0); 233 VMatrix _.edge.adjacency = Rand(0,0,0,0); 50 VMatrix _.edge.adjacency.out = Rand(0,0,0,0); 51 VMatrix _.edge.adjacency.in = Rand(0,0,0,0); 234 52 VMatrix _.edge.repeated = Rand(0,0,0,0); 235 53 Real _.edge.check.repeated = ?; … … 281 99 VMatrix triplet.rep = Triplet(outInRep,_.vertex.num,_.vertex.num); 282 100 VMatrix _.edge.repeated := Convert(triplet.rep,"Cholmod.R.Sparse"); 283 VMatrix _.edge.adjacency := Or(_.edge.repeated, Zeros(_.vertex.num)); 284 VMatrix _.edge.repeated := _.edge.repeated - _.edge.adjacency; 101 VMatrix _.edge.adjacency.out := Or(_.edge.repeated, Zeros(_.vertex.num)); 102 VMatrix _.edge.adjacency.in := Tra(_.edge.adjacency.out); 103 VMatrix _.edge.repeated := _.edge.repeated - _.edge.adjacency.out; 285 104 Real _.edge.check.repeated := VMatSum(_.edge.repeated); 286 105 Code funTriplet = Case( … … 308 127 VMatrix triplet.map = funTriplet(out|in|idx,_.vertex.num,_.vertex.num); 309 128 VMatrix _.edge.map := Convert(triplet.map,"Cholmod.R.Sparse"); 129 VMatrix _.edge.map.t := Tra(_.edge.map); 310 130 True 311 131 }; … … 409 229 //////////////////////////////////////////////////////////////////////////// 410 230 Text _.autodoc.member.get.edge.feature = "Returns specified feature for " 411 "an edge. Feature identifier can be the name (Text) or the position (Real ";231 "an edge. Feature identifier can be the name (Text) or the position (Real)"; 412 232 Anything get.edge.feature( 413 233 Real edge.idx, … … 416 236 { 417 237 _.edge::get(edge.idx, feature.id) 238 }; 239 240 //////////////////////////////////////////////////////////////////////////// 241 Text _.autodoc.member.vertex.in.degree = "Returns the number of input " 242 "vertices in edges whose output is the specified vertex"; 243 //////////////////////////////////////////////////////////////////////////// 244 Real vertex.in.degree(Real vertex.index) 245 { 246 VMatSum(SubCol(_.edge.adjacency.in,[[vertex.index]])) 247 }; 248 249 //////////////////////////////////////////////////////////////////////////// 250 Text _.autodoc.member.vertex.out.degree = "Returns the number of output " 251 "vertices in edges whose input is the specified vertex"; 252 //////////////////////////////////////////////////////////////////////////// 253 Real vertex.out.degree(Real vertex.index) 254 { 255 VMatSum(SubCol(_.edge.adjacency.out,[[vertex.index]])) 256 }; 257 258 Real vertex.in.degree(Real vertex.index) 259 { 260 VMatSum(SubCol(_.edge.adjacency.out,[[vertex.inde]])) 418 261 } 419 262 … … 430 273 //////////////////////////////////////////////////////////////////////////// 431 274 432 //////////////////////////////////////////////////////////////////////////// 433 Devuelve los vértices adyacentes de salida de orden dado para el vértice 434 especificado. V -> ... (n-1) ... -> U 435 Set get.adyacency.output(Real V, Real order) 436 //////////////////////////////////////////////////////////////////////////// 437 438 //////////////////////////////////////////////////////////////////////////// 439 Devuelve los vértices adyacentes U de entrada de orden dado para el vértice 440 especificado. U -> ... (n-1) ... -> V 441 Set get.adyacency.input(Real V, Real order) 442 //////////////////////////////////////////////////////////////////////////// 275 //////////////////////////////////////////////////////////////////////////// 276 Text _.autodoc.member.get.adyacent.input = "Returns the set of vertices " 277 "U which are input adyacent with specified order n of a given output " 278 "vertex V \n" 279 " V -> ... (n-1) ... -> U \n"; 280 Set get.input.order(Real V, Real n) 281 //////////////////////////////////////////////////////////////////////////// 282 { 283 VMatrix match = SubCol(_.edge.adjacency.out, [[V]]); 284 Matrix select = VMat2Triplet(Convert(match,"Cholmod.R.Triplet")); 285 If(!Rows(select),Copy(Empty), MatSet(Tra(SubCol(select,[[1]])))[1]) 286 }; 287 288 //////////////////////////////////////////////////////////////////////////// 289 Text _.autodoc.member.get.adyacent.output = "Returns the set of vertices " 290 "U which are output adyacent with specified order n of a given input " 291 "vertex V \n" 292 " U -> ... (n-1) ... -> V \n"; 293 Set get.output.order(Real V, Real n) 294 //////////////////////////////////////////////////////////////////////////// 295 { 296 VMatrix match = SubCol(_.edge.adjacency.in, [[V]]); 297 Matrix select = VMat2Triplet(Convert(match,"Cholmod.R.Triplet")); 298 If(!Rows(select),Copy(Empty), MatSet(Tra(SubCol(select,[[1]])))[1]) 299 } 443 300 444 301 */
Note: See TracChangeset
for help on using the changeset viewer.
