source: tol/tol_pkg/MatQuery/MatQuery.tol @ 345a4d7

1941-docker-image-to-testdevel
Last change on this file since 345a4d7 was 345a4d7, checked in by Víctor de Buen <vdebuen@…>, 17 years ago

New method MatchRowsFullKnown
New private method _checkColumnVector
New member Real _.packSparsity

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

  • Property mode set to 100644
File size: 8.1 KB
RevLine 
[4ddca89]1/* -*- mode: C++ -*- */
2//////////////////////////////////////////////////////////////////////////////
3// FILE   : MatQuery.tol
4// PURPOSE: Defines package MatQuery
5//////////////////////////////////////////////////////////////////////////////
6
7NameBlock MatQuery = [[
8
9Text _.autodoc.description = "Functions and classes related with efficient "
10"selection and classification methods over large matricial data.";
11Text _.autodoc.url = "";
12Set _.autodoc.keys = [["select","classify"]];
13Set _.autodoc.authors = [[
14   "vdebuen@tol-project.org"   ]];
15
[345a4d7]16//It will be passed to built-in function Pack to convert to the best storage
17//Default value is set to unknown that will ensure minimum space
18Real _.packSparsity = ?;
19
20//////////////////////////////////////////////////////////////////////////////
21Real _checkColumnVector(Text method, VMatrix M)
22//////////////////////////////////////////////////////////////////////////////
23{
24  If(VColumns(M)==1,True,
25  {
26    WriteLn("[MatQuery::"+method+"] a column matrix was "
27    "expected instead of ("<<VRows(M)+"x"+VColumns(M)+")","E");
28    False
29  })
30};
[59dc492]31
32//////////////////////////////////////////////////////////////////////////////
33Text _.autodoc.member.SelectMatch =
34"Returns the rows where thecolumn matrix 'match' has non zero value";
35Set SelectMatch(VMatrix match)
36//////////////////////////////////////////////////////////////////////////////
37{
[345a4d7]38  If(!_checkColumnVector("SelectMatch",match),Copy(Empty), {
[59dc492]39  Matrix select = VMat2Triplet(Convert(match,"Cholmod.R.Triplet"));
40  If(!Rows(select),Copy(Empty), MatSet(Tra(SubCol(select,[[1]])))[1])
[345a4d7]41})};
[59dc492]42
[345a4d7]43//////////////////////////////////////////////////////////////////////////////
44Text _.autodoc.member.MatchRowsFullKnown =
45"Returns a column matrix with True in rows where all cells have konwn values";
46VMatrix MatchRowsFullKnown(VMatrix M)
47//////////////////////////////////////////////////////////////////////////////
48{
49  Real m = VRows(M);
50  Real n = VColumns(M);
51  Pack(Not(IsUnknown(M)*Rand(n,1,1,1)),_.packSparsity)
52};
[59dc492]53
54//////////////////////////////////////////////////////////////////////////////
55Text _.autodoc.member.MatchRowsWithValue =
56"Returns a column matrix with True in rows where given column matrix M has "
[f816c60]57"exactly the specified value, that might even be the unknown value";
[59dc492]58VMatrix MatchRowsWithValue(VMatrix M, Real value)
59//////////////////////////////////////////////////////////////////////////////
60{
[345a4d7]61  If(!_checkColumnVector("MatchRowsWithValue",M), Rand(0,0,0,0), {
62  Pack(If(IsUnknown(value), IsUnknown(M),
[f816c60]63  {
64    Real r = VRows(M);
65    VMatrix V = Rand(r,1,value,value);
66    EQ(M,V)
[345a4d7]67  }),_.packSparsity)
68})};
[59dc492]69
[4ddca89]70//////////////////////////////////////////////////////////////////////////////
71Text _.autodoc.member.SelectRowsWithValue =
72"Selects rows of a column matrix whose cell is exactly the specified value";
73Set SelectRowsWithValue(VMatrix M, Real value)
74//////////////////////////////////////////////////////////////////////////////
75{
[59dc492]76  SelectMatch(MatchRowsWithValue(M,value))   
77};
78
79//////////////////////////////////////////////////////////////////////////////
80Text _.autodoc.member.MatchRowsInInterval =
81"Returns a column matrix with True in rows where given column matrix M is "
82"inside the interval defined as: \n"
83"  [min,max]: if minIncluded=True  and minIncluded=True\n"
84"  (min,max]: if minIncluded=False and minIncluded=True\n"
85"  [min,max): if minIncluded=True  and minIncluded=False\n"
86"  (min,max): if minIncluded=False and minIncluded=False\n";
87VMatrix MatchRowsInInterval(
88  VMatrix M,
89  Real minIncluded,
90  Real min,
91  Real max,
92  Real maxIncluded )
93//////////////////////////////////////////////////////////////////////////////
94{
[345a4d7]95  If(!_checkColumnVector("MatchRowsInInterval",M), Rand(0,0,0,0), {
[4ddca89]96  Real r = VRows(M);
[59dc492]97  VMatrix Min = Rand(r,1,min,min);
98  VMatrix Max = Rand(r,1,max,max);
[345a4d7]99  Pack(Case(
[59dc492]100     minIncluded &  maxIncluded, And(LE(Min,M),LE(M,Max)),
101    !minIncluded &  maxIncluded, And(LT(Min,M),LE(M,Max)),
102     minIncluded & !maxIncluded, And(LE(Min,M),LT(M,Max)),
103    !minIncluded & !maxIncluded, And(LT(Min,M),LT(M,Max))
[345a4d7]104  ),_.packSparsity)
105})};
[4ddca89]106
107//////////////////////////////////////////////////////////////////////////////
108Text _.autodoc.member.SelectRowsInInterval =
109"Selects rows of a column matrix whose cell is inside the interval defined "
110"as: \n"
111"  [min,max]: if minIncluded=True  and minIncluded=True\n"
112"  (min,max]: if minIncluded=False and minIncluded=True\n"
113"  [min,max): if minIncluded=True  and minIncluded=False\n"
114"  (min,max): if minIncluded=False and minIncluded=False\n";
115Set SelectRowsInInterval(
116 VMatrix M,
117 Real minIncluded,
118 Real min,
119 Real max,
120 Real maxIncluded )
121//////////////////////////////////////////////////////////////////////////////
122{
[59dc492]123  SelectMatch(MatchRowsInInterval(M,minIncluded,min,max,maxIncluded))   
[4ddca89]124};
125
126//////////////////////////////////////////////////////////////////////////////
[59dc492]127Text _.autodoc.member.MatchRowsInHyperCube = "Returns a column matrix with "
128"True in rows of matrix M which are inside the hipercube whose side length "
129"is 2*d and is centered in point given by row matrix P ";
130VMatrix MatchRowsInHyperCube(VMatrix M, VMatrix P, Real d)
[4ddca89]131//////////////////////////////////////////////////////////////////////////////
132{
133  Real r = VRows(M);
134  Real c = VColumns(M);
135  VMatrix D = Rand(r,1,d,d);
[59dc492]136  Group("And",For(1,c, VMatrix(Real k)
[4ddca89]137  {
[345a4d7]138    Pack(LE(Abs(SubCol(M,[[k]])-VMatDat(P,1,k)),D), _.packSparsity)
[59dc492]139  }))
[4ddca89]140};
141
142//////////////////////////////////////////////////////////////////////////////
[59dc492]143Text _.autodoc.member.SelectRowsInHyperCube = "Selects rows of matrix "
144"M that are inside the hipercube whose side length is 2*d and is centered in "
145"point given by row matrix P ";
146Set SelectRowsInHyperCube(VMatrix M, VMatrix P, Real d)
147//////////////////////////////////////////////////////////////////////////////
148{
149  SelectMatch(MatchRowsInHyperCube(M,P,d))   
150};
151
152//////////////////////////////////////////////////////////////////////////////
153Text _.autodoc.member.MatchRowsInHyperSphere = "Returns a column matrix with "
154"True in rows of matrix M which are inside the hipersphere with specified "
155"ratio and is centered in point given by row matrix P";
156VMatrix MatchRowsInHyperSphere(VMatrix M, VMatrix P, Real ratio)
[4ddca89]157//////////////////////////////////////////////////////////////////////////////
158{
159  Real r = VRows(M);
160  Real c = VColumns(M);
161  Real ratio2 = ratio^2;
162  VMatrix R2 = Rand(r,1,ratio2,ratio2);
163  VMatrix dist2 = SetSum(For(1,c, VMatrix(Real k)
164  {
165    RPow(SubCol(M,[[k]])-VMatDat(P,1,k),2)
166  }));
[345a4d7]167  Pack(LE(dist2,R2),_.packSparsity)
[59dc492]168};
169
170//////////////////////////////////////////////////////////////////////////////
171Text _.autodoc.member.SelectRowsInHyperSphere = "Selects rows of "
172"matrix M that are inside the hipersphere with specified  ratio and is "
173"centered in point given by row matrix P";
174Set SelectRowsInHyperSphere(VMatrix M, VMatrix P, Real ratio)
175//////////////////////////////////////////////////////////////////////////////
176{
177  SelectMatch(MatchRowsInHyperSphere(M,P,ratio))   
[4ddca89]178};
179
180//////////////////////////////////////////////////////////////////////////////
181Text _.autodoc.member.ClassifyRowsByRank = "Classifies the rows of a column "
182"matrix by the ranking position respect a set of R sorted numbers. The class "
183"of the i-th row will be assigned in this way:\n"
184" class[i] = 1   <=>                 M[i] < rankBound[1]\n"
185" class(i] = k+1 <=> rankBound[k] <= M[i] < rankBound[k+1]; k=1...R\n"
186" class[i] = R+1 <=> rankBound[R] <= M[i]\n";
187VMatrix ClassifyRowsByRank(VMatrix M, Set rankBound)
188//////////////////////////////////////////////////////////////////////////////
189{
[345a4d7]190  If(!_checkColumnVector("ClassifyRowsByRank",M), Rand(0,0,0,0), {
[4ddca89]191  Real m = VRows(M);
192  Real R = Card(rankBound);
193  Real min =
194  {
195    Real aux = VMatMin(M);
196    aux - Abs(aux) - 1
197  };
198  Real max =
199  {
200    Real aux = VMatMax(M);
201    aux + Abs(aux) + 1
202  };
203  Set rnk = SetOfReal(min)<<rankBound<<SetOfReal(max);
204  VMatrix class = Zeros(m,1);
205  Real r = 1;
206  VMatrix constant = Rand(m,1,1,1);
207  While(r<=R+1,
208  {
209    VMatrix left.match = LE(constant*rnk[r],M);
210    VMatrix right.match = LT(M,constant*rnk[r+1]);
211    VMatrix class := class + And(left.match,right.match)*r;
212    Real r := r+1
213  });
214  class
[345a4d7]215})}
216
[4ddca89]217
218
219]];
Note: See TracBrowser for help on using the repository browser.