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