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

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

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

  • Property mode set to 100644
File size: 4.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//////////////////////////////////////////////////////////////////////////////
17Text _.autodoc.member.SelectRowsWithValue =
18"Selects rows of a column matrix whose cell is exactly the specified value";
19Set SelectRowsWithValue(VMatrix M, Real value)
20//////////////////////////////////////////////////////////////////////////////
21{
22  Real r = VRows(M);
23  VMatrix V = Rand(r,1,value,value);
24  VMatrix match = EQ(M,V);
25  Matrix select = VMat2Triplet(Convert(match,"Cholmod.R.Triplet"));
26  If(!Rows(select),Copy(Empty), MatSet(Tra(SubCol(select,[[1]])))[1])
27};
28
29//////////////////////////////////////////////////////////////////////////////
30Text _.autodoc.member.SelectRowsInInterval =
31"Selects rows of a column matrix whose cell is inside the interval defined "
32"as: \n"
33"  [min,max]: if minIncluded=True  and minIncluded=True\n"
34"  (min,max]: if minIncluded=False and minIncluded=True\n"
35"  [min,max): if minIncluded=True  and minIncluded=False\n"
36"  (min,max): if minIncluded=False and minIncluded=False\n";
37Set SelectRowsInInterval(
38 VMatrix M,
39 Real minIncluded,
40 Real min,
41 Real max,
42 Real maxIncluded )
43//////////////////////////////////////////////////////////////////////////////
44{
45  Real r = VRows(M);
46  VMatrix Min = Rand(r,1,min,min);
47  VMatrix Max = Rand(r,1,max,max);
48  VMatrix match =
49  Case(
50     minIncluded &  maxIncluded, And(LE(Min,M),LE(M,Max)),
51    !minIncluded &  maxIncluded, And(LT(Min,M),LE(M,Max)),
52     minIncluded & !maxIncluded, And(LE(Min,M),LT(M,Max)),
53    !minIncluded & !maxIncluded, And(LT(Min,M),LT(M,Max))
54  );
55  Matrix select = VMat2Triplet(Convert(match,"Cholmod.R.Triplet"));
56  If(!Rows(select),Copy(Empty), MatSet(Tra(SubCol(select,[[1]])))[1])
57};
58
59//////////////////////////////////////////////////////////////////////////////
60Text _.autodoc.member.SelectRowsInHyperCube = "Selects rows of matrix "
61"M that are inside the hipercube whose side length is 2*d and is centered in "
62"point given by row matrix P ";
63Set SelectRowsInHyperCube(VMatrix M, VMatrix P, Real d)
64//////////////////////////////////////////////////////////////////////////////
65{
66  Real r = VRows(M);
67  Real c = VColumns(M);
68  VMatrix D = Rand(r,1,d,d);
69  VMatrix match = Group("And",For(1,c, VMatrix(Real k)
70  {
71    LE(Abs(SubCol(M,[[k]])-VMatDat(P,1,k)),D)
72  }));
73  Matrix select = VMat2Triplet(Convert(match,"Cholmod.R.Triplet"));
74  If(!Rows(select),Copy(Empty), MatSet(Tra(SubCol(select,[[1]])))[1])
75};
76
77//////////////////////////////////////////////////////////////////////////////
78Text _.autodoc.member.SelectRowsInHyperSphere = "Selects rows of "
79"matrix M that are inside the hipersphere with specified  ratio and is "
80"centered in point given by row matrix P";
81Set SelectRowsInHyperSphere(VMatrix M, VMatrix P, Real ratio)
82//////////////////////////////////////////////////////////////////////////////
83{
84  Real r = VRows(M);
85  Real c = VColumns(M);
86  Real ratio2 = ratio^2;
87  VMatrix R2 = Rand(r,1,ratio2,ratio2);
88  VMatrix dist2 = SetSum(For(1,c, VMatrix(Real k)
89  {
90    RPow(SubCol(M,[[k]])-VMatDat(P,1,k),2)
91  }));
92  VMatrix match = LE(dist2,R2);
93  Matrix select = VMat2Triplet(Convert(match,"Cholmod.R.Triplet"));
94  If(!Rows(select),Copy(Empty), MatSet(Tra(SubCol(select,[[1]])))[1])
95};
96
97//////////////////////////////////////////////////////////////////////////////
98Text _.autodoc.member.ClassifyRowsByRank = "Classifies the rows of a column "
99"matrix by the ranking position respect a set of R sorted numbers. The class "
100"of the i-th row will be assigned in this way:\n"
101" class[i] = 1   <=>                 M[i] < rankBound[1]\n"
102" class(i] = k+1 <=> rankBound[k] <= M[i] < rankBound[k+1]; k=1...R\n"
103" class[i] = R+1 <=> rankBound[R] <= M[i]\n";
104VMatrix ClassifyRowsByRank(VMatrix M, Set rankBound)
105//////////////////////////////////////////////////////////////////////////////
106{
107  Real m = VRows(M);
108  Real R = Card(rankBound);
109  Real min =
110  {
111    Real aux = VMatMin(M);
112    aux - Abs(aux) - 1
113  };
114  Real max =
115  {
116    Real aux = VMatMax(M);
117    aux + Abs(aux) + 1
118  };
119  Set rnk = SetOfReal(min)<<rankBound<<SetOfReal(max);
120  VMatrix class = Zeros(m,1);
121  Real r = 1;
122  VMatrix constant = Rand(m,1,1,1);
123  While(r<=R+1,
124  {
125    VMatrix left.match = LE(constant*rnk[r],M);
126    VMatrix right.match = LT(M,constant*rnk[r+1]);
127    VMatrix class := class + And(left.match,right.match)*r;
128    Real r := r+1
129  });
130  class
131}
132
133
134]];
Note: See TracBrowser for help on using the repository browser.