Changeset 1615946 in tol


Ignore:
Timestamp:
Nov 12, 2009, 9:26:11 PM (17 years ago)
Author:
Víctor de Buen <vdebuen@…>
Branches:
1941-docker-image-to-test, devel, master
Children:
af628fe
Parents:
789dc8a
Message:

Reorganization of members in specialized packages MatEncode and MatEncode
New methods vertex.in.degree and vertex.out.degree

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@1691 be4846ae-f051-483a-831f-b64e7d08527f

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tol_pkg/GraphTools/GraphTools.tol

    r789dc8a r1615946  
    11/* -*- mode: C++ -*- */
    22//////////////////////////////////////////////////////////////////////////////
    3 // FILE   : graphtools.tol
    4 // PURPOSE: Defines NameBlock GraphTools
     3// FILE   : GraphTools.tol
     4// PURPOSE: Defines Package GraphTools
    55//////////////////////////////////////////////////////////////////////////////
    66
    … …  
    99//////////////////////////////////////////////////////////////////////////////
    1010[[
     11#Require MatEncode;
     12#Require MatQuery;
    1113
    1214Text _.autodoc.description = "Functions and classes related with Graph "
    … …  
    2224
    2325//////////////////////////////////////////////////////////////////////////////
    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     0
    36   },
    37   {
    38     Set aux =  [[ Copy(newElement) ]];
    39     Anything PutName(name, aux[1]);
    40     Set Append(set, aux, True);
    41     idx
    42   })
    43 };
    44 
    45 //////////////////////////////////////////////////////////////////////////////
    46 Class @Feature.Abstract
    47 //////////////////////////////////////////////////////////////////////////////
    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 feature
    53   Text _.name;
    54   //The number of the feature in the full set of features
    55   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.Abstract
    66 //////////////////////////////////////////////////////////////////////////////
    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  = position
    82     ]]
    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.Abstract
    95 //////////////////////////////////////////////////////////////////////////////
    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 name
    103   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 = domain   
    116     ]];
    117     Real SetIndexByName(aux::_.domain);
    118     aux
    119   };
    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 @FeatureSet
    133 //////////////////////////////////////////////////////////////////////////////
    134 {
    135   //Stores and index by name a set of instances od @Feature.Abstract
    136   Set _.feature = Copy(Empty);
    137   //In each row of this matrix are stored the values or indexes of features
    138   //related to an individual entity
    139   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 aux
    148   };
    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 name
    187   ////////////////////////////////////////////////////////////////////////////
    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 //////////////////////////////////////////////////////////////////////////////
    20926Class @Graph
    21027//////////////////////////////////////////////////////////////////////////////
    … …  
    22239  Real _.directed;
    22340  //Features of vertices. First feature must be the identifier of vertices
    224   @FeatureSet _.vertex = @FeatureSet::New(?);
     41  MatEncode::@FeatureSet _.vertex = MatEncode::@FeatureSet::New(?);
    22542  //Number of vertices
    22643  Real _.vertex.num = 0; 
    22744  //Features of edges. First two features must be input and output vertex
    228   @FeatureSet _.edge = @FeatureSet::New(?);
     45  MatEncode::@FeatureSet _.edge = MatEncode::@FeatureSet::New(?);
    22946  //Number of edges
    23047  Real _.edge.num = 0; 
    23148  //Sparse matrix storing index of edge for each cell (out,in)
    23249  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);
    23452  VMatrix _.edge.repeated = Rand(0,0,0,0);
    23553  Real _.edge.check.repeated = ?;
    … …  
    28199    VMatrix triplet.rep = Triplet(outInRep,_.vertex.num,_.vertex.num);
    282100    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;
    285104    Real _.edge.check.repeated := VMatSum(_.edge.repeated);
    286105    Code funTriplet = Case(
    … …  
    308127    VMatrix triplet.map = funTriplet(out|in|idx,_.vertex.num,_.vertex.num);
    309128    VMatrix _.edge.map := Convert(triplet.map,"Cholmod.R.Sparse");
     129    VMatrix _.edge.map.t := Tra(_.edge.map);
    310130    True
    311131  };
    … …  
    409229  ////////////////////////////////////////////////////////////////////////////
    410230  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)";
    412232  Anything get.edge.feature(
    413233    Real edge.idx,
    … …  
    416236  {
    417237    _.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]]))
    418261  }
    419262
    … …  
    430273////////////////////////////////////////////////////////////////////////////
    431274
    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  }
    443300
    444301*/
Note: See TracChangeset for help on using the changeset viewer.