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>
|
---|
13 | #include <stdlib.h>
|
---|
14 | #include <stdio.h>
|
---|
15 | #include <string.h>
|
---|
16 | #include <math.h>
|
---|
17 | #include <unistd.h>
|
---|
18 | #include "timing.h"
|
---|
19 | #include "ntoolsinit.h"
|
---|
20 | #include "pexceptions.h"
|
---|
21 | #include "array.h"
|
---|
22 | #include "srandgen.h"
|
---|
23 |
|
---|
24 | #ifdef ALSO_LAPACK
|
---|
25 | #include "intflapack.h"
|
---|
26 | #endif
|
---|
27 |
|
---|
28 |
|
---|
29 | #if defined(COMPLEX)
|
---|
30 | #define ABS_VAL(_x_) sqrt((double)((_x_).real()*(_x_).real() + (_x_).imag()*(_x_).imag()))
|
---|
31 | #if defined(PRECIS32)
|
---|
32 | #define TYPE complex<r_4>
|
---|
33 | #else
|
---|
34 | #define TYPE complex<r_8>
|
---|
35 | #endif
|
---|
36 | #else
|
---|
37 | #define ABS_VAL(_x_) fabs((double)_x_)
|
---|
38 | #if defined(PRECIS32)
|
---|
39 | #define TYPE r_4
|
---|
40 | #else
|
---|
41 | #define TYPE r_8
|
---|
42 | #endif
|
---|
43 | #endif
|
---|
44 |
|
---|
45 | int main(int narg,char *arg[])
|
---|
46 | {
|
---|
47 | //--------------------------------------------------------
|
---|
48 | // number of lines/columns
|
---|
49 | uint_4 N = 5;
|
---|
50 | // scale of the value (if =1 values between -1 and 1)
|
---|
51 | r_8 scale = 1.;
|
---|
52 | // number of values change by +/- vbig
|
---|
53 | uint_4 nbig = N;
|
---|
54 | r_8 vbig = 1000.;
|
---|
55 | // Nombre de lignes de matrice a imprimer
|
---|
56 | uint_4 nprline = 10;
|
---|
57 | // Initialisation du pauvre de l'aleatoire
|
---|
58 | uint_4 nalea = 0;
|
---|
59 | // data scaling for gauss pivoting and determinant
|
---|
60 | int tscal = 1;
|
---|
61 | bool detok=false;
|
---|
62 | bool timok = false;
|
---|
63 | //--------------------------------------------------------
|
---|
64 |
|
---|
65 | //-- Decodage arguments
|
---|
66 | char c;
|
---|
67 | while((c = getopt(narg,arg,"hdtv:l:a:n:")) != -1) {
|
---|
68 | switch (c) {
|
---|
69 | case 'v' :
|
---|
70 | sscanf(optarg,"%d,%lf,%d,%lf",&N,&scale,&nbig,&vbig);
|
---|
71 | break;
|
---|
72 | case 'l' :
|
---|
73 | sscanf(optarg,"%d",&nprline);
|
---|
74 | break;
|
---|
75 | case 'a' :
|
---|
76 | sscanf(optarg,"%d",&nalea);
|
---|
77 | break;
|
---|
78 | case 'n' :
|
---|
79 | sscanf(optarg,"%d",&tscal);
|
---|
80 | break;
|
---|
81 | case 'd' :
|
---|
82 | detok = true;
|
---|
83 | break;
|
---|
84 | case 't' :
|
---|
85 | timok = true;
|
---|
86 | break;
|
---|
87 | case 'h' :
|
---|
88 | cout<<"tsttminv [-h] [-v N,scale,nbig,vbig] [-l nprline] [-a nalea]"<<endl
|
---|
89 | <<" [-n typs] [-d] [-t]"<<endl
|
---|
90 | <<"matrix filled with : {[-1,1] +/- vbig(nbig time)}*scale"<<endl
|
---|
91 | <<"-n 0/1/2 : data scaling 0=no, 1=global (def), 2=row-by-row"<<endl
|
---|
92 | <<"-d : also compute determinant"<<endl
|
---|
93 | <<"-t : do timing"<<endl;
|
---|
94 | exit(-1);
|
---|
95 | }
|
---|
96 | }
|
---|
97 | if(N<=1) N = 1;
|
---|
98 | cout<<"Taille matrice NxN, N = "<<N<<endl;
|
---|
99 | cout<<"Elements entre +/- scale = "<<scale<<endl;
|
---|
100 | cout<<"Nombre de valeurs hors standard nbig = "<<nbig<<endl;
|
---|
101 | cout<<"Valeurs hors standard (+/- vbig = "<<vbig<<" ) * scale"<<endl;
|
---|
102 | cout<<"Nombre de lignes de matrice a imprimer "<<nprline<<endl;
|
---|
103 | cout<<"Initialisation de l aleatoire par "<<nalea<<" tirages"<<endl;
|
---|
104 | cout<<"Data scaling "<<tscal<<" determinant="<<detok<<" timing="<<timok<<endl;
|
---|
105 | cout<<endl;
|
---|
106 |
|
---|
107 | //-- Initialization arrays
|
---|
108 | SophyaInit();
|
---|
109 | if(timok) InitTim();
|
---|
110 | #ifdef ALSO_LAPACK
|
---|
111 | BaseArray::SetDefaultMemoryMapping(BaseArray::FortranMemoryMapping);
|
---|
112 | #endif
|
---|
113 |
|
---|
114 | //-- Definition arrays
|
---|
115 | TMatrix< TYPE > A(N,N);
|
---|
116 | TMatrix< TYPE > InvA(N,N);
|
---|
117 | TMatrix< TYPE > AiA(N,N);
|
---|
118 | TMatrix< TYPE > B(N,N);
|
---|
119 | TMatrix< TYPE > Adum(N,N);
|
---|
120 | TVector< int > Vind(N*N);
|
---|
121 | A.Show();
|
---|
122 |
|
---|
123 | SimpleMatrixOperation< TYPE >::SetGausPivScal(tscal);
|
---|
124 |
|
---|
125 | //-- Mise a zero
|
---|
126 | A = (TYPE) 0;
|
---|
127 | InvA = (TYPE) 0;
|
---|
128 | AiA = (TYPE) 0;
|
---|
129 | B = (TYPE) 0;
|
---|
130 | Adum = (TYPE) 0;
|
---|
131 | BaseArray::SetMaxPrint(nprline*N,0);
|
---|
132 |
|
---|
133 | //-- Fill matrices
|
---|
134 | double vmax=-1.;
|
---|
135 | uint_8 k;
|
---|
136 | uint_4 i,j; double s;
|
---|
137 | uint_4 nbr=0, nbi=0;
|
---|
138 | Vind = 0;
|
---|
139 | if(nalea>0) for(i=0;i<nalea;i++) drand01();
|
---|
140 | A = RandomSequence(RandomSequence::Flat,0.,1.);
|
---|
141 | if(nbig>0) for(i=0;i<nbig;i++) {
|
---|
142 | k=(uint_8)(drand01()*N*N); if(k>=N*N) k--;
|
---|
143 | s=(drand01()>0.5)?1.:-1.;
|
---|
144 | if(Vind[k]==0) {A[k] += (TYPE) s*vbig; Vind[k]=1; nbr++;}
|
---|
145 | }
|
---|
146 | A *= (TYPE) scale;
|
---|
147 | #if defined(COMPLEX)
|
---|
148 | Vind = 0;
|
---|
149 | B = RandomSequence(RandomSequence::Flat,0.,1.);
|
---|
150 | if(nbig>0) for(i=0;i<nbig;i++) {
|
---|
151 | k=(uint_8)(drand01()*N*N); if(k>=N*N) k--;
|
---|
152 | s=(drand01()>0.5)?1.:-1.;
|
---|
153 | if(Vind[k]==0) {B[k] += (TYPE) s*vbig; Vind[k]=1; nbi++;}
|
---|
154 | }
|
---|
155 | B *= (TYPE) scale;
|
---|
156 | A += TYPE(0.,1.)*B;
|
---|
157 | #endif
|
---|
158 | cout<<"Nombre de valeurs BIG reelles = "<<nbr<<", imaginaires = "<<nbi<<endl;
|
---|
159 | Adum = A;
|
---|
160 |
|
---|
161 | //-- Print matrice A
|
---|
162 | cout<<"------------ TMatrix A :"<<endl;
|
---|
163 | if(nprline>0) {cout<<A; cout<<endl<<endl;}
|
---|
164 |
|
---|
165 | //-- Inversion
|
---|
166 | cout<<"------------ Inversion"<<endl;
|
---|
167 | //InvA = Inverse(A);
|
---|
168 | InvA = IdentityMatrix(1.,N);
|
---|
169 | if(timok) PrtTim("--- End of filling ---");
|
---|
170 | TYPE det = GausPiv(A,InvA,detok);
|
---|
171 | if(timok) PrtTim("--- End of GausPiv ---");
|
---|
172 | cout<<"Det = "<<det<<endl;
|
---|
173 | cout<<"------------ TMatrix InvA = A^(-1):"<<endl;
|
---|
174 | if(nprline>0) {cout<<InvA; cout<<endl<<endl;}
|
---|
175 |
|
---|
176 | //-- AiA = A * InvA
|
---|
177 | cout<<"AiA = A * InvA"<<endl;
|
---|
178 | AiA = A * InvA;
|
---|
179 | cout<<"------------ TMatrix AiA = A * InvA:"<<endl;
|
---|
180 | if(nprline>0) {cout<<AiA; cout<<endl;}
|
---|
181 |
|
---|
182 | //-- Check
|
---|
183 | vmax=-1.;
|
---|
184 | for(i=0;i<N;i++) {
|
---|
185 | double absv = ABS_VAL( 1. - AiA(i,i) );
|
---|
186 | if( absv > vmax ) vmax = absv;
|
---|
187 | }
|
---|
188 | cout<<"Ecart maximum par rapport a 1 sur diagonale = "<<vmax<<endl;
|
---|
189 | vmax = -1.;
|
---|
190 | for(i=0;i<N;i++) for(j=0;j<N;j++) {
|
---|
191 | if( i == j ) continue;
|
---|
192 | double absv = ABS_VAL( AiA(i,j) );
|
---|
193 | if( absv > vmax ) vmax = absv;
|
---|
194 | }
|
---|
195 | cout<<"Ecart maximum par rapport a 0 hors diagonale = "<<vmax<<endl;
|
---|
196 |
|
---|
197 | ////////////////////////////////////
|
---|
198 | ///////// Test avec Lapack /////////
|
---|
199 | ////////////////////////////////////
|
---|
200 | #ifdef ALSO_LAPACK
|
---|
201 |
|
---|
202 | cout<<endl<<"LAPACK Cross-Check"<<endl;
|
---|
203 | InvA = IdentityMatrix(1.,N);
|
---|
204 | if(timok) PrtTim("--- End of check ---");
|
---|
205 | LapackLinSolve(Adum,InvA);
|
---|
206 | if(timok) PrtTim("--- End of LapackLinSolve ---");
|
---|
207 | AiA = A * InvA;
|
---|
208 | vmax=-1.;
|
---|
209 | for(i=0;i<N;i++) {
|
---|
210 | double absv = ABS_VAL( 1. - AiA(i,i) );
|
---|
211 | if( absv > vmax ) vmax = absv;
|
---|
212 | }
|
---|
213 | cout<<"Ecart maximum par rapport a 1 sur diagonale = "<<vmax<<endl;
|
---|
214 | vmax = -1.;
|
---|
215 | for(i=0;i<N;i++) for(j=0;j<N;j++) {
|
---|
216 | if( i == j ) continue;
|
---|
217 | double absv = ABS_VAL( AiA(i,j) );
|
---|
218 | if( absv > vmax ) vmax = absv;
|
---|
219 | }
|
---|
220 | cout<<"Ecart maximum par rapport a 0 hors diagonale = "<<vmax<<endl;
|
---|
221 |
|
---|
222 | #endif
|
---|
223 |
|
---|
224 | if(timok) PrtTim("--- End of Job ---");
|
---|
225 | exit(0);
|
---|
226 | }
|
---|