[934] | 1 | // Test de l'inversion de matrice (cmv 14/04/00)
|
---|
| 2 | #include "machdefs.h"
|
---|
| 3 | #include <iostream.h>
|
---|
| 4 | #include <stdlib.h>
|
---|
| 5 | #include <stdio.h>
|
---|
| 6 | #include <string.h>
|
---|
| 7 | #include <math.h>
|
---|
| 8 | #include <unistd.h>
|
---|
| 9 | #include "ntoolsinit.h"
|
---|
| 10 | #include "pexceptions.h"
|
---|
| 11 | #include "array.h"
|
---|
| 12 | #include "srandgen.h"
|
---|
| 13 |
|
---|
| 14 | // if defined COMPLEX , if not REAL
|
---|
| 15 | #define COMPLEX
|
---|
| 16 | // if defined 32 bits precision, if not 64 bits
|
---|
| 17 | // #define PRECIS32
|
---|
| 18 |
|
---|
| 19 | #if defined(COMPLEX)
|
---|
[975] | 20 | #define ABS_VAL(_x_) sqrt((double)((_x_).real()*(_x_).real() + (_x_).imag()*(_x_).imag()))
|
---|
| 21 | #if defined(PRECIS32)
|
---|
| 22 | #define TYPE complex<r_4>
|
---|
| 23 | #else
|
---|
| 24 | #define TYPE complex<r_8>
|
---|
| 25 | #endif
|
---|
[934] | 26 | #else
|
---|
[975] | 27 | #define ABS_VAL(_x_) fabs((double)_x_)
|
---|
| 28 | #if defined(PRECIS32)
|
---|
| 29 | #define TYPE r_4
|
---|
| 30 | #else
|
---|
| 31 | #define TYPE r_8
|
---|
| 32 | #endif
|
---|
[934] | 33 | #endif
|
---|
| 34 |
|
---|
| 35 | int main(int narg,char *arg[])
|
---|
| 36 | {
|
---|
| 37 | //--------------------------------------------------------
|
---|
| 38 | // number of lines/columns
|
---|
[975] | 39 | uint_4 N = 5;
|
---|
[934] | 40 | // scale of the value (if =1 values between -1 and 1)
|
---|
| 41 | r_8 scale = 1.;
|
---|
| 42 | // number of values change by +/- vbig
|
---|
[975] | 43 | uint_4 nbig = N;
|
---|
[934] | 44 | r_8 vbig = 1000.;
|
---|
[975] | 45 | // Nombre de lignes de matrice a imprimer
|
---|
| 46 | uint_4 nprline = 10;
|
---|
| 47 | // Initialisation du pauvre de l'aleatoire
|
---|
| 48 | uint_4 nalea = 0;
|
---|
[934] | 49 | //--------------------------------------------------------
|
---|
| 50 |
|
---|
| 51 | //-- Decodage arguments
|
---|
| 52 | char c;
|
---|
[975] | 53 | while((c = getopt(narg,arg,"hv:l:a:")) != -1) {
|
---|
[934] | 54 | switch (c) {
|
---|
| 55 | case 'v' :
|
---|
| 56 | sscanf(optarg,"%d,%lf,%d,%lf",&N,&scale,&nbig,&vbig);
|
---|
| 57 | break;
|
---|
| 58 | case 'l' :
|
---|
| 59 | sscanf(optarg,"%d",&nprline);
|
---|
| 60 | break;
|
---|
[975] | 61 | case 'a' :
|
---|
| 62 | sscanf(optarg,"%d",&nalea);
|
---|
| 63 | break;
|
---|
[934] | 64 | case 'h' :
|
---|
[975] | 65 | cout<<"tsttminv [-h] [-v N,scale,nbig,vbig] [-l nprline] [-a nalea]"<<endl;
|
---|
[934] | 66 | cout<<"matrix filled with : {[-1,1] +/- vbig(nbig time)}*scale"<<endl;
|
---|
| 67 | exit(-1);
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 | if(N<=1) N = 1;
|
---|
[975] | 71 | cout<<"Taille matrice NxN, N = "<<N<<endl;
|
---|
| 72 | cout<<"Elements entre +/- scale = "<<scale<<endl;
|
---|
| 73 | cout<<"Nombre de valeurs hors standard nbig = "<<nbig<<endl;
|
---|
| 74 | cout<<"Valeurs hors standard (+/- vbig = "<<vbig<<" ) * scale"<<endl;
|
---|
[934] | 75 | cout<<"Nombre de lignes de matrice a imprimer "<<nprline<<endl;
|
---|
[975] | 76 | cout<<"Initialisation de l aleatoire par "<<nalea<<" tirages"<<endl;
|
---|
[934] | 77 | cout<<endl;
|
---|
| 78 |
|
---|
| 79 | //-- Initialization arrays
|
---|
| 80 | SophyaInit();
|
---|
| 81 |
|
---|
| 82 | //-- Definition arrays
|
---|
| 83 | TMatrix< TYPE > A(N,N);
|
---|
| 84 | TMatrix< TYPE > InvA(N,N);
|
---|
| 85 | TMatrix< TYPE > AiA(N,N);
|
---|
| 86 | TMatrix< TYPE > B(N,N);
|
---|
| 87 | TMatrix< TYPE > C(N,N);
|
---|
[975] | 88 | TVector< int > Vind(N*N);
|
---|
[934] | 89 | A.Show();
|
---|
| 90 |
|
---|
| 91 | //-- Mise a zero
|
---|
[975] | 92 | A = (TYPE) 0;
|
---|
[934] | 93 | InvA = (TYPE) 0;
|
---|
[975] | 94 | AiA = (TYPE) 0;
|
---|
| 95 | B = (TYPE) 0;
|
---|
| 96 | C = (TYPE) 0;
|
---|
[934] | 97 | BaseArray::SetMaxPrint(nprline*N,0);
|
---|
| 98 |
|
---|
| 99 | //-- Fill matrices
|
---|
| 100 | uint_8 k;
|
---|
[975] | 101 | uint_4 i,j; double s;
|
---|
| 102 | uint_4 nbr=0, nbi=0;
|
---|
| 103 | Vind = 0;
|
---|
| 104 | if(nalea>0) for(i=0;i<nalea;i++) drand01();
|
---|
[934] | 105 | A = Sequence(RandomSequence(RandomSequence::Flat,0.,1.));
|
---|
[975] | 106 | if(nbig>0) for(i=0;i<nbig;i++) {
|
---|
| 107 | k=(uint_8)(drand01()*N*N); if(k>=N*N) k--;
|
---|
| 108 | s=(drand01()>0.5)?1.:-1.;
|
---|
| 109 | if(Vind[k]==0) {A[k] += (TYPE) s*vbig; Vind[k]=1; nbr++;}
|
---|
| 110 | }
|
---|
[934] | 111 | A *= (TYPE) scale;
|
---|
| 112 | #if defined(COMPLEX)
|
---|
[975] | 113 | Vind = 0;
|
---|
[934] | 114 | B = Sequence(RandomSequence(RandomSequence::Flat,0.,1.));
|
---|
[975] | 115 | if(nbig>0) for(i=0;i<nbig;i++) {
|
---|
| 116 | k=(uint_8)(drand01()*N*N); if(k>=N*N) k--;
|
---|
| 117 | s=(drand01()>0.5)?1.:-1.;
|
---|
| 118 | if(Vind[k]==0) {B[k] += (TYPE) s*vbig; Vind[k]=1; nbi++;}
|
---|
| 119 | }
|
---|
[934] | 120 | B *= (TYPE) scale;
|
---|
| 121 | A += TYPE(0.,1.)*B;
|
---|
| 122 | #endif
|
---|
[975] | 123 | cout<<"Nombre de valeurs BIG reelles = "<<nbr<<", imaginaires = "<<nbi<<endl;
|
---|
[934] | 124 |
|
---|
| 125 | //-- Print matrice A
|
---|
| 126 | cout<<"------------ TMatrix A :"<<endl;
|
---|
| 127 | if(nprline>0) {cout<<A; cout<<endl<<endl;}
|
---|
| 128 |
|
---|
| 129 | //-- Inversion
|
---|
[975] | 130 | cout<<"------------ Inversion"<<endl;
|
---|
[934] | 131 | InvA = Inverse(A);
|
---|
| 132 | cout<<"------------ TMatrix InvA = A^(-1):"<<endl;
|
---|
| 133 | if(nprline>0) {cout<<InvA; cout<<endl<<endl;}
|
---|
| 134 |
|
---|
| 135 | //-- AiA = A * InvA
|
---|
| 136 | cout<<"AiA = A * InvA"<<endl;
|
---|
| 137 | AiA = A * InvA;
|
---|
| 138 | cout<<"------------ TMatrix AiA = A * InvA:"<<endl;
|
---|
| 139 | if(nprline>0) {cout<<AiA; cout<<endl;}
|
---|
| 140 |
|
---|
| 141 | //-- Check
|
---|
[975] | 142 | double vmax=-1.;
|
---|
| 143 | for(i=0;i<N;i++) {
|
---|
| 144 | double absv = ABS_VAL( 1. - AiA(i,i) );
|
---|
[934] | 145 | if( absv > vmax ) vmax = absv;
|
---|
| 146 | }
|
---|
[975] | 147 | cout<<"Ecart maximum par rapport a 1 sur diagonale = "<<vmax<<endl;
|
---|
| 148 | vmax = -1.;
|
---|
| 149 | for(i=0;i<N;i++) for(j=0;j<N;j++) {
|
---|
[934] | 150 | if( i == j ) continue;
|
---|
| 151 | double absv = ABS_VAL( AiA(i,j) );
|
---|
| 152 | if( absv > vmax ) vmax = absv;
|
---|
| 153 | }
|
---|
[975] | 154 | cout<<"Ecart maximum par rapport a 0 hors diagonale = "<<vmax<<endl;
|
---|
[934] | 155 |
|
---|
| 156 | exit(0);
|
---|
| 157 | }
|
---|