| 1 | // Test de l'inversion de matrices et valeurs propres (avec Lapack) (cmv 21/07/04) | 
|---|
| 2 | //       cmvtminv -a 1234 -l 0 -s 1 -b 25,10000 -n 50 -S | 
|---|
| 3 |  | 
|---|
| 4 | /////////////////////////////////////////////////// | 
|---|
| 5 | /////////////////////////////////////////////////// | 
|---|
| 6 | // PARTIE POUVANT ETRE CHANGEE PAR L'UTILISATEUR // | 
|---|
| 7 | /////////////////////////////////////////////////// | 
|---|
| 8 | /////////////////////////////////////////////////// | 
|---|
| 9 |  | 
|---|
| 10 | // --- Choix de travailler avec des matrices complexes ? | 
|---|
| 11 | //#define COMPLEX | 
|---|
| 12 |  | 
|---|
| 13 | ////////////////////////////////////////////////// | 
|---|
| 14 | // --- Choix de travailler en simple precision ? | 
|---|
| 15 | //#define PRECIS32 | 
|---|
| 16 |  | 
|---|
| 17 | ////////////////////////////////////////////////// | 
|---|
| 18 | // --- Choix GausPiv + Lapack ? | 
|---|
| 19 | #define USE_GAUSPIV | 
|---|
| 20 | #define USE_LAPACK | 
|---|
| 21 |  | 
|---|
| 22 | // --- Choix de ce que doit faire Lapack | 
|---|
| 23 | #define ALSO_LAPACK_INV | 
|---|
| 24 | #define ALSO_LAPACK_INV_SYM | 
|---|
| 25 | #define ALSO_LAPACK_INV_LSS | 
|---|
| 26 | #define ALSO_LAPACK_INV_LSS_SVD | 
|---|
| 27 | #define ALSO_LAPACK_EV | 
|---|
| 28 | #define ALSO_LAPACK_EV_SYM | 
|---|
| 29 | #define ALSO_LAPACK_SVD | 
|---|
| 30 | #define ALSO_LAPACK_SVD_DC | 
|---|
| 31 |  | 
|---|
| 32 | ////////////////////////////////////////////////// | 
|---|
| 33 | ////////////////////////////////////////////////// | 
|---|
| 34 | //           NE RIEN CHANGER CI-APRES           // | 
|---|
| 35 | ////////////////////////////////////////////////// | 
|---|
| 36 | ////////////////////////////////////////////////// | 
|---|
| 37 |  | 
|---|
| 38 | ////////////////////////////////////////////////// | 
|---|
| 39 | #include "machdefs.h" | 
|---|
| 40 | #include <iostream> | 
|---|
| 41 | #include <stdlib.h> | 
|---|
| 42 | #include <stdio.h> | 
|---|
| 43 | #include <string.h> | 
|---|
| 44 | #include <math.h> | 
|---|
| 45 | #include <unistd.h> | 
|---|
| 46 | #include "timing.h" | 
|---|
| 47 | #include "ntoolsinit.h" | 
|---|
| 48 | #include "pexceptions.h" | 
|---|
| 49 | #include "array.h" | 
|---|
| 50 | #include "srandgen.h" | 
|---|
| 51 | #if defined(USE_LAPACK) | 
|---|
| 52 | #include "intflapack.h" | 
|---|
| 53 | #endif | 
|---|
| 54 |  | 
|---|
| 55 | ////////////////////////////////////////////////// | 
|---|
| 56 | #if defined(COMPLEX) | 
|---|
| 57 | #if defined(PRECIS32) | 
|---|
| 58 | #define TYPE complex<r_4> | 
|---|
| 59 | #define TYPER r_4 | 
|---|
| 60 | #else | 
|---|
| 61 | #define TYPE complex<r_8> | 
|---|
| 62 | #define TYPER r_8 | 
|---|
| 63 | #endif | 
|---|
| 64 | #define REAL_PART(_x_) (TYPE((_x_).real(),0.)) | 
|---|
| 65 | #define CONJ_VAL(_x_) (TYPE((_x_).real(),-(_x_).imag())) | 
|---|
| 66 | #define ABS_VAL(_x_) sqrt((double)((_x_).real()*(_x_).real() + (_x_).imag()*(_x_).imag())) | 
|---|
| 67 | #else | 
|---|
| 68 | #if defined(PRECIS32) | 
|---|
| 69 | #define TYPE r_4 | 
|---|
| 70 | #define TYPER r_4 | 
|---|
| 71 | #else | 
|---|
| 72 | #define TYPE r_8 | 
|---|
| 73 | #define TYPER r_8 | 
|---|
| 74 | #endif | 
|---|
| 75 | #define REAL_PART(_x_) (_x_) | 
|---|
| 76 | #define CONJ_VAL(_x_) (_x_) | 
|---|
| 77 | #define ABS_VAL(_x_) fabs((double)_x_) | 
|---|
| 78 | #endif | 
|---|
| 79 |  | 
|---|
| 80 | ////////////////////////////////////////////////// | 
|---|
| 81 | void Symetrize(TMatrix< TYPE >& A); | 
|---|
| 82 | void Hermitian(TMatrix< TYPE >& A); | 
|---|
| 83 | r_8  Check_Mat_Ident(TMatrix< TYPE >& A); | 
|---|
| 84 | r_8  Check_Mat_Zero(TMatrix< TYPE >& A); | 
|---|
| 85 | r_8  Check_Mat_VecCol_0(TMatrix< TYPE >& A); | 
|---|
| 86 | void  Check_Mat_VecCol_2(TMatrix< complex<TYPER> >& A); | 
|---|
| 87 |  | 
|---|
| 88 |  | 
|---|
| 89 | ////////////////////////////////////////////////// | 
|---|
| 90 | int main(int narg,char *arg[]) | 
|---|
| 91 | { | 
|---|
| 92 | //-------------------------------------------------------- | 
|---|
| 93 | //-- Initialisation | 
|---|
| 94 | //-------------------------------------------------------- | 
|---|
| 95 | // number of lines/columns | 
|---|
| 96 | uint_4 N = 5; | 
|---|
| 97 | // scale of the value (if =1 values between -1 and 1) | 
|---|
| 98 | r_8 scale = 1.; | 
|---|
| 99 | // number of values change by +/- vbig | 
|---|
| 100 | uint_4 nbig = N; | 
|---|
| 101 | r_8 vbig = 100.; | 
|---|
| 102 | // Nombre de lignes de matrice a imprimer | 
|---|
| 103 | uint_4 nprline = N; | 
|---|
| 104 | // Initialisation du pauvre de l'aleatoire | 
|---|
| 105 | uint_4 nalea = 0; | 
|---|
| 106 | // data scaling for gauss pivoting and determinant | 
|---|
| 107 | int tscal = 1; | 
|---|
| 108 | bool detok=false; | 
|---|
| 109 | // Please symetrize the input matrice | 
|---|
| 110 | bool symetok=false; | 
|---|
| 111 | // Please symetrize the input matrice | 
|---|
| 112 | bool gaussok=false; | 
|---|
| 113 |  | 
|---|
| 114 | //-------------------------------------------------------- | 
|---|
| 115 | //-- Decodage arguments | 
|---|
| 116 | //-------------------------------------------------------- | 
|---|
| 117 | char c; | 
|---|
| 118 | while((c = getopt(narg,arg,"Sdgn:s:b:l:a:t:h")) != -1) { | 
|---|
| 119 | switch (c) { | 
|---|
| 120 | case 'S' : | 
|---|
| 121 | symetok = true; | 
|---|
| 122 | break; | 
|---|
| 123 | case 'd' : | 
|---|
| 124 | detok = true; | 
|---|
| 125 | break; | 
|---|
| 126 | case 'g' : | 
|---|
| 127 | gaussok = true; | 
|---|
| 128 | break; | 
|---|
| 129 | case 'n' : | 
|---|
| 130 | sscanf(optarg,"%d",&N); | 
|---|
| 131 | break; | 
|---|
| 132 | case 's' : | 
|---|
| 133 | sscanf(optarg,"%lf",&scale); | 
|---|
| 134 | break; | 
|---|
| 135 | case 'b' : | 
|---|
| 136 | sscanf(optarg,"%d,%lf",&nbig,&vbig); | 
|---|
| 137 | break; | 
|---|
| 138 | case 'l' : | 
|---|
| 139 | sscanf(optarg,"%d",&nprline); | 
|---|
| 140 | break; | 
|---|
| 141 | case 'a' : | 
|---|
| 142 | sscanf(optarg,"%d",&nalea); | 
|---|
| 143 | break; | 
|---|
| 144 | case 't' : | 
|---|
| 145 | sscanf(optarg,"%d",&tscal); | 
|---|
| 146 | break; | 
|---|
| 147 | case 'h' : | 
|---|
| 148 | cout<<"tsttminv [-h] [-n N] [-S] [-s scale] [-b nbig,vbig] [-g]"<<endl | 
|---|
| 149 | <<"         [-l nprline] [-a nalea] [-t tscal] [-d]"<<endl | 
|---|
| 150 | <<"-- matrix A(N,N) filled with {[-1,1] +/- vbig(nbig time)}*scale --"<<endl | 
|---|
| 151 | <<"-g : instead of flat [-1,1] use normal gaussian distribution for A(i,j)"<<endl | 
|---|
| 152 | <<"-S : symetrize the input matrix"<<endl | 
|---|
| 153 | <<"-l : print nprline of input and test matrices"<<endl | 
|---|
| 154 | <<"-a : for random (pseudo) changing"<<endl | 
|---|
| 155 | <<"-- Only GausPiv --"<<endl | 
|---|
| 156 | <<"-t 0/1/2 : data scaling 0=no, 1=global (def), 2=row-by-row"<<endl | 
|---|
| 157 | <<"-d : also compute determinant (ne pas utiliser si N est grand)"<<endl; | 
|---|
| 158 | return(-1); | 
|---|
| 159 | } | 
|---|
| 160 | } | 
|---|
| 161 | if(N<=1) N = 1; | 
|---|
| 162 | cout<<"Taille matrice NxN, N = "<<N<<endl; | 
|---|
| 163 | if(gaussok) cout<<"Elements gaussian normal * scale = "<<scale<<endl; | 
|---|
| 164 | else      cout<<"Elements entre +/- 1 * scale = "<<scale<<endl; | 
|---|
| 165 | cout<<"Nombre de valeurs hors standard nbig = "<<nbig<<endl; | 
|---|
| 166 | cout<<"Valeurs hors standard (+/- vbig = "<<vbig<<" ) * scale = "<<vbig*scale<<endl; | 
|---|
| 167 | cout<<"Nombre de lignes de matrice a imprimer "<<nprline<<endl; | 
|---|
| 168 | cout<<"Initialisation de l aleatoire par "<<nalea<<" tirages"<<endl; | 
|---|
| 169 | cout<<"Data scaling "<<tscal<<" determinant="<<detok<<endl; | 
|---|
| 170 | if(symetok) cout<<"Input matrix has been symetrized "<<symetok<<endl; | 
|---|
| 171 | cout<<endl; | 
|---|
| 172 |  | 
|---|
| 173 | //-------------------------------------------------------- | 
|---|
| 174 | //-- Initialization arrays | 
|---|
| 175 | //-------------------------------------------------------- | 
|---|
| 176 | SophyaInit(); | 
|---|
| 177 | InitTim(); | 
|---|
| 178 | #if defined(USE_LAPACK) | 
|---|
| 179 | BaseArray::SetDefaultMemoryMapping(BaseArray::FortranMemoryMapping); | 
|---|
| 180 | #endif | 
|---|
| 181 | if(nalea>0) for(int i=0;i<nalea;i++) drand01(); | 
|---|
| 182 | BaseArray::SetMaxPrint(nprline*N,0); | 
|---|
| 183 |  | 
|---|
| 184 | //-------------------------------------------------------- | 
|---|
| 185 | //-- Definition global arrays | 
|---|
| 186 | //-------------------------------------------------------- | 
|---|
| 187 | TMatrix< TYPE > Ainput(N,N); Ainput = (TYPE) 0; | 
|---|
| 188 | TMatrix< TYPE > A(N,N); A = (TYPE) 0; | 
|---|
| 189 | Ainput.Show(); | 
|---|
| 190 |  | 
|---|
| 191 | //-------------------------------------------------------- | 
|---|
| 192 | //-- Fill matrices with flat random | 
|---|
| 193 | //-------------------------------------------------------- | 
|---|
| 194 | if(gaussok) Ainput = RandomSequence(RandomSequence::Gaussian,0.,1.); | 
|---|
| 195 | else      Ainput = RandomSequence(RandomSequence::Flat,0.,1.); | 
|---|
| 196 | #if defined(COMPLEX) | 
|---|
| 197 | if(gaussok) A = RandomSequence(RandomSequence::Gaussian,0.,1.); | 
|---|
| 198 | else      A = RandomSequence(RandomSequence::Flat,0.,1.); | 
|---|
| 199 | Ainput += TYPE(0.,1.)*A; | 
|---|
| 200 | #endif | 
|---|
| 201 |  | 
|---|
| 202 | //-------------------------------------------------------- | 
|---|
| 203 | //-- Fill matrices with big values | 
|---|
| 204 | //-------------------------------------------------------- | 
|---|
| 205 | if(nbig>0) { | 
|---|
| 206 | #if defined(COMPLEX) | 
|---|
| 207 | nbig = (nbig+1)/2; | 
|---|
| 208 | #endif | 
|---|
| 209 | TMatrix< uint_2 > Vind(N,N); Vind = 0; | 
|---|
| 210 | // for real part | 
|---|
| 211 | uint_4 nbr=0; | 
|---|
| 212 | for(int k=0;k<nbig;k++) { | 
|---|
| 213 | int i = (int) (drand01()*N); int j = (int) (drand01()*N); | 
|---|
| 214 | double s=(drand01()>0.5)?1.:-1.; | 
|---|
| 215 | if(Vind(i,j)==0) {Ainput(i,j) += (TYPER) s*vbig; Vind(i,j)+=1; nbr++;} | 
|---|
| 216 | } | 
|---|
| 217 | cout<<"Nombre de valeurs BIG reelles = "<<nbr<<endl; | 
|---|
| 218 | #if defined(COMPLEX) | 
|---|
| 219 | // for imaginary part | 
|---|
| 220 | uint_4 nbi=0; | 
|---|
| 221 | for(int k=0;k<nbig;k++) { | 
|---|
| 222 | int i = (int) (drand01()*N); int j = (int) (drand01()*N); | 
|---|
| 223 | double s=(drand01()>0.5)?1.:-1.; | 
|---|
| 224 | if(Vind(i,j)<=1) {Ainput(i,j) += TYPE(0.,(TYPER)s*vbig); Vind(i,j)+=2; nbi++;} | 
|---|
| 225 | } | 
|---|
| 226 | cout<<"Nombre de valeurs BIG imaginaires = "<<nbi<<endl; | 
|---|
| 227 | cout<<"Nombre de valeurs BIG = "<<nbr+nbi<<endl; | 
|---|
| 228 | #endif | 
|---|
| 229 | } | 
|---|
| 230 |  | 
|---|
| 231 | //-------------------------------------------------------- | 
|---|
| 232 | //-- Scale matrix for machine precision tests | 
|---|
| 233 | //-------------------------------------------------------- | 
|---|
| 234 | Ainput *= (TYPE) scale; | 
|---|
| 235 |  | 
|---|
| 236 | //-------------------------------------------------------- | 
|---|
| 237 | //-- Create symetric matrix for all A if requested | 
|---|
| 238 | //-------------------------------------------------------- | 
|---|
| 239 | if(symetok) Symetrize(Ainput); | 
|---|
| 240 |  | 
|---|
| 241 | //-------------------------------------------------------- | 
|---|
| 242 | //-- Print matrice Ainput | 
|---|
| 243 | //-------------------------------------------------------- | 
|---|
| 244 | cout<<"------------ TMatrix Ainput :"<<endl; | 
|---|
| 245 | if(nprline>0) {cout<<Ainput; cout<<endl;} | 
|---|
| 246 | PrtTim("--- End of Matrix filling ---"); | 
|---|
| 247 |  | 
|---|
| 248 |  | 
|---|
| 249 | ////////////////////////////////////////////// | 
|---|
| 250 | ///////// Test Inversion avec Lapack ///////// | 
|---|
| 251 | ////////////////////////////////////////////// | 
|---|
| 252 | #if defined(USE_LAPACK) && defined(ALSO_LAPACK_INV) | 
|---|
| 253 | { | 
|---|
| 254 | cout<<"\n=========================================="<<endl; | 
|---|
| 255 | cout<<"------------ Inversion LAPACK (LU factorization)"<<endl; | 
|---|
| 256 | A = Ainput; | 
|---|
| 257 | //-- Inversion | 
|---|
| 258 | TMatrix< TYPE > InvA(N,N); InvA = IdentityMatrix(1.,N); | 
|---|
| 259 | int_4 info = LapackLinSolve(A,InvA); | 
|---|
| 260 | cout<<"info="<<info<<endl; | 
|---|
| 261 | PrtTim("--- End of LapackLinSolve Inversion ---"); | 
|---|
| 262 | //-- AiA = A * InvA | 
|---|
| 263 | cout<<"Compute AiA = A * InvA"<<endl; | 
|---|
| 264 | TMatrix< TYPE > AiA(N,N); AiA = Ainput * InvA; | 
|---|
| 265 | if(nprline>0) {cout<<AiA; cout<<endl;} | 
|---|
| 266 | //-- Check | 
|---|
| 267 | Check_Mat_Ident(AiA); | 
|---|
| 268 | PrtTim("--- End of LapackLinSolve Test ---"); | 
|---|
| 269 | } | 
|---|
| 270 | #endif | 
|---|
| 271 |  | 
|---|
| 272 |  | 
|---|
| 273 | ////////////////////////////////////////////////// | 
|---|
| 274 | ///////// Test Inversion avec Lapack sym ///////// | 
|---|
| 275 | ////////////////////////////////////////////////// | 
|---|
| 276 | #if defined(USE_LAPACK) && defined(ALSO_LAPACK_INV_SYM) | 
|---|
| 277 | { | 
|---|
| 278 | cout<<"\n=========================================="<<endl; | 
|---|
| 279 | cout<<"------------ Inversion LAPACK symetric (LU factorization)"<<endl; | 
|---|
| 280 | TMatrix< TYPE > Asym(N,N); Asym=Ainput; Symetrize(Asym); A=Asym; | 
|---|
| 281 | //-- Inversion | 
|---|
| 282 | TMatrix< TYPE > InvA(N,N); InvA = IdentityMatrix(1.,N); | 
|---|
| 283 | int_4 info = LapackLinSolveSym(A,InvA); | 
|---|
| 284 | cout<<"info="<<info<<endl; | 
|---|
| 285 | PrtTim("--- End of LapackLinSolveSym Inversion ---"); | 
|---|
| 286 | //-- AiA = A * InvA | 
|---|
| 287 | cout<<"Compute AiA = A * InvA"<<endl; | 
|---|
| 288 | TMatrix< TYPE > AiA(N,N); AiA = Asym * InvA; | 
|---|
| 289 | cout<<"------------ TMatrix AiA = A * InvA:"<<endl; | 
|---|
| 290 | if(nprline>0) {cout<<AiA; cout<<endl;} | 
|---|
| 291 | //-- Check | 
|---|
| 292 | Check_Mat_Ident(AiA); | 
|---|
| 293 | PrtTim("--- End of LapackLinSolveSym Test ---"); | 
|---|
| 294 | } | 
|---|
| 295 | #endif | 
|---|
| 296 |  | 
|---|
| 297 |  | 
|---|
| 298 | //////////////////////////////////////////////// | 
|---|
| 299 | ///////// Test avec Lapack LeastSquare ///////// | 
|---|
| 300 | //////////////////////////////////////////////// | 
|---|
| 301 | #if defined(USE_LAPACK) && defined(ALSO_LAPACK_INV_LSS) | 
|---|
| 302 | { | 
|---|
| 303 | cout<<"\n=========================================="<<endl; | 
|---|
| 304 | cout<<"------------ Inversion LAPACK LeastSquare (QR or LQ factorization)"<<endl; | 
|---|
| 305 | A = Ainput; | 
|---|
| 306 | //-- Inversion | 
|---|
| 307 | TMatrix< TYPE > InvA(N,N); InvA = IdentityMatrix(1.,N); | 
|---|
| 308 | int_4 info = LapackLeastSquareSolve(A,InvA); | 
|---|
| 309 | cout<<"info="<<info<<endl; | 
|---|
| 310 | PrtTim("--- End of LapackLeastSquareSolve Inversion ---"); | 
|---|
| 311 | //-- AiA = A * InvA | 
|---|
| 312 | cout<<"Compute AiA = A * InvA"<<endl; | 
|---|
| 313 | TMatrix< TYPE > AiA(N,N); AiA = Ainput * InvA; | 
|---|
| 314 | if(nprline>0) {cout<<AiA; cout<<endl;} | 
|---|
| 315 | //-- Check | 
|---|
| 316 | Check_Mat_Ident(AiA); | 
|---|
| 317 | PrtTim("--- End of LapackLeastSquareSolve Test ---"); | 
|---|
| 318 | } | 
|---|
| 319 | #endif | 
|---|
| 320 |  | 
|---|
| 321 |  | 
|---|
| 322 | /////////////////////////////////////////////////////// | 
|---|
| 323 | ///////// Test avec Lapack LeastSquare by SVD ///////// | 
|---|
| 324 | /////////////////////////////////////////////////////// | 
|---|
| 325 | #if defined(USE_LAPACK) && defined(ALSO_LAPACK_INV_LSS_SVD) | 
|---|
| 326 | { | 
|---|
| 327 | cout<<"\n=========================================="<<endl; | 
|---|
| 328 | cout<<"------------ Inversion LAPACK LeastSquare (SVD decomposition)"<<endl; | 
|---|
| 329 | A = Ainput; | 
|---|
| 330 | //-- Inversion | 
|---|
| 331 | TMatrix< TYPE > InvA(N,N); InvA = IdentityMatrix(1.,N); | 
|---|
| 332 | TVector<r_8> S; | 
|---|
| 333 | int_4 rank; | 
|---|
| 334 | r_8 rcond = -1.; | 
|---|
| 335 | int_4 info = LapackLeastSquareSolveSVD_DC(A,InvA,S,rank,rcond); | 
|---|
| 336 | cout<<"info="<<info<<"  (rank="<<rank<<")"<<endl; | 
|---|
| 337 | PrtTim("--- End of LapackLeastSquareSolveSVD_DC Inversion ---"); | 
|---|
| 338 | if(nprline>0) {cout<<S; cout<<endl;} | 
|---|
| 339 | double smax = fabs(S(0)), smin = fabs(S(S.Size()-1)); | 
|---|
| 340 | cout<<" Smin = |"<<S(S.Size()-1)<<"| = "<<smin<<", " | 
|---|
| 341 | <<" Smax = |"<<S(0)<<"| = "<<smax<<", " | 
|---|
| 342 | <<" --> Smin/Smax = "<<smin/smax<<endl; | 
|---|
| 343 | //-- AiA = A * InvA | 
|---|
| 344 | cout<<"Compute AiA = A * InvA"<<endl; | 
|---|
| 345 | TMatrix< TYPE > AiA(N,N); AiA = Ainput * InvA; | 
|---|
| 346 | if(nprline>0) {cout<<AiA; cout<<endl;} | 
|---|
| 347 | //-- Check | 
|---|
| 348 | Check_Mat_Ident(AiA); | 
|---|
| 349 | PrtTim("--- End of LapackLeastSquareSolveSVD_DC Test ---"); | 
|---|
| 350 | } | 
|---|
| 351 | #endif | 
|---|
| 352 |  | 
|---|
| 353 |  | 
|---|
| 354 | //////////////////////////////////////////////// | 
|---|
| 355 | ///////// Test avec Lapack pour EV ///////// | 
|---|
| 356 | //////////////////////////////////////////////// | 
|---|
| 357 | #if defined(USE_LAPACK) && defined(ALSO_LAPACK_EV) | 
|---|
| 358 | { | 
|---|
| 359 | cout<<"\n=========================================="<<endl; | 
|---|
| 360 | cout<<"------------ Eigen decompositon LapackEigen "<<endl; | 
|---|
| 361 | A=Ainput; | 
|---|
| 362 | TMatrix< TYPE > Evec; | 
|---|
| 363 | TVector< complex<r_8> > Eval; | 
|---|
| 364 | //-- Decompositon | 
|---|
| 365 | int_4 info = LapackEigen(A,Eval,Evec,true); | 
|---|
| 366 | cout<<"info="<<info<<endl; | 
|---|
| 367 | PrtTim("--- End of LapackEigen decompositon ---"); | 
|---|
| 368 | if(nprline>0) {cout<<Eval; cout<<endl; cout<<Evec; cout<<endl;} | 
|---|
| 369 | //-- Find the complex conjugate pairs | 
|---|
| 370 | #if !defined(COMPLEX) | 
|---|
| 371 | TVector< uint_2 > Evalconj(N); Evalconj = 0; | 
|---|
| 372 | int_4 nconj=0; | 
|---|
| 373 | for(int i=0;i<N-1;i++) { | 
|---|
| 374 | if(Evalconj(i)!=0) continue;   // deja traite | 
|---|
| 375 | if(Eval(i).imag()==0.) continue; // real eigenvalue | 
|---|
| 376 | if(fabs(Eval(i).imag()+Eval(i+1).imag())>1e-150) continue; // les 2 consecutives ne sont pas conjuguees | 
|---|
| 377 | if(Eval(i).imag()<0.) continue; // first conjugate have positive imaginary part | 
|---|
| 378 | if(Eval(i+1).imag()>0.) continue; | 
|---|
| 379 | Evalconj(i) = 1; Evalconj(i+1) = 2; nconj++; | 
|---|
| 380 | } | 
|---|
| 381 | //cout<<Evalconj<<endl; | 
|---|
| 382 | cout<<"Number of conjugate eigen values: "<<nconj<<" *2 = "<<2*nconj<<" / "<<N<<endl; | 
|---|
| 383 | #endif | 
|---|
| 384 | //-- Azmlz = A*z(l) - l*z(l) | 
|---|
| 385 | cout<<"Compute Azmlz(l) = A*z(l) - l*z(l)"<<endl; | 
|---|
| 386 | TMatrix< complex<TYPER> > Azmlz(N,N); Azmlz = (complex<TYPER>) 0; | 
|---|
| 387 | for(int l=0;l<N;l++) { // eigen value | 
|---|
| 388 | complex<TYPER> Eval_l = complex<TYPER>(Eval(l).real(),Eval(l).imag()); | 
|---|
| 389 | for(int i=0;i<N;i++) { | 
|---|
| 390 | complex<TYPER> Evec_il; | 
|---|
| 391 | #if defined(COMPLEX) | 
|---|
| 392 | Evec_il = Evec(i,l); | 
|---|
| 393 | #else | 
|---|
| 394 | Evec_il = complex<TYPER>(Evec(i,l),0.); | 
|---|
| 395 | if(Evalconj(l)==1)        Evec_il = complex<TYPER>(Evec(i,l),Evec(i,l+1)); | 
|---|
| 396 | else if(Evalconj(l)==2) Evec_il = complex<TYPER>(Evec(i,l-1),-Evec(i,l)); | 
|---|
| 397 | #endif | 
|---|
| 398 | for(int j=0;j<N;j++) { | 
|---|
| 399 | complex<TYPER> Evec_jl; | 
|---|
| 400 | #if defined(COMPLEX) | 
|---|
| 401 | Evec_jl = Evec(j,l); | 
|---|
| 402 | #else | 
|---|
| 403 | Evec_jl = complex<TYPER>(Evec(j,l),0.); | 
|---|
| 404 | if(Evalconj(l)==1)        Evec_jl = complex<TYPER>(Evec(j,l),Evec(j,l+1)); | 
|---|
| 405 | else if(Evalconj(l)==2) Evec_jl = complex<TYPER>(Evec(j,l-1),-Evec(j,l)); | 
|---|
| 406 | #endif | 
|---|
| 407 | Azmlz(i,l) += Ainput(i,j) * Evec_jl; | 
|---|
| 408 | } | 
|---|
| 409 | Azmlz(i,l) -= Eval_l*Evec_il; | 
|---|
| 410 | } | 
|---|
| 411 | } | 
|---|
| 412 | if(nprline>0) {cout<<Azmlz; cout<<endl;} | 
|---|
| 413 | //-- Check | 
|---|
| 414 | Check_Mat_VecCol_2(Azmlz); | 
|---|
| 415 | PrtTim("--- End of LapackEigen Test ---"); | 
|---|
| 416 | } | 
|---|
| 417 | #endif | 
|---|
| 418 |  | 
|---|
| 419 |  | 
|---|
| 420 | //////////////////////////////////////////////// | 
|---|
| 421 | ///////// Test avec Lapack sym pour EV ///////// | 
|---|
| 422 | //////////////////////////////////////////////// | 
|---|
| 423 | #if defined(USE_LAPACK) && defined(ALSO_LAPACK_EV_SYM) | 
|---|
| 424 | { | 
|---|
| 425 | cout<<"\n=========================================="<<endl; | 
|---|
| 426 | cout<<"------------ Eigen decompositon LapackEigenSym "<<endl; | 
|---|
| 427 | TMatrix< TYPE > Aher(N,N); Aher=Ainput; Hermitian(Aher); A=Aher; | 
|---|
| 428 | TVector<r_8> Eval; | 
|---|
| 429 | //-- Decompositon | 
|---|
| 430 | int_4 info = LapackEigenSym(A,Eval,true); | 
|---|
| 431 | cout<<"info="<<info<<endl; | 
|---|
| 432 | PrtTim("--- End of LapackEigenSym decompositon ---"); | 
|---|
| 433 | if(nprline>0) {cout<<Eval; cout<<endl; cout<<A; cout<<endl;} | 
|---|
| 434 | //-- Azmlz = A*z(l) - l*z(l) | 
|---|
| 435 | // le vecteur propre z pour la l-ieme valeur propre est dans A(.,l): | 
|---|
| 436 | //    z_i = A(i,l)  ou "l" est la l-ieme valeur propre | 
|---|
| 437 | cout<<"Compute Azmlz(l) = A*z(l) - l*z(l)"<<endl; | 
|---|
| 438 | TMatrix< TYPE > Azmlz(N,N); Azmlz = (TYPE) 0; | 
|---|
| 439 | for(int l=0;l<N;l++)  // eigen value | 
|---|
| 440 | for(int i=0;i<N;i++) | 
|---|
| 441 | {for(int j=0;j<N;j++) Azmlz(i,l)+=Aher(i,j)*A(j,l); Azmlz(i,l)-=(TYPER)Eval(l)*A(i,l);} | 
|---|
| 442 | if(nprline>0) {cout<<Azmlz; cout<<endl;} | 
|---|
| 443 | //-- Check | 
|---|
| 444 | Check_Mat_VecCol_0(Azmlz); | 
|---|
| 445 | PrtTim("--- End of LapackEigenSym Test ---"); | 
|---|
| 446 | } | 
|---|
| 447 | #endif | 
|---|
| 448 |  | 
|---|
| 449 |  | 
|---|
| 450 | //////////////////////////////////////////////// | 
|---|
| 451 | ///////////// Test avec Lapack SVD ///////////// | 
|---|
| 452 | //////////////////////////////////////////////// | 
|---|
| 453 | #if defined(USE_LAPACK) && defined(ALSO_LAPACK_SVD) | 
|---|
| 454 | { | 
|---|
| 455 | cout<<"\n=========================================="<<endl; | 
|---|
| 456 | cout<<"------------ SVD decompositon LapackSVD "<<endl; | 
|---|
| 457 | A=Ainput; | 
|---|
| 458 | TVector< TYPE > S; TMatrix< TYPE > U; TMatrix< TYPE > VT; | 
|---|
| 459 | //-- Decompositon | 
|---|
| 460 | int_4 info = LapackSVD(A,S,U,VT); | 
|---|
| 461 | cout<<"info="<<info<<endl; | 
|---|
| 462 | PrtTim("--- End of LapackSVD decompositon ---"); | 
|---|
| 463 | if(nprline>0) {cout<<S; cout<<endl;} | 
|---|
| 464 | double smax = ABS_VAL(S(0)), smin = ABS_VAL(S(N-1)); | 
|---|
| 465 | cout<<" Smin = |"<<S(N-1)<<"| = "<<smin<<", " | 
|---|
| 466 | <<" Smax = |"<<S(0)<<"| = "<<smax<<", " | 
|---|
| 467 | <<" --> Smin/Smax = "<<smin/smax<<endl; | 
|---|
| 468 | //-- A = U*S*VT | 
|---|
| 469 | cout<<"Compute A = U*S*VT"<<endl; | 
|---|
| 470 | TMatrix< TYPE > AmUSVt(N,N); AmUSVt = U; | 
|---|
| 471 | for(int i=0;i<N;i++) for(int j=0;j<N;j++) AmUSVt(i,j) *= S(j); | 
|---|
| 472 | AmUSVt = AmUSVt * VT; AmUSVt -= Ainput; | 
|---|
| 473 | if(nprline>0) {cout<<AmUSVt; cout<<endl;} | 
|---|
| 474 | //-- Check | 
|---|
| 475 | Check_Mat_Zero(AmUSVt); | 
|---|
| 476 | PrtTim("--- End of LapackSVD Test ---"); | 
|---|
| 477 | } | 
|---|
| 478 | #endif | 
|---|
| 479 |  | 
|---|
| 480 |  | 
|---|
| 481 | /////////////////////////////////////////////////// | 
|---|
| 482 | ///////////// Test avec Lapack SVD_DC ///////////// | 
|---|
| 483 | /////////////////////////////////////////////////// | 
|---|
| 484 | #if defined(USE_LAPACK) && defined(ALSO_LAPACK_SVD_DC) | 
|---|
| 485 | { | 
|---|
| 486 | cout<<"\n=========================================="<<endl; | 
|---|
| 487 | cout<<"------------ SVD decompositon LapackSVD_DC "<<endl; | 
|---|
| 488 | A=Ainput; | 
|---|
| 489 | TVector< r_8 > S; TMatrix< TYPE > U; TMatrix< TYPE > VT; | 
|---|
| 490 | //-- Decompositon | 
|---|
| 491 | int_4 info = LapackSVD_DC(A,S,U,VT); | 
|---|
| 492 | cout<<"info="<<info<<endl; | 
|---|
| 493 | PrtTim("--- End of LapackSVD_DC decompositon ---"); | 
|---|
| 494 | if(nprline>0) {cout<<S; cout<<endl;} | 
|---|
| 495 | double smax = fabs(S(0)), smin = fabs(S(N-1)); | 
|---|
| 496 | cout<<"CMV: "<<S(0)<<endl; | 
|---|
| 497 | //cout<<" Smin = |"<<S(N-1)<<"| = "<<smin<<", " | 
|---|
| 498 | //    <<" Smax = |"<<S(0)<<"| = "<<smax<<", " | 
|---|
| 499 | //    <<" --> Smin/Smax = "<<smin/smax<<endl; | 
|---|
| 500 | //-- A = U*S*VT | 
|---|
| 501 | cout<<"Compute A = U*S*VT"<<endl; | 
|---|
| 502 | TMatrix< TYPE > AmUSVt(N,N); AmUSVt = U; | 
|---|
| 503 | for(int i=0;i<N;i++) for(int j=0;j<N;j++) AmUSVt(i,j) *= (TYPE) S(j); | 
|---|
| 504 | AmUSVt = AmUSVt * VT; AmUSVt -= Ainput; | 
|---|
| 505 | if(nprline>0) {cout<<AmUSVt; cout<<endl;} | 
|---|
| 506 | //-- Check | 
|---|
| 507 | Check_Mat_Zero(AmUSVt); | 
|---|
| 508 | PrtTim("--- End of LapackSVD_DC Test ---"); | 
|---|
| 509 | } | 
|---|
| 510 | #endif | 
|---|
| 511 |  | 
|---|
| 512 |  | 
|---|
| 513 | /////////////////////////////////////////////// | 
|---|
| 514 | ///////// Test Inversion avec GausPiv ///////// | 
|---|
| 515 | /////////////////////////////////////////////// | 
|---|
| 516 | #if defined(USE_GAUSPIV) | 
|---|
| 517 | { | 
|---|
| 518 | cout<<"\n==========================================\n" | 
|---|
| 519 | <<"------------ Inversion GausPiv"<<endl; | 
|---|
| 520 | SimpleMatrixOperation< TYPE >::SetGausPivScal(tscal); | 
|---|
| 521 | A = Ainput; | 
|---|
| 522 | //-- Inversion | 
|---|
| 523 | TMatrix< TYPE > InvA(N,N); InvA = IdentityMatrix(1.,N); | 
|---|
| 524 | TYPE det = GausPiv(A,InvA,detok); | 
|---|
| 525 | PrtTim("--- End of GausPiv Inversion ---"); | 
|---|
| 526 | cout<<"Det = "<<det<<endl; | 
|---|
| 527 | cout<<"------------ TMatrix InvA = A^(-1):"<<endl; | 
|---|
| 528 | //-- AiA = A * InvA | 
|---|
| 529 | cout<<"Compute AiA = A * InvA"<<endl; | 
|---|
| 530 | TMatrix< TYPE > AiA(N,N); AiA = Ainput * InvA; | 
|---|
| 531 | cout<<"------------ TMatrix AiA = A * InvA:"<<endl; | 
|---|
| 532 | if(nprline>0) {cout<<AiA; cout<<endl;} | 
|---|
| 533 | //-- Check | 
|---|
| 534 | Check_Mat_Ident(AiA); | 
|---|
| 535 | PrtTim("--- End of GausPiv Test ---"); | 
|---|
| 536 | } | 
|---|
| 537 | #endif | 
|---|
| 538 |  | 
|---|
| 539 |  | 
|---|
| 540 | PrtTim("--- End of Job ---"); | 
|---|
| 541 | exit(0); | 
|---|
| 542 | } | 
|---|
| 543 |  | 
|---|
| 544 |  | 
|---|
| 545 |  | 
|---|
| 546 | //////////////////////////////////////////////////////////// | 
|---|
| 547 | ////------------------------------------------------------- | 
|---|
| 548 | void Symetrize(TMatrix< TYPE >& A) | 
|---|
| 549 | // Symetrize A | 
|---|
| 550 | { | 
|---|
| 551 | int_4 N = A.NRows(); | 
|---|
| 552 | for(int i=0;i<N-1;i++) for(int j=i+1;j<N;j++) A(j,i) = A(i,j); | 
|---|
| 553 | } | 
|---|
| 554 |  | 
|---|
| 555 | ////------------------------------------------------------- | 
|---|
| 556 | void Hermitian(TMatrix< TYPE >& A) | 
|---|
| 557 | // Put A hermitian | 
|---|
| 558 | { | 
|---|
| 559 | int_4 N = A.NRows(); | 
|---|
| 560 | for(int i=0;i<N-1;i++) for(int j=i+1;j<N;j++) A(j,i) = CONJ_VAL(A(i,j)); | 
|---|
| 561 | for(int i=0;i<N;i++) A(i,i) = REAL_PART(A(i,i)); | 
|---|
| 562 | } | 
|---|
| 563 |  | 
|---|
| 564 | ////------------------------------------------------------- | 
|---|
| 565 | r_8  Check_Mat_Ident(TMatrix< TYPE >& A) | 
|---|
| 566 | // Compute the biggest difference element by element of A / Identity | 
|---|
| 567 | { | 
|---|
| 568 | int_4 N = A.NRows(); | 
|---|
| 569 | r_8 vmaxd=-1.; | 
|---|
| 570 | for(int i=0;i<N;i++) | 
|---|
| 571 | if( ABS_VAL((TYPER)1.-A(i,i)) > vmaxd ) vmaxd = ABS_VAL((TYPER)1.-A(i,i)); | 
|---|
| 572 | cout<<"Ecart maximum par rapport a 1 sur diagonale = "<<vmaxd<<endl; | 
|---|
| 573 | r_8 vmaxh = -1.; | 
|---|
| 574 | for(int i=0;i<N;i++) for(int j=0;j<N;j++) { | 
|---|
| 575 | if(i==j) continue; | 
|---|
| 576 | if( ABS_VAL(A(i,j)) > vmaxh ) vmaxh = ABS_VAL(A(i,j)); | 
|---|
| 577 | } | 
|---|
| 578 | cout<<"Ecart maximum par rapport a 0 hors diagonale = "<<vmaxh<<endl; | 
|---|
| 579 | return (vmaxd>vmaxh)? vmaxd: vmaxh; | 
|---|
| 580 | } | 
|---|
| 581 |  | 
|---|
| 582 | ////------------------------------------------------------- | 
|---|
| 583 | r_8  Check_Mat_Zero(TMatrix< TYPE >& A) | 
|---|
| 584 | // Compute the biggest difference element by element of A / Zero matrix | 
|---|
| 585 | { | 
|---|
| 586 | int_4 N = A.NRows(); | 
|---|
| 587 | r_8 vmax = -1.; | 
|---|
| 588 | for(int i=0;i<N;i++) for(int j=0;j<N;j++) | 
|---|
| 589 | if( ABS_VAL(A(i,j)) > vmax ) vmax = ABS_VAL(A(i,j)); | 
|---|
| 590 | cout<<"Ecart maximum par rapport a zero = "<<vmax<<endl; | 
|---|
| 591 | return vmax; | 
|---|
| 592 | } | 
|---|
| 593 |  | 
|---|
| 594 | ////------------------------------------------------------- | 
|---|
| 595 | r_8  Check_Mat_VecCol_0(TMatrix< TYPE >& A) | 
|---|
| 596 | // Return the biggest norm of the N vectors column of matrix | 
|---|
| 597 | { | 
|---|
| 598 | int_4 N = A.NRows(); | 
|---|
| 599 | r_8 vmax=-1.; | 
|---|
| 600 | for(int l=0;l<N;l++) { | 
|---|
| 601 | r_8 absv = 0.; | 
|---|
| 602 | for(int i=0;i<N;i++) absv += ABS_VAL(A(i,l)) * ABS_VAL(A(i,l)); | 
|---|
| 603 | if( absv > vmax ) vmax = absv; | 
|---|
| 604 | } | 
|---|
| 605 | vmax = sqrt(vmax); | 
|---|
| 606 | cout<<"Longueur max de ||A*z-l*z|| pour tous l = "<<vmax<<endl; | 
|---|
| 607 | return vmax; | 
|---|
| 608 | } | 
|---|
| 609 |  | 
|---|
| 610 | ////------------------------------------------------------- | 
|---|
| 611 | void  Check_Mat_VecCol_2(TMatrix< complex<TYPER> >& A) | 
|---|
| 612 | // Return the biggest norm of : | 
|---|
| 613 | //   - the real part of the N vectors column of matrix | 
|---|
| 614 | //   - the imaginary part of the N vectors column of matrix | 
|---|
| 615 | //   - the module of the N vectors column of matrix | 
|---|
| 616 | { | 
|---|
| 617 | int_4 N = A.NRows(); | 
|---|
| 618 | r_8 vmaxr=-1., vmaxi=-1., vmaxn=-1.; | 
|---|
| 619 | for(int l=0;l<N;l++) { | 
|---|
| 620 | double absvr = 0., absvi = 0., absvn = 0.; | 
|---|
| 621 | for(int i=0;i<N;i++) { | 
|---|
| 622 | absvr += A(i,l).real()*A(i,l).real(); | 
|---|
| 623 | absvi += A(i,l).imag()*A(i,l).imag(); | 
|---|
| 624 | absvn += A(i,l).real()*A(i,l).real() + A(i,l).imag()*A(i,l).imag(); | 
|---|
| 625 | } | 
|---|
| 626 | if( absvr > vmaxr ) vmaxr = absvr; | 
|---|
| 627 | if( absvi > vmaxi ) vmaxi = absvi; | 
|---|
| 628 | if( absvn > vmaxn ) vmaxn = absvn; | 
|---|
| 629 | } | 
|---|
| 630 | vmaxr=sqrt(vmaxr); vmaxi=sqrt(vmaxi); vmaxn=sqrt(vmaxn); | 
|---|
| 631 | cout<<"Longueur max de ||A*z-l*z|| pour tous l, reel = "<<vmaxr | 
|---|
| 632 | <<", imag = "<<vmaxi<<", module = "<<vmaxn<<endl; | 
|---|
| 633 | } | 
|---|