[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 |
|
---|
| 44 | int main(int narg,char *arg[])
|
---|
| 45 | {
|
---|
| 46 | //--------------------------------------------------------
|
---|
| 47 | // number of lines/columns
|
---|
[975] | 48 | uint_4 N = 5;
|
---|
[934] | 49 | // scale of the value (if =1 values between -1 and 1)
|
---|
| 50 | r_8 scale = 1.;
|
---|
| 51 | // number of values change by +/- vbig
|
---|
[975] | 52 | uint_4 nbig = N;
|
---|
[934] | 53 | r_8 vbig = 1000.;
|
---|
[975] | 54 | // Nombre de lignes de matrice a imprimer
|
---|
| 55 | uint_4 nprline = 10;
|
---|
| 56 | // Initialisation du pauvre de l'aleatoire
|
---|
| 57 | uint_4 nalea = 0;
|
---|
[934] | 58 | //--------------------------------------------------------
|
---|
| 59 |
|
---|
| 60 | //-- Decodage arguments
|
---|
| 61 | char c;
|
---|
[975] | 62 | while((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 | }
|
---|
| 79 | if(N<=1) N = 1;
|
---|
[975] | 80 | cout<<"Taille matrice NxN, N = "<<N<<endl;
|
---|
| 81 | cout<<"Elements entre +/- scale = "<<scale<<endl;
|
---|
| 82 | cout<<"Nombre de valeurs hors standard nbig = "<<nbig<<endl;
|
---|
| 83 | cout<<"Valeurs hors standard (+/- vbig = "<<vbig<<" ) * scale"<<endl;
|
---|
[934] | 84 | cout<<"Nombre de lignes de matrice a imprimer "<<nprline<<endl;
|
---|
[975] | 85 | cout<<"Initialisation de l aleatoire par "<<nalea<<" tirages"<<endl;
|
---|
[934] | 86 | cout<<endl;
|
---|
| 87 |
|
---|
| 88 | //-- Initialization arrays
|
---|
| 89 | SophyaInit();
|
---|
[995] | 90 | #ifdef ALSO_LAPACK
|
---|
| 91 | BaseArray::SetDefaultMemoryMapping(BaseArray::FortranMemoryMapping);
|
---|
| 92 | #endif
|
---|
[934] | 93 |
|
---|
| 94 | //-- Definition arrays
|
---|
| 95 | TMatrix< TYPE > A(N,N);
|
---|
| 96 | TMatrix< TYPE > InvA(N,N);
|
---|
| 97 | TMatrix< TYPE > AiA(N,N);
|
---|
| 98 | TMatrix< TYPE > B(N,N);
|
---|
[995] | 99 | TMatrix< TYPE > Adum(N,N);
|
---|
[975] | 100 | TVector< int > Vind(N*N);
|
---|
[934] | 101 | A.Show();
|
---|
| 102 |
|
---|
| 103 | //-- Mise a zero
|
---|
[975] | 104 | A = (TYPE) 0;
|
---|
[934] | 105 | InvA = (TYPE) 0;
|
---|
[975] | 106 | AiA = (TYPE) 0;
|
---|
| 107 | B = (TYPE) 0;
|
---|
[995] | 108 | Adum = (TYPE) 0;
|
---|
[934] | 109 | BaseArray::SetMaxPrint(nprline*N,0);
|
---|
| 110 |
|
---|
| 111 | //-- Fill matrices
|
---|
| 112 | uint_8 k;
|
---|
[975] | 113 | uint_4 i,j; double s;
|
---|
| 114 | uint_4 nbr=0, nbi=0;
|
---|
| 115 | Vind = 0;
|
---|
| 116 | if(nalea>0) for(i=0;i<nalea;i++) drand01();
|
---|
[934] | 117 | A = Sequence(RandomSequence(RandomSequence::Flat,0.,1.));
|
---|
[975] | 118 | if(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] | 123 | A *= (TYPE) scale;
|
---|
| 124 | #if defined(COMPLEX)
|
---|
[975] | 125 | Vind = 0;
|
---|
[934] | 126 | B = Sequence(RandomSequence(RandomSequence::Flat,0.,1.));
|
---|
[975] | 127 | if(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] | 132 | B *= (TYPE) scale;
|
---|
| 133 | A += TYPE(0.,1.)*B;
|
---|
| 134 | #endif
|
---|
[975] | 135 | cout<<"Nombre de valeurs BIG reelles = "<<nbr<<", imaginaires = "<<nbi<<endl;
|
---|
[995] | 136 | Adum = A;
|
---|
[934] | 137 |
|
---|
| 138 | //-- Print matrice A
|
---|
| 139 | cout<<"------------ TMatrix A :"<<endl;
|
---|
| 140 | if(nprline>0) {cout<<A; cout<<endl<<endl;}
|
---|
| 141 |
|
---|
| 142 | //-- Inversion
|
---|
[975] | 143 | cout<<"------------ Inversion"<<endl;
|
---|
[998] | 144 | //InvA = Inverse(A);
|
---|
| 145 | InvA = IdentityMatrix(1.,N); TYPE det = GausPiv(A,InvA); cout<<"Det = "<<det<<endl;
|
---|
[934] | 146 | cout<<"------------ TMatrix InvA = A^(-1):"<<endl;
|
---|
| 147 | if(nprline>0) {cout<<InvA; cout<<endl<<endl;}
|
---|
| 148 |
|
---|
| 149 | //-- AiA = A * InvA
|
---|
| 150 | cout<<"AiA = A * InvA"<<endl;
|
---|
| 151 | AiA = A * InvA;
|
---|
| 152 | cout<<"------------ TMatrix AiA = A * InvA:"<<endl;
|
---|
| 153 | if(nprline>0) {cout<<AiA; cout<<endl;}
|
---|
| 154 |
|
---|
| 155 | //-- Check
|
---|
[975] | 156 | double vmax=-1.;
|
---|
| 157 | for(i=0;i<N;i++) {
|
---|
| 158 | double absv = ABS_VAL( 1. - AiA(i,i) );
|
---|
[934] | 159 | if( absv > vmax ) vmax = absv;
|
---|
| 160 | }
|
---|
[975] | 161 | cout<<"Ecart maximum par rapport a 1 sur diagonale = "<<vmax<<endl;
|
---|
| 162 | vmax = -1.;
|
---|
| 163 | for(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] | 168 | cout<<"Ecart maximum par rapport a 0 hors diagonale = "<<vmax<<endl;
|
---|
[934] | 169 |
|
---|
[995] | 170 | ////////////////////////////////////
|
---|
| 171 | ///////// Test avec Lapack /////////
|
---|
| 172 | ////////////////////////////////////
|
---|
| 173 | #ifdef ALSO_LAPACK
|
---|
| 174 |
|
---|
| 175 | cout<<endl<<"LAPACK Cross-Check"<<endl;
|
---|
| 176 | InvA = IdentityMatrix(1.,N);
|
---|
| 177 | LapackLinSolve(Adum,InvA);
|
---|
| 178 | AiA = A * InvA;
|
---|
| 179 | vmax=-1.;
|
---|
| 180 | for(i=0;i<N;i++) {
|
---|
| 181 | double absv = ABS_VAL( 1. - AiA(i,i) );
|
---|
| 182 | if( absv > vmax ) vmax = absv;
|
---|
| 183 | }
|
---|
| 184 | cout<<"Ecart maximum par rapport a 1 sur diagonale = "<<vmax<<endl;
|
---|
| 185 | vmax = -1.;
|
---|
| 186 | for(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 | }
|
---|
| 191 | cout<<"Ecart maximum par rapport a 0 hors diagonale = "<<vmax<<endl;
|
---|
| 192 |
|
---|
| 193 | #endif
|
---|
| 194 |
|
---|
[934] | 195 | exit(0);
|
---|
| 196 | }
|
---|