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