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

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

test inversion de matrice real+complex cmv 14/4/00

File size: 3.6 KB
Line 
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
35int main(int narg,char *arg[])
36{
37//--------------------------------------------------------
38// number of lines/columns
39int_4 N = 5;
40// scale of the value (if =1 values between -1 and 1)
41r_8 scale = 1.;
42// number of values change by +/- vbig
43int_4 nbig = N;
44r_8 vbig = 1000.;
45// Nombre de ligne de matrice a imprimer
46 int_4 nprline = 10;
47//--------------------------------------------------------
48
49//-- Decodage arguments
50char c;
51while((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}
65if(N<=1) N = 1;
66cout<<"Taille matrice N = "<<N<<endl;
67cout<<"Elements entre +/- "<<scale<<endl;
68cout<<"Nombre de valeurs hors standard "<<nbig<<endl;
69cout<<"Valeurs hors standard v = v*(+/- "<<vbig<<" )"<<endl;
70cout<<"Nombre de lignes de matrice a imprimer "<<nprline<<endl;
71cout<<endl;
72
73//-- Initialization arrays
74SophyaInit();
75
76//-- Definition arrays
77TMatrix< TYPE > A(N,N);
78TMatrix< TYPE > InvA(N,N);
79TMatrix< TYPE > AiA(N,N);
80TMatrix< TYPE > B(N,N);
81TMatrix< TYPE > C(N,N);
82A.Show();
83
84//-- Mise a zero
85A = (TYPE) 0;
86InvA = (TYPE) 0;
87AiA = (TYPE) 0;
88B = (TYPE) 0;
89C = (TYPE) 0;
90BaseArray::SetMaxPrint(nprline*N,0);
91
92//-- Fill matrices
93uint_8 k;
94int_4 i,j; double s;
95A = Sequence(RandomSequence(RandomSequence::Flat,0.,1.));
96if(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;}
99A *= (TYPE) scale;
100#if defined(COMPLEX)
101B = Sequence(RandomSequence(RandomSequence::Flat,0.,1.));
102if(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;}
105B *= (TYPE) scale;
106A += TYPE(0.,1.)*B;
107#endif
108
109//-- Print matrice A
110cout<<"------------ TMatrix A :"<<endl;
111if(nprline>0) {cout<<A; cout<<endl<<endl;}
112
113//-- Inversion
114cout<<"Inversion"<<endl;
115InvA = Inverse(A);
116cout<<"------------ TMatrix InvA = A^(-1):"<<endl;
117if(nprline>0) {cout<<InvA; cout<<endl<<endl;}
118
119//-- AiA = A * InvA
120cout<<"AiA = A * InvA"<<endl;
121AiA = A * InvA;
122cout<<"------------ TMatrix AiA = A * InvA:"<<endl;
123if(nprline>0) {cout<<AiA; cout<<endl;}
124
125//-- Check
126double vmin,vmax;
127k = 0;
128for(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}
135cout<<"Limites sur la diagonale "
136 <<vmin<<" , "<<vmax<<" n="<<k<<endl;
137k = 0;
138for(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}
146cout<<"Limites hors diagonale "
147 <<vmin<<" , "<<vmax<<" n="<<k<<endl;
148
149exit(0);
150}
Note: See TracBrowser for help on using the repository browser.