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

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

compute also determinant cmv 3/5/00

File size: 4.9 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>
18#include "ntoolsinit.h"
19#include "pexceptions.h"
20#include "array.h"
21#include "srandgen.h"
22
[995]23#ifdef ALSO_LAPACK
24#include "intflapack.h"
25#endif
[934]26
[995]27
[934]28#if defined(COMPLEX)
[975]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
[934]35#else
[975]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
[934]42#endif
43
44int main(int narg,char *arg[])
45{
46//--------------------------------------------------------
47// number of lines/columns
[975]48uint_4 N = 5;
[934]49// scale of the value (if =1 values between -1 and 1)
50r_8 scale = 1.;
51// number of values change by +/- vbig
[975]52uint_4 nbig = N;
[934]53r_8 vbig = 1000.;
[975]54// Nombre de lignes de matrice a imprimer
55uint_4 nprline = 10;
56// Initialisation du pauvre de l'aleatoire
57 uint_4 nalea = 0;
[934]58//--------------------------------------------------------
59
60//-- Decodage arguments
61char c;
[975]62while((c = getopt(narg,arg,"hv:l:a:")) != -1) {
[934]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;
[975]70 case 'a' :
71 sscanf(optarg,"%d",&nalea);
72 break;
[934]73 case 'h' :
[975]74 cout<<"tsttminv [-h] [-v N,scale,nbig,vbig] [-l nprline] [-a nalea]"<<endl;
[934]75 cout<<"matrix filled with : {[-1,1] +/- vbig(nbig time)}*scale"<<endl;
76 exit(-1);
77 }
78}
79if(N<=1) N = 1;
[975]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;
[934]84cout<<"Nombre de lignes de matrice a imprimer "<<nprline<<endl;
[975]85cout<<"Initialisation de l aleatoire par "<<nalea<<" tirages"<<endl;
[934]86cout<<endl;
87
88//-- Initialization arrays
89SophyaInit();
[995]90#ifdef ALSO_LAPACK
91 BaseArray::SetDefaultMemoryMapping(BaseArray::FortranMemoryMapping);
92#endif
[934]93
94//-- Definition arrays
95TMatrix< TYPE > A(N,N);
96TMatrix< TYPE > InvA(N,N);
97TMatrix< TYPE > AiA(N,N);
98TMatrix< TYPE > B(N,N);
[995]99TMatrix< TYPE > Adum(N,N);
[975]100TVector< int > Vind(N*N);
[934]101A.Show();
102
103//-- Mise a zero
[975]104A = (TYPE) 0;
[934]105InvA = (TYPE) 0;
[975]106AiA = (TYPE) 0;
107B = (TYPE) 0;
[995]108Adum = (TYPE) 0;
[934]109BaseArray::SetMaxPrint(nprline*N,0);
110
111//-- Fill matrices
112uint_8 k;
[975]113uint_4 i,j; double s;
114uint_4 nbr=0, nbi=0;
115Vind = 0;
116if(nalea>0) for(i=0;i<nalea;i++) drand01();
[934]117A = Sequence(RandomSequence(RandomSequence::Flat,0.,1.));
[975]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}
[934]123A *= (TYPE) scale;
124#if defined(COMPLEX)
[975]125Vind = 0;
[934]126B = Sequence(RandomSequence(RandomSequence::Flat,0.,1.));
[975]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}
[934]132B *= (TYPE) scale;
133A += TYPE(0.,1.)*B;
134#endif
[975]135cout<<"Nombre de valeurs BIG reelles = "<<nbr<<", imaginaires = "<<nbi<<endl;
[995]136Adum = A;
[934]137
138//-- Print matrice A
139cout<<"------------ TMatrix A :"<<endl;
140if(nprline>0) {cout<<A; cout<<endl<<endl;}
141
142//-- Inversion
[975]143cout<<"------------ Inversion"<<endl;
[998]144//InvA = Inverse(A);
145InvA = IdentityMatrix(1.,N); TYPE det = GausPiv(A,InvA); cout<<"Det = "<<det<<endl;
[934]146cout<<"------------ TMatrix InvA = A^(-1):"<<endl;
147if(nprline>0) {cout<<InvA; cout<<endl<<endl;}
148
149//-- AiA = A * InvA
150cout<<"AiA = A * InvA"<<endl;
151AiA = A * InvA;
152cout<<"------------ TMatrix AiA = A * InvA:"<<endl;
153if(nprline>0) {cout<<AiA; cout<<endl;}
154
155//-- Check
[975]156double vmax=-1.;
157for(i=0;i<N;i++) {
158 double absv = ABS_VAL( 1. - AiA(i,i) );
[934]159 if( absv > vmax ) vmax = absv;
160}
[975]161cout<<"Ecart maximum par rapport a 1 sur diagonale = "<<vmax<<endl;
162vmax = -1.;
163for(i=0;i<N;i++) for(j=0;j<N;j++) {
[934]164 if( i == j ) continue;
165 double absv = ABS_VAL( AiA(i,j) );
166 if( absv > vmax ) vmax = absv;
167}
[975]168cout<<"Ecart maximum par rapport a 0 hors diagonale = "<<vmax<<endl;
[934]169
[995]170////////////////////////////////////
171///////// Test avec Lapack /////////
172////////////////////////////////////
173#ifdef ALSO_LAPACK
174
175cout<<endl<<"LAPACK Cross-Check"<<endl;
176InvA = IdentityMatrix(1.,N);
177LapackLinSolve(Adum,InvA);
178AiA = A * InvA;
179vmax=-1.;
180for(i=0;i<N;i++) {
181 double absv = ABS_VAL( 1. - AiA(i,i) );
182 if( absv > vmax ) vmax = absv;
183}
184cout<<"Ecart maximum par rapport a 1 sur diagonale = "<<vmax<<endl;
185vmax = -1.;
186for(i=0;i<N;i++) for(j=0;j<N;j++) {
187 if( i == j ) continue;
188 double absv = ABS_VAL( AiA(i,j) );
189 if( absv > vmax ) vmax = absv;
190}
191cout<<"Ecart maximum par rapport a 0 hors diagonale = "<<vmax<<endl;
192
193#endif
194
[934]195exit(0);
196}
Note: See TracBrowser for help on using the repository browser.