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

Last change on this file since 2877 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
Line 
1#include "sopnamsp.h"
2#include "machdefs.h"
3
4#include <math.h>
5#include <iostream>
6#include <fstream>
7
8#include "tarrinit.h"
9#include "array.h"
10#include "timing.h"
11
12static void tstarr(bool tio);
13static void tstasciiio();
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);
17
18static int prtlev = 1;
19
20int main(int narg, char* arg[])
21{
22
23 SophyaInit();
24 InitTim(); // Initializing the CPU timer
25
26
27 if (narg < 2) {
28 cout << " arrt - TArray<T> Test - Usage arrt a/w/m/z [size=5] [prtlev=1] [nprt=50]\n"
29 << " a: Simple Array Test w: a+ PPersist Test (FIO_TArray<T>) \n"
30 << " m: Matrix and Vector Test \n"
31 << " ascio: Test of ascii I/O \n"
32 << " z: Appel_ZZ_TestTMatrixRC_YY_ " << endl;
33 exit(0);
34 }
35
36 string opt = arg[1];
37 int n = 5;
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]);
42
43 BaseArray::SetMaxPrint(nprt, prtlev);
44
45 try {
46 if (opt == "w") tstarr(true);
47 else if (opt == "a") tstarr(false);
48 else if (opt == "z") {
49 if (n<5) n = 5;
50 TMatrix<r_8> tza(n,n+3);
51 tza = RegularSequence(2.,1.5);
52 _ZZ_TestTMatrixRC_YY_(tza);
53 TMatrix<r_8> tza2 = tza(Range(1,3), Range(2,5));
54 _ZZ_TestTMatrixRC_YY_(tza2);
55 }
56 else if (opt == "m") tstmtx(n);
57 else if (opt == "ascio") tstasciiio();
58 else {
59 cout << " arrt/Error : unknown option ! " << endl;
60 }
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);
76 ma = RegularSequence(10., 2.);
77 cout << "\n :::::: Testing TMatrix - TVector :::::: " << endl;
78 TMatrix<r_4> sma1 = ma(Range(1,2), Range(2,4));
79 if (prtlev > 0)
80 cout << " ----- Matrix ma = \n " << ma << endl;
81 sma1 = 3.14;
82 if (prtlev > 0)
83 cout << " ----- Matrix ma (Avec sma1=ma(Range(1,2), Range(2,3))=3.14) = \n " << ma << endl;
84 TMatrix<r_4> mb(2,3);
85 mb = 50.;
86 TMatrix<r_4> mc = mb+sma1;
87 if (prtlev > 0)
88 cout << " ----- Matrix mc = mb(=50.)+sma1(=3.14) = " << mc << endl;
89 TMatrix<r_4> md = (float)100+mc;
90 if (prtlev > 0) {
91 cout << " ----- Matrix md=100.+mc = " << md << endl;
92 cout << " ----- Matrix md-50 = " << md-(float)50. << endl;
93 }
94
95 TMatrix<int_4> ima(3,10);
96 ima = RegularSequence(0.01,1.);
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 }
101 TVector<int_4> vcol = ima.Column(7);
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 }
106 TVector<int_4> vlig;
107 vlig = ima.Row(1);
108 if (prtlev > 0)
109 cout << " ----- Vector vlig=ima.Row(1) = \n" << vlig << endl;
110
111 TMatrix<int_4> imb(3,10);
112 imb = RegularSequence(100.01,10.);
113 if (prtlev > 0)
114 cout << " ----- Matrix imb(3,10) = RegularSequence(100.01,10.) = " << imb << endl;
115 TMatrix<int_4> imc = ima+imb;
116 if (prtlev > 0)
117 cout << " ----- Matrix imc=ima+imb = " << imc << endl;
118 TMatrix<int_4> imd(3,10);
119 TMatrix<int_4> imd2(3,10);
120 imd += 25;
121 imd *= 2;
122 imd2 = 2;
123 imd.MulElt(imd2,imd);
124 if (prtlev > 0)
125 cout << " ----- Matrix imd (? = 100) " << imd << endl;
126 imb -= imd;
127 imc -= imd;
128 if (prtlev > 0)
129 cout << " ----- Matrix ((imc-=imd)-(imb-=imd))-ima (== 0 ?) "
130 << (imc-imb)-ima << endl;
131 TVector<r_4> va(16, BaseArray::RowVector);
132 va = RegularSequence(0.,1.);
133 if (prtlev > 0)
134 cout << " ----- Vector va = RegularSequence(0.,1.) = " << va << endl;
135 va(Range(2,0,6,2)) *= 10.;
136 if (prtlev > 0)
137 cout << " ----- Vector va apres va(Range(2,0,6,2)) *= 10. " << va << endl;
138 va(Range(0,5)) += 100.;
139 if (prtlev > 0)
140 cout << " ----- Vector va apres va(Range(0,6)) += 100. " << va << endl;
141
142 TMatrix<r_4> esm(3,5);
143 EnumeratedSequence ess;
144 ess ,
145 11 , 12 , 13, 14, 15,
146 21 , 22 , 23, 24, 25,
147 31 , 32 , 33, 34, 35 ;
148 esm = ess;
149 if (prtlev > 0)
150 cout << "\n TMatrix<r_4> esm(3,5) = EnumeratedSequence(), 11 , 12 , 13, 14, 15 ... "
151 << esm << endl;
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();
157
158 if (prtlev > 0)
159 cout << " Min= " << emin << " Max= " << emax
160 << " Sum= " << esum << " Prod=" << eprod << " SumX2=" << esumx2 << endl;
161
162 cout << "\n :::::: Testing Matrix operations :::::: " << endl;
163 TMatrix<r_4> ra(2,2), ira(2,2);
164 /* ra(0,0) = 2.; ra(0,1) = 1.;
165 ra(1,0) = 4.; ra(1,1) = 3.;
166 */
167 ira(0,0) = 1.5; ira(0,1) = -0.5;
168 ira(1,0) = -2.; ira(1,1) = 1.;
169 EnumeratedSequence es;
170 es, 2 , 1 , 4 , 3 ;
171 ra = es ;
172
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 }
178
179 cout << "\n ::::::: Testing Matrix Inversion ::::::: " << endl;
180 SimpleMatrixOperation<r_4> smr;
181 TMatrix<r_4> cira = smr.Inverse(ra);
182 if (prtlev > 0) {
183 cout << " ----- Matrix Inverse(ra) = " << cira << endl;
184 cout << " ----- ira-Inverse(ra) = " << ira-cira << endl;
185 }
186 Matrix a(n,n);
187
188
189 // for(int i=0; i<n; i++)
190 // for(int j=0; j<n; j++) a(j,i) = GauRnd(0., 1.);
191 a = RandomSequence(RandomSequence::Gaussian, 0., 2.5);
192 if (prtlev > 0)
193 cout << " ----- Matrix a=RandomSequence(RandomSequence::Gaussian, 0., 2.5) = \n " << a << endl;
194 Vector x(n);
195 x = RegularSequence(1.,3.);
196 Vector b = a*x;
197 if (prtlev > 0) {
198 cout << " ----- Vector x = \n " << x << endl;
199 cout << " ----- Vector b = a*x = \n " << b << endl;
200 }
201 SimpleMatrixOperation<r_8> smo;
202 Matrix inva = smo.Inverse(a);
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 }
209}
210
211void tstarr(bool tio)
212{
213 cout << "\n -----> Testing TArray <---- " << endl;
214 // We create a integer array SizeX=7, SizeY=5
215 TArray<int_4> ia(7,5);
216 // We fill it with a sequence of numbers starting at 10., with step = 2.
217 ia = RegularSequence(10., 2.);
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));
221 cout << " ----- Array IC IA(Range(2,3),Range(1,3)) = \n " << ic << endl;
222 // we set the sub-array to zero, this should reflect in the original array
223 // sub-arrays share their data with parent array
224 ic = 0;
225 cout << " ----- Array IC Apres (=0) = \n " << ic << endl;
226 cout << " ----- Array IA Apres IC=0 = \n " << ia << endl;
227
228 cout << " :::: 3 Dim arrays ::::: " << endl;
229 TArray<int_4>::SetMaxPrint(1000);
230 // Creating 3-dim array (X=8 x Y=7 x Z=2) , filling it with 5
231 TArray<int_4> ib(8,7,2);
232 ib = 5;
233 cout << " ----- Array IB = \n " << ib << endl;
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
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;
238 cout << ib;
239 cout << " -- Array ib(Range(1,4),Range(2,4)) = : "
240 << ib(Range(1,4),Range(2,4), Range(0)) << endl;
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.
245 fa = RegularSequence(0.,1./(5*2));
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)
251 cout << " ------ sin(fa2=fa*2*Pi) = \n" << Sin(fa2) << endl;
252 cout << " ------ cos(fa2=fa*2*Pi) = \n" << Cos(fa2) << endl;
253
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
270 if (tio) tstioarr(ia, ib, ic);
271}
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;
297 cout << " ----- Array IAA = \n " << iaa << endl;
298 cout << " ----- Array IBB = \n " << ibb << endl;
299 cout << " ----- Array ICC = \n " << icc << endl;
300 icc = 12;
301 cout << " ----- Array ICC (=12) = \n " << icc << endl;
302 cout << " ----- Array IAA (ICC=12) = \n " << iaa << endl;
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
317 cout << " ----- Array IAAA = \n " << iaa << endl;
318 cout << " ----- Array IBBB = \n " << ibb << endl;
319 cout << " ----- Array ICCC = \n " << icc << endl;
320 icc = 68;
321 cout << " ----- Array ICCC (=12) = \n " << icc << endl;
322 cout << " ----- Array IAAA (ICC=12) = \n " << iaa << endl;
323 }
324
325}
326
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);
358}
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);
373ifs >> C;
374}
375cout << " Vector C; ifstream( " << flnm << ") >> C " << endl;
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.