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

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

cm 27/4/00

File size: 4.0 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
39uint_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
43uint_4 nbig = N;
44r_8 vbig = 1000.;
45// Nombre de lignes de matrice a imprimer
46uint_4 nprline = 10;
47// Initialisation du pauvre de l'aleatoire
48 uint_4 nalea = 0;
49//--------------------------------------------------------
50
51//-- Decodage arguments
52char c;
53while((c = getopt(narg,arg,"hv:l:a:")) != -1) {
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;
61 case 'a' :
62 sscanf(optarg,"%d",&nalea);
63 break;
64 case 'h' :
65 cout<<"tsttminv [-h] [-v N,scale,nbig,vbig] [-l nprline] [-a nalea]"<<endl;
66 cout<<"matrix filled with : {[-1,1] +/- vbig(nbig time)}*scale"<<endl;
67 exit(-1);
68 }
69}
70if(N<=1) N = 1;
71cout<<"Taille matrice NxN, N = "<<N<<endl;
72cout<<"Elements entre +/- scale = "<<scale<<endl;
73cout<<"Nombre de valeurs hors standard nbig = "<<nbig<<endl;
74cout<<"Valeurs hors standard (+/- vbig = "<<vbig<<" ) * scale"<<endl;
75cout<<"Nombre de lignes de matrice a imprimer "<<nprline<<endl;
76cout<<"Initialisation de l aleatoire par "<<nalea<<" tirages"<<endl;
77cout<<endl;
78
79//-- Initialization arrays
80SophyaInit();
81
82//-- Definition arrays
83TMatrix< TYPE > A(N,N);
84TMatrix< TYPE > InvA(N,N);
85TMatrix< TYPE > AiA(N,N);
86TMatrix< TYPE > B(N,N);
87TMatrix< TYPE > C(N,N);
88TVector< int > Vind(N*N);
89A.Show();
90
91//-- Mise a zero
92A = (TYPE) 0;
93InvA = (TYPE) 0;
94AiA = (TYPE) 0;
95B = (TYPE) 0;
96C = (TYPE) 0;
97BaseArray::SetMaxPrint(nprline*N,0);
98
99//-- Fill matrices
100uint_8 k;
101uint_4 i,j; double s;
102uint_4 nbr=0, nbi=0;
103Vind = 0;
104if(nalea>0) for(i=0;i<nalea;i++) drand01();
105A = Sequence(RandomSequence(RandomSequence::Flat,0.,1.));
106if(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}
111A *= (TYPE) scale;
112#if defined(COMPLEX)
113Vind = 0;
114B = Sequence(RandomSequence(RandomSequence::Flat,0.,1.));
115if(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}
120B *= (TYPE) scale;
121A += TYPE(0.,1.)*B;
122#endif
123cout<<"Nombre de valeurs BIG reelles = "<<nbr<<", imaginaires = "<<nbi<<endl;
124
125//-- Print matrice A
126cout<<"------------ TMatrix A :"<<endl;
127if(nprline>0) {cout<<A; cout<<endl<<endl;}
128
129//-- Inversion
130cout<<"------------ Inversion"<<endl;
131InvA = Inverse(A);
132cout<<"------------ TMatrix InvA = A^(-1):"<<endl;
133if(nprline>0) {cout<<InvA; cout<<endl<<endl;}
134
135//-- AiA = A * InvA
136cout<<"AiA = A * InvA"<<endl;
137AiA = A * InvA;
138cout<<"------------ TMatrix AiA = A * InvA:"<<endl;
139if(nprline>0) {cout<<AiA; cout<<endl;}
140
141//-- Check
142double vmax=-1.;
143for(i=0;i<N;i++) {
144 double absv = ABS_VAL( 1. - AiA(i,i) );
145 if( absv > vmax ) vmax = absv;
146}
147cout<<"Ecart maximum par rapport a 1 sur diagonale = "<<vmax<<endl;
148vmax = -1.;
149for(i=0;i<N;i++) for(j=0;j<N;j++) {
150 if( i == j ) continue;
151 double absv = ABS_VAL( AiA(i,j) );
152 if( absv > vmax ) vmax = absv;
153}
154cout<<"Ecart maximum par rapport a 0 hors diagonale = "<<vmax<<endl;
155
156exit(0);
157}
Note: See TracBrowser for help on using the repository browser.