source: Sophya/trunk/SophyaProg/Tests/tsttminv.cc@ 2877

Last change on this file since 2877 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

File size: 20.6 KB
RevLine 
[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//////////////////////////////////////////////////
82void Symetrize(TMatrix< TYPE >& A);
83void Hermitian(TMatrix< TYPE >& A);
84r_8 Check_Mat_Ident(TMatrix< TYPE >& A);
[2558]85r_8 Check_Mat_Zero(TMatrix< TYPE >& A);
[2555]86r_8 Check_Mat_VecCol_0(TMatrix< TYPE >& A);
87void Check_Mat_VecCol_2(TMatrix< complex<TYPER> >& A);
88
89
90//////////////////////////////////////////////////
[934]91int main(int narg,char *arg[])
92{
93//--------------------------------------------------------
[2555]94//-- Initialisation
95//--------------------------------------------------------
[934]96// number of lines/columns
[975]97uint_4 N = 5;
[934]98// scale of the value (if =1 values between -1 and 1)
99r_8 scale = 1.;
100// number of values change by +/- vbig
[975]101uint_4 nbig = N;
[2555]102r_8 vbig = 100.;
[975]103// Nombre de lignes de matrice a imprimer
[2555]104uint_4 nprline = N;
[975]105// Initialisation du pauvre de l'aleatoire
[1008]106uint_4 nalea = 0;
107// data scaling for gauss pivoting and determinant
108int tscal = 1;
109bool detok=false;
[2555]110// Please symetrize the input matrice
111bool symetok=false;
112// Please symetrize the input matrice
113bool gaussok=false;
114
[934]115//--------------------------------------------------------
116//-- Decodage arguments
[2555]117//--------------------------------------------------------
[934]118char c;
[2555]119while((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}
162if(N<=1) N = 1;
[975]163cout<<"Taille matrice NxN, N = "<<N<<endl;
[2555]164if(gaussok) cout<<"Elements gaussian normal * scale = "<<scale<<endl;
165 else cout<<"Elements entre +/- 1 * scale = "<<scale<<endl;
[975]166cout<<"Nombre de valeurs hors standard nbig = "<<nbig<<endl;
[2555]167cout<<"Valeurs hors standard (+/- vbig = "<<vbig<<" ) * scale = "<<vbig*scale<<endl;
[934]168cout<<"Nombre de lignes de matrice a imprimer "<<nprline<<endl;
[975]169cout<<"Initialisation de l aleatoire par "<<nalea<<" tirages"<<endl;
[2555]170cout<<"Data scaling "<<tscal<<" determinant="<<detok<<endl;
171if(symetok) cout<<"Input matrix has been symetrized "<<symetok<<endl;
[934]172cout<<endl;
173
[2555]174//--------------------------------------------------------
[934]175//-- Initialization arrays
[2555]176//--------------------------------------------------------
[934]177SophyaInit();
[2555]178InitTim();
179#if defined(USE_LAPACK)
[995]180 BaseArray::SetDefaultMemoryMapping(BaseArray::FortranMemoryMapping);
181#endif
[2555]182if(nalea>0) for(int i=0;i<nalea;i++) drand01();
183BaseArray::SetMaxPrint(nprline*N,0);
[934]184
[2555]185//--------------------------------------------------------
186//-- Definition global arrays
187//--------------------------------------------------------
188TMatrix< TYPE > Ainput(N,N); Ainput = (TYPE) 0;
189TMatrix< TYPE > A(N,N); A = (TYPE) 0;
190Ainput.Show();
[934]191
[2555]192//--------------------------------------------------------
193//-- Fill matrices with flat random
194//--------------------------------------------------------
195if(gaussok) Ainput = RandomSequence(RandomSequence::Gaussian,0.,1.);
196 else Ainput = RandomSequence(RandomSequence::Flat,0.,1.);
197#if defined(COMPLEX)
198if(gaussok) A = RandomSequence(RandomSequence::Gaussian,0.,1.);
199 else A = RandomSequence(RandomSequence::Flat,0.,1.);
200Ainput += TYPE(0.,1.)*A;
201#endif
[1008]202
[2555]203//--------------------------------------------------------
204//-- Fill matrices with big values
205//--------------------------------------------------------
206if(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//--------------------------------------------------------
235Ainput *= (TYPE) scale;
236
237//--------------------------------------------------------
238//-- Create symetric matrix for all A if requested
239//--------------------------------------------------------
240if(symetok) Symetrize(Ainput);
241
242//--------------------------------------------------------
243//-- Print matrice Ainput
244//--------------------------------------------------------
245cout<<"------------ TMatrix Ainput :"<<endl;
246if(nprline>0) {cout<<Ainput; cout<<endl;}
247PrtTim("--- 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{
255cout<<"\n=========================================="<<endl;
[2566]256cout<<"------------ Inversion LAPACK (LU factorization)"<<endl;
[2555]257A = Ainput;
258//-- Inversion
259TMatrix< TYPE > InvA(N,N); InvA = IdentityMatrix(1.,N);
260int_4 info = LapackLinSolve(A,InvA);
261cout<<"info="<<info<<endl;
262PrtTim("--- End of LapackLinSolve Inversion ---");
263//-- AiA = A * InvA
264cout<<"Compute AiA = A * InvA"<<endl;
265TMatrix< TYPE > AiA(N,N); AiA = Ainput * InvA;
266if(nprline>0) {cout<<AiA; cout<<endl;}
267//-- Check
268Check_Mat_Ident(AiA);
269PrtTim("--- 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{
279cout<<"\n=========================================="<<endl;
[2566]280cout<<"------------ Inversion LAPACK symetric (LU factorization)"<<endl;
[2555]281TMatrix< TYPE > Asym(N,N); Asym=Ainput; Symetrize(Asym); A=Asym;
282//-- Inversion
283TMatrix< TYPE > InvA(N,N); InvA = IdentityMatrix(1.,N);
284int_4 info = LapackLinSolveSym(A,InvA);
285cout<<"info="<<info<<endl;
286PrtTim("--- End of LapackLinSolveSym Inversion ---");
287//-- AiA = A * InvA
288cout<<"Compute AiA = A * InvA"<<endl;
289TMatrix< TYPE > AiA(N,N); AiA = Asym * InvA;
290cout<<"------------ TMatrix AiA = A * InvA:"<<endl;
291if(nprline>0) {cout<<AiA; cout<<endl;}
292//-- Check
293Check_Mat_Ident(AiA);
294PrtTim("--- 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{
304cout<<"\n=========================================="<<endl;
[2566]305cout<<"------------ Inversion LAPACK LeastSquare (QR or LQ factorization)"<<endl;
[2555]306A = Ainput;
[934]307//-- Inversion
[2555]308TMatrix< TYPE > InvA(N,N); InvA = IdentityMatrix(1.,N);
309int_4 info = LapackLeastSquareSolve(A,InvA);
310cout<<"info="<<info<<endl;
311PrtTim("--- End of LapackLeastSquareSolve Inversion ---");
312//-- AiA = A * InvA
313cout<<"Compute AiA = A * InvA"<<endl;
314TMatrix< TYPE > AiA(N,N); AiA = Ainput * InvA;
315if(nprline>0) {cout<<AiA; cout<<endl;}
316//-- Check
317Check_Mat_Ident(AiA);
318PrtTim("--- 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{
328cout<<"\n=========================================="<<endl;
329cout<<"------------ Inversion LAPACK LeastSquare (SVD decomposition)"<<endl;
330A = Ainput;
331//-- Inversion
332TMatrix< TYPE > InvA(N,N); InvA = IdentityMatrix(1.,N);
333TVector<r_8> S;
334int_4 rank;
335r_8 rcond = -1.;
336int_4 info = LapackLeastSquareSolveSVD_DC(A,InvA,S,rank,rcond);
337cout<<"info="<<info<<" (rank="<<rank<<")"<<endl;
338PrtTim("--- End of LapackLeastSquareSolveSVD_DC Inversion ---");
339if(nprline>0) {cout<<S; cout<<endl;}
340double smax = fabs(S(0)), smin = fabs(S(S.Size()-1));
341cout<<" Smin = |"<<S(S.Size()-1)<<"| = "<<smin<<", "
342 <<" Smax = |"<<S(0)<<"| = "<<smax<<", "
343 <<" --> Smin/Smax = "<<smin/smax<<endl;
344//-- AiA = A * InvA
345cout<<"Compute AiA = A * InvA"<<endl;
346TMatrix< TYPE > AiA(N,N); AiA = Ainput * InvA;
347if(nprline>0) {cout<<AiA; cout<<endl;}
348//-- Check
349Check_Mat_Ident(AiA);
350PrtTim("--- 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{
360cout<<"\n=========================================="<<endl;
361cout<<"------------ Eigen decompositon LapackEigen "<<endl;
362A=Ainput;
[2562]363TMatrix< TYPE > Evec;
364TVector< complex<r_8> > Eval;
[2555]365//-- Decompositon
366int_4 info = LapackEigen(A,Eval,Evec,true);
367cout<<"info="<<info<<endl;
368PrtTim("--- End of LapackEigen decompositon ---");
369if(nprline>0) {cout<<Eval; cout<<endl; cout<<Evec; cout<<endl;}
370//-- Find the complex conjugate pairs
[2557]371#if !defined(COMPLEX)
[2555]372TVector< uint_2 > Evalconj(N); Evalconj = 0;
373int_4 nconj=0;
374for(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;
383cout<<"Number of conjugate eigen values: "<<nconj<<" *2 = "<<2*nconj<<" / "<<N<<endl;
384#endif
385//-- Azmlz = A*z(l) - l*z(l)
386cout<<"Compute Azmlz(l) = A*z(l) - l*z(l)"<<endl;
387TMatrix< complex<TYPER> > Azmlz(N,N); Azmlz = (complex<TYPER>) 0;
388for(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}
413if(nprline>0) {cout<<Azmlz; cout<<endl;}
414//-- Check
415Check_Mat_VecCol_2(Azmlz);
416PrtTim("--- 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{
426cout<<"\n=========================================="<<endl;
427cout<<"------------ Eigen decompositon LapackEigenSym "<<endl;
428TMatrix< TYPE > Aher(N,N); Aher=Ainput; Hermitian(Aher); A=Aher;
429TVector<r_8> Eval;
430//-- Decompositon
431int_4 info = LapackEigenSym(A,Eval,true);
432cout<<"info="<<info<<endl;
433PrtTim("--- End of LapackEigenSym decompositon ---");
434if(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
438cout<<"Compute Azmlz(l) = A*z(l) - l*z(l)"<<endl;
439TMatrix< TYPE > Azmlz(N,N); Azmlz = (TYPE) 0;
440for(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);}
443if(nprline>0) {cout<<Azmlz; cout<<endl;}
444//-- Check
445Check_Mat_VecCol_0(Azmlz);
446PrtTim("--- 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{
456cout<<"\n=========================================="<<endl;
457cout<<"------------ SVD decompositon LapackSVD "<<endl;
458A=Ainput;
[2562]459TVector< TYPE > S; TMatrix< TYPE > U; TMatrix< TYPE > VT;
[2558]460//-- Decompositon
461int_4 info = LapackSVD(A,S,U,VT);
462cout<<"info="<<info<<endl;
463PrtTim("--- End of LapackSVD decompositon ---");
464if(nprline>0) {cout<<S; cout<<endl;}
465double smax = ABS_VAL(S(0)), smin = ABS_VAL(S(N-1));
466cout<<" Smin = |"<<S(N-1)<<"| = "<<smin<<", "
467 <<" Smax = |"<<S(0)<<"| = "<<smax<<", "
468 <<" --> Smin/Smax = "<<smin/smax<<endl;
469//-- A = U*S*VT
470cout<<"Compute A = U*S*VT"<<endl;
471TMatrix< TYPE > AmUSVt(N,N); AmUSVt = U;
472for(int i=0;i<N;i++) for(int j=0;j<N;j++) AmUSVt(i,j) *= S(j);
[2580]473AmUSVt = AmUSVt * VT; AmUSVt -= Ainput;
[2558]474if(nprline>0) {cout<<AmUSVt; cout<<endl;}
475//-- Check
476Check_Mat_Zero(AmUSVt);
477PrtTim("--- 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{
487cout<<"\n=========================================="<<endl;
488cout<<"------------ SVD decompositon LapackSVD_DC "<<endl;
489A=Ainput;
[2566]490TVector< r_8 > S; TMatrix< TYPE > U; TMatrix< TYPE > VT;
[2562]491//-- Decompositon
492int_4 info = LapackSVD_DC(A,S,U,VT);
493cout<<"info="<<info<<endl;
494PrtTim("--- End of LapackSVD_DC decompositon ---");
495if(nprline>0) {cout<<S; cout<<endl;}
[2566]496double 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
502cout<<"Compute A = U*S*VT"<<endl;
503TMatrix< TYPE > AmUSVt(N,N); AmUSVt = U;
[2566]504for(int i=0;i<N;i++) for(int j=0;j<N;j++) AmUSVt(i,j) *= (TYPE) S(j);
[2580]505AmUSVt = AmUSVt * VT; AmUSVt -= Ainput;
[2562]506if(nprline>0) {cout<<AmUSVt; cout<<endl;}
507//-- Check
508Check_Mat_Zero(AmUSVt);
509PrtTim("--- 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{
519cout<<"\n==========================================\n"
520 <<"------------ Inversion GausPiv"<<endl;
521SimpleMatrixOperation< TYPE >::SetGausPivScal(tscal);
522A = Ainput;
523//-- Inversion
524TMatrix< TYPE > InvA(N,N); InvA = IdentityMatrix(1.,N);
[1008]525TYPE det = GausPiv(A,InvA,detok);
[2555]526PrtTim("--- End of GausPiv Inversion ---");
[1008]527cout<<"Det = "<<det<<endl;
[934]528cout<<"------------ TMatrix InvA = A^(-1):"<<endl;
529//-- AiA = A * InvA
[2555]530cout<<"Compute AiA = A * InvA"<<endl;
531TMatrix< TYPE > AiA(N,N); AiA = Ainput * InvA;
[934]532cout<<"------------ TMatrix AiA = A * InvA:"<<endl;
533if(nprline>0) {cout<<AiA; cout<<endl;}
534//-- Check
[2555]535Check_Mat_Ident(AiA);
536PrtTim("--- End of GausPiv Test ---");
[934]537}
[2555]538#endif
539
540
541PrtTim("--- End of Job ---");
542exit(0);
[934]543}
544
[995]545
[2555]546
547////////////////////////////////////////////////////////////
548////-------------------------------------------------------
549void 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////-------------------------------------------------------
557void 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////-------------------------------------------------------
566r_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]584r_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]596r_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////-------------------------------------------------------
612void 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}
Note: See TracBrowser for help on using the repository browser.