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

Last change on this file since 995 was 995, checked in by ansari, 25 years ago

add LaPack comparison 2/5/00 cmv

File size: 4.9 KB
Line 
1// Test de l'inversion de matrice (cmv 14/04/00)
2// Pb numeriques: dapax22 et daplxa115
3// tsttminv -v 55,100,130,1000000000 -l 0 -a 1
4
5// if defined COMPLEX , if not REAL
6//#define COMPLEX
7// if defined 32 bits precision, if not 64 bits
8// #define PRECIS32
9#define ALSO_LAPACK
10
11#include "machdefs.h"
12#include <iostream.h>
13#include <stdlib.h>
14#include <stdio.h>
15#include <string.h>
16#include <math.h>
17#include <unistd.h>
18#include "ntoolsinit.h"
19#include "pexceptions.h"
20#include "array.h"
21#include "srandgen.h"
22
23#ifdef ALSO_LAPACK
24#include "intflapack.h"
25#endif
26
27
28#if defined(COMPLEX)
29 #define ABS_VAL(_x_) sqrt((double)((_x_).real()*(_x_).real() + (_x_).imag()*(_x_).imag()))
30 #if defined(PRECIS32)
31 #define TYPE complex<r_4>
32 #else
33 #define TYPE complex<r_8>
34 #endif
35#else
36 #define ABS_VAL(_x_) fabs((double)_x_)
37 #if defined(PRECIS32)
38 #define TYPE r_4
39 #else
40 #define TYPE r_8
41 #endif
42#endif
43
44int main(int narg,char *arg[])
45{
46//--------------------------------------------------------
47// number of lines/columns
48uint_4 N = 5;
49// scale of the value (if =1 values between -1 and 1)
50r_8 scale = 1.;
51// number of values change by +/- vbig
52uint_4 nbig = N;
53r_8 vbig = 1000.;
54// Nombre de lignes de matrice a imprimer
55uint_4 nprline = 10;
56// Initialisation du pauvre de l'aleatoire
57 uint_4 nalea = 0;
58//--------------------------------------------------------
59
60//-- Decodage arguments
61char c;
62while((c = getopt(narg,arg,"hv:l:a:")) != -1) {
63 switch (c) {
64 case 'v' :
65 sscanf(optarg,"%d,%lf,%d,%lf",&N,&scale,&nbig,&vbig);
66 break;
67 case 'l' :
68 sscanf(optarg,"%d",&nprline);
69 break;
70 case 'a' :
71 sscanf(optarg,"%d",&nalea);
72 break;
73 case 'h' :
74 cout<<"tsttminv [-h] [-v N,scale,nbig,vbig] [-l nprline] [-a nalea]"<<endl;
75 cout<<"matrix filled with : {[-1,1] +/- vbig(nbig time)}*scale"<<endl;
76 exit(-1);
77 }
78}
79if(N<=1) N = 1;
80cout<<"Taille matrice NxN, N = "<<N<<endl;
81cout<<"Elements entre +/- scale = "<<scale<<endl;
82cout<<"Nombre de valeurs hors standard nbig = "<<nbig<<endl;
83cout<<"Valeurs hors standard (+/- vbig = "<<vbig<<" ) * scale"<<endl;
84cout<<"Nombre de lignes de matrice a imprimer "<<nprline<<endl;
85cout<<"Initialisation de l aleatoire par "<<nalea<<" tirages"<<endl;
86cout<<endl;
87
88//-- Initialization arrays
89SophyaInit();
90#ifdef ALSO_LAPACK
91 BaseArray::SetDefaultMemoryMapping(BaseArray::FortranMemoryMapping);
92#endif
93
94//-- Definition arrays
95TMatrix< TYPE > A(N,N);
96TMatrix< TYPE > InvA(N,N);
97TMatrix< TYPE > AiA(N,N);
98TMatrix< TYPE > B(N,N);
99TMatrix< TYPE > Adum(N,N);
100TVector< int > Vind(N*N);
101A.Show();
102
103//-- Mise a zero
104A = (TYPE) 0;
105InvA = (TYPE) 0;
106AiA = (TYPE) 0;
107B = (TYPE) 0;
108Adum = (TYPE) 0;
109BaseArray::SetMaxPrint(nprline*N,0);
110
111//-- Fill matrices
112uint_8 k;
113uint_4 i,j; double s;
114uint_4 nbr=0, nbi=0;
115Vind = 0;
116if(nalea>0) for(i=0;i<nalea;i++) drand01();
117A = Sequence(RandomSequence(RandomSequence::Flat,0.,1.));
118if(nbig>0) for(i=0;i<nbig;i++) {
119 k=(uint_8)(drand01()*N*N); if(k>=N*N) k--;
120 s=(drand01()>0.5)?1.:-1.;
121 if(Vind[k]==0) {A[k] += (TYPE) s*vbig; Vind[k]=1; nbr++;}
122}
123A *= (TYPE) scale;
124#if defined(COMPLEX)
125Vind = 0;
126B = Sequence(RandomSequence(RandomSequence::Flat,0.,1.));
127if(nbig>0) for(i=0;i<nbig;i++) {
128 k=(uint_8)(drand01()*N*N); if(k>=N*N) k--;
129 s=(drand01()>0.5)?1.:-1.;
130 if(Vind[k]==0) {B[k] += (TYPE) s*vbig; Vind[k]=1; nbi++;}
131}
132B *= (TYPE) scale;
133A += TYPE(0.,1.)*B;
134#endif
135cout<<"Nombre de valeurs BIG reelles = "<<nbr<<", imaginaires = "<<nbi<<endl;
136Adum = A;
137
138//-- Print matrice A
139cout<<"------------ TMatrix A :"<<endl;
140if(nprline>0) {cout<<A; cout<<endl<<endl;}
141
142//-- Inversion
143cout<<"------------ Inversion"<<endl;
144InvA = Inverse(A);
145cout<<"------------ TMatrix InvA = A^(-1):"<<endl;
146if(nprline>0) {cout<<InvA; cout<<endl<<endl;}
147
148//-- AiA = A * InvA
149cout<<"AiA = A * InvA"<<endl;
150AiA = A * InvA;
151cout<<"------------ TMatrix AiA = A * InvA:"<<endl;
152if(nprline>0) {cout<<AiA; cout<<endl;}
153
154//-- Check
155double vmax=-1.;
156for(i=0;i<N;i++) {
157 double absv = ABS_VAL( 1. - AiA(i,i) );
158 if( absv > vmax ) vmax = absv;
159}
160cout<<"Ecart maximum par rapport a 1 sur diagonale = "<<vmax<<endl;
161vmax = -1.;
162for(i=0;i<N;i++) for(j=0;j<N;j++) {
163 if( i == j ) continue;
164 double absv = ABS_VAL( AiA(i,j) );
165 if( absv > vmax ) vmax = absv;
166}
167cout<<"Ecart maximum par rapport a 0 hors diagonale = "<<vmax<<endl;
168
169////////////////////////////////////
170///////// Test avec Lapack /////////
171////////////////////////////////////
172#ifdef ALSO_LAPACK
173
174cout<<endl<<"LAPACK Cross-Check"<<endl;
175InvA = IdentityMatrix(1.,N);
176LapackLinSolve(Adum,InvA);
177AiA = A * InvA;
178vmax=-1.;
179for(i=0;i<N;i++) {
180 double absv = ABS_VAL( 1. - AiA(i,i) );
181 if( absv > vmax ) vmax = absv;
182}
183cout<<"Ecart maximum par rapport a 1 sur diagonale = "<<vmax<<endl;
184vmax = -1.;
185for(i=0;i<N;i++) for(j=0;j<N;j++) {
186 if( i == j ) continue;
187 double absv = ABS_VAL( AiA(i,j) );
188 if( absv > vmax ) vmax = absv;
189}
190cout<<"Ecart maximum par rapport a 0 hors diagonale = "<<vmax<<endl;
191
192#endif
193
194exit(0);
195}
Note: See TracBrowser for help on using the repository browser.