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

Last change on this file since 3852 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

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