| 1 | /* -*- mode: C++ -*- */ |
|---|
| 2 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 3 | // FILE : ModelC2.tol |
|---|
| 4 | // PURPOSE: Defines Class @ModelC2 |
|---|
| 5 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 6 | |
|---|
| 7 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 8 | Text _.autodoc.member.IndexedGrid = |
|---|
| 9 | ""; |
|---|
| 10 | |
|---|
| 11 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 12 | Class @IndexedGrid |
|---|
| 13 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 14 | { |
|---|
| 15 | VMatrix _.X; |
|---|
| 16 | Real _.edge.length; |
|---|
| 17 | |
|---|
| 18 | Real _.n = ?; |
|---|
| 19 | Real _.m = ?; |
|---|
| 20 | Set _.key.dim =Copy(Empty); |
|---|
| 21 | Matrix _.key.factor = Rand(0,0,0,0); |
|---|
| 22 | Matrix _.Y.key = Rand(0,0,0,0); |
|---|
| 23 | Matrix _.Y.h = Rand(0,0,0,0); |
|---|
| 24 | VMatrix _.Y = Rand(0,0,0,0); |
|---|
| 25 | Real _.H = 0; |
|---|
| 26 | Set _.Y.neighbourhood.point = Copy(Empty); |
|---|
| 27 | VMatrix _.Y.neighbourhood.index = Rand(0,0,0,0); |
|---|
| 28 | |
|---|
| 29 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 30 | Static @IndexedGrid None(Real void) |
|---|
| 31 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 32 | { |
|---|
| 33 | @IndexedGrid new = [[ |
|---|
| 34 | VMatrix _.X = Rand(0,0,0,0); |
|---|
| 35 | Real _.edge.length = ? |
|---|
| 36 | ]]; |
|---|
| 37 | new |
|---|
| 38 | }; |
|---|
| 39 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 40 | Static @IndexedGrid New( |
|---|
| 41 | VMatrix X, |
|---|
| 42 | Real edge.length, |
|---|
| 43 | Real do.build.neighbourhood) |
|---|
| 44 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 45 | { |
|---|
| 46 | @IndexedGrid new = [[ |
|---|
| 47 | VMatrix _.X = X; |
|---|
| 48 | Real _.edge.length = edge.length |
|---|
| 49 | ]]; |
|---|
| 50 | Real new::build.index(0); |
|---|
| 51 | Real If(do.build.neighbourhood, new::build.neighbourhood(0)); |
|---|
| 52 | new |
|---|
| 53 | }; |
|---|
| 54 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 55 | Static NameBlock MatModule(Matrix A, Matrix B) |
|---|
| 56 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 57 | {[[ |
|---|
| 58 | Matrix quotient = Round(A $/ B); |
|---|
| 59 | Matrix rest = A - quotient $* B |
|---|
| 60 | ]]}; |
|---|
| 61 | |
|---|
| 62 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 63 | Static NameBlock MatModuleR(Matrix A, Real b) |
|---|
| 64 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 65 | {[[ |
|---|
| 66 | Matrix quotient = Floor(A *(1/b)); |
|---|
| 67 | Matrix rest = A - quotient * b |
|---|
| 68 | ]]}; |
|---|
| 69 | |
|---|
| 70 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 71 | Matrix point2key(VMatrix A) |
|---|
| 72 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 73 | { |
|---|
| 74 | Matrix zero = Rand(VRows(A),1,0,0); |
|---|
| 75 | Matrix A.key.dim = Group("ConcatColumns", For(1,_.n, Matrix(Real i) |
|---|
| 76 | { |
|---|
| 77 | -Min(zero, MatReplace( |
|---|
| 78 | Round(VMat2Mat(SubCol(A,[[i]]))*(1/_.edge.length)), |
|---|
| 79 | SubCol(_.key.dim[i],[[2]]), |
|---|
| 80 | -SubCol(_.key.dim[i],[[1]]))) |
|---|
| 81 | })); |
|---|
| 82 | A.key.dim * _.key.factor |
|---|
| 83 | }; |
|---|
| 84 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 85 | VMatrix key2point(Matrix key) |
|---|
| 86 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 87 | { |
|---|
| 88 | Group("ConcatColumns", |
|---|
| 89 | { |
|---|
| 90 | Matrix aux = Copy(key); |
|---|
| 91 | Set reverse = EvalSet(Range(_.n,1,-1), VMatrix(Real i) |
|---|
| 92 | { |
|---|
| 93 | NameBlock mod=@IndexedGrid::MatModuleR(aux,MatDat(_.key.factor,i,1)); |
|---|
| 94 | Matrix aux := Copy(mod::rest); |
|---|
| 95 | Mat2VMat(MatReplace( |
|---|
| 96 | mod::quotient, |
|---|
| 97 | SubCol(_.key.dim[i],[[1]]), |
|---|
| 98 | SubCol(_.key.dim[i],[[2]])))*_.edge.length |
|---|
| 99 | }); |
|---|
| 100 | EvalSet(Range(_.n,1,-1), VMatrix(Real i) { reverse[i] }) |
|---|
| 101 | }) |
|---|
| 102 | }; |
|---|
| 103 | ///////////////////////////////////////////////////////////////////////////// |
|---|
| 104 | Matrix index2key(Matrix idx) |
|---|
| 105 | ///////////////////////////////////////////////////////////////////////////// |
|---|
| 106 | { |
|---|
| 107 | -Min(Rand(Rows(idx),1,0,0), MatReplace(idx, _.Y.h, -_.Y.key)) |
|---|
| 108 | }; |
|---|
| 109 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 110 | Matrix key2index(Matrix key) |
|---|
| 111 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 112 | { |
|---|
| 113 | -Min(Rand(Rows(key),1,0,0), MatReplace(key, _.Y.key, -_.Y.h)) |
|---|
| 114 | }; |
|---|
| 115 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 116 | VMatrix index2point(Matrix idx) |
|---|
| 117 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 118 | { |
|---|
| 119 | key2point(index2key(idx)) |
|---|
| 120 | }; |
|---|
| 121 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 122 | Matrix point2index(VMatrix A) |
|---|
| 123 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 124 | { |
|---|
| 125 | key2index(point2key(A)) |
|---|
| 126 | }; |
|---|
| 127 | |
|---|
| 128 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 129 | Real build.index(Real void) |
|---|
| 130 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 131 | { |
|---|
| 132 | Real _.n := VColumns(_.X); |
|---|
| 133 | Real _.m := VRows(_.X); |
|---|
| 134 | VMatrix X.round = Round(_.X/_.edge.length); |
|---|
| 135 | Set _.key.dim := For(1,_.n, Matrix(Real i) |
|---|
| 136 | { |
|---|
| 137 | Matrix key = SetCol(Sort(Unique(VCol2Set(SubCol(X.round,[[i]]))),Compare)); |
|---|
| 138 | Matrix idx = DifEq(1/(1-B),Rand(Rows(key),1,1,1)); |
|---|
| 139 | idx|key |
|---|
| 140 | }); |
|---|
| 141 | |
|---|
| 142 | Matrix _.key.factor := |
|---|
| 143 | { |
|---|
| 144 | Matrix aux.1 = SetCol(EvalSet(_.key.dim,Rows)); |
|---|
| 145 | Matrix aux.2 = Floor(Log10(aux.1))+1; |
|---|
| 146 | Matrix aux.3 = DifEq(1/(1-B),Col(0)<<SubRow(aux.2,Range(1,_.n-1,1))); |
|---|
| 147 | Matrix aux.4 = Exp(aux.3*Log(10)) |
|---|
| 148 | }; |
|---|
| 149 | Matrix X.key = point2key(X); |
|---|
| 150 | Matrix _.Y.key := SetCol(Sort(Unique(Col2Set(X.key)),Compare)); |
|---|
| 151 | Matrix _.Y.h := DifEq(1/(1-B),Rand(Rows(_.Y.key),1,1,1)); |
|---|
| 152 | VMatrix _.Y := key2point(_.Y.key); |
|---|
| 153 | Real _.H := VRows(_.Y) |
|---|
| 154 | }; |
|---|
| 155 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 156 | Real build.neighbourhood(Real void) |
|---|
| 157 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 158 | { |
|---|
| 159 | VMatrix Y.neighbour.pos = _.Y + Rand(_.H,_.n,_.edge.length,_.edge.length); |
|---|
| 160 | VMatrix Y.neighbour.neg = _.Y - Rand(_.H,_.n,_.edge.length,_.edge.length); |
|---|
| 161 | Set _.Y.neighbourhood.point := BinGroup("<<", For(1,_.n,Set(Real i) |
|---|
| 162 | { |
|---|
| 163 | VMatrix pre = SubCol(_.Y,Range(1, i-1,1)); |
|---|
| 164 | VMatrix post = SubCol(_.Y,Range(i+1,_.n,1)); |
|---|
| 165 | SetOfVMatrix(pre | SubCol(Y.neighbour.neg,[[i]]) | post, |
|---|
| 166 | pre | SubCol(Y.neighbour.pos,[[i]]) | post) |
|---|
| 167 | })); |
|---|
| 168 | |
|---|
| 169 | VMatrix _.Y.neighbourhood.index := Mat2VMat(Group("ConcatColumns", |
|---|
| 170 | EvalSet(_.Y.neighbourhood.point, point2index))); |
|---|
| 171 | True |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | }; |
|---|
| 175 | |
|---|
| 176 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 177 | Class @IndexedGrid.Border : @IndexedGrid |
|---|
| 178 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 179 | { |
|---|
| 180 | @IndexedGrid _.border = @IndexedGrid::None(0); |
|---|
| 181 | Matrix _.border.assign = Rand(0,0,0,0); |
|---|
| 182 | |
|---|
| 183 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 184 | Static @IndexedGrid.Border New( |
|---|
| 185 | VMatrix X, |
|---|
| 186 | Real edge.length, |
|---|
| 187 | Real do.build.neighbourhood, |
|---|
| 188 | Real do.build.border) |
|---|
| 189 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 190 | { |
|---|
| 191 | @IndexedGrid.Border new = [[ |
|---|
| 192 | VMatrix _.X = X; |
|---|
| 193 | Real _.edge.length = edge.length |
|---|
| 194 | ]]; |
|---|
| 195 | Real new::build.index(0); |
|---|
| 196 | Real If(do.build.neighbourhood | do.build.border, |
|---|
| 197 | new::build.neighbourhood(0)); |
|---|
| 198 | Real If(do.build.border, |
|---|
| 199 | new::build.border(0)); |
|---|
| 200 | new |
|---|
| 201 | }; |
|---|
| 202 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 203 | Real build.border(Real void) |
|---|
| 204 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 205 | { |
|---|
| 206 | Set match.0 = For(1, 2*_.n, VMatrix(Real i) |
|---|
| 207 | { |
|---|
| 208 | MatQuery::MatchRowsWithValue(SubCol(_.Y.neighbourhood.index,[[i]]),0) |
|---|
| 209 | }); |
|---|
| 210 | VMatrix border = Group("ConcatRows", For(1, 2*_.n, VMatrix(Real i) |
|---|
| 211 | { |
|---|
| 212 | SubRow(_.Y.neighbourhood.point[i], MatQuery::SelectMatch(match.0[i])) |
|---|
| 213 | })); |
|---|
| 214 | @IndexedGrid _.border := |
|---|
| 215 | @IndexedGrid::New(border,_.edge.length, False); |
|---|
| 216 | |
|---|
| 217 | Matrix _.border.assign := Group("ConcatRows", For(1, 2*_.n, Matrix(Real i) |
|---|
| 218 | { |
|---|
| 219 | Set sel.0.i = MatQuery::SelectMatch(match.0[i]); |
|---|
| 220 | Matrix Y.h.i = SubRow(_.Y.h, sel.0.i); |
|---|
| 221 | Matrix B.b.i = _.border::point2index( |
|---|
| 222 | SubRow(_.Y.neighbourhood.point[i], sel.0.i)); |
|---|
| 223 | B.b.i|Y.h.i|Rand(Rows(Y.h.i),1,i,i) |
|---|
| 224 | })); |
|---|
| 225 | |
|---|
| 226 | True |
|---|
| 227 | } |
|---|
| 228 | }; |
|---|