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

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

adaptation aux nouvelles Sequences et pb de int/uint cmv 28/7/00

File size: 5.8 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 "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
45int main(int narg,char *arg[])
46{
47//--------------------------------------------------------
48// number of lines/columns
49uint_4 N = 5;
50// scale of the value (if =1 values between -1 and 1)
51r_8 scale = 1.;
52// number of values change by +/- vbig
53uint_4 nbig = N;
54r_8 vbig = 1000.;
55// Nombre de lignes de matrice a imprimer
56uint_4 nprline = 10;
57// Initialisation du pauvre de l'aleatoire
58uint_4 nalea = 0;
59// data scaling for gauss pivoting and determinant
60int tscal = 1;
61bool detok=false;
62bool timok = false;
63//--------------------------------------------------------
64
65//-- Decodage arguments
66char c;
67while((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}
97if(N<=1) N = 1;
98cout<<"Taille matrice NxN, N = "<<N<<endl;
99cout<<"Elements entre +/- scale = "<<scale<<endl;
100cout<<"Nombre de valeurs hors standard nbig = "<<nbig<<endl;
101cout<<"Valeurs hors standard (+/- vbig = "<<vbig<<" ) * scale"<<endl;
102cout<<"Nombre de lignes de matrice a imprimer "<<nprline<<endl;
103cout<<"Initialisation de l aleatoire par "<<nalea<<" tirages"<<endl;
104 cout<<"Data scaling "<<tscal<<" determinant="<<detok<<" timing="<<timok<<endl;
105cout<<endl;
106
107//-- Initialization arrays
108SophyaInit();
109if(timok) InitTim();
110#ifdef ALSO_LAPACK
111 BaseArray::SetDefaultMemoryMapping(BaseArray::FortranMemoryMapping);
112#endif
113
114//-- Definition arrays
115TMatrix< TYPE > A(N,N);
116TMatrix< TYPE > InvA(N,N);
117TMatrix< TYPE > AiA(N,N);
118TMatrix< TYPE > B(N,N);
119TMatrix< TYPE > Adum(N,N);
120TVector< int > Vind(N*N);
121A.Show();
122
123SimpleMatrixOperation< TYPE >::SetGausPivScal(tscal);
124
125//-- Mise a zero
126A = (TYPE) 0;
127InvA = (TYPE) 0;
128AiA = (TYPE) 0;
129B = (TYPE) 0;
130Adum = (TYPE) 0;
131BaseArray::SetMaxPrint(nprline*N,0);
132
133//-- Fill matrices
134double vmax=-1.;
135uint_8 k;
136uint_4 i,j; double s;
137uint_4 nbr=0, nbi=0;
138Vind = 0;
139if(nalea>0) for(i=0;i<nalea;i++) drand01();
140A = RandomSequence(RandomSequence::Flat,0.,1.);
141if(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}
146A *= (TYPE) scale;
147#if defined(COMPLEX)
148Vind = 0;
149B = RandomSequence(RandomSequence::Flat,0.,1.);
150if(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}
155B *= (TYPE) scale;
156A += TYPE(0.,1.)*B;
157#endif
158cout<<"Nombre de valeurs BIG reelles = "<<nbr<<", imaginaires = "<<nbi<<endl;
159Adum = A;
160
161//-- Print matrice A
162cout<<"------------ TMatrix A :"<<endl;
163if(nprline>0) {cout<<A; cout<<endl<<endl;}
164
165//-- Inversion
166cout<<"------------ Inversion"<<endl;
167//InvA = Inverse(A);
168InvA = IdentityMatrix(1.,N);
169if(timok) PrtTim("--- End of filling ---");
170TYPE det = GausPiv(A,InvA,detok);
171if(timok) PrtTim("--- End of GausPiv ---");
172cout<<"Det = "<<det<<endl;
173cout<<"------------ TMatrix InvA = A^(-1):"<<endl;
174if(nprline>0) {cout<<InvA; cout<<endl<<endl;}
175
176//-- AiA = A * InvA
177cout<<"AiA = A * InvA"<<endl;
178AiA = A * InvA;
179cout<<"------------ TMatrix AiA = A * InvA:"<<endl;
180if(nprline>0) {cout<<AiA; cout<<endl;}
181
182//-- Check
183vmax=-1.;
184for(i=0;i<N;i++) {
185 double absv = ABS_VAL( 1. - AiA(i,i) );
186 if( absv > vmax ) vmax = absv;
187}
188cout<<"Ecart maximum par rapport a 1 sur diagonale = "<<vmax<<endl;
189vmax = -1.;
190for(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}
195cout<<"Ecart maximum par rapport a 0 hors diagonale = "<<vmax<<endl;
196
197////////////////////////////////////
198///////// Test avec Lapack /////////
199////////////////////////////////////
200#ifdef ALSO_LAPACK
201
202cout<<endl<<"LAPACK Cross-Check"<<endl;
203InvA = IdentityMatrix(1.,N);
204if(timok) PrtTim("--- End of check ---");
205LapackLinSolve(Adum,InvA);
206if(timok) PrtTim("--- End of LapackLinSolve ---");
207AiA = A * InvA;
208vmax=-1.;
209for(i=0;i<N;i++) {
210 double absv = ABS_VAL( 1. - AiA(i,i) );
211 if( absv > vmax ) vmax = absv;
212}
213cout<<"Ecart maximum par rapport a 1 sur diagonale = "<<vmax<<endl;
214vmax = -1.;
215for(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}
220cout<<"Ecart maximum par rapport a 0 hors diagonale = "<<vmax<<endl;
221
222#endif
223
224if(timok) PrtTim("--- End of Job ---");
225exit(0);
226}
Note: See TracBrowser for help on using the repository browser.