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

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

MatchRowsWithValue accepts even unknown value

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

  • Property mode set to 100644
File size: 6.8 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
17//////////////////////////////////////////////////////////////////////////////
18Text _.autodoc.member.SelectMatch =
19"Returns the rows where thecolumn matrix 'match' has non zero value";
20Set SelectMatch(VMatrix match)
21//////////////////////////////////////////////////////////////////////////////
22{
23  Matrix select = VMat2Triplet(Convert(match,"Cholmod.R.Triplet"));
24  If(!Rows(select),Copy(Empty), MatSet(Tra(SubCol(select,[[1]])))[1])
25};
26
27
28//////////////////////////////////////////////////////////////////////////////
29Text _.autodoc.member.MatchRowsWithValue =
30"Returns a column matrix with True in rows where given column matrix M has "
31"exactly the specified value, that might even be the unknown value";
32VMatrix MatchRowsWithValue(VMatrix M, Real value)
33//////////////////////////////////////////////////////////////////////////////
34{
35  If(IsUnknown(value), IsUnknown(M),
36  {
37    Real r = VRows(M);
38    VMatrix V = Rand(r,1,value,value);
39    EQ(M,V)
40  })
41};
42
43//////////////////////////////////////////////////////////////////////////////
44Text _.autodoc.member.SelectRowsWithValue =
45"Selects rows of a column matrix whose cell is exactly the specified value";
46Set SelectRowsWithValue(VMatrix M, Real value)
47//////////////////////////////////////////////////////////////////////////////
48{
49  SelectMatch(MatchRowsWithValue(M,value))   
50};
51
52//////////////////////////////////////////////////////////////////////////////
53Text _.autodoc.member.MatchRowsInInterval =
54"Returns a column matrix with True in rows where given column matrix M is "
55"inside the interval defined as: \n"
56"  [min,max]: if minIncluded=True  and minIncluded=True\n"
57"  (min,max]: if minIncluded=False and minIncluded=True\n"
58"  [min,max): if minIncluded=True  and minIncluded=False\n"
59"  (min,max): if minIncluded=False and minIncluded=False\n";
60VMatrix MatchRowsInInterval(
61  VMatrix M,
62  Real minIncluded,
63  Real min,
64  Real max,
65  Real maxIncluded )
66//////////////////////////////////////////////////////////////////////////////
67{
68  Real r = VRows(M);
69  VMatrix Min = Rand(r,1,min,min);
70  VMatrix Max = Rand(r,1,max,max);
71  Case(
72     minIncluded &  maxIncluded, And(LE(Min,M),LE(M,Max)),
73    !minIncluded &  maxIncluded, And(LT(Min,M),LE(M,Max)),
74     minIncluded & !maxIncluded, And(LE(Min,M),LT(M,Max)),
75    !minIncluded & !maxIncluded, And(LT(Min,M),LT(M,Max))
76  )
77};
78
79//////////////////////////////////////////////////////////////////////////////
80Text _.autodoc.member.SelectRowsInInterval =
81"Selects rows of a column matrix whose cell is inside the interval defined "
82"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";
87Set SelectRowsInInterval(
88 VMatrix M,
89 Real minIncluded,
90 Real min,
91 Real max,
92 Real maxIncluded )
93//////////////////////////////////////////////////////////////////////////////
94{
95  SelectMatch(MatchRowsInInterval(M,minIncluded,min,max,maxIncluded))   
96};
97
98//////////////////////////////////////////////////////////////////////////////
99Text _.autodoc.member.MatchRowsInHyperCube = "Returns a column matrix with "
100"True in rows of matrix M which are inside the hipercube whose side length "
101"is 2*d and is centered in point given by row matrix P ";
102VMatrix MatchRowsInHyperCube(VMatrix M, VMatrix P, Real d)
103//////////////////////////////////////////////////////////////////////////////
104{
105  Real r = VRows(M);
106  Real c = VColumns(M);
107  VMatrix D = Rand(r,1,d,d);
108  Group("And",For(1,c, VMatrix(Real k)
109  {
110    LE(Abs(SubCol(M,[[k]])-VMatDat(P,1,k)),D)
111  }))
112};
113
114//////////////////////////////////////////////////////////////////////////////
115Text _.autodoc.member.SelectRowsInHyperCube = "Selects rows of matrix "
116"M that are inside the hipercube whose side length is 2*d and is centered in "
117"point given by row matrix P ";
118Set SelectRowsInHyperCube(VMatrix M, VMatrix P, Real d)
119//////////////////////////////////////////////////////////////////////////////
120{
121  SelectMatch(MatchRowsInHyperCube(M,P,d))   
122};
123
124//////////////////////////////////////////////////////////////////////////////
125Text _.autodoc.member.MatchRowsInHyperSphere = "Returns a column matrix with "
126"True in rows of matrix M which are inside the hipersphere with specified "
127"ratio and is centered in point given by row matrix P";
128VMatrix MatchRowsInHyperSphere(VMatrix M, VMatrix P, Real ratio)
129//////////////////////////////////////////////////////////////////////////////
130{
131  Real r = VRows(M);
132  Real c = VColumns(M);
133  Real ratio2 = ratio^2;
134  VMatrix R2 = Rand(r,1,ratio2,ratio2);
135  VMatrix dist2 = SetSum(For(1,c, VMatrix(Real k)
136  {
137    RPow(SubCol(M,[[k]])-VMatDat(P,1,k),2)
138  }));
139  LE(dist2,R2)
140};
141
142//////////////////////////////////////////////////////////////////////////////
143Text _.autodoc.member.SelectRowsInHyperSphere = "Selects rows of "
144"matrix M that are inside the hipersphere with specified  ratio and is "
145"centered in point given by row matrix P";
146Set SelectRowsInHyperSphere(VMatrix M, VMatrix P, Real ratio)
147//////////////////////////////////////////////////////////////////////////////
148{
149  SelectMatch(MatchRowsInHyperSphere(M,P,ratio))   
150};
151
152//////////////////////////////////////////////////////////////////////////////
153Text _.autodoc.member.ClassifyRowsByRank = "Classifies the rows of a column "
154"matrix by the ranking position respect a set of R sorted numbers. The class "
155"of the i-th row will be assigned in this way:\n"
156" class[i] = 1   <=>                 M[i] < rankBound[1]\n"
157" class(i] = k+1 <=> rankBound[k] <= M[i] < rankBound[k+1]; k=1...R\n"
158" class[i] = R+1 <=> rankBound[R] <= M[i]\n";
159VMatrix ClassifyRowsByRank(VMatrix M, Set rankBound)
160//////////////////////////////////////////////////////////////////////////////
161{
162  Real m = VRows(M);
163  Real R = Card(rankBound);
164  Real min =
165  {
166    Real aux = VMatMin(M);
167    aux - Abs(aux) - 1
168  };
169  Real max =
170  {
171    Real aux = VMatMax(M);
172    aux + Abs(aux) + 1
173  };
174  Set rnk = SetOfReal(min)<<rankBound<<SetOfReal(max);
175  VMatrix class = Zeros(m,1);
176  Real r = 1;
177  VMatrix constant = Rand(m,1,1,1);
178  While(r<=R+1,
179  {
180    VMatrix left.match = LE(constant*rnk[r],M);
181    VMatrix right.match = LT(M,constant*rnk[r+1]);
182    VMatrix class := class + And(left.match,right.match)*r;
183    Real r := r+1
184  });
185  class
186}
187
188
189]];
Note: See TracBrowser for help on using the repository browser.