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

Last change on this file since 837 was 820, checked in by ansari, 25 years ago

Modif test TArray<T> - Reza 06/04/2000

File size: 8.4 KB
Line 
1#include "machdefs.h"
2
3#include <math.h>
4#include <iostream.h>
5
6#include "nbrandom.h"
7#include "tarrinit.h"
8#include "array.h"
9// #include "fioarr.h"
10// #include "matharr.h"
11#include "timing.h"
12
13static void tstarr(bool tio);
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
18int main(int narg, char* arg[])
19{
20
21 SophyaInit();
22 InitTim(); // Initializing the CPU timer
23
24
25 if (narg < 2) {
26 cout << " arrt - TArray<T> Test - Usage arrt a/p/m/z [size=5] [prtlev=1] [nprt=50]\n"
27 << " a: Simple Array Test w: a+ PPersist Test (FIO_TArray<T>) \n"
28 << " m: Matrix and Vector Test \n"
29 << " z: Appel_ZZ_TestTMatrixRC_YY_ " << endl;
30 exit(0);
31 }
32
33 int 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') || (opt == 'a')) tstarr((opt=='w'));
45 else if (opt == 'z') {
46 if (n<5) n = 5;
47 TMatrix<r_8> tza(n,n+3);
48 tza = Sequence(2.,1.5);
49 _ZZ_TestTMatrixRC_YY_(tza);
50 TMatrix<r_8> tza2 = tza(Range(1,3), Range(2,5));
51 _ZZ_TestTMatrixRC_YY_(tza2);
52 }
53 else tstmtx(n);
54 }
55 catch (PThrowable exc) {
56 cerr << " catched Exception " << exc.Msg() << endl;
57 }
58 catch (...) {
59 cerr << " catched unknown (...) exception " << endl;
60 }
61 PrtTim("--- End of arrt ---");
62 cout << " --------------- END of ArrayTest programme -------------- " << endl;
63}
64
65
66void tstmtx(int n)
67{
68 TMatrix<r_4> ma(4,7);
69 ma = Sequence(10., 2.);
70 cout << "\n :::::: Testing TMatrix - TVector :::::: " << endl;
71 TMatrix<r_4> sma1 = ma(Range(1,2), Range(2,4));
72 cout << " ----- Matrix ma = \n " << ma << endl;
73 sma1 = 3.14;
74 cout << " ----- Matrix ma (Avec sma1=ma(Range(1,2), Range(2,3))=3.14) = \n " << ma << endl;
75 TMatrix<r_4> mb(2,3);
76 mb = 50.;
77 TMatrix<r_4> mc = mb+sma1;
78 cout << " ----- Matrix mc = mb(=50.)+sma1(=3.14) = " << mc << endl;
79 TMatrix<r_4> md = (float)100+mc;
80 cout << " ----- Matrix md=100.+mc = " << md << endl;
81 cout << " ----- Matrix md-50 = " << md-(float)50. << endl;
82
83 TMatrix<int_4> ima(3,10);
84 ima = Sequence(0.01,1.);
85 cout << " ----- Matrix ima(3,10) = Sequence(0.01,1.) = " << ima << endl;
86 cout << " ----- Matrix ima.Column(3) = \n" << ima.Column(3) << endl;
87 TVector<int_4> vcol = ima.Column(7);
88 cout << " ----- Vector vcol=ima.Column(7) = \n" << vcol << endl;
89 cout << " ----- Matrix ima.Row(0) = \n" << ima.Row(0) << endl;
90 TVector<int_4> vlig;
91 vlig = ima.Row(1);
92 cout << " ----- Vector vlig=ima.Row(1) = \n" << vlig << endl;
93
94 TMatrix<int_4> imb(3,10);
95 imb = Sequence(100.01,10.);
96 cout << " ----- Matrix imb(3,10) = Sequence(100.01,10.) = " << imb << endl;
97 TMatrix<int_4> imc = ima+imb;
98 cout << " ----- Matrix imc=ima+imb = " << imc << endl;
99 TMatrix<int_4> imd(3,10);
100 TMatrix<int_4> imd2(3,10);
101 imd += 25;
102 imd *= 2;
103 imd2 = 2;
104 imd.MulElt(imd2);
105 cout << " ----- Matrix imd (? = 100) " << imd << endl;
106 imb -= imd;
107 imc -= imd;
108 cout << " ----- Matrix ((imc-=imd)-(imb-=imd))-ima (== 0 ?) "
109 << (imc-imb)-ima << endl;
110 TVector<r_4> va(16, BaseArray::RowVector);
111 va = Sequence(0.,1.);
112 cout << " ----- Vector va = Sequence(0.,1.) = " << va << endl;
113 va(Range(2,0,6,2)) *= 10.;
114 cout << " ----- Vector va apres va(Range(2,0,6,2)) *= 10. " << va << endl;
115 va(Range(0,5)) += 100.;
116 cout << " ----- Vector va apres va(Range(0,6)) += 100. " << va << endl;
117
118 cout << "\n :::::: Testing Matrix operations :::::: " << endl;
119 TMatrix<r_4> ra(2,2), ira(2,2);
120 ra(0,0) = 2.; ra(0,1) = 1.;
121 ra(1,0) = 4.; ra(1,1) = 3.;
122 ira(0,0) = 1.5; ira(0,1) = -0.5;
123 ira(1,0) = -2.; ira(1,1) = 1.;
124 cout << " ----- Matrix ra = \n " << ra << endl;
125 cout << " ----- Matrix ira = \n " << ira << endl;
126 cout << " ----- Matrix ira*ra = (= Identity ?) " << ira*ra << endl;
127
128
129 cout << "\n ::::::: Testing Matrix Inversion ::::::: " << endl;
130 SimpleMatrixOperation<r_4> smr;
131 TMatrix<r_4> cira = smr.Inverse(ra);
132 cout << " ----- Matrix Inverse(ra) = " << cira << endl;
133 cout << " ----- ira-Inverse(ra) = " << ira-cira << endl;
134
135 Matrix a(n,n);
136
137
138 for(int i=0; i<n; i++)
139 for(int j=0; j<n; j++) a(j,i) = GauRnd(0., 1.);
140 cout << " ----- Matrix a = \n " << a << endl;
141 Vector x(n);
142 x = Sequence(1.,3.);
143 Vector b = a*x;
144 cout << " ----- Vector x = \n " << x << endl;
145 cout << " ----- Vector b = a*x = \n " << b << endl;
146 SimpleMatrixOperation<r_8> smo;
147 Matrix inva = smo.Inverse(a);
148 cout << " ----- Matrix Inverse(a) = \n " << inva << endl;
149 cout << " ----- Matrix a*Inverse(a) = \n " << inva*a << endl;
150 cout << " ----- Matrix Inverse(a)*b (=Inv(a)*a*x) = \n " << inva*b << endl;
151 cout << " ----- Matrix x-Inverse(a)*b = (=0 ?)\n " << x-inva*b << endl;
152}
153
154void tstarr(bool tio)
155{
156 cout << "\n -----> Testing TArray <---- " << endl;
157 // We create a integer array SizeX=7, SizeY=5
158 TArray<int_4> ia(7,5);
159 // We fill it with a sequence of numbers starting at 10., with step = 2.
160 ia = Sequence(10., 2.);
161 cout << " ----- Array IA = \n " << ia << endl;
162 // sub array extraction, Range(2,4) : starting position=2 , End=4
163 TArray<int_4> ic = ia(Range(2,3),Range(1,3),Range(0));
164 cout << " ----- Array IC IA(Range(2,4),Range(1,2)) = \n " << ic << endl;
165 // we set the sub-array to zero, this should reflect in the original array
166 // sub-arrays share their data with parent array
167 ic = 0;
168 cout << " ----- Array IC Apres (=0) = \n " << ic << endl;
169 cout << " ----- Array IA Apres IC=0 = \n " << ia << endl;
170
171 cout << " :::: 3 Dim arrays ::::: " << endl;
172 TArray<int_4>::SetMaxPrint(1000);
173 // Creating 3-dim array (X=8 x Y=7 x Z=2) , filling it with 5
174 TArray<int_4> ib(8,7,2);
175 ib = 5;
176 cout << " ----- Array IB = \n " << ib << endl;
177 // Sub array extraction X from 1 , size 4 - Y from 2 , size 3 , in Z default, from 0, size 1
178 // we multiply this sub-array elements by 3
179 ib(Range(1,4),Range(2,4), Range(0)) *= 3;
180 cout << " -- Array IB , Apres ib(Range(1,4),Range(2,4))*=3 : " << endl;
181 cout << ib;
182 cout << " -- Array ib(Range(1,4),Range(2,4)) = : "
183 << ib(Range(1,4),Range(2,4), Range(0)) << endl;
184
185 // Creating a double array X=5 x Y=2
186 TArray<r_4> fa(5,2);
187 // fill it up with a sequence of 0. to 1.
188 fa = Sequence(0.,1./(5*2));
189 cout << " ------ TArray<r_4> fa(5,2) = \n" << fa << endl;
190 // Create a new array from the original array , multiplying it by 2*Pi
191 TArray<r_4> fa2 = fa*(float)(2.*M_PI);
192 cout << " ------ TArray<r_4> fa2=fa*2*Pi = \n" << fa2 << endl;
193 // Compute sin(fa2) cos(fa2)
194 cout << " ------ sin(fa2=fa*2*Pi) = \n" << sin(fa2) << endl;
195 cout << " ------ cos(fa2=fa*2*Pi) = \n" << cos(fa2) << endl;
196
197 if (tio) tstioarr(ia, ib, ic);
198}
199
200void tstioarr(TArray<int_4> & ia, TArray<int_4> & ib, TArray<int_4> & ic)
201{
202 cout << " ------ tstioarr(TArray<int_4> & ia, TArray<int_4> & ib, TArray<int_4> & ic) ---- " << endl;
203 {
204 cout << " >>>>>> Writing in arrt.ppf <<<<<<< " << endl;
205 POutPersist pos("arrt.ppf");
206 // We write the three arrays in the stream
207 pos << ia << ib << ic;
208 cout << " >>>>>> Writing in arrtn.ppf with names <<<<<<<" << endl;
209 POutPersist posn("arrtn.ppf");
210 string tag = "ArrIA";
211 posn.PutObject(ia, tag);
212 tag = "ArrIB";
213 posn.PutObject(ib, tag);
214 tag = "ArrIC";
215 posn.PutObject(ic, tag);
216 }
217
218 {
219 cout << " >>>>>>> Reading from arrt.ppf <<<<< " << endl;
220 PInPersist pis("arrt.ppf");
221 TArray<int_4> iaa, ibb, icc;
222 // We read the three arrays from the stream
223 pis >> iaa >> ibb >> icc;
224 cout << " ----- Array IAA = \n " << iaa << endl;
225 cout << " ----- Array IBB = \n " << ibb << endl;
226 cout << " ----- Array ICC = \n " << icc << endl;
227 icc = 12;
228 cout << " ----- Array ICC (=12) = \n " << icc << endl;
229 cout << " ----- Array IAA (ICC=12) = \n " << iaa << endl;
230 }
231
232 {
233 cout << " >>>>>>> Reading from arrtn.ppf <<<<< " << endl;
234 PInPersist pis("arrtn.ppf");
235 TArray<int_4> iaa, ibb, icc;
236 // We read the three arrays from the stream
237 string tag = "ArrIC";
238 pis.GetObject(icc, tag);
239 tag = "ArrIB";
240 pis.GetObject(ibb, tag);
241 tag = "ArrIA";
242 pis.GetObject(iaa, tag);
243
244 cout << " ----- Array IAAA = \n " << iaa << endl;
245 cout << " ----- Array IBBB = \n " << ibb << endl;
246 cout << " ----- Array ICCC = \n " << icc << endl;
247 icc = 68;
248 cout << " ----- Array ICCC (=12) = \n " << icc << endl;
249 cout << " ----- Array IAAA (ICC=12) = \n " << iaa << endl;
250 }
251
252
253}
Note: See TracBrowser for help on using the repository browser.