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