source: Sophya/trunk/SophyaProg/Tests/arrt.cc@ 2647

Last change on this file since 2647 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

File size: 12.0 KB
RevLine 
[2615]1#include "sopnamsp.h"
[786]2#include "machdefs.h"
3
4#include <math.h>
[2322]5#include <iostream>
6#include <fstream>
[786]7
8#include "tarrinit.h"
[812]9#include "array.h"
[786]10#include "timing.h"
11
[812]12static void tstarr(bool tio);
[1561]13static void tstasciiio();
[812]14static void tstioarr(TArray<int_4> & ia, TArray<int_4> & ib, TArray<int_4> & ic);
15static void tstmtx(int n);
16void _ZZ_TestTMatrixRC_YY_(TMatrix<r_8> & m);
[786]17
[2578]18static int prtlev = 1;
19
[786]20int main(int narg, char* arg[])
21{
22
23 SophyaInit();
24 InitTim(); // Initializing the CPU timer
25
26
[812]27 if (narg < 2) {
[1100]28 cout << " arrt - TArray<T> Test - Usage arrt a/w/m/z [size=5] [prtlev=1] [nprt=50]\n"
[812]29 << " a: Simple Array Test w: a+ PPersist Test (FIO_TArray<T>) \n"
30 << " m: Matrix and Vector Test \n"
[1561]31 << " ascio: Test of ascii I/O \n"
[812]32 << " z: Appel_ZZ_TestTMatrixRC_YY_ " << endl;
33 exit(0);
34 }
35
[1561]36 string opt = arg[1];
[786]37 int n = 5;
[812]38 if (narg > 2) n = atoi(arg[2]);
39 uint_4 nprt = 50;
40 if (narg > 3) prtlev = atoi(arg[3]);
41 if (narg > 4) nprt = atoi(arg[4]);
[786]42
[812]43 BaseArray::SetMaxPrint(nprt, prtlev);
[786]44
45 try {
[1561]46 if (opt == "w") tstarr(true);
47 else if (opt == "a") tstarr(false);
48 else if (opt == "z") {
[812]49 if (n<5) n = 5;
50 TMatrix<r_8> tza(n,n+3);
[1102]51 tza = RegularSequence(2.,1.5);
[812]52 _ZZ_TestTMatrixRC_YY_(tza);
53 TMatrix<r_8> tza2 = tza(Range(1,3), Range(2,5));
54 _ZZ_TestTMatrixRC_YY_(tza2);
55 }
[1561]56 else if (opt == "m") tstmtx(n);
57 else if (opt == "ascio") tstasciiio();
58 else {
59 cout << " arrt/Error : unknown option ! " << endl;
60 }
[812]61 }
62 catch (PThrowable exc) {
63 cerr << " catched Exception " << exc.Msg() << endl;
64 }
65 catch (...) {
66 cerr << " catched unknown (...) exception " << endl;
67 }
68 PrtTim("--- End of arrt ---");
69 cout << " --------------- END of ArrayTest programme -------------- " << endl;
70}
71
72
73void tstmtx(int n)
74{
75 TMatrix<r_4> ma(4,7);
[1102]76 ma = RegularSequence(10., 2.);
[812]77 cout << "\n :::::: Testing TMatrix - TVector :::::: " << endl;
78 TMatrix<r_4> sma1 = ma(Range(1,2), Range(2,4));
[2578]79 if (prtlev > 0)
80 cout << " ----- Matrix ma = \n " << ma << endl;
[812]81 sma1 = 3.14;
[2578]82 if (prtlev > 0)
83 cout << " ----- Matrix ma (Avec sma1=ma(Range(1,2), Range(2,3))=3.14) = \n " << ma << endl;
[812]84 TMatrix<r_4> mb(2,3);
85 mb = 50.;
86 TMatrix<r_4> mc = mb+sma1;
[2578]87 if (prtlev > 0)
88 cout << " ----- Matrix mc = mb(=50.)+sma1(=3.14) = " << mc << endl;
[812]89 TMatrix<r_4> md = (float)100+mc;
[2578]90 if (prtlev > 0) {
91 cout << " ----- Matrix md=100.+mc = " << md << endl;
92 cout << " ----- Matrix md-50 = " << md-(float)50. << endl;
93 }
[812]94
95 TMatrix<int_4> ima(3,10);
[1102]96 ima = RegularSequence(0.01,1.);
[2578]97 if (prtlev > 0) {
98 cout << " ----- Matrix ima(3,10) = RegularSequence(0.01,1.) = " << ima << endl;
99 cout << " ----- Matrix ima.Column(3) = \n" << ima.Column(3) << endl;
100 }
[812]101 TVector<int_4> vcol = ima.Column(7);
[2578]102 if (prtlev > 0) {
103 cout << " ----- Vector vcol=ima.Column(7) = \n" << vcol << endl;
104 cout << " ----- Matrix ima.Row(0) = \n" << ima.Row(0) << endl;
105 }
[820]106 TVector<int_4> vlig;
107 vlig = ima.Row(1);
[2578]108 if (prtlev > 0)
109 cout << " ----- Vector vlig=ima.Row(1) = \n" << vlig << endl;
[812]110
111 TMatrix<int_4> imb(3,10);
[1102]112 imb = RegularSequence(100.01,10.);
[2578]113 if (prtlev > 0)
114 cout << " ----- Matrix imb(3,10) = RegularSequence(100.01,10.) = " << imb << endl;
[812]115 TMatrix<int_4> imc = ima+imb;
[2578]116 if (prtlev > 0)
117 cout << " ----- Matrix imc=ima+imb = " << imc << endl;
[812]118 TMatrix<int_4> imd(3,10);
119 TMatrix<int_4> imd2(3,10);
120 imd += 25;
121 imd *= 2;
122 imd2 = 2;
[2578]123 imd.MulElt(imd2,imd);
124 if (prtlev > 0)
125 cout << " ----- Matrix imd (? = 100) " << imd << endl;
[812]126 imb -= imd;
127 imc -= imd;
[2578]128 if (prtlev > 0)
129 cout << " ----- Matrix ((imc-=imd)-(imb-=imd))-ima (== 0 ?) "
130 << (imc-imb)-ima << endl;
[812]131 TVector<r_4> va(16, BaseArray::RowVector);
[1102]132 va = RegularSequence(0.,1.);
[2578]133 if (prtlev > 0)
134 cout << " ----- Vector va = RegularSequence(0.,1.) = " << va << endl;
[812]135 va(Range(2,0,6,2)) *= 10.;
[2578]136 if (prtlev > 0)
137 cout << " ----- Vector va apres va(Range(2,0,6,2)) *= 10. " << va << endl;
[812]138 va(Range(0,5)) += 100.;
[2578]139 if (prtlev > 0)
140 cout << " ----- Vector va apres va(Range(0,6)) += 100. " << va << endl;
[812]141
[1107]142 TMatrix<r_4> esm(3,5);
[1114]143 EnumeratedSequence ess;
144 ess ,
[1107]145 11 , 12 , 13, 14, 15,
[1114]146 21 , 22 , 23, 24, 25,
147 31 , 32 , 33, 34, 35 ;
148 esm = ess;
[2578]149 if (prtlev > 0)
150 cout << "\n TMatrix<r_4> esm(3,5) = EnumeratedSequence(), 11 , 12 , 13, 14, 15 ... "
151 << esm << endl;
[1114]152 r_4 emin,emax,esum,eprod,esumx2;
153 esm.MinMax(emin, emax);
154 esum = esm.Sum();
155 eprod = esm.Product();
156 esumx2 = esm.SumX2();
[1107]157
[2578]158 if (prtlev > 0)
159 cout << " Min= " << emin << " Max= " << emax
160 << " Sum= " << esum << " Prod=" << eprod << " SumX2=" << esumx2 << endl;
[1114]161
[812]162 cout << "\n :::::: Testing Matrix operations :::::: " << endl;
163 TMatrix<r_4> ra(2,2), ira(2,2);
[1102]164 /* ra(0,0) = 2.; ra(0,1) = 1.;
[812]165 ra(1,0) = 4.; ra(1,1) = 3.;
[1102]166 */
[812]167 ira(0,0) = 1.5; ira(0,1) = -0.5;
168 ira(1,0) = -2.; ira(1,1) = 1.;
[1102]169 EnumeratedSequence es;
170 es, 2 , 1 , 4 , 3 ;
171 ra = es ;
172
[2578]173 if (prtlev > 0) {
174 cout << " ----- Matrix ra = \n " << ra << endl;
175 cout << " ----- Matrix ira = \n " << ira << endl;
176 cout << " ----- Matrix ira*ra = (= Identity ?) " << ira*ra << endl;
177 }
[812]178
179 cout << "\n ::::::: Testing Matrix Inversion ::::::: " << endl;
180 SimpleMatrixOperation<r_4> smr;
181 TMatrix<r_4> cira = smr.Inverse(ra);
[2578]182 if (prtlev > 0) {
183 cout << " ----- Matrix Inverse(ra) = " << cira << endl;
184 cout << " ----- ira-Inverse(ra) = " << ira-cira << endl;
185 }
[812]186 Matrix a(n,n);
187
188
[857]189 // for(int i=0; i<n; i++)
190 // for(int j=0; j<n; j++) a(j,i) = GauRnd(0., 1.);
[1102]191 a = RandomSequence(RandomSequence::Gaussian, 0., 2.5);
[2578]192 if (prtlev > 0)
193 cout << " ----- Matrix a=RandomSequence(RandomSequence::Gaussian, 0., 2.5) = \n " << a << endl;
[812]194 Vector x(n);
[1102]195 x = RegularSequence(1.,3.);
[812]196 Vector b = a*x;
[2578]197 if (prtlev > 0) {
198 cout << " ----- Vector x = \n " << x << endl;
199 cout << " ----- Vector b = a*x = \n " << b << endl;
200 }
[812]201 SimpleMatrixOperation<r_8> smo;
202 Matrix inva = smo.Inverse(a);
[2578]203 if (prtlev > 0) {
204 cout << " ----- Matrix Inverse(a) = \n " << inva << endl;
205 cout << " ----- Matrix a*Inverse(a) = \n " << inva*a << endl;
206 cout << " ----- Matrix Inverse(a)*b (=Inv(a)*a*x) = \n " << inva*b << endl;
207 cout << " ----- Matrix x-Inverse(a)*b = (=0 ?)\n " << x-inva*b << endl;
208 }
[812]209}
210
211void tstarr(bool tio)
212{
[786]213 cout << "\n -----> Testing TArray <---- " << endl;
[789]214 // We create a integer array SizeX=7, SizeY=5
[786]215 TArray<int_4> ia(7,5);
[789]216 // We fill it with a sequence of numbers starting at 10., with step = 2.
[1102]217 ia = RegularSequence(10., 2.);
[812]218 cout << " ----- Array IA = \n " << ia << endl;
219 // sub array extraction, Range(2,4) : starting position=2 , End=4
220 TArray<int_4> ic = ia(Range(2,3),Range(1,3),Range(0));
[1100]221 cout << " ----- Array IC IA(Range(2,3),Range(1,3)) = \n " << ic << endl;
[789]222 // we set the sub-array to zero, this should reflect in the original array
223 // sub-arrays share their data with parent array
[786]224 ic = 0;
[812]225 cout << " ----- Array IC Apres (=0) = \n " << ic << endl;
226 cout << " ----- Array IA Apres IC=0 = \n " << ia << endl;
[786]227
228 cout << " :::: 3 Dim arrays ::::: " << endl;
229 TArray<int_4>::SetMaxPrint(1000);
[789]230 // Creating 3-dim array (X=8 x Y=7 x Z=2) , filling it with 5
[786]231 TArray<int_4> ib(8,7,2);
232 ib = 5;
[812]233 cout << " ----- Array IB = \n " << ib << endl;
[789]234 // Sub array extraction X from 1 , size 4 - Y from 2 , size 3 , in Z default, from 0, size 1
235 // we multiply this sub-array elements by 3
[812]236 ib(Range(1,4),Range(2,4), Range(0)) *= 3;
237 cout << " -- Array IB , Apres ib(Range(1,4),Range(2,4))*=3 : " << endl;
[786]238 cout << ib;
[812]239 cout << " -- Array ib(Range(1,4),Range(2,4)) = : "
240 << ib(Range(1,4),Range(2,4), Range(0)) << endl;
[789]241
242 // Creating a double array X=5 x Y=2
243 TArray<r_4> fa(5,2);
244 // fill it up with a sequence of 0. to 1.
[1102]245 fa = RegularSequence(0.,1./(5*2));
[789]246 cout << " ------ TArray<r_4> fa(5,2) = \n" << fa << endl;
247 // Create a new array from the original array , multiplying it by 2*Pi
248 TArray<r_4> fa2 = fa*(float)(2.*M_PI);
249 cout << " ------ TArray<r_4> fa2=fa*2*Pi = \n" << fa2 << endl;
250 // Compute sin(fa2) cos(fa2)
[1229]251 cout << " ------ sin(fa2=fa*2*Pi) = \n" << Sin(fa2) << endl;
252 cout << " ------ cos(fa2=fa*2*Pi) = \n" << Cos(fa2) << endl;
[807]253
[1519]254 // Test des fonctions sur complexes
255 TVector<r_4> p_real(10, BaseArray::RowVector), p_imag(10, BaseArray::RowVector);
256 p_real = RegularSequence(0., 0.5);
257 p_imag = RegularSequence(0., 0.25);
258 TVector< complex<r_4> > zvec = ComplexArray(p_real, p_imag);
259 cout << " ------ zvec = ComplexArray(p_real, p_imag) -------- " << endl;
260 cout << " :: p_real= " << p_real;
261 cout << " :: p_imag= " << p_imag;
262 cout << " :: zvec= " << zvec;
263 cout << " :: real(zvec) = " << real(zvec) ;
264 cout << " :::: imag(zvec) = " << imag(zvec) ;
265 cout << " :::: module2(zvec) = " << module2(zvec) ;
266 cout << " :::: module(zvec) = " << module(zvec) ;
267 cout << " :::: phase(zvec) = " << phase(zvec) ;
268 cout << " ----------------------------------------------------- " << endl;
269
[807]270 if (tio) tstioarr(ia, ib, ic);
[786]271}
[807]272
273void tstioarr(TArray<int_4> & ia, TArray<int_4> & ib, TArray<int_4> & ic)
274{
275 cout << " ------ tstioarr(TArray<int_4> & ia, TArray<int_4> & ib, TArray<int_4> & ic) ---- " << endl;
276 {
277 cout << " >>>>>> Writing in arrt.ppf <<<<<<< " << endl;
278 POutPersist pos("arrt.ppf");
279 // We write the three arrays in the stream
280 pos << ia << ib << ic;
281 cout << " >>>>>> Writing in arrtn.ppf with names <<<<<<<" << endl;
282 POutPersist posn("arrtn.ppf");
283 string tag = "ArrIA";
284 posn.PutObject(ia, tag);
285 tag = "ArrIB";
286 posn.PutObject(ib, tag);
287 tag = "ArrIC";
288 posn.PutObject(ic, tag);
289 }
290
291 {
292 cout << " >>>>>>> Reading from arrt.ppf <<<<< " << endl;
293 PInPersist pis("arrt.ppf");
294 TArray<int_4> iaa, ibb, icc;
295 // We read the three arrays from the stream
296 pis >> iaa >> ibb >> icc;
[812]297 cout << " ----- Array IAA = \n " << iaa << endl;
298 cout << " ----- Array IBB = \n " << ibb << endl;
299 cout << " ----- Array ICC = \n " << icc << endl;
[807]300 icc = 12;
[812]301 cout << " ----- Array ICC (=12) = \n " << icc << endl;
302 cout << " ----- Array IAA (ICC=12) = \n " << iaa << endl;
[807]303 }
304
305 {
306 cout << " >>>>>>> Reading from arrtn.ppf <<<<< " << endl;
307 PInPersist pis("arrtn.ppf");
308 TArray<int_4> iaa, ibb, icc;
309 // We read the three arrays from the stream
310 string tag = "ArrIC";
311 pis.GetObject(icc, tag);
312 tag = "ArrIB";
313 pis.GetObject(ibb, tag);
314 tag = "ArrIA";
315 pis.GetObject(iaa, tag);
316
[812]317 cout << " ----- Array IAAA = \n " << iaa << endl;
318 cout << " ----- Array IBBB = \n " << ibb << endl;
319 cout << " ----- Array ICCC = \n " << icc << endl;
[807]320 icc = 68;
[812]321 cout << " ----- Array ICCC (=12) = \n " << icc << endl;
322 cout << " ----- Array IAAA (ICC=12) = \n " << iaa << endl;
[807]323 }
324
[1561]325}
[807]326
[1561]327
328void tstasciiio()
329{
330
331cout << "\n -----> Testing EnumeratedSequence/TArray ASCII I/O <---- " << endl;
332
333EnumeratedSequence ess;
334int nbad;
335ess.Append("56.5 (1.,-1.) 4 8 16", nbad);
336cout << " --- ess.Append(\"56.5 (1.,-1.) 4 8 16\", nbad) : ess=" << endl;
337cout << ess;
338
339EnumeratedSequence ess2;
340ess2.Append("56.5 (-3.14, 12) 'Hello' 4. 8.565432 12 32 3.1415e-6", nbad);
341cout << " --- ess2.Append(...) ess2=" << endl;
342cout << ess2;
343cout << " --- ess2.Append(...) (2) ess2=" << endl;
344ess2.Append("(2.7, -8) Bonjour ByeBye 'Comment ca va ?' 64 64.7 128 1.28 25.6e3",
345 nbad);
346cout << ess2;
347
348Matrix A(4,6);
349A = RegularSequence(0.5, 0.2);
350cout << " Matrix A(4,6); A=RegularSequence(0.5, 0.2) : " << endl;
351cout << A ;
352
353char * flnm = "arr_ascii.txt";
354{
355cout << " Writing A to file " << flnm << endl;
356ofstream ofs(flnm);
357A.WriteASCII(ofs);
[807]358}
[1561]359
360Matrix B;
361sa_size_t nr, nc;
362{
363ifstream ifs(flnm);
364B.ReadASCII(ifs,nr,nc);
365}
366
367cout << " Matrix B; B.ReadASCII() from file " << flnm << endl;
368cout << B ;
369
370Vector C;
371{
372ifstream ifs(flnm);
[1577]373ifs >> C;
[1561]374}
[1577]375cout << " Vector C; ifstream( " << flnm << ") >> C " << endl;
[1561]376cout << " C.Transpose()= " << C.Transpose() ;
377
378TVector<int_4> IC;
379{
380ifstream ifs(flnm);
381IC.ReadASCII(ifs,nr,nc);
382}
383cout << " TVector<int_4> IC; IC.ReadASCII() from file " << flnm << endl;
384cout << " IC.Transpose()= " << IC.Transpose() ;
385
386TArray<r_4> D(4,6,2);
387{
388ifstream ifs(flnm);
389D.ReadASCII(ifs,nr,nc);
390}
391cout << " TArray<r_4> D(4,6,2); D.ReadASCII() from file " << flnm << endl;
392cout << D ;
393
394}
Note: See TracBrowser for help on using the repository browser.