[4565] | 1 | |
---|
| 2 | //#define WANT_STREAM |
---|
| 3 | |
---|
| 4 | #include "include.h" |
---|
| 5 | |
---|
| 6 | #include "newmat.h" |
---|
| 7 | |
---|
| 8 | #include "tmt.h" |
---|
| 9 | |
---|
| 10 | #ifdef use_namespace |
---|
| 11 | using namespace NEWMAT; |
---|
| 12 | #endif |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | /**************************** test program ******************************/ |
---|
| 16 | |
---|
| 17 | // make sure matrices work as members of a class |
---|
| 18 | |
---|
| 19 | class TestClass |
---|
| 20 | { |
---|
| 21 | Matrix A; |
---|
| 22 | Matrix B; |
---|
| 23 | public: |
---|
| 24 | TestClass(); |
---|
| 25 | ReturnMatrix Sum(); |
---|
| 26 | }; |
---|
| 27 | |
---|
| 28 | TestClass::TestClass() : A(2,3) |
---|
| 29 | { |
---|
| 30 | B.ReSize(2,3); |
---|
| 31 | A << 1 << 4 |
---|
| 32 | << 4 << 1 |
---|
| 33 | << 2 << 9; |
---|
| 34 | B << 8 << 5 |
---|
| 35 | << 5 << 8 |
---|
| 36 | << 7 << 0; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | ReturnMatrix TestClass::Sum() { return Matrix(A + B).ForReturn(); } |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | void trymatb() |
---|
| 44 | { |
---|
| 45 | // cout << "\nEleventh test of Matrix package\n"; |
---|
| 46 | Tracer et("Eleventh test of Matrix package"); |
---|
| 47 | Tracer::PrintTrace(); |
---|
| 48 | int i; int j; |
---|
| 49 | RowVector RV; RV.ReSize(10); |
---|
| 50 | { |
---|
| 51 | Tracer et1("Stage 1"); |
---|
| 52 | for (i=1;i<=10;i++) RV(i)=i*i-3; |
---|
| 53 | Matrix X(1,1); X(1,1) = .25; |
---|
| 54 | Print(RowVector(X.i() * RV - RV / .25)); |
---|
| 55 | // Print(RowVector(X.i() * Matrix(RV) - RV / .25)); // != zortech, AT&T |
---|
| 56 | Print(RowVector(X.i() * RV - RV / .25)); |
---|
| 57 | } |
---|
| 58 | LowerTriangularMatrix L(5); UpperTriangularMatrix U(5); |
---|
| 59 | for (i=1; i<=5; i++) for (j=1; j<=i; j++) |
---|
| 60 | { L(i,j) = i*i + j -2.0; U(j,i) = i*i*j+3; } |
---|
| 61 | DiagonalMatrix D(5); |
---|
| 62 | for (i=1; i<=5; i++) D(i,i) = i*i + i + 2; |
---|
| 63 | Matrix M1 = -L; Matrix M2 = L-U; Matrix M3 = U*3; Matrix M4 = U-L; |
---|
| 64 | Matrix M5 = M1 - D; M1 = D * (-3) - M3; |
---|
| 65 | { |
---|
| 66 | Tracer et1("Stage 2"); |
---|
| 67 | Print(Matrix((M2-M4*2)+M5*3-M1)); |
---|
| 68 | M1 = L.t(); Print(Matrix(M1.t()-L)); |
---|
| 69 | M1 = U.t(); Print(Matrix(M1.t()-U)); |
---|
| 70 | } |
---|
| 71 | { |
---|
| 72 | Tracer et1("Stage 3"); |
---|
| 73 | SymmetricMatrix S(5); |
---|
| 74 | for (i=1; i<=5; i++) for (j=1; j<=i; j++) S(i,j) = i*j+i-j+5; |
---|
| 75 | M2 = S.i() * M4; M1 = S; M3=M1*M2-M4; Clean(M3,0.00000001); Print(M3); |
---|
| 76 | SymmetricMatrix T(5); |
---|
| 77 | for (i=1; i<=5; i++) for (j=1; j<=i; j++) T(i,j) = i*i*j*j+i-j+5-i*j; |
---|
| 78 | M1 = S.i() * T; M1 = S * M1; M1 = M1-T; Clean(M1,0.00000001); Print(M1); |
---|
| 79 | ColumnVector CV(5); for (i=1; i<=5; i++) CV(i) = i*i*i+10; |
---|
| 80 | M1 = CV * RV; |
---|
| 81 | } |
---|
| 82 | { |
---|
| 83 | Tracer et1("Stage 4"); |
---|
| 84 | M4.ReSize(5,10); |
---|
| 85 | for (i=1; i<=5; i++) for (j=1; j<=10; j++) M4(i,j) = (i*i*i+10)*(j*j-3); |
---|
| 86 | Print(Matrix(M1-M4)); |
---|
| 87 | M1 = L.t(); M2 = U.t(); M3 = L+U; Print(Matrix(M1-M3.t()+M2)); |
---|
| 88 | } |
---|
| 89 | // UpperTriangularMatrix U2((const UpperTriangularMatrix&)U); // != zortech |
---|
| 90 | UpperTriangularMatrix U2((UpperTriangularMatrix&)U); |
---|
| 91 | { |
---|
| 92 | Tracer et1("Stage 5"); |
---|
| 93 | Print(Matrix(U2-U)); |
---|
| 94 | M2.ReSize(10,10); |
---|
| 95 | for (i=1; i<=10; i++) for (j=1; j<=10; j++) M2(i,j) = (i*i*i+10)*(j*j-3); |
---|
| 96 | D << M2; L << M2; U << M2; // check copy into |
---|
| 97 | Print( Matrix( (D+M2)-(L+U) ) ); |
---|
| 98 | } |
---|
| 99 | { |
---|
| 100 | Tracer et1("Stage 6"); |
---|
| 101 | M1.ReSize(6,10); |
---|
| 102 | for (i=1; i<=6; i++) for (j=1; j<=10; j++) M1(i,j) = 100*i + j; |
---|
| 103 | M2 = M1.SubMatrix(3,5,4,7); M3.ReSize(3,4); |
---|
| 104 | for (i=3; i<=5; i++) for (j=4; j<=7; j++) M3(i-2,j-3) = 100*i + j; |
---|
| 105 | Print(Matrix(M2-M3)); |
---|
| 106 | } |
---|
| 107 | int a1,a2,a3,a4; |
---|
| 108 | { |
---|
| 109 | Tracer et1("Stage 7"); |
---|
| 110 | int a1,a2,a3,a4; |
---|
| 111 | a1=4; a2=9; a3=4; a4=7; |
---|
| 112 | U.ReSize(10); |
---|
| 113 | for (i=1; i<=10; i++) for (j=i; j<=10; j++) U(i,j) = 100*i + j; |
---|
| 114 | M2 = U.SubMatrix(a1,a2,a3,a4); |
---|
| 115 | M3.ReSize(a2-a1+1,a4-a3+1); M3=0.0; |
---|
| 116 | for (i=a1; i<=a2; i++) for (j=(i>a3) ? i : a3; j<=a4; j++) |
---|
| 117 | M3(i-a1+1,j-a3+1) = 100*i + j; |
---|
| 118 | Print(Matrix(M2-M3)); |
---|
| 119 | } |
---|
| 120 | { |
---|
| 121 | Tracer et1("Stage 8"); |
---|
| 122 | a1=3; a2=9; a3=2; a4=7; |
---|
| 123 | U.ReSize(10); |
---|
| 124 | for (i=1; i<=10; i++) for (j=i; j<=10; j++) U(i,j) = 100*i + j; |
---|
| 125 | M2 = U.SubMatrix(a1,a2,a3,a4); |
---|
| 126 | M3.ReSize(a2-a1+1,a4-a3+1); M3=0.0; |
---|
| 127 | for (i=a1; i<=a2; i++) for (j=(i>a3) ? i : a3; j<=a4; j++) |
---|
| 128 | M3(i-a1+1,j-a3+1) = 100*i + j; |
---|
| 129 | Print(Matrix(M2-M3)); |
---|
| 130 | } |
---|
| 131 | { |
---|
| 132 | Tracer et1("Stage 9"); |
---|
| 133 | a1=4; a2=6; a3=2; a4=5; |
---|
| 134 | U.ReSize(10); |
---|
| 135 | for (i=1; i<=10; i++) for (j=i; j<=10; j++) U(i,j) = 100*i + j; |
---|
| 136 | M2 = U.SubMatrix(a1,a2,a3,a4); |
---|
| 137 | M3.ReSize(a2-a1+1,a4-a3+1); M3=0.0; |
---|
| 138 | for (i=a1; i<=a2; i++) for (j=(i>a3) ? i : a3; j<=a4; j++) |
---|
| 139 | M3(i-a1+1,j-a3+1) = 100*i + j; |
---|
| 140 | Print(Matrix(M2-M3)); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | { |
---|
| 144 | Tracer et1("Stage 10"); |
---|
| 145 | TestClass TC; |
---|
| 146 | Matrix M = TC.Sum() - 9; |
---|
| 147 | Print(M); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | // cout << "\nEnd of eleventh test\n"; |
---|
| 152 | } |
---|