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