| [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 |  | 
|---|
|  | 45 | int main(int narg,char *arg[]) | 
|---|
|  | 46 | { | 
|---|
|  | 47 | //-------------------------------------------------------- | 
|---|
|  | 48 | // number of lines/columns | 
|---|
| [975] | 49 | uint_4 N = 5; | 
|---|
| [934] | 50 | // scale of the value (if =1 values between -1 and 1) | 
|---|
|  | 51 | r_8 scale = 1.; | 
|---|
|  | 52 | // number of values change by +/- vbig | 
|---|
| [975] | 53 | uint_4 nbig = N; | 
|---|
| [934] | 54 | r_8 vbig = 1000.; | 
|---|
| [975] | 55 | // Nombre de lignes de matrice a imprimer | 
|---|
|  | 56 | uint_4 nprline = 10; | 
|---|
|  | 57 | // Initialisation du pauvre de l'aleatoire | 
|---|
| [1008] | 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; | 
|---|
| [934] | 63 | //-------------------------------------------------------- | 
|---|
|  | 64 |  | 
|---|
|  | 65 | //-- Decodage arguments | 
|---|
|  | 66 | char c; | 
|---|
| [1008] | 67 | while((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 | } | 
|---|
|  | 97 | if(N<=1) N = 1; | 
|---|
| [975] | 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; | 
|---|
| [934] | 102 | cout<<"Nombre de lignes de matrice a imprimer "<<nprline<<endl; | 
|---|
| [975] | 103 | cout<<"Initialisation de l aleatoire par "<<nalea<<" tirages"<<endl; | 
|---|
| [1008] | 104 | cout<<"Data scaling "<<tscal<<" determinant="<<detok<<" timing="<<timok<<endl; | 
|---|
| [934] | 105 | cout<<endl; | 
|---|
|  | 106 |  | 
|---|
|  | 107 | //-- Initialization arrays | 
|---|
|  | 108 | SophyaInit(); | 
|---|
| [1008] | 109 | if(timok) InitTim(); | 
|---|
| [995] | 110 | #ifdef ALSO_LAPACK | 
|---|
|  | 111 | BaseArray::SetDefaultMemoryMapping(BaseArray::FortranMemoryMapping); | 
|---|
|  | 112 | #endif | 
|---|
| [934] | 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); | 
|---|
| [995] | 119 | TMatrix< TYPE > Adum(N,N); | 
|---|
| [975] | 120 | TVector< int >  Vind(N*N); | 
|---|
| [934] | 121 | A.Show(); | 
|---|
|  | 122 |  | 
|---|
| [1008] | 123 | SimpleMatrixOperation< TYPE >::SetGausPivScal(tscal); | 
|---|
|  | 124 |  | 
|---|
| [934] | 125 | //-- Mise a zero | 
|---|
| [975] | 126 | A    = (TYPE) 0; | 
|---|
| [934] | 127 | InvA = (TYPE) 0; | 
|---|
| [975] | 128 | AiA  = (TYPE) 0; | 
|---|
|  | 129 | B    = (TYPE) 0; | 
|---|
| [995] | 130 | Adum = (TYPE) 0; | 
|---|
| [934] | 131 | BaseArray::SetMaxPrint(nprline*N,0); | 
|---|
|  | 132 |  | 
|---|
|  | 133 | //-- Fill matrices | 
|---|
| [1008] | 134 | double vmax=-1.; | 
|---|
| [934] | 135 | uint_8 k; | 
|---|
| [975] | 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(); | 
|---|
| [934] | 140 | A = Sequence(RandomSequence(RandomSequence::Flat,0.,1.)); | 
|---|
| [975] | 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 | } | 
|---|
| [934] | 146 | A *= (TYPE) scale; | 
|---|
|  | 147 | #if defined(COMPLEX) | 
|---|
| [975] | 148 | Vind = 0; | 
|---|
| [934] | 149 | B = Sequence(RandomSequence(RandomSequence::Flat,0.,1.)); | 
|---|
| [975] | 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 | } | 
|---|
| [934] | 155 | B *= (TYPE) scale; | 
|---|
|  | 156 | A += TYPE(0.,1.)*B; | 
|---|
|  | 157 | #endif | 
|---|
| [975] | 158 | cout<<"Nombre de valeurs BIG reelles = "<<nbr<<", imaginaires = "<<nbi<<endl; | 
|---|
| [995] | 159 | Adum = A; | 
|---|
| [934] | 160 |  | 
|---|
|  | 161 | //-- Print matrice A | 
|---|
|  | 162 | cout<<"------------ TMatrix A :"<<endl; | 
|---|
|  | 163 | if(nprline>0) {cout<<A; cout<<endl<<endl;} | 
|---|
|  | 164 |  | 
|---|
|  | 165 | //-- Inversion | 
|---|
| [975] | 166 | cout<<"------------ Inversion"<<endl; | 
|---|
| [998] | 167 | //InvA = Inverse(A); | 
|---|
| [1008] | 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; | 
|---|
| [934] | 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 | 
|---|
| [1008] | 183 | vmax=-1.; | 
|---|
| [975] | 184 | for(i=0;i<N;i++) { | 
|---|
|  | 185 | double absv = ABS_VAL( 1. - AiA(i,i) ); | 
|---|
| [934] | 186 | if( absv > vmax ) vmax = absv; | 
|---|
|  | 187 | } | 
|---|
| [975] | 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++) { | 
|---|
| [934] | 191 | if( i == j ) continue; | 
|---|
|  | 192 | double absv = ABS_VAL( AiA(i,j) ); | 
|---|
|  | 193 | if( absv > vmax ) vmax = absv; | 
|---|
|  | 194 | } | 
|---|
| [975] | 195 | cout<<"Ecart maximum par rapport a 0 hors diagonale = "<<vmax<<endl; | 
|---|
| [934] | 196 |  | 
|---|
| [995] | 197 | //////////////////////////////////// | 
|---|
|  | 198 | ///////// Test avec Lapack ///////// | 
|---|
|  | 199 | //////////////////////////////////// | 
|---|
|  | 200 | #ifdef ALSO_LAPACK | 
|---|
|  | 201 |  | 
|---|
|  | 202 | cout<<endl<<"LAPACK Cross-Check"<<endl; | 
|---|
|  | 203 | InvA = IdentityMatrix(1.,N); | 
|---|
| [1008] | 204 | if(timok) PrtTim("--- End of check ---"); | 
|---|
| [995] | 205 | LapackLinSolve(Adum,InvA); | 
|---|
| [1008] | 206 | if(timok) PrtTim("--- End of LapackLinSolve ---"); | 
|---|
| [995] | 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 |  | 
|---|
| [1008] | 224 | if(timok) PrtTim("--- End of Job ---"); | 
|---|
| [934] | 225 | exit(0); | 
|---|
|  | 226 | } | 
|---|