| 1 | /* -*- mode: C++ -*- */ |
|---|
| 2 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 3 | // FILE : test.tol |
|---|
| 4 | // PURPOSE: Package MulVarFunAprox test |
|---|
| 5 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 6 | |
|---|
| 7 | Text email_ = "vdebuen@tol-project.org"; |
|---|
| 8 | Text link_ = ""; |
|---|
| 9 | Text summary_ = "Test for mutivariant function aproximation"; |
|---|
| 10 | |
|---|
| 11 | //Write here the initial test TOL code if needed |
|---|
| 12 | Real numErr0 = Copy(NError); |
|---|
| 13 | Real numWar0 = Copy(NWarning); |
|---|
| 14 | |
|---|
| 15 | //Random seed setting |
|---|
| 16 | Real PutRandomSeed(0); |
|---|
| 17 | Real rndSeed = GetRandomSeed(0); |
|---|
| 18 | WriteLn("Current Random Seed = "<<rndSeed); |
|---|
| 19 | |
|---|
| 20 | //#Require MulVarFunAprox; |
|---|
| 21 | #Embed "../../../_embed_all.tol"; |
|---|
| 22 | |
|---|
| 23 | Real n = 2; |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | Set Base.VFun = {SetOfCode( |
|---|
| 27 | VMatrix p00 (VMatrix x) { Rand(VRows(x),1,1,1) }, |
|---|
| 28 | VMatrix p10 (VMatrix x) { SubCol(x,[[1]]) }, |
|---|
| 29 | VMatrix p01 (VMatrix x) { SubCol(x,[[2]]) }, |
|---|
| 30 | VMatrix p20 (VMatrix x) { RPow(SubCol(x,[[1]]),2) }, |
|---|
| 31 | VMatrix p02 (VMatrix x) { RPow(SubCol(x,[[2]]),2) }, |
|---|
| 32 | VMatrix p11 (VMatrix x) { SubCol(x,[[1]])$*SubCol(x,[[2]]) } |
|---|
| 33 | )}; |
|---|
| 34 | |
|---|
| 35 | Set Base.VDerFst = {[[ |
|---|
| 36 | {SetOfCode( |
|---|
| 37 | VMatrix p00 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 38 | VMatrix p10 (VMatrix x) { Rand(VRows(x),1,1,1) }, |
|---|
| 39 | VMatrix p01 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 40 | VMatrix p20 (VMatrix x) { SubCol(x,[[1]])*2 }, |
|---|
| 41 | VMatrix p02 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 42 | VMatrix p11 (VMatrix x) { SubCol(x,[[2]]) } |
|---|
| 43 | )}; |
|---|
| 44 | {SetOfCode( |
|---|
| 45 | VMatrix p00 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 46 | VMatrix p10 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 47 | VMatrix p01 (VMatrix x) { Rand(VRows(x),1,1,1) }, |
|---|
| 48 | VMatrix p20 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 49 | VMatrix p02 (VMatrix x) { SubCol(x,[[2]])*2 }, |
|---|
| 50 | VMatrix p11 (VMatrix x) { SubCol(x,[[1]]) } |
|---|
| 51 | )} |
|---|
| 52 | ]]}; |
|---|
| 53 | |
|---|
| 54 | Set Base.VDerSnd = {[[ |
|---|
| 55 | {[[ |
|---|
| 56 | {SetOfCode( |
|---|
| 57 | VMatrix p00 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 58 | VMatrix p10 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 59 | VMatrix p01 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 60 | VMatrix p20 (VMatrix x) { Rand(VRows(x),1,2,2) }, |
|---|
| 61 | VMatrix p02 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 62 | VMatrix p11 (VMatrix x) { Rand(VRows(x),1,0,0) } |
|---|
| 63 | )}; |
|---|
| 64 | {SetOfCode( |
|---|
| 65 | VMatrix p00 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 66 | VMatrix p10 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 67 | VMatrix p01 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 68 | VMatrix p20 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 69 | VMatrix p02 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 70 | VMatrix p11 (VMatrix x) { Rand(VRows(x),1,1,1) } |
|---|
| 71 | )} |
|---|
| 72 | ]]}, |
|---|
| 73 | {[[ |
|---|
| 74 | {SetOfCode( |
|---|
| 75 | VMatrix p00 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 76 | VMatrix p10 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 77 | VMatrix p01 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 78 | VMatrix p20 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 79 | VMatrix p02 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 80 | VMatrix p11 (VMatrix x) { Rand(VRows(x),1,1,1) } |
|---|
| 81 | )}; |
|---|
| 82 | {SetOfCode( |
|---|
| 83 | VMatrix p00 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 84 | VMatrix p10 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 85 | VMatrix p01 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 86 | VMatrix p20 (VMatrix x) { Rand(VRows(x),1,0,0) }, |
|---|
| 87 | VMatrix p02 (VMatrix x) { Rand(VRows(x),1,2,2) }, |
|---|
| 88 | VMatrix p11 (VMatrix x) { Rand(VRows(x),1,0,0) } |
|---|
| 89 | )} |
|---|
| 90 | ]]} ]]}; |
|---|
| 91 | |
|---|
| 92 | Real K = Card(Base.VFun); |
|---|
| 93 | |
|---|
| 94 | /* */ |
|---|
| 95 | |
|---|
| 96 | Real J = K*10000; |
|---|
| 97 | |
|---|
| 98 | VMatrix X = Group("ConcatColumns",For(1,n,VMatrix(Real i) |
|---|
| 99 | { |
|---|
| 100 | Real min = Rand(0,0); |
|---|
| 101 | Real len = Rand(5,10)*(i^(1/n)); |
|---|
| 102 | Rand(J,1,min,min+len) |
|---|
| 103 | })); |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | VMatrix fun.eval.intra.base(VMatrix X) |
|---|
| 107 | { |
|---|
| 108 | Set x = For(1,n,VMatrix(Real i) { SubCol(X,[[i]]) }); |
|---|
| 109 | (x[1]*0+1)*Rand(-10,10)+ |
|---|
| 110 | x[1]*Rand(-10,10)+ |
|---|
| 111 | x[2]*Rand(-10,10)+ |
|---|
| 112 | x[1]^2*Rand(-10,10)+ |
|---|
| 113 | x[2]^2*Rand(-10,10)+ |
|---|
| 114 | x[1]$*x[2]*Rand(-10,10) |
|---|
| 115 | }; |
|---|
| 116 | |
|---|
| 117 | VMatrix fun.eval.extra.base(VMatrix X) |
|---|
| 118 | { |
|---|
| 119 | Set x = For(1,n,VMatrix(Real i) { SubCol(X,[[i]]) }); |
|---|
| 120 | Sin(x[1])+Cos(x[1]) |
|---|
| 121 | }; |
|---|
| 122 | |
|---|
| 123 | VMatrix Fx = fun.eval.extra.base(X); |
|---|
| 124 | |
|---|
| 125 | Real lengthRatio = 1*(J/(K*n))^(1/n); |
|---|
| 126 | |
|---|
| 127 | Set X.col.info = For(1,n,Set(Real i) |
|---|
| 128 | {[[ |
|---|
| 129 | VMatrix col = SubCol(X,[[i]]); |
|---|
| 130 | Real min = VMatMin(col); |
|---|
| 131 | Real max = VMatMax(col); |
|---|
| 132 | Real length = max-min; |
|---|
| 133 | Real grid.size = 1*length/lengthRatio |
|---|
| 134 | ]]}); |
|---|
| 135 | |
|---|
| 136 | Real grid.size = SetAvr(Traspose(Extract(X.col.info,5))[1]); |
|---|
| 137 | Real radius = grid.size / Sqrt(2); |
|---|
| 138 | |
|---|
| 139 | Set range = For(1,n,Set(Real i) |
|---|
| 140 | { |
|---|
| 141 | Set info = X.col.info[i]; |
|---|
| 142 | Real points = 1+Ceil(info::length/grid.size); |
|---|
| 143 | Real exceed = (points-1)*grid.size-info::length; |
|---|
| 144 | Matrix rnk = Tra(DifEq(1/(1-B),Rand(points,1,1,1))-Rand(points,1,1,1)); |
|---|
| 145 | Set MatSet((rnk*grid.size)+info::min-exceed/2)[1] |
|---|
| 146 | }); |
|---|
| 147 | |
|---|
| 148 | VMatrix Y = Mat2VMat(SetMat(Group("CartProd",range))); |
|---|
| 149 | Real H = VRows(Y); |
|---|
| 150 | Real N = H*K; |
|---|
| 151 | |
|---|
| 152 | VMatrix Y.min = Group("ConcatColumns", For(1,n,VMatrix(Real i) |
|---|
| 153 | { |
|---|
| 154 | Real aux = range[i][1]; |
|---|
| 155 | Rand(H,1,aux,aux) |
|---|
| 156 | })); |
|---|
| 157 | |
|---|
| 158 | VMatrix Y.max = Group("ConcatColumns", For(1,n,VMatrix(Real i) |
|---|
| 159 | { |
|---|
| 160 | Real aux = range[i][Card(range[i])]; |
|---|
| 161 | Rand(H,1,aux,aux) |
|---|
| 162 | })); |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | VMatrix Y.index = Mat2VMat(SetMat(Group("CartProd",For(1,n,Set(Real i) |
|---|
| 166 | { |
|---|
| 167 | Range(1,Card(range[i]),1) |
|---|
| 168 | })))); |
|---|
| 169 | |
|---|
| 170 | VMatrix show.X.Y = |
|---|
| 171 | ((X*0) << Y ) | |
|---|
| 172 | ( X << (Y*0)); |
|---|
| 173 | |
|---|
| 174 | VMatrix closest.Y.index(VMatrix A) |
|---|
| 175 | { |
|---|
| 176 | Real m = VRows(A); |
|---|
| 177 | VMatrix cumulated = Rand(m,1,1,1); |
|---|
| 178 | VMatrix A.position = Rand(m,1,1,1)+SetSum(For(1,n,VMatrix(Real i_) |
|---|
| 179 | { |
|---|
| 180 | Real i = n-i_+1; |
|---|
| 181 | Real cRi = Card(range[i]); |
|---|
| 182 | Set info = X.col.info[i]; |
|---|
| 183 | VMatrix Ai = SubCol(A,[[i]]); |
|---|
| 184 | VMatrix Yi.min = Rand(m,1,range[i][1],range[i][1]); |
|---|
| 185 | VMatrix Ri = Round((Ai-Yi.min)/grid.size); |
|---|
| 186 | VMatrix minRi = Rand(m,1,0,0); |
|---|
| 187 | VMatrix maxRi = Rand(m,1,cRi-1,cRi-1); |
|---|
| 188 | VMatrix aux = Min(maxRi,Max(minRi,Ri)); |
|---|
| 189 | VMatrix pos = aux $* cumulated; |
|---|
| 190 | VMatrix cumulated := cumulated*cRi; |
|---|
| 191 | pos |
|---|
| 192 | })) |
|---|
| 193 | }; |
|---|
| 194 | |
|---|
| 195 | VMatrix Y.closest.Y.index = closest.Y.index(Y); |
|---|
| 196 | VMatrix Y.closest.Y = SubRow(Y,VCol2Set(Y.closest.Y.index)); |
|---|
| 197 | Real ok.1 = VMatMax(Abs(Y-Y.closest.Y))==0; |
|---|
| 198 | |
|---|
| 199 | VMatrix Y.neighbour.pos = Min(Y.max, Y + Rand(H,n,grid.size,grid.size)); |
|---|
| 200 | VMatrix Y.neighbour.neg = Max(Y.min, Y - Rand(H,n,grid.size,grid.size)); |
|---|
| 201 | |
|---|
| 202 | VMatrix Y.neighbourhood = DifEq(1/(1-B),Rand(H,1,1,1)) | |
|---|
| 203 | { |
|---|
| 204 | Set aux.1 = BinGroup("<<", For(1,n,Set(Real i) |
|---|
| 205 | { |
|---|
| 206 | VMatrix pre = SubCol(Y,Range(1, i-1,1)); |
|---|
| 207 | VMatrix post = SubCol(Y,Range(i+1,n, 1)); |
|---|
| 208 | SetOfVMatrix(pre | SubCol(Y.neighbour.neg,[[i]]) | post, |
|---|
| 209 | pre | SubCol(Y.neighbour.pos,[[i]]) | post) |
|---|
| 210 | })); |
|---|
| 211 | Group("ConcatColumns",EvalSet(aux.1,closest.Y.index)) |
|---|
| 212 | }; |
|---|
| 213 | |
|---|
| 214 | Set Y.neighbourhood.extrictly = For(1,H,Set(Real h) |
|---|
| 215 | { |
|---|
| 216 | VCol2Set(SubRow(Y.neighbourhood,[[h]])) - [[h]] |
|---|
| 217 | }); |
|---|
| 218 | |
|---|
| 219 | Real X.maxNeighbour = 1+2*n; |
|---|
| 220 | |
|---|
| 221 | VMatrix X.closest.Y.index = closest.Y.index(X); |
|---|
| 222 | VMatrix X.closest.Y = SubRow(Y,VCol2Set(X.closest.Y.index)); |
|---|
| 223 | |
|---|
| 224 | VMatrix X.j=DifEq(1/(1-B),Rand(J,1,1,1)); |
|---|
| 225 | |
|---|
| 226 | VMatrix X.h=SubRow(Y.neighbourhood, VCol2Set(X.closest.Y.index)); |
|---|
| 227 | |
|---|
| 228 | Set X.dif = For(1,X.maxNeighbour,VMatrix(Real i) |
|---|
| 229 | { |
|---|
| 230 | X - SubRow(Y,VCol2Set(SubCol(X.h,[[i]]))) |
|---|
| 231 | }); |
|---|
| 232 | |
|---|
| 233 | Set X.tao = For(1,X.maxNeighbour,VMatrix(Real i) |
|---|
| 234 | { |
|---|
| 235 | VMatrix X.norm2 = RPow(X.dif[i],2)*Rand(n,1,1,1); |
|---|
| 236 | RPow(X.norm2,0.5)/radius |
|---|
| 237 | }); |
|---|
| 238 | |
|---|
| 239 | Set X.tao2 = For(1,X.maxNeighbour,VMatrix(Real i) |
|---|
| 240 | { |
|---|
| 241 | RPow(X.tao[i],2) |
|---|
| 242 | }); |
|---|
| 243 | |
|---|
| 244 | Set X.match = For(1,X.maxNeighbour,VMatrix(Real i) |
|---|
| 245 | { |
|---|
| 246 | LE(X.tao[i],Rand(J,1,1,1)) |
|---|
| 247 | }); |
|---|
| 248 | |
|---|
| 249 | Set X.lambda = For(1,X.maxNeighbour,VMatrix(Real i) |
|---|
| 250 | { |
|---|
| 251 | RPow(X.match[i] $* (Rand(J,1,1,1)-X.tao2[i]),4) |
|---|
| 252 | }); |
|---|
| 253 | |
|---|
| 254 | VMatrix X.lambda.sum = SetSum(X.lambda); |
|---|
| 255 | |
|---|
| 256 | Set X.weight = For(1,X.maxNeighbour,VMatrix(Real i) |
|---|
| 257 | { |
|---|
| 258 | X.lambda[i] $/ X.lambda.sum |
|---|
| 259 | }); |
|---|
| 260 | |
|---|
| 261 | Matrix QX.triplet = Group("ConcatRows", For(1,X.maxNeighbour,Matrix(Real i) |
|---|
| 262 | { |
|---|
| 263 | VMatrix X.hiK = (SubCol(X.h,[[i]])-1)*K; |
|---|
| 264 | Group("ConcatRows", For(1,K,Matrix(Real k) |
|---|
| 265 | { |
|---|
| 266 | Code bvf = Base.VFun[k]; |
|---|
| 267 | VMatrix pk = bvf(X.dif[i]); |
|---|
| 268 | VMatrix qk = X.weight[i]$*pk; |
|---|
| 269 | Set sel.rows = MatQuery::SelectMatch(NE(Drop(Abs(qk),1.E-15),qk*0)); |
|---|
| 270 | VMat2Mat( |
|---|
| 271 | SubRow(X.j,sel.rows) | |
|---|
| 272 | (SubRow(X.hiK,sel.rows)+k) | |
|---|
| 273 | SubRow(qk,sel.rows)) |
|---|
| 274 | })) |
|---|
| 275 | })); |
|---|
| 276 | |
|---|
| 277 | VMatrix QX = Pack(Convert(Triplet(QX.triplet, J, N),"Cholmod.R.Sparse")); |
|---|
| 278 | |
|---|
| 279 | VMatrix displ.Z.1 = |
|---|
| 280 | Rand(H,1,-0.1,0.1)*grid.size/2 | |
|---|
| 281 | Rand(H,1, 0.9,1.1)*grid.size/2; |
|---|
| 282 | VMatrix displ.Z.2 = |
|---|
| 283 | Rand(H,1, 0.9,1.1)*grid.size/2 | |
|---|
| 284 | Rand(H,1,-0.1,0.1)*grid.size/2; |
|---|
| 285 | VMatrix displ.Z.3 = |
|---|
| 286 | Rand(H,1,-0.9,-1.1)*grid.size/2 | |
|---|
| 287 | Rand(H,1,-0.9,-1.1)*grid.size/2; |
|---|
| 288 | |
|---|
| 289 | VMatrix Z.all = (Y+displ.Z.1) << (Y+displ.Z.2) << (Y+displ.Z.3); |
|---|
| 290 | VMatrix Z.Y.1.all = Y << Y << Y; |
|---|
| 291 | VMatrix Z.Y.2.all = (Y+displ.Z.1*2) << (Y+displ.Z.2*2) << (Y+displ.Z.3*2); |
|---|
| 292 | |
|---|
| 293 | VMatrix Z = Z.all; |
|---|
| 294 | VMatrix Z.Y.1 = Z.Y.1.all; |
|---|
| 295 | VMatrix Z.Y.2 = Z.Y.2.all; |
|---|
| 296 | VMatrix Z.h.1 = closest.Y.index(Z.Y.1); |
|---|
| 297 | VMatrix Z.h.2 = closest.Y.index(Z.Y.2); |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | VMatrix show.X.Y.Z = |
|---|
| 301 | ( X << (Y*0) << (Z*0)) | |
|---|
| 302 | ((X*0) << Y << (Z*0)) | |
|---|
| 303 | ((X*0) << (Y*0) << Z ); |
|---|
| 304 | |
|---|
| 305 | VMatrix show.Y.Z = |
|---|
| 306 | ( Y << (Z*0)) | |
|---|
| 307 | ((Y*0) << Z ); |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | Real T = VRows(Z); |
|---|
| 311 | Real prior.Z.order = 2; |
|---|
| 312 | Real Mz = Case( |
|---|
| 313 | prior.Z.order< 0, 0, |
|---|
| 314 | prior.Z.order==0, T, |
|---|
| 315 | prior.Z.order==1, T*(1+n*K), |
|---|
| 316 | prior.Z.order==2, T*(1+(n^2)*K)); |
|---|
| 317 | |
|---|
| 318 | Real Z.maxNeighbour = If(Mz,2,0); |
|---|
| 319 | VMatrix Z.j=DifEq(1/(1-B),Rand(T,1,1,1)); |
|---|
| 320 | |
|---|
| 321 | VMatrix Z.h=Z.h.1 | Z.h.2; |
|---|
| 322 | |
|---|
| 323 | Set Z.dif = For(1,Z.maxNeighbour,VMatrix(Real i) |
|---|
| 324 | { |
|---|
| 325 | Z - SubRow(Y,VCol2Set(SubCol(Z.h,[[i]]))) |
|---|
| 326 | }); |
|---|
| 327 | |
|---|
| 328 | Set Z.tao = For(1,Z.maxNeighbour,VMatrix(Real i) |
|---|
| 329 | { |
|---|
| 330 | VMatrix Z.norm2 = RPow(Z.dif[i],2)*Rand(n,1,1,1); |
|---|
| 331 | RPow(Z.norm2,0.5)/radius |
|---|
| 332 | }); |
|---|
| 333 | |
|---|
| 334 | Set Z.match = For(1,Z.maxNeighbour,VMatrix(Real i) |
|---|
| 335 | { |
|---|
| 336 | LE(Z.tao[i],Rand(T,1,1,1)) |
|---|
| 337 | }); |
|---|
| 338 | |
|---|
| 339 | Matrix PZ.triplet = Group("ConcatRows", For(1,Z.maxNeighbour,Matrix(Real i) |
|---|
| 340 | { |
|---|
| 341 | VMatrix Z.hiK = (SubCol(Z.h,[[i]])-1)*K; |
|---|
| 342 | Group("ConcatRows", For(1,K,Matrix(Real k) |
|---|
| 343 | { |
|---|
| 344 | Real cumRows = 0; |
|---|
| 345 | Code bvf = Base.VFun[k]; |
|---|
| 346 | VMatrix pk = bvf(Z.dif[i])*If(i==1,+1,-1); |
|---|
| 347 | Set sel.rows = MatQuery::SelectMatch(NE(Drop(Abs(pk),1.E-15),pk*0)); |
|---|
| 348 | VMatrix col.idx = (SubRow(Z.hiK,sel.rows)+k); |
|---|
| 349 | { |
|---|
| 350 | VMatrix row.idx = SubRow(Z.j,sel.rows)+cumRows; |
|---|
| 351 | Real cumRows := cumRows + T; |
|---|
| 352 | VMat2Mat(row.idx | col.idx | SubRow(pk,sel.rows)) |
|---|
| 353 | } << If(prior.Z.order<1,Rand(0,3,0,0), |
|---|
| 354 | Group("ConcatRows",For(1,n,Matrix(Real d) |
|---|
| 355 | { |
|---|
| 356 | Code bvfd = Base.VDerFst[d][k]; |
|---|
| 357 | VMatrix pkd = bvfd(Z.dif[i])*If(i==1,+1/2,-1/2); |
|---|
| 358 | VMatrix row.idx = SubRow(Z.j,sel.rows)+cumRows; |
|---|
| 359 | Real cumRows := cumRows + T; |
|---|
| 360 | VMat2Mat(row.idx | col.idx | SubRow(pkd,sel.rows)) |
|---|
| 361 | })))<< If(prior.Z.order<2,Rand(0,3,0,0), |
|---|
| 362 | Group("ConcatRows",For(1,n,Matrix(Real d1) |
|---|
| 363 | { |
|---|
| 364 | Group("ConcatRows",For(d1,n,Matrix(Real d2) |
|---|
| 365 | { |
|---|
| 366 | Code bvfd2 = Base.VDerSnd[d1][d2][k]; |
|---|
| 367 | VMatrix pkd2 = bvfd2(Z.dif[i])*If(i==1,+1/4,-1/4); |
|---|
| 368 | VMatrix row.idx = SubRow(Z.j,sel.rows)+cumRows; |
|---|
| 369 | Real cumRows := cumRows + T; |
|---|
| 370 | VMat2Mat(row.idx | col.idx | SubRow(pkd2,sel.rows)) |
|---|
| 371 | })) |
|---|
| 372 | }))) |
|---|
| 373 | })) |
|---|
| 374 | })); |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | Real M = J+Mz; |
|---|
| 378 | |
|---|
| 379 | VMatrix PZ = Pack(Convert(Triplet(PZ.triplet, Mz, N),"Cholmod.R.Sparse")); |
|---|
| 380 | |
|---|
| 381 | VMatrix Fz = Rand(Mz,1,0,0); |
|---|
| 382 | VMatrix F = Fx<<Fz; |
|---|
| 383 | VMatrix Q.all = QX << PZ; |
|---|
| 384 | Set Q.active = MatQuery::SelectMatch( |
|---|
| 385 | NE(Tra(Rand(1,M,1,1)*Abs(Q.all)),Rand(N,1,0,0))); |
|---|
| 386 | |
|---|
| 387 | VMatrix Q = SubCol(Q.all,Q.active); |
|---|
| 388 | |
|---|
| 389 | VMatrix LQ = CholeskiFactor(Q,"XtX"); |
|---|
| 390 | |
|---|
| 391 | VMatrix alpha = CholeskiSolve(LQ,Tra(Tra(F)*Q),"PtLLtP"); |
|---|
| 392 | VMatrix F.frc = SubRow(Q*alpha,Range(1,J,1)); |
|---|
| 393 | VMatrix F.cmp = Fx | F.frc; |
|---|
| 394 | VMatrix E = Fx - F.frc; |
|---|
| 395 | Real E.avr = VMatAvr(E); |
|---|
| 396 | Real E.ste = VMatStDs(E); |
|---|
| 397 | |
|---|
| 398 | Set View([[ |
|---|
| 399 | Real n, K, J, H, |
|---|
| 400 | X.maxNeighbour, Z.maxNeighbour, |
|---|
| 401 | T, N, Mz,M, E.avr, E.ste, "\n"]],""); |
|---|
| 402 | |
|---|
| 403 | /* |
|---|
| 404 | Matrix Q_ = VMat2Mat(Q); |
|---|
| 405 | |
|---|
| 406 | Set svd = SVD(Q_); |
|---|
| 407 | Matrix svd.D = Tra(SubDiag(svd[2],0)); |
|---|
| 408 | |
|---|
| 409 | /* |
|---|
| 410 | Real numErr1 = Copy(NError); |
|---|
| 411 | Real numWar1 = Copy(NWarning); |
|---|
| 412 | |
|---|
| 413 | Set partialResults_ = [[numErr0, numErr1, |
|---|
| 414 | ok.1, ok.2]]; |
|---|
| 415 | |
|---|
| 416 | //This is a messure of the success of the test |
|---|
| 417 | Real quality_ = And(numErr1 == numErr0, |
|---|
| 418 | numWar1 == numWar0, |
|---|
| 419 | ok.1, ok.2); |
|---|
| 420 | |
|---|
| 421 | //Return the results |
|---|
| 422 | Set resultStr_ = @strTestStatus(summary_, link_, quality_, |
|---|
| 423 | "Partial results = "<<partialResults_, |
|---|
| 424 | "NO DBServerType", "NO DBServerHost", "NO DBAlias", |
|---|
| 425 | email_); |
|---|
| 426 | WriteLn(""<<resultStr_); |
|---|
| 427 | resultStr_; |
|---|
| 428 | |
|---|
| 429 | /* */ |
|---|
| 430 | |
|---|
| 431 | /* |
|---|
| 432 | VMatrix Z.min = Group("ConcatColumns", For(1,n,VMatrix(Real i) |
|---|
| 433 | { |
|---|
| 434 | Real aux = range[i][1]; |
|---|
| 435 | Rand(2*H,1,aux,aux) |
|---|
| 436 | })); |
|---|
| 437 | |
|---|
| 438 | VMatrix Z.max = Group("ConcatColumns", For(1,n,VMatrix(Real i) |
|---|
| 439 | { |
|---|
| 440 | Real aux = range[i][Card(range[i])]; |
|---|
| 441 | Rand(2*H,1,aux,aux) |
|---|
| 442 | })); |
|---|
| 443 | |
|---|
| 444 | VMatrix Z.interior = EQ( |
|---|
| 445 | And(GE(Z.all,Z.min), LE(Z.all,Z.max))*Rand(n,1,1,1), |
|---|
| 446 | Rand(2*H,1,n,n)); |
|---|
| 447 | |
|---|
| 448 | VMatrix Z = SubRow(Z.all,MatQuery::SelectMatch(Z.interior)); |
|---|
| 449 | VMatrix Z.Y.1 = SubRow(Z.Y.1.all,MatQuery::SelectMatch(Z.interior)); |
|---|
| 450 | VMatrix Z.Y.2 = SubRow(Z.Y.2.all,MatQuery::SelectMatch(Z.interior)); |
|---|
| 451 | VMatrix Z.h.1 = closest.Y.index(Z.Y.1); |
|---|
| 452 | VMatrix Z.h.2 = closest.Y.index(Z.Y.2); |
|---|
| 453 | */ |
|---|
| 454 | |
|---|
| 455 | /* |
|---|
| 456 | Real deg = 3; |
|---|
| 457 | |
|---|
| 458 | Real dim = 2; |
|---|
| 459 | Real maxDeg = 3; |
|---|
| 460 | VMatrix candidates = Mat2VMat(SetMat(Range(0,maxDeg,1)^dim)); |
|---|
| 461 | VMatrix match = LE(candidates*Rand(dim,1,1,1),Rand(VRows(candidates),1,maxDeg,maxDeg)); |
|---|
| 462 | VMatrix degrees = SubRow(candidates, MatQuery::SelectMatch(match)); |
|---|
| 463 | Real K = VRows(degrees); |
|---|
| 464 | Class @PolynBasic |
|---|
| 465 | { |
|---|
| 466 | Real k; |
|---|
| 467 | VMatrix fun (VMatrix x) |
|---|
| 468 | { |
|---|
| 469 | SetProd(For(1,dim,VMatrix(Real i) |
|---|
| 470 | { |
|---|
| 471 | |
|---|
| 472 | }) |
|---|
| 473 | }; |
|---|
| 474 | |
|---|
| 475 | }; |
|---|
| 476 | Set base = For(1,K,NameBlock(Real k) |
|---|
| 477 | { |
|---|
| 478 | }); |
|---|
| 479 | /* */ |
|---|
| 480 | |
|---|