source: tol/tol_pkg/MatQuery/MatQuery.tol @ bf5e53b

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

New method SortAndCount

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

  • Property mode set to 100644
File size: 8.9 KB
Line 
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
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};
31
32//////////////////////////////////////////////////////////////////////////////
33Text _.autodoc.member.SelectMatch =
34"Returns the rows where thecolumn matrix 'match' has non zero value";
35Set SelectMatch(VMatrix match)
36//////////////////////////////////////////////////////////////////////////////
37{
38  If(!_checkColumnVector("SelectMatch",match),Copy(Empty), {
39  Matrix select = VMat2Triplet(Convert(match,"Cholmod.R.Triplet"));
40  If(!Rows(select),Copy(Empty), MatSet(Tra(SubCol(select,[[1]])))[1])
41})};
42
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};
53
54//////////////////////////////////////////////////////////////////////////////
55Text _.autodoc.member.MatchRowsWithValue =
56"Returns a column matrix with True in rows where given column matrix M has "
57"exactly the specified value, that might even be the unknown value";
58VMatrix MatchRowsWithValue(VMatrix M, Real value)
59//////////////////////////////////////////////////////////////////////////////
60{
61  If(!_checkColumnVector("MatchRowsWithValue",M), Rand(0,0,0,0), {
62  Pack(If(IsUnknown(value), IsUnknown(M),
63  {
64    Real r = VRows(M);
65    VMatrix V = Rand(r,1,value,value);
66    EQ(M,V)
67  }),_.packSparsity)
68})};
69
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{
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{
95  If(!_checkColumnVector("MatchRowsInInterval",M), Rand(0,0,0,0), {
96  Real r = VRows(M);
97  VMatrix Min = Rand(r,1,min,min);
98  VMatrix Max = Rand(r,1,max,max);
99  Pack(Case(
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))
104  ),_.packSparsity)
105})};
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{
123  SelectMatch(MatchRowsInInterval(M,minIncluded,min,max,maxIncluded))   
124};
125
126//////////////////////////////////////////////////////////////////////////////
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)
131//////////////////////////////////////////////////////////////////////////////
132{
133  Real r = VRows(M);
134  Real c = VColumns(M);
135  VMatrix D = Rand(r,1,d,d);
136  Group("And",For(1,c, VMatrix(Real k)
137  {
138    Pack(LE(Abs(SubCol(M,[[k]])-VMatDat(P,1,k)),D), _.packSparsity)
139  }))
140};
141
142//////////////////////////////////////////////////////////////////////////////
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)
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  }));
167  Pack(LE(dist2,R2),_.packSparsity)
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))   
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{
190  If(!_checkColumnVector("ClassifyRowsByRank",M), Rand(0,0,0,0), {
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
215})};
216
217//////////////////////////////////////////////////////////////////////////////
218Text _.autodoc.member.SortAndCount = "Sorts the rows of a column matrix and "
219"returns a matrix with two columns. The first one contains sorted unique "
220"values found in given matrixand second one conatins the number of times "
221"that this value is presented.";
222VMatrix SortAndCount(VMatrix A)
223//////////////////////////////////////////////////////////////////////////////
224{
225  Matrix B = VMat2Mat(A);
226  VMatrix C = Mat2VMat(PivotByRows(B,Sort(B,[[1]])));
227  Real m = VRows(C);
228  Set change = Col2Set(Sort(VMat2Mat(C),[[1]],True));
229  Set next = ExtractByIndex(change,Range(2,Card(change),1))<<[[Real m+1]];
230  VMatrix aux.1 = DifEq(1/(1-B),Rand(m+1,1,1,1));
231  VMatrix aux.2 = SubRow(aux.1,change);
232  VMatrix aux.3 = SubRow(aux.1,next);
233  SubRow(C,change) | (aux.3-aux.2)
234}
235
236]];
Note: See TracBrowser for help on using the repository browser.