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

Last change on this file since 2586 was 2578, checked in by ansari, 21 years ago

Adaptation carrt.cc suites modifs methodes Add/Sub/Mul/DivElt(.., res) de TArray - Reza 29 Juillet 2004

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