source: tol/tol_pkg/MulVarFunAprox/test/test_0001/test.tol @ 345a4d7

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

prospective test

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

  • Property mode set to 100644
File size: 12.5 KB
Line 
1/* -*- mode: C++ -*- */
2//////////////////////////////////////////////////////////////////////////////
3// FILE   : test.tol
4// PURPOSE: Package MulVarFunAprox test
5//////////////////////////////////////////////////////////////////////////////
6
7Text email_ = "vdebuen@tol-project.org";
8Text link_ = "";
9Text summary_ = "Test for mutivariant function aproximation";
10
11//Write here the initial test TOL code if needed
12Real numErr0 = Copy(NError);
13Real numWar0 = Copy(NWarning);
14
15//Random seed setting
16Real PutRandomSeed(0);
17Real rndSeed = GetRandomSeed(0);
18WriteLn("Current Random Seed = "<<rndSeed);
19
20//#Require MulVarFunAprox;
21#Embed "../../../_embed_all.tol";
22
23Real n = 2;
24
25
26Set 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
35Set 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
54Set 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
92Real K = Card(Base.VFun);
93
94/* */
95
96Real J = K*10000;
97
98VMatrix 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
106VMatrix 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
117VMatrix 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
123VMatrix Fx = fun.eval.extra.base(X);
124
125Real lengthRatio = 1*(J/(K*n))^(1/n);
126
127Set 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
136Real grid.size = SetAvr(Traspose(Extract(X.col.info,5))[1]);
137Real radius = grid.size / Sqrt(2);
138
139Set 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
148VMatrix Y = Mat2VMat(SetMat(Group("CartProd",range)));
149Real H = VRows(Y);
150Real N = H*K;
151
152VMatrix 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
158VMatrix 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
165VMatrix Y.index = Mat2VMat(SetMat(Group("CartProd",For(1,n,Set(Real i)
166{
167  Range(1,Card(range[i]),1)
168}))));
169
170VMatrix show.X.Y =
171  ((X*0) <<  Y   ) |
172  ( X    << (Y*0));
173
174VMatrix 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
195VMatrix Y.closest.Y.index = closest.Y.index(Y);
196VMatrix Y.closest.Y = SubRow(Y,VCol2Set(Y.closest.Y.index));
197Real ok.1 = VMatMax(Abs(Y-Y.closest.Y))==0;
198
199VMatrix Y.neighbour.pos = Min(Y.max, Y + Rand(H,n,grid.size,grid.size));
200VMatrix Y.neighbour.neg = Max(Y.min, Y - Rand(H,n,grid.size,grid.size));
201
202VMatrix 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
214Set Y.neighbourhood.extrictly = For(1,H,Set(Real h)
215{
216  VCol2Set(SubRow(Y.neighbourhood,[[h]]))  - [[h]]
217});
218
219Real X.maxNeighbour = 1+2*n;
220
221VMatrix X.closest.Y.index = closest.Y.index(X);
222VMatrix X.closest.Y = SubRow(Y,VCol2Set(X.closest.Y.index));
223
224VMatrix X.j=DifEq(1/(1-B),Rand(J,1,1,1));
225
226VMatrix X.h=SubRow(Y.neighbourhood, VCol2Set(X.closest.Y.index));
227
228Set X.dif = For(1,X.maxNeighbour,VMatrix(Real i)
229{
230  X - SubRow(Y,VCol2Set(SubCol(X.h,[[i]])))
231});
232
233Set 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
239Set X.tao2 = For(1,X.maxNeighbour,VMatrix(Real i)
240{
241  RPow(X.tao[i],2)
242});
243
244Set X.match = For(1,X.maxNeighbour,VMatrix(Real i)
245{
246  LE(X.tao[i],Rand(J,1,1,1))
247});
248
249Set 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
254VMatrix X.lambda.sum = SetSum(X.lambda);
255
256Set X.weight = For(1,X.maxNeighbour,VMatrix(Real i)
257{
258  X.lambda[i] $/ X.lambda.sum
259});
260
261Matrix 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
277VMatrix QX = Pack(Convert(Triplet(QX.triplet, J, N),"Cholmod.R.Sparse"));
278
279VMatrix 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;
282VMatrix 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;
285VMatrix 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
289VMatrix Z.all = (Y+displ.Z.1) << (Y+displ.Z.2) << (Y+displ.Z.3);
290VMatrix Z.Y.1.all = Y << Y << Y;
291VMatrix Z.Y.2.all = (Y+displ.Z.1*2) << (Y+displ.Z.2*2) << (Y+displ.Z.3*2);
292
293VMatrix Z = Z.all;
294VMatrix Z.Y.1 = Z.Y.1.all;
295VMatrix Z.Y.2 = Z.Y.2.all;
296VMatrix Z.h.1 = closest.Y.index(Z.Y.1);
297VMatrix Z.h.2 = closest.Y.index(Z.Y.2);
298
299
300VMatrix show.X.Y.Z =
301  ( X    << (Y*0) << (Z*0)) |
302  ((X*0) <<  Y    << (Z*0)) |
303  ((X*0) << (Y*0) <<  Z   );
304
305VMatrix show.Y.Z =
306  ( Y    << (Z*0)) |
307  ((Y*0) <<  Z   );
308
309
310Real T = VRows(Z);
311Real prior.Z.order = 2;
312Real 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
318Real Z.maxNeighbour = If(Mz,2,0);
319VMatrix Z.j=DifEq(1/(1-B),Rand(T,1,1,1));
320
321VMatrix Z.h=Z.h.1 | Z.h.2;
322
323Set Z.dif = For(1,Z.maxNeighbour,VMatrix(Real i)
324{
325  Z - SubRow(Y,VCol2Set(SubCol(Z.h,[[i]])))
326});
327
328Set 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
334Set Z.match = For(1,Z.maxNeighbour,VMatrix(Real i)
335{
336  LE(Z.tao[i],Rand(T,1,1,1))
337});
338
339Matrix 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
377Real M = J+Mz;
378
379VMatrix PZ = Pack(Convert(Triplet(PZ.triplet, Mz, N),"Cholmod.R.Sparse"));
380
381VMatrix Fz = Rand(Mz,1,0,0);
382VMatrix F = Fx<<Fz;
383VMatrix Q.all = QX << PZ;
384Set Q.active = MatQuery::SelectMatch(
385  NE(Tra(Rand(1,M,1,1)*Abs(Q.all)),Rand(N,1,0,0)));
386
387VMatrix Q = SubCol(Q.all,Q.active);
388
389VMatrix LQ = CholeskiFactor(Q,"XtX");
390
391VMatrix alpha = CholeskiSolve(LQ,Tra(Tra(F)*Q),"PtLLtP");
392VMatrix F.frc = SubRow(Q*alpha,Range(1,J,1));
393VMatrix F.cmp = Fx | F.frc;
394VMatrix E = Fx - F.frc;
395Real E.avr = VMatAvr(E);
396Real E.ste = VMatStDs(E);
397
398Set View([[
399Real n, K, J, H,
400X.maxNeighbour, Z.maxNeighbour,
401T, N, Mz,M, E.avr, E.ste, "\n"]],"");
402
403/*
404Matrix Q_ = VMat2Mat(Q);
405
406Set svd = SVD(Q_);
407Matrix svd.D = Tra(SubDiag(svd[2],0));
408
409/*
410Real numErr1 = Copy(NError);
411Real numWar1 = Copy(NWarning);
412
413Set partialResults_ = [[numErr0, numErr1,
414                        ok.1, ok.2]];
415
416//This is a messure of the success of the test
417Real quality_ = And(numErr1 == numErr0,
418                    numWar1 == numWar0,
419                    ok.1, ok.2);
420
421//Return the results
422Set resultStr_ = @strTestStatus(summary_, link_, quality_,
423                  "Partial results = "<<partialResults_,
424                  "NO DBServerType", "NO DBServerHost", "NO DBAlias",
425                  email_);
426WriteLn(""<<resultStr_);
427resultStr_;
428
429/* */
430
431/*
432VMatrix 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
438VMatrix 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
444VMatrix 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
448VMatrix Z = SubRow(Z.all,MatQuery::SelectMatch(Z.interior));
449VMatrix Z.Y.1 = SubRow(Z.Y.1.all,MatQuery::SelectMatch(Z.interior));
450VMatrix Z.Y.2 = SubRow(Z.Y.2.all,MatQuery::SelectMatch(Z.interior));
451VMatrix Z.h.1 = closest.Y.index(Z.Y.1);
452VMatrix Z.h.2 = closest.Y.index(Z.Y.2);
453*/
454
455/*
456Real deg = 3;
457
458Real dim = 2;
459Real maxDeg = 3;
460VMatrix candidates = Mat2VMat(SetMat(Range(0,maxDeg,1)^dim));
461VMatrix match = LE(candidates*Rand(dim,1,1,1),Rand(VRows(candidates),1,maxDeg,maxDeg));
462VMatrix degrees = SubRow(candidates, MatQuery::SelectMatch(match));
463Real K = VRows(degrees);
464Class @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};
476Set base = For(1,K,NameBlock(Real k)
477{
478});
479/* */
480
Note: See TracBrowser for help on using the repository browser.