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