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