source: Sophya/trunk/SophyaProg/Tests/tsttmat.cc@ 2586

Last change on this file since 2586 was 2580, checked in by cmv, 21 years ago

plus de *= pour multipl matrice inplace rz+cmv 29/07/04

File size: 6.6 KB
Line 
1#include "machdefs.h"
2#include <iostream>
3#include <stdlib.h>
4#include <stdio.h>
5#include <string.h>
6#include <math.h>
7#include "ntoolsinit.h"
8#include "pexceptions.h"
9#include "array.h"
10#include "srandgen.h"
11
12////////////////////////////////////////////////////////////////////////////////////////
13int main(int narg,char *arg[])
14{
15SophyaInit();
16r_8 v[3][4] = {{1,2,3,4},{4,5,6,5},{7,8,9,10}};
17
18{
19
20TMatrix<r_8> A(2,3);
21{for(int i=0;i<2;i++) for(int j=0;j<3;j++) A(i,j) = v[i][j];}
22cout<<"A:"<<endl<<A;
23
24{
25cout << " Test ecriture PPersist TMatrix " << endl;
26FIO_TArray<r_8> ftm(A);
27ftm.Write("tmtx.ppf");
28}
29{
30cout << " Test lecture PPersist TMatrix " << endl;
31FIO_TArray<r_8> ftm("tmtx.ppf");
32cout<<"Alue:"<<(TMatrix<r_8>)ftm<<endl;
33}
34
35cout<<"Matrices B(2,3)"<<endl;
36TMatrix<r_8> B(3,2);
37{for(int i=0;i<3;i++) for(int j=0;j<2;j++) B(i,j) = v[i][j];}
38cout<<"B:"<<endl<<B;
39
40cout<<"Matrices C(3,3)"<<endl;
41TMatrix<r_8> C(3,3);
42{for(int i=0;i<3;i++) for(int j=0;j<3;j++) C(i,j) = v[i][j];}
43cout<<"C:"<<endl<<C;
44
45cout<<"Matrices D(2,2)"<<endl;
46TMatrix<r_8> D(2,2);
47{for(int i=0;i<2;i++) for(int j=0;j<2;j++) D(i,j) = v[i][j];}
48cout<<"D:"<<endl<<D;
49
50cout<<"Matrices E(3,4)"<<endl;
51TMatrix<r_8> E(3,4);
52{for(int i=0;i<3;i++) for(int j=0;j<4;j++) E(i,j) = v[i][j];}
53cout<<"E:"<<endl<<E;
54
55//-----------------------------------------------
56cout<<endl<<"Matrices AA(A)"<<endl;
57TMatrix<r_8> AA(A);
58cout<<"AA:"<<endl<<AA;
59
60//-----------------------------------------------
61cout<<endl<<"Matrices AA2; AA2=A;"<<endl;
62TMatrix<r_8> AA2; AA2=A;
63cout<<"AA2:"<<endl<<AA2;
64
65//-----------------------------------------------
66cout<<endl<<"Matrices AAA.Clone(A)"<<endl;
67TMatrix<r_8> AAA; AAA.Clone(A);
68cout<<"AAA:"<<endl<<AAA;
69cout<<endl<<"Matrices BBB.Clone(B)"<<endl;
70TMatrix<r_8> BBB; BBB.Clone(B);
71cout<<"BBB:"<<endl<<BBB;
72cout<<"Matrices CCC.Clone(C)"<<endl;
73TMatrix<r_8> CCC; CCC.Clone(C);
74cout<<"CCC:"<<endl<<CCC;
75
76//-----------------------------------------------
77cout<<endl<<"Matrices I(5,5)=1.234"<<endl;
78TMatrix<r_8> I(5,5); I = 5;
79cout<<"I:"<<endl<<I;
80
81//-----------------------------------------------
82cout<<endl<<"Matrices AAA+=1"<<endl;
83AAA += 1;
84cout<<"AAA:"<<endl<<AAA;
85cout<<"Matrices AAA-=1"<<endl;
86AAA -= 1;
87cout<<"AAA:"<<endl<<AAA;
88cout<<"Matrices AAA*=10"<<endl;
89AAA *= 10;
90cout<<"AAA:"<<endl<<AAA;
91cout<<"Matrices AAA/=10"<<endl;
92AAA /= 10;
93cout<<"AAA:"<<endl<<AAA;
94
95//-----------------------------------------------
96cout<<endl<<"Matrices AAA+=A"<<endl;
97AAA += A;
98cout<<"AAA:"<<endl<<AAA;
99cout<<"Matrices AAA-=A"<<endl;
100AAA -= A;
101cout<<"AAA:"<<endl<<AAA;
102
103//-----------------------------------------------
104cout<<endl<<"Matrices AAA=A"<<endl;
105AAA = A;
106cout<<"AAA:"<<endl<<AAA;
107cout<<endl<<"Matrices BBB=B"<<endl;
108BBB = B;
109cout<<"BBB:"<<endl<<BBB;
110
111//---------------------------------------------
112TMatrix<r_8> a(A,false);
113cout<<endl<<"Matrices AAA = AAA * C"<<endl;
114{for(uint_4 i=0;i<AAA.NRows();i++) for(uint_4 j=0;j<C.NCols();j++)
115 {a(i,j) = 0; for(uint_4 k=0;k<AAA.NCols();k++) a(i,j) += AAA(i,k)*C(k,j);}}
116AAA = AAA * C;
117cout<<"AAA:"<<endl<<AAA;
118cout<<"a:"<<endl<<a;
119TMatrix<r_8> b(B,false);
120cout<<"Matrices BBB = BBB * D"<<endl;
121{for(uint_4 i=0;i<BBB.NRows();i++) for(uint_4 j=0;j<D.NCols();j++)
122 {b(i,j) = 0; for(uint_4 k=0;k<BBB.NCols();k++) b(i,j) += BBB(i,k)*D(k,j);}}
123BBB = BBB * D;
124cout<<"B:"<<endl<<BBB;
125cout<<"b:"<<endl<<b;
126
127//-----------------------------------------------
128AA.Clone(A);
129cout<<endl<<"Matrices AA = AA + 10"<<endl;
130AA = AA + 10.; cout<<"AA"<<endl<<AA;
131cout<<endl<<"Matrices AA = AA - 10"<<endl;
132AA = AA - 10.; cout<<"AA"<<endl<<AA;
133cout<<endl<<"Matrices AA = 10 - AA"<<endl;
134AA = 10. - AA; cout<<"AA"<<endl<<AA;
135cout<<endl<<"Matrices AA = AA - 10"<<endl;
136AA = AA - 10.; cout<<"AA"<<endl<<AA;
137cout<<endl<<"Matrices AA = -1 * AA"<<endl;
138AA = -1. * AA; cout<<"AA"<<endl<<AA;
139cout<<endl<<"Matrices AA = 10 * AA"<<endl;
140AA = 10. * AA; cout<<"AA"<<endl<<AA;
141cout<<endl<<"Matrices AA = AA * 10"<<endl;
142AA = AA * 10.; cout<<"AA"<<endl<<AA;
143cout<<endl<<"Matrices AA = AA / 100"<<endl;
144AA = AA / 100.; cout<<"AA"<<endl<<AA;
145
146//-----------------------------------------------
147TMatrix<r_8> R;
148AA.Clone(A); AA += 10.; AAA.Clone(A); AAA += 100.;
149cout<<endl<<"Matrices R = A + AA"<<endl;
150R = A + AA; cout<<"R"<<endl<<R;
151cout<<endl<<"Matrices R = AA - A"<<endl;
152R = AA - A; cout<<"R"<<endl<<R;
153cout<<endl<<"Matrices R = A + AA + AAA"<<endl;
154R = A + AA + AAA; cout<<"R"<<endl<<R;
155cout<<endl<<"Matrices R = A + 100 + 2*A"<<endl;
156R = A + 100. + 2.*A; cout<<"R"<<endl<<R;
157
158//-----------------------------------------------
159TMatrix<r_8> RR(2,4);
160{for(uint_4 i=0;i<A.NRows();i++) for(uint_4 j=0;j<E.NCols();j++)
161 {RR(i,j) = 0; for(uint_4 k=0;k<A.NCols();k++) RR(i,j) += A(i,k)*E(k,j);}}
162cout<<endl<<"Matrices R = A * E"<<endl;
163R = A * E;
164cout<<"R"<<endl<<R; cout<<"RR"<<endl<<RR;
165
166//-----------------------------------------------
167cout<<endl<<"Matrices A = A.Transpose 2x"<<endl;
168cout<<"A"<<endl<<A;
169A = A.Transpose(); cout<<"trA"<<endl<<A;
170A = A.Transpose(); cout<<"trtrA"<<endl<<A;
171cout<<endl<<"Matrices AT = A.Transpose() 2x"<<endl;
172TMatrix<r_8> AT;
173AT = A.Transpose(); cout<<"AT"<<endl<<AT;
174AT = AT.Transpose(); cout<<"trAT"<<endl<<AT;
175
176//-----------------------------------------------
177cout<<endl<<"Test Inversion avec couplage a M=Matrix(C)"<<endl;
178TMatrix<r_8> X(10,10);
179{for(int i=0;i<10;i++) for(int j=0;j<10;j++)
180 {X(i,j) = drand01(); if(drand01()>0.8) X(i,j) += 1.e+15*drand01();}}
181cout<<"TMatrix X"<<endl<<X;
182{
183Matrix M(X); cout<<"Matrix M"<<endl<<M<<endl;
184Matrix MI = Inverse(M); cout<<"MI"<<endl<<MI;
185MI = MI * M; cout<<"MI*M"<<endl<<MI;
186r_8 xmin=-1.;
187for(int i=0;i<10;i++) for(int j=0;j<10;j++)
188 if(i!=j && fabs(MI(i,j))>xmin) xmin=fabs(MI(i,j));
189cout<<"Biggest off diagonal identity matrix element is "<<xmin<<endl;
190} // destruction de M,MI
191cout<<"TMatrix X"<<endl<<X;
192
193//-----------------------------------------------
194{
195cout<<endl<<"Test Inversion directe"<<endl;
196cout<<"TMatrix X"<<endl<<X;
197TMatrix<r_8> Xinv;
198Xinv = Inverse(X);
199cout<<"TMatrix Xinv"<<endl<<Xinv;
200Xinv = Xinv * X;
201cout<<"Xinv = Xinv * X"<<endl<<Xinv;
202r_8 xmin=-1.;
203for(int i=0;i<10;i++) for(int j=0;j<10;j++)
204 if(i!=j && fabs(Xinv(i,j))>xmin) xmin=fabs(Xinv(i,j));
205cout<<"Biggest off diagonal identity matrix element is "<<xmin<<endl;
206cout<<"TMatrix X"<<endl<<X;
207}
208
209//-----------------------------------------------
210cout<<endl<<"Test liens TMatrix L avec Matrix MI"<<endl;
211Matrix MI(10,5);
212{for(int i=0;i<10;i++) for(int j=0;j<5;j++) MI(i,j) = 1000*i+j;}
213cout<<"MI"<<endl<<MI;
214{
215 sa_size_t siz[5];
216 siz[0] = MI.NCols();
217 siz[1] = MI.NRows();
218 TArray<r_8> A(2, siz, MI.Data(), 1, 0, new Bridge);
219 TMatrix<r_8> L(A);
220 cout<<"L"<<endl<<L;
221 L *= 100.; cout<<"L*=100."<<endl<<L;
222} // destruction de L
223cout<<"MI(4,4)="<<MI(4,4)<<endl;
224
225} // destruction de toutes les matrices
226exit(0);
227}
Note: See TracBrowser for help on using the repository browser.