00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <iostream>
00019 #include <cmath>
00020 #include "array.h"
00021 #include "matpackVII.h"
00022 #include "interpolation.h"
00023 #include "interpolation_poly.h"
00024 #include "make_vector.h"
00025 #include "xml_io.h"
00026 #include "math_funcs.h"
00027
00028 void test01()
00029 {
00030 cout << "Simple interpolation cases\n"
00031 << "--------------------------\n";
00032
00033 Vector og(1,5,+1);
00034 Vector ng(2,5,0.25);
00035
00036 cout << "og:\n" << og << "\n";
00037 cout << "ng:\n" << ng << "\n";
00038
00039
00040 ArrayOfGridPos gp(ng.nelem());
00041
00042 gridpos(gp,og,ng);
00043 cout << "gp:\n" << gp << "\n";
00044
00045 cout << "1D:\n"
00046 << "---\n";
00047 {
00048
00049 Matrix itw(gp.nelem(),2);
00050 interpweights(itw,gp);
00051
00052 cout << "itw:\n" << itw << "\n";
00053
00054
00055 Vector of(og.nelem(),0);
00056 of[2] = 10;
00057
00058 cout << "of:\n" << of << "\n";
00059
00060
00061 Vector nf(ng.nelem());
00062
00063 interp(nf, itw, of, gp);
00064
00065 cout << "nf:\n" << nf << "\n";
00066 }
00067
00068 cout << "Blue 2D:\n"
00069 << "--------\n";
00070 {
00071
00072 Matrix itw(gp.nelem(),4);
00073 interpweights(itw,gp,gp);
00074
00075 cout << "itw:\n" << itw << "\n";
00076
00077
00078 Matrix of(og.nelem(),og.nelem(),0);
00079 of(2,2) = 10;
00080
00081 cout << "of:\n" << of << "\n";
00082
00083
00084 Vector nf(ng.nelem());
00085
00086 interp(nf, itw, of, gp, gp);
00087
00088 cout << "nf:\n" << nf << "\n";
00089 }
00090
00091 cout << "Blue 6D:\n"
00092 << "--------\n";
00093 {
00094
00095 Matrix itw(gp.nelem(),64);
00096 interpweights(itw,gp,gp,gp,gp,gp,gp);
00097
00098
00099
00100
00101 Index n = og.nelem();
00102 Tensor6 of(n,n,n,n,n,n,0);
00103 of(2,2,2,2,2,2) = 10;
00104
00105
00106
00107
00108 Vector nf(ng.nelem());
00109
00110 interp(nf, itw, of, gp, gp, gp, gp, gp, gp);
00111
00112 cout << "nf:\n" << nf << "\n";
00113 }
00114
00115 cout << "Green 2D:\n"
00116 << "---------\n";
00117 {
00118
00119 Tensor3 itw(gp.nelem(),gp.nelem(),4);
00120 interpweights(itw,gp,gp);
00121
00122 for ( Index i=0; i<itw.ncols(); ++i )
00123 cout << "itw " << i << ":\n" << itw(Range(joker),Range(joker),i) << "\n";
00124
00125
00126 Matrix of(og.nelem(),og.nelem(),0);
00127 of(2,2) = 10;
00128
00129 cout << "of:\n" << of << "\n";
00130
00131
00132 Matrix nf(ng.nelem(),ng.nelem());
00133
00134 interp(nf, itw, of, gp, gp);
00135
00136 cout << "nf:\n" << nf << "\n";
00137 }
00138
00139 cout << "Green 6D:\n"
00140 << "---------\n";
00141 {
00142
00143 Tensor7 itw(gp.nelem(),
00144 gp.nelem(),
00145 gp.nelem(),
00146 gp.nelem(),
00147 gp.nelem(),
00148 gp.nelem(),
00149 64);
00150 interpweights(itw,gp,gp,gp,gp,gp,gp);
00151
00152
00153 Tensor6 of(og.nelem(),og.nelem(),og.nelem(),og.nelem(),og.nelem(),og.nelem(),0);
00154 of(2,2,2,2,2,2) = 10;
00155
00156 cout << "Middle slice of of:\n" << of(2,2,2,2,Range(joker),Range(joker)) << "\n";
00157
00158
00159 Tensor6 nf(ng.nelem(),ng.nelem(),ng.nelem(),ng.nelem(),ng.nelem(),ng.nelem());
00160
00161 interp(nf, itw, of, gp, gp, gp, gp, gp, gp);
00162
00163 cout << "Last slice of nf:\n" << nf(4,4,4,4,Range(joker),Range(joker)) << "\n";
00164 }
00165 }
00166
00167 void test02(Index n)
00168 {
00169 cout << "Test whether for loop or iterator are quicker\n"
00170 << "a) for loop\n"
00171 << "---------------------------------------------\n";
00172
00173 Vector a(n);
00174 for (Index i=0; i<a.nelem(); ++i)
00175 a[i] = (Numeric)i;
00176 }
00177
00178 void test03(Index n)
00179 {
00180 cout << "Test whether for loop or iterator are quicker\n"
00181 << "b) iterator\n"
00182 << "---------------------------------------------\n";
00183
00184 Vector a(n);
00185 Iterator1D ai=a.begin();
00186 const Iterator1D ae=a.end();
00187 Index i=0;
00188 for ( ; ai!=ae; ++ai, ++i )
00189 *ai = (Numeric)i;
00190 }
00191
00192
00193
00194
00195 void test04()
00196 {
00197 cout << "Green type interpolation of all pages of a Tensor3\n";
00198
00199
00200
00201
00202 Vector a_pgrid(1,3,1), a_rgrid(1,3,1), a_cgrid(1,3,1);
00203 Tensor3 a( a_pgrid.nelem(), a_rgrid.nelem(), a_cgrid.nelem() );
00204
00205 a = 0;
00206
00207 a(0,1,1) = 10;
00208 a(1,1,1) = 20;
00209 a(2,1,1) = 30;
00210
00211
00212
00213 Vector n_rgrid(1,5,.5), n_cgrid(1,5,.5);
00214 Tensor3 n( a_pgrid.nelem(), n_rgrid.nelem(), n_cgrid.nelem() );
00215
00216
00217
00218
00219 ArrayOfGridPos n_rgp(n_rgrid.nelem());
00220 ArrayOfGridPos n_cgp(n_cgrid.nelem());
00221
00222 gridpos( n_rgp, a_rgrid, n_rgrid );
00223 gridpos( n_cgp, a_cgrid, n_cgrid );
00224
00225
00226 Tensor3 itw( n_rgrid.nelem(), n_cgrid.nelem(), 4 );
00227 interpweights( itw, n_rgp, n_cgp );
00228
00229
00230
00231 for ( Index i=0; i<a.npages(); ++i )
00232 {
00233
00234 ConstMatrixView ap = a( i, Range(joker), Range(joker) );
00235 MatrixView np = n( i, Range(joker), Range(joker) );
00236
00237
00238 interp( np, itw, ap, n_rgp, n_cgp );
00239
00240
00241
00242 }
00243
00244 cout << "Original field:\n";
00245 for ( Index i=0; i<a.npages(); ++i )
00246 cout << "page " << i << ":\n" << a(i,Range(joker),Range(joker)) << "\n";
00247
00248 cout << "Interpolated field:\n";
00249 for ( Index i=0; i<n.npages(); ++i )
00250 cout << "page " << i << ":\n" << n(i,Range(joker),Range(joker)) << "\n";
00251 }
00252
00253 void test05()
00254 {
00255 cout << "Very simple interpolation case\n";
00256
00257 Vector og(1,5,+1);
00258 Vector ng(2,5,0.25);
00259
00260 cout << "Original grid:\n" << og << "\n";
00261 cout << "New grid:\n" << ng << "\n";
00262
00263
00264 ArrayOfGridPos gp(ng.nelem());
00265
00266 gridpos(gp,og,ng);
00267 cout << "Grid positions:\n" << gp;
00268
00269
00270 Matrix itw(gp.nelem(),2);
00271 interpweights(itw,gp);
00272
00273 cout << "Interpolation weights:\n" << itw << "\n";
00274
00275
00276 Vector of(og.nelem(),0);
00277 of[2] = 10;
00278
00279 cout << "Original field:\n" << of << "\n";
00280
00281
00282 Vector nf(ng.nelem());
00283
00284 interp(nf, itw, of, gp);
00285
00286 cout << "New field:\n" << nf << "\n";
00287 }
00288
00289 void test06()
00290 {
00291 cout << "Simple extrapolation cases\n"
00292 << "--------------------------\n";
00293
00294 Vector og(1,5,+1);
00295 MakeVector ng(0.9,1.5,3,4.5,5.1);
00296
00297 cout << "og:\n" << og << "\n";
00298 cout << "ng:\n" << ng << "\n";
00299
00300
00301 ArrayOfGridPos gp(ng.nelem());
00302
00303 gridpos(gp,og,ng);
00304 cout << "gp:\n" << gp << "\n";
00305
00306 cout << "1D:\n"
00307 << "---\n";
00308 {
00309
00310 Matrix itw(gp.nelem(),2);
00311 interpweights(itw,gp);
00312
00313 cout << "itw:\n" << itw << "\n";
00314
00315
00316 Vector of(og.nelem(),0);
00317 for ( Index i=0; i<og.nelem(); ++i )
00318 of[i] = (Numeric)(10*(i+1));
00319
00320 cout << "of:\n" << of << "\n";
00321
00322
00323 Vector nf(ng.nelem());
00324
00325 interp(nf, itw, of, gp);
00326
00327 cout << "nf:\n" << nf << "\n";
00328 }
00329
00330 cout << "Blue 2D:\n"
00331 << "--------\n";
00332 {
00333
00334 Matrix itw(gp.nelem(),4);
00335 interpweights(itw,gp,gp);
00336
00337 cout << "itw:\n" << itw << "\n";
00338
00339
00340 Matrix of(og.nelem(),og.nelem(),0);
00341 of(2,2) = 10;
00342
00343 cout << "of:\n" << of << "\n";
00344
00345
00346 Vector nf(ng.nelem());
00347
00348 interp(nf, itw, of, gp, gp);
00349
00350 cout << "nf:\n" << nf << "\n";
00351 }
00352
00353 cout << "Blue 6D:\n"
00354 << "--------\n";
00355 {
00356
00357 Matrix itw(gp.nelem(),64);
00358 interpweights(itw,gp,gp,gp,gp,gp,gp);
00359
00360
00361
00362
00363 Index n = og.nelem();
00364 Tensor6 of(n,n,n,n,n,n,0);
00365 of(2,2,2,2,2,2) = 10;
00366
00367
00368
00369
00370 Vector nf(ng.nelem());
00371
00372 interp(nf, itw, of, gp, gp, gp, gp, gp, gp);
00373
00374 cout << "nf:\n" << nf << "\n";
00375 }
00376
00377 cout << "Green 2D:\n"
00378 << "---------\n";
00379 {
00380
00381 Tensor3 itw(gp.nelem(),gp.nelem(),4);
00382 interpweights(itw,gp,gp);
00383
00384 for ( Index i=0; i<itw.ncols(); ++i )
00385 cout << "itw " << i << ":\n" << itw(Range(joker),Range(joker),i) << "\n";
00386
00387
00388 Matrix of(og.nelem(),og.nelem(),0);
00389 of(2,2) = 10;
00390
00391 cout << "of:\n" << of << "\n";
00392
00393
00394 Matrix nf(ng.nelem(),ng.nelem());
00395
00396 interp(nf, itw, of, gp, gp);
00397
00398 cout << "nf:\n" << nf << "\n";
00399 }
00400
00401 cout << "Green 6D:\n"
00402 << "---------\n";
00403 {
00404
00405 Tensor7 itw(gp.nelem(),
00406 gp.nelem(),
00407 gp.nelem(),
00408 gp.nelem(),
00409 gp.nelem(),
00410 gp.nelem(),
00411 64);
00412 interpweights(itw,gp,gp,gp,gp,gp,gp);
00413
00414
00415 Tensor6 of(og.nelem(),og.nelem(),og.nelem(),og.nelem(),og.nelem(),og.nelem(),0);
00416 of(2,2,2,2,2,2) = 10;
00417
00418 cout << "Middle slice of of:\n" << of(2,2,2,2,Range(joker),Range(joker)) << "\n";
00419
00420
00421 Tensor6 nf(ng.nelem(),ng.nelem(),ng.nelem(),ng.nelem(),ng.nelem(),ng.nelem());
00422
00423 interp(nf, itw, of, gp, gp, gp, gp, gp, gp);
00424
00425 cout << "Last slice of nf:\n" << nf(4,4,4,4,Range(joker),Range(joker)) << "\n";
00426 }
00427 }
00428
00429
00430 void test07()
00431 {
00432
00433 Vector new_x(0, 21, +0.25);
00434 Vector x(0, 10, +1);
00435
00436 ArrayOfGridPos gp(new_x.nelem());
00437 gridpos(gp, x, new_x);
00438
00439 Vector y1(x.nelem());
00440 Vector y2(x.nelem());
00441 Vector y3(x.nelem());
00442
00443 for(Index i = 0; i < x.nelem(); i++)
00444 {
00445
00446 y1[i] = 3*x[i];
00447
00448 y2[i] = pow(x[i],3) + 2;
00449
00450 y3[i] = sin(x[i]);
00451 }
00452
00453
00454 Matrix itw(gp.nelem(),2);
00455 interpweights(itw, gp);
00456
00457 Vector y1_lin(new_x.nelem());
00458 Vector y2_lin(new_x.nelem());
00459 Vector y3_lin(new_x.nelem());
00460
00461 interp(y1_lin, itw, y1, gp);
00462 interp(y2_lin, itw, y2, gp);
00463 interp(y3_lin, itw, y3, gp);
00464
00465 cout << "y1_lin = ["<< y1_lin << "];\n";
00466 cout << "y2_lin = ["<< y2_lin << "];\n";
00467 cout << "y3_lin = ["<< y3_lin << "];\n";
00468
00469
00470 Vector y1_cub(new_x.nelem());
00471 Vector y2_cub(new_x.nelem());
00472 Vector y3_cub(new_x.nelem());
00473
00474 for(Index i = 0; i < new_x.nelem(); i++)
00475 {
00476 y1_cub[i] = interp_poly(x, y1, new_x[i], gp[i]);
00477 y2_cub[i] = interp_poly(x, y2, new_x[i], gp[i]);
00478 y3_cub[i] = interp_poly(x, y3, new_x[i], gp[i]);
00479 }
00480
00481 cout << "y1_cub = ["<< y1_cub << "];\n";
00482 cout << "y2_cub = ["<< y2_cub << "];\n";
00483 cout << "y3_cub = ["<< y3_cub << "];\n";
00484
00485
00486 Index order = 2;
00487
00488 Vector y1_new(new_x.nelem());
00489 Vector y2_new(new_x.nelem());
00490 Vector y3_new(new_x.nelem());
00491
00492 ArrayOfGridPosPoly gpp(new_x.nelem());
00493 gridpos_poly(gpp, x, new_x, order);
00494 Matrix itwp(new_x.nelem(), order+1);
00495 interpweights(itwp, gpp);
00496
00497 interp(y1_new, itwp, y1, gpp);
00498 interp(y2_new, itwp, y2, gpp);
00499 interp(y3_new, itwp, y3, gpp);
00500
00501 cout << "y1_new = ["<< y1_new << "];\n";
00502 cout << "y2_new = ["<< y2_new << "];\n";
00503 cout << "y3_new = ["<< y3_new << "];\n";
00504 }
00505
00506 void test08()
00507 {
00508 cout << "Very simple interpolation case for the "
00509 << "new higher order polynomials.\n";
00510
00511 Vector og(1,5,+1);
00512 Vector ng(2,5,0.25);
00513
00514 cout << "Original grid:\n" << og << "\n";
00515 cout << "New grid:\n" << ng << "\n";
00516
00517
00518 ArrayOfGridPosPoly gp(ng.nelem());
00519
00520 Index order=2;
00521
00522 gridpos_poly(gp,og,ng,order);
00523 cout << "Grid positions:\n" << gp;
00524
00525
00526 Matrix itw(gp.nelem(),order+1);
00527 interpweights(itw,gp);
00528
00529 cout << "Interpolation weights:\n" << itw << "\n";
00530
00531
00532 Vector of(og.nelem(),0);
00533 of[2] = 10;
00534
00535 cout << "Original field:\n" << of << "\n";
00536
00537
00538 Vector nf(ng.nelem());
00539
00540 interp(nf, itw, of, gp);
00541
00542 cout << "New field (order=" << order << "):\n" << nf << "\n";
00543
00544 cout << "All orders systematically:\n";
00545 for (order=1; order<5; ++order)
00546 {
00547 gridpos_poly(gp,og,ng,order);
00548 itw.resize(gp.nelem(),order+1);
00549 interpweights(itw,gp);
00550 interp(nf, itw, of, gp);
00551
00552 cout << "order " << order << ": ";
00553 for (Index i=0; i<nf.nelem(); ++i)
00554 cout << setw(8) << nf[i] << " ";
00555 cout << "\n";
00556 }
00557 }
00558
00559 int main()
00560 {
00561 test08();
00562 }