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