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

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

more options... cmv 16/5/00

File size: 5.8 KB
RevLine 
[934]1// Test de l'inversion de matrice (cmv 14/04/00)
[995]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
[934]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>
[1008]18#include "timing.h"
[934]19#include "ntoolsinit.h"
20#include "pexceptions.h"
21#include "array.h"
22#include "srandgen.h"
23
[995]24#ifdef ALSO_LAPACK
25#include "intflapack.h"
26#endif
[934]27
[995]28
[934]29#if defined(COMPLEX)
[975]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
[934]36#else
[975]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
[934]43#endif
44
45int main(int narg,char *arg[])
46{
47//--------------------------------------------------------
48// number of lines/columns
[975]49uint_4 N = 5;
[934]50// scale of the value (if =1 values between -1 and 1)
51r_8 scale = 1.;
52// number of values change by +/- vbig
[975]53uint_4 nbig = N;
[934]54r_8 vbig = 1000.;
[975]55// Nombre de lignes de matrice a imprimer
56uint_4 nprline = 10;
57// Initialisation du pauvre de l'aleatoire
[1008]58uint_4 nalea = 0;
59// data scaling for gauss pivoting and determinant
60int tscal = 1;
61bool detok=false;
62bool timok = false;
[934]63//--------------------------------------------------------
64
65//-- Decodage arguments
66char c;
[1008]67while((c = getopt(narg,arg,"hdtv:l:a:n:")) != -1) {
[934]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;
[975]75 case 'a' :
76 sscanf(optarg,"%d",&nalea);
77 break;
[1008]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;
[934]87 case 'h' :
[1008]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;
[934]94 exit(-1);
95 }
96}
97if(N<=1) N = 1;
[975]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;
[934]102cout<<"Nombre de lignes de matrice a imprimer "<<nprline<<endl;
[975]103cout<<"Initialisation de l aleatoire par "<<nalea<<" tirages"<<endl;
[1008]104 cout<<"Data scaling "<<tscal<<" determinant="<<detok<<" timing="<<timok<<endl;
[934]105cout<<endl;
106
107//-- Initialization arrays
108SophyaInit();
[1008]109if(timok) InitTim();
[995]110#ifdef ALSO_LAPACK
111 BaseArray::SetDefaultMemoryMapping(BaseArray::FortranMemoryMapping);
112#endif
[934]113
114//-- Definition arrays
115TMatrix< TYPE > A(N,N);
116TMatrix< TYPE > InvA(N,N);
117TMatrix< TYPE > AiA(N,N);
118TMatrix< TYPE > B(N,N);
[995]119TMatrix< TYPE > Adum(N,N);
[975]120TVector< int > Vind(N*N);
[934]121A.Show();
122
[1008]123SimpleMatrixOperation< TYPE >::SetGausPivScal(tscal);
124
[934]125//-- Mise a zero
[975]126A = (TYPE) 0;
[934]127InvA = (TYPE) 0;
[975]128AiA = (TYPE) 0;
129B = (TYPE) 0;
[995]130Adum = (TYPE) 0;
[934]131BaseArray::SetMaxPrint(nprline*N,0);
132
133//-- Fill matrices
[1008]134double vmax=-1.;
[934]135uint_8 k;
[975]136uint_4 i,j; double s;
137uint_4 nbr=0, nbi=0;
138Vind = 0;
139if(nalea>0) for(i=0;i<nalea;i++) drand01();
[934]140A = Sequence(RandomSequence(RandomSequence::Flat,0.,1.));
[975]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}
[934]146A *= (TYPE) scale;
147#if defined(COMPLEX)
[975]148Vind = 0;
[934]149B = Sequence(RandomSequence(RandomSequence::Flat,0.,1.));
[975]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}
[934]155B *= (TYPE) scale;
156A += TYPE(0.,1.)*B;
157#endif
[975]158cout<<"Nombre de valeurs BIG reelles = "<<nbr<<", imaginaires = "<<nbi<<endl;
[995]159Adum = A;
[934]160
161//-- Print matrice A
162cout<<"------------ TMatrix A :"<<endl;
163if(nprline>0) {cout<<A; cout<<endl<<endl;}
164
165//-- Inversion
[975]166cout<<"------------ Inversion"<<endl;
[998]167//InvA = Inverse(A);
[1008]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;
[934]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
[1008]183vmax=-1.;
[975]184for(i=0;i<N;i++) {
185 double absv = ABS_VAL( 1. - AiA(i,i) );
[934]186 if( absv > vmax ) vmax = absv;
187}
[975]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++) {
[934]191 if( i == j ) continue;
192 double absv = ABS_VAL( AiA(i,j) );
193 if( absv > vmax ) vmax = absv;
194}
[975]195cout<<"Ecart maximum par rapport a 0 hors diagonale = "<<vmax<<endl;
[934]196
[995]197////////////////////////////////////
198///////// Test avec Lapack /////////
199////////////////////////////////////
200#ifdef ALSO_LAPACK
201
202cout<<endl<<"LAPACK Cross-Check"<<endl;
203InvA = IdentityMatrix(1.,N);
[1008]204if(timok) PrtTim("--- End of check ---");
[995]205LapackLinSolve(Adum,InvA);
[1008]206if(timok) PrtTim("--- End of LapackLinSolve ---");
[995]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
[1008]224if(timok) PrtTim("--- End of Job ---");
[934]225exit(0);
226}
Note: See TracBrowser for help on using the repository browser.