| 1 | /* -*- mode: C++ -*- */ |
|---|
| 2 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 3 | // FILE : MatQuery.tol |
|---|
| 4 | // PURPOSE: Defines package MatQuery |
|---|
| 5 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 6 | |
|---|
| 7 | NameBlock MatQuery = [[ |
|---|
| 8 | |
|---|
| 9 | Text _.autodoc.description = "Functions and classes related with efficient " |
|---|
| 10 | "selection and classification methods over large matricial data."; |
|---|
| 11 | Text _.autodoc.url = ""; |
|---|
| 12 | Set _.autodoc.keys = [["select","classify"]]; |
|---|
| 13 | Set _.autodoc.authors = [[ |
|---|
| 14 | "vdebuen@tol-project.org" ]]; |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 18 | Text _.autodoc.member.SelectMatch = |
|---|
| 19 | "Returns the rows where thecolumn matrix 'match' has non zero value"; |
|---|
| 20 | Set 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 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 29 | Text _.autodoc.member.MatchRowsWithValue = |
|---|
| 30 | "Returns a column matrix with True in rows where given column matrix M has " |
|---|
| 31 | "exactly the specified value"; |
|---|
| 32 | VMatrix MatchRowsWithValue(VMatrix M, Real value) |
|---|
| 33 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 34 | { |
|---|
| 35 | Real r = VRows(M); |
|---|
| 36 | VMatrix V = Rand(r,1,value,value); |
|---|
| 37 | EQ(M,V) |
|---|
| 38 | }; |
|---|
| 39 | |
|---|
| 40 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 41 | Text _.autodoc.member.SelectRowsWithValue = |
|---|
| 42 | "Selects rows of a column matrix whose cell is exactly the specified value"; |
|---|
| 43 | Set SelectRowsWithValue(VMatrix M, Real value) |
|---|
| 44 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 45 | { |
|---|
| 46 | SelectMatch(MatchRowsWithValue(M,value)) |
|---|
| 47 | }; |
|---|
| 48 | |
|---|
| 49 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 50 | Text _.autodoc.member.MatchRowsInInterval = |
|---|
| 51 | "Returns a column matrix with True in rows where given column matrix M is " |
|---|
| 52 | "inside the interval defined as: \n" |
|---|
| 53 | " [min,max]: if minIncluded=True and minIncluded=True\n" |
|---|
| 54 | " (min,max]: if minIncluded=False and minIncluded=True\n" |
|---|
| 55 | " [min,max): if minIncluded=True and minIncluded=False\n" |
|---|
| 56 | " (min,max): if minIncluded=False and minIncluded=False\n"; |
|---|
| 57 | VMatrix MatchRowsInInterval( |
|---|
| 58 | VMatrix M, |
|---|
| 59 | Real minIncluded, |
|---|
| 60 | Real min, |
|---|
| 61 | Real max, |
|---|
| 62 | Real maxIncluded ) |
|---|
| 63 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 64 | { |
|---|
| 65 | Real r = VRows(M); |
|---|
| 66 | VMatrix Min = Rand(r,1,min,min); |
|---|
| 67 | VMatrix Max = Rand(r,1,max,max); |
|---|
| 68 | Case( |
|---|
| 69 | minIncluded & maxIncluded, And(LE(Min,M),LE(M,Max)), |
|---|
| 70 | !minIncluded & maxIncluded, And(LT(Min,M),LE(M,Max)), |
|---|
| 71 | minIncluded & !maxIncluded, And(LE(Min,M),LT(M,Max)), |
|---|
| 72 | !minIncluded & !maxIncluded, And(LT(Min,M),LT(M,Max)) |
|---|
| 73 | ) |
|---|
| 74 | }; |
|---|
| 75 | |
|---|
| 76 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 77 | Text _.autodoc.member.SelectRowsInInterval = |
|---|
| 78 | "Selects rows of a column matrix whose cell is inside the interval defined " |
|---|
| 79 | "as: \n" |
|---|
| 80 | " [min,max]: if minIncluded=True and minIncluded=True\n" |
|---|
| 81 | " (min,max]: if minIncluded=False and minIncluded=True\n" |
|---|
| 82 | " [min,max): if minIncluded=True and minIncluded=False\n" |
|---|
| 83 | " (min,max): if minIncluded=False and minIncluded=False\n"; |
|---|
| 84 | Set SelectRowsInInterval( |
|---|
| 85 | VMatrix M, |
|---|
| 86 | Real minIncluded, |
|---|
| 87 | Real min, |
|---|
| 88 | Real max, |
|---|
| 89 | Real maxIncluded ) |
|---|
| 90 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 91 | { |
|---|
| 92 | SelectMatch(MatchRowsInInterval(M,minIncluded,min,max,maxIncluded)) |
|---|
| 93 | }; |
|---|
| 94 | |
|---|
| 95 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 96 | Text _.autodoc.member.MatchRowsInHyperCube = "Returns a column matrix with " |
|---|
| 97 | "True in rows of matrix M which are inside the hipercube whose side length " |
|---|
| 98 | "is 2*d and is centered in point given by row matrix P "; |
|---|
| 99 | VMatrix MatchRowsInHyperCube(VMatrix M, VMatrix P, Real d) |
|---|
| 100 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 101 | { |
|---|
| 102 | Real r = VRows(M); |
|---|
| 103 | Real c = VColumns(M); |
|---|
| 104 | VMatrix D = Rand(r,1,d,d); |
|---|
| 105 | Group("And",For(1,c, VMatrix(Real k) |
|---|
| 106 | { |
|---|
| 107 | LE(Abs(SubCol(M,[[k]])-VMatDat(P,1,k)),D) |
|---|
| 108 | })) |
|---|
| 109 | }; |
|---|
| 110 | |
|---|
| 111 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 112 | Text _.autodoc.member.SelectRowsInHyperCube = "Selects rows of matrix " |
|---|
| 113 | "M that are inside the hipercube whose side length is 2*d and is centered in " |
|---|
| 114 | "point given by row matrix P "; |
|---|
| 115 | Set SelectRowsInHyperCube(VMatrix M, VMatrix P, Real d) |
|---|
| 116 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 117 | { |
|---|
| 118 | SelectMatch(MatchRowsInHyperCube(M,P,d)) |
|---|
| 119 | }; |
|---|
| 120 | |
|---|
| 121 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 122 | Text _.autodoc.member.MatchRowsInHyperSphere = "Returns a column matrix with " |
|---|
| 123 | "True in rows of matrix M which are inside the hipersphere with specified " |
|---|
| 124 | "ratio and is centered in point given by row matrix P"; |
|---|
| 125 | VMatrix MatchRowsInHyperSphere(VMatrix M, VMatrix P, Real ratio) |
|---|
| 126 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 127 | { |
|---|
| 128 | Real r = VRows(M); |
|---|
| 129 | Real c = VColumns(M); |
|---|
| 130 | Real ratio2 = ratio^2; |
|---|
| 131 | VMatrix R2 = Rand(r,1,ratio2,ratio2); |
|---|
| 132 | VMatrix dist2 = SetSum(For(1,c, VMatrix(Real k) |
|---|
| 133 | { |
|---|
| 134 | RPow(SubCol(M,[[k]])-VMatDat(P,1,k),2) |
|---|
| 135 | })); |
|---|
| 136 | LE(dist2,R2) |
|---|
| 137 | }; |
|---|
| 138 | |
|---|
| 139 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 140 | Text _.autodoc.member.SelectRowsInHyperSphere = "Selects rows of " |
|---|
| 141 | "matrix M that are inside the hipersphere with specified ratio and is " |
|---|
| 142 | "centered in point given by row matrix P"; |
|---|
| 143 | Set SelectRowsInHyperSphere(VMatrix M, VMatrix P, Real ratio) |
|---|
| 144 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 145 | { |
|---|
| 146 | SelectMatch(MatchRowsInHyperSphere(M,P,ratio)) |
|---|
| 147 | }; |
|---|
| 148 | |
|---|
| 149 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 150 | Text _.autodoc.member.ClassifyRowsByRank = "Classifies the rows of a column " |
|---|
| 151 | "matrix by the ranking position respect a set of R sorted numbers. The class " |
|---|
| 152 | "of the i-th row will be assigned in this way:\n" |
|---|
| 153 | " class[i] = 1 <=> M[i] < rankBound[1]\n" |
|---|
| 154 | " class(i] = k+1 <=> rankBound[k] <= M[i] < rankBound[k+1]; k=1...R\n" |
|---|
| 155 | " class[i] = R+1 <=> rankBound[R] <= M[i]\n"; |
|---|
| 156 | VMatrix ClassifyRowsByRank(VMatrix M, Set rankBound) |
|---|
| 157 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 158 | { |
|---|
| 159 | Real m = VRows(M); |
|---|
| 160 | Real R = Card(rankBound); |
|---|
| 161 | Real min = |
|---|
| 162 | { |
|---|
| 163 | Real aux = VMatMin(M); |
|---|
| 164 | aux - Abs(aux) - 1 |
|---|
| 165 | }; |
|---|
| 166 | Real max = |
|---|
| 167 | { |
|---|
| 168 | Real aux = VMatMax(M); |
|---|
| 169 | aux + Abs(aux) + 1 |
|---|
| 170 | }; |
|---|
| 171 | Set rnk = SetOfReal(min)<<rankBound<<SetOfReal(max); |
|---|
| 172 | VMatrix class = Zeros(m,1); |
|---|
| 173 | Real r = 1; |
|---|
| 174 | VMatrix constant = Rand(m,1,1,1); |
|---|
| 175 | While(r<=R+1, |
|---|
| 176 | { |
|---|
| 177 | VMatrix left.match = LE(constant*rnk[r],M); |
|---|
| 178 | VMatrix right.match = LT(M,constant*rnk[r+1]); |
|---|
| 179 | VMatrix class := class + And(left.match,right.match)*r; |
|---|
| 180 | Real r := r+1 |
|---|
| 181 | }); |
|---|
| 182 | class |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | ]]; |
|---|