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)
|
---|
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
|
---|
26 | #else
|
---|
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
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | int main(int narg,char *arg[])
|
---|
36 | {
|
---|
37 | //--------------------------------------------------------
|
---|
38 | // number of lines/columns
|
---|
39 | int_4 N = 5;
|
---|
40 | // scale of the value (if =1 values between -1 and 1)
|
---|
41 | r_8 scale = 1.;
|
---|
42 | // number of values change by +/- vbig
|
---|
43 | int_4 nbig = N;
|
---|
44 | r_8 vbig = 1000.;
|
---|
45 | // Nombre de ligne de matrice a imprimer
|
---|
46 | int_4 nprline = 10;
|
---|
47 | //--------------------------------------------------------
|
---|
48 |
|
---|
49 | //-- Decodage arguments
|
---|
50 | char c;
|
---|
51 | while((c = getopt(narg,arg,"hv:l:")) != -1) {
|
---|
52 | switch (c) {
|
---|
53 | case 'v' :
|
---|
54 | sscanf(optarg,"%d,%lf,%d,%lf",&N,&scale,&nbig,&vbig);
|
---|
55 | break;
|
---|
56 | case 'l' :
|
---|
57 | sscanf(optarg,"%d",&nprline);
|
---|
58 | break;
|
---|
59 | case 'h' :
|
---|
60 | cout<<"tsttminv [-h] [-v N,scale,nbig,vbig] [-l nprline]"<<endl;
|
---|
61 | cout<<"matrix filled with : {[-1,1] +/- vbig(nbig time)}*scale"<<endl;
|
---|
62 | exit(-1);
|
---|
63 | }
|
---|
64 | }
|
---|
65 | if(N<=1) N = 1;
|
---|
66 | cout<<"Taille matrice N = "<<N<<endl;
|
---|
67 | cout<<"Elements entre +/- "<<scale<<endl;
|
---|
68 | cout<<"Nombre de valeurs hors standard "<<nbig<<endl;
|
---|
69 | cout<<"Valeurs hors standard v = v*(+/- "<<vbig<<" )"<<endl;
|
---|
70 | cout<<"Nombre de lignes de matrice a imprimer "<<nprline<<endl;
|
---|
71 | cout<<endl;
|
---|
72 |
|
---|
73 | //-- Initialization arrays
|
---|
74 | SophyaInit();
|
---|
75 |
|
---|
76 | //-- Definition arrays
|
---|
77 | TMatrix< TYPE > A(N,N);
|
---|
78 | TMatrix< TYPE > InvA(N,N);
|
---|
79 | TMatrix< TYPE > AiA(N,N);
|
---|
80 | TMatrix< TYPE > B(N,N);
|
---|
81 | TMatrix< TYPE > C(N,N);
|
---|
82 | A.Show();
|
---|
83 |
|
---|
84 | //-- Mise a zero
|
---|
85 | A = (TYPE) 0;
|
---|
86 | InvA = (TYPE) 0;
|
---|
87 | AiA = (TYPE) 0;
|
---|
88 | B = (TYPE) 0;
|
---|
89 | C = (TYPE) 0;
|
---|
90 | BaseArray::SetMaxPrint(nprline*N,0);
|
---|
91 |
|
---|
92 | //-- Fill matrices
|
---|
93 | uint_8 k;
|
---|
94 | int_4 i,j; double s;
|
---|
95 | A = Sequence(RandomSequence(RandomSequence::Flat,0.,1.));
|
---|
96 | if(nbig>0) for(i=0;i<nbig;i++)
|
---|
97 | {k=(uint_8)(drand01()*N*N); if(k>=N*N) k--;
|
---|
98 | s=(drand01()>0.5)?1.:-1.; A[k] += (TYPE) s*vbig;}
|
---|
99 | A *= (TYPE) scale;
|
---|
100 | #if defined(COMPLEX)
|
---|
101 | B = Sequence(RandomSequence(RandomSequence::Flat,0.,1.));
|
---|
102 | if(nbig>0) for(i=0;i<nbig;i++)
|
---|
103 | {k=(uint_8)(drand01()*N*N); if(k>=N*N) k--;
|
---|
104 | s=(drand01()>0.5)?1.:-1.; B[k] += (TYPE) s*vbig;}
|
---|
105 | B *= (TYPE) scale;
|
---|
106 | A += TYPE(0.,1.)*B;
|
---|
107 | #endif
|
---|
108 |
|
---|
109 | //-- Print matrice A
|
---|
110 | cout<<"------------ TMatrix A :"<<endl;
|
---|
111 | if(nprline>0) {cout<<A; cout<<endl<<endl;}
|
---|
112 |
|
---|
113 | //-- Inversion
|
---|
114 | cout<<"Inversion"<<endl;
|
---|
115 | InvA = Inverse(A);
|
---|
116 | cout<<"------------ TMatrix InvA = A^(-1):"<<endl;
|
---|
117 | if(nprline>0) {cout<<InvA; cout<<endl<<endl;}
|
---|
118 |
|
---|
119 | //-- AiA = A * InvA
|
---|
120 | cout<<"AiA = A * InvA"<<endl;
|
---|
121 | AiA = A * InvA;
|
---|
122 | cout<<"------------ TMatrix AiA = A * InvA:"<<endl;
|
---|
123 | if(nprline>0) {cout<<AiA; cout<<endl;}
|
---|
124 |
|
---|
125 | //-- Check
|
---|
126 | double vmin,vmax;
|
---|
127 | k = 0;
|
---|
128 | for(int i=0;i<N;i++) {
|
---|
129 | double absv = ABS_VAL( AiA(i,i) );
|
---|
130 | if(k==0) {vmin = vmax = absv; k++; continue;}
|
---|
131 | if( absv > vmax ) vmax = absv;
|
---|
132 | if( absv < vmin ) vmin = absv;
|
---|
133 | k++;
|
---|
134 | }
|
---|
135 | cout<<"Limites sur la diagonale "
|
---|
136 | <<vmin<<" , "<<vmax<<" n="<<k<<endl;
|
---|
137 | k = 0;
|
---|
138 | for(int i=0;i<N;i++) for(int j=0;j<N;j++) {
|
---|
139 | if( i == j ) continue;
|
---|
140 | double absv = ABS_VAL( AiA(i,j) );
|
---|
141 | if(k==0) {vmin = vmax = absv; k++; continue;}
|
---|
142 | if( absv > vmax ) vmax = absv;
|
---|
143 | if( absv < vmin ) vmin = absv;
|
---|
144 | k++;
|
---|
145 | }
|
---|
146 | cout<<"Limites hors diagonale "
|
---|
147 | <<vmin<<" , "<<vmax<<" n="<<k<<endl;
|
---|
148 |
|
---|
149 | exit(0);
|
---|
150 | }
|
---|