| 1 |  | 
|---|
| 2 | //#define WANT_STREAM | 
|---|
| 3 | #define WANT_MATH | 
|---|
| 4 |  | 
|---|
| 5 |  | 
|---|
| 6 | #include "include.h" | 
|---|
| 7 |  | 
|---|
| 8 | #include "newmatap.h" | 
|---|
| 9 |  | 
|---|
| 10 | #include "tmt.h" | 
|---|
| 11 |  | 
|---|
| 12 | #ifdef use_namespace | 
|---|
| 13 | using namespace NEWMAT; | 
|---|
| 14 | #endif | 
|---|
| 15 |  | 
|---|
| 16 |  | 
|---|
| 17 | /**************************** test program ******************************/ | 
|---|
| 18 |  | 
|---|
| 19 |  | 
|---|
| 20 | // slow sort program | 
|---|
| 21 |  | 
|---|
| 22 | static void SimpleSortDescending(Real* first, const int length) | 
|---|
| 23 | { | 
|---|
| 24 |    int i = length; | 
|---|
| 25 |    while (--i) | 
|---|
| 26 |    { | 
|---|
| 27 |       Real x = *first; Real* f = first; Real* g = f; | 
|---|
| 28 |       int j = i; | 
|---|
| 29 |       while (j--) if (x < *(++f)) { g = f; x = *g; } | 
|---|
| 30 |       *g = *first; *first++ = x; | 
|---|
| 31 |    } | 
|---|
| 32 | } | 
|---|
| 33 |  | 
|---|
| 34 | static void TestSort(int n) | 
|---|
| 35 | { | 
|---|
| 36 |    // make some data | 
|---|
| 37 |    RowVector X(n); | 
|---|
| 38 |    int i; | 
|---|
| 39 |    for (i = 1; i <= n; i++) | 
|---|
| 40 |       X(i) = sin((Real)i) + 0.3 * cos(i/5.0) - 0.6 * sin(i/7.0) + 0.2 * sin(2.0 * i); | 
|---|
| 41 |    RowVector X_Sorted = X; SimpleSortDescending(X_Sorted.Store(), n); | 
|---|
| 42 |    RowVector A = X + X.Reverse(); SimpleSortDescending(A.Store(), n); | 
|---|
| 43 |  | 
|---|
| 44 |    // test descending sort | 
|---|
| 45 |  | 
|---|
| 46 |    RowVector Y = X; SortDescending(Y); Y -= X_Sorted; Print(Y); | 
|---|
| 47 |    Y = X_Sorted; SortDescending(Y); Y -= X_Sorted; Print(Y); | 
|---|
| 48 |    Y = X_Sorted.Reverse(); SortDescending(Y); Y -= X_Sorted; Print(Y); | 
|---|
| 49 |    Y = X + X.Reverse(); SortDescending(Y); Y -= A; Print(Y); | 
|---|
| 50 |  | 
|---|
| 51 |    // test ascending sort | 
|---|
| 52 |  | 
|---|
| 53 |    Y = X; SortAscending(Y); Y -= X_Sorted.Reverse(); Print(Y); | 
|---|
| 54 |    Y = X_Sorted; SortAscending(Y); Y -= X_Sorted.Reverse(); Print(Y); | 
|---|
| 55 |    Y = X_Sorted.Reverse(); SortAscending(Y); Y -= X_Sorted.Reverse(); Print(Y); | 
|---|
| 56 |    Y = X + X.Reverse(); SortAscending(Y); Y -= A.Reverse(); Print(Y); | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 |  | 
|---|
| 60 | void trymat6() | 
|---|
| 61 | { | 
|---|
| 62 |    Tracer et("Sixth test of Matrix package"); | 
|---|
| 63 |    Tracer::PrintTrace(); | 
|---|
| 64 |  | 
|---|
| 65 |    int i,j; | 
|---|
| 66 |  | 
|---|
| 67 |  | 
|---|
| 68 |    DiagonalMatrix D(6); | 
|---|
| 69 |    UpperTriangularMatrix U(6); | 
|---|
| 70 |    for (i=1;i<=6;i++) { for (j=i;j<=6;j++) U(i,j)=i*i*i-50; D(i,i)=i*i+i-10; } | 
|---|
| 71 |    LowerTriangularMatrix L=(U*3.0).t(); | 
|---|
| 72 |    SymmetricMatrix S(6); | 
|---|
| 73 |    for (i=1;i<=6;i++) for (j=i;j<=6;j++) S(i,j)=i*i+2.0+j; | 
|---|
| 74 |    Matrix MD=D; Matrix ML=L; Matrix MU=U; Matrix MS=S; | 
|---|
| 75 |    Matrix M(6,6); | 
|---|
| 76 |    for (i=1;i<=6;i++) for (j=1;j<=6;j++) M(i,j)=i*j+i*i-10.0;   | 
|---|
| 77 |    { | 
|---|
| 78 |       Tracer et1("Stage 1"); | 
|---|
| 79 |       Print(Matrix(MS+(-MS))); | 
|---|
| 80 |       Print(Matrix((S+M)-(MS+M))); | 
|---|
| 81 |       Print(Matrix((M+U)-(M+MU))); | 
|---|
| 82 |       Print(Matrix((M+L)-(M+ML))); | 
|---|
| 83 |    } | 
|---|
| 84 |    { | 
|---|
| 85 |       Tracer et1("Stage 2"); | 
|---|
| 86 |       Print(Matrix((M+D)-(M+MD))); | 
|---|
| 87 |       Print(Matrix((U+D)-(MU+MD))); | 
|---|
| 88 |       Print(Matrix((D+L)-(ML+MD))); | 
|---|
| 89 |       Print(Matrix((-U+D)+MU-MD)); | 
|---|
| 90 |       Print(Matrix((-L+D)+ML-MD)); | 
|---|
| 91 |    } | 
|---|
| 92 |    { | 
|---|
| 93 |       Tracer et1("Stage 3 - concatenate"); | 
|---|
| 94 |       RowVector A(5); | 
|---|
| 95 |       A << 1 << 2 << 3 << 4 << 5; | 
|---|
| 96 |       RowVector B(5); | 
|---|
| 97 |       B << 3 << 1 << 4 << 1 << 5; | 
|---|
| 98 |       Matrix C(3,5); | 
|---|
| 99 |       C <<  2 <<  3 <<  5 <<  7 << 11 | 
|---|
| 100 |         << 13 << 17 << 19 << 23 << 29 | 
|---|
| 101 |         << 31 << 37 << 41 << 43 << 47; | 
|---|
| 102 |       Matrix X1 = A & B & C; | 
|---|
| 103 |       Matrix X2 = (A.t() | B.t() | C.t()).t(); | 
|---|
| 104 |       Matrix X3(5,5); | 
|---|
| 105 |       X3.Row(1)=A; X3.Row(2)=B; X3.Rows(3,5)=C; | 
|---|
| 106 |       Print(Matrix(X1-X2)); | 
|---|
| 107 |       Print(Matrix(X1-X3)); | 
|---|
| 108 |       LowerTriangularMatrix LT1; LT1 << (A & B & C); | 
|---|
| 109 |       UpperTriangularMatrix UT1; UT1 << (A.t() | B.t() | C.t()); | 
|---|
| 110 |       Print(LowerTriangularMatrix(LT1-UT1.t())); | 
|---|
| 111 |       DiagonalMatrix D1; D1 << (A.t() | B.t() | C.t()); | 
|---|
| 112 |       ColumnVector At = A.t(); | 
|---|
| 113 |       ColumnVector Bt = B.t(); | 
|---|
| 114 |       Matrix Ct = C.t(); | 
|---|
| 115 |       LowerTriangularMatrix LT2; LT2 << (At | Bt | Ct); | 
|---|
| 116 |       UpperTriangularMatrix UT2; UT2 << (At.t() & Bt.t() & Ct.t()); | 
|---|
| 117 |       Matrix ABt = At | Bt; | 
|---|
| 118 |       DiagonalMatrix D2; D2 << (ABt | Ct); | 
|---|
| 119 |       Print(LowerTriangularMatrix(LT2-UT2.t())); | 
|---|
| 120 |       Print(DiagonalMatrix(D1-D2)); | 
|---|
| 121 |       Print(Matrix(LT1+UT2-D2-X1)); | 
|---|
| 122 |       Matrix M1 = LT1 | UT2; Matrix M2 = UT1 & LT2; | 
|---|
| 123 |       Print(Matrix(M1-M2.t())); | 
|---|
| 124 |       M1 = UT2 | LT1; M2 = LT2 & UT1; | 
|---|
| 125 |       Print(Matrix(M1-M2.t())); | 
|---|
| 126 |       M1 = (LT1 | UT2) & (UT2 | LT1); | 
|---|
| 127 |       M2 = (UT1 & LT2) | (LT2 & UT1); | 
|---|
| 128 |       Print(Matrix(M1-M2.t())); | 
|---|
| 129 |       SymmetricMatrix SM1; SM1 << (M1 + M1.t()); | 
|---|
| 130 |       SymmetricMatrix SM2; SM2 << ((SM1 | M1) & (M1.t() | SM1)); | 
|---|
| 131 |       Matrix M3(20,20); | 
|---|
| 132 |       M3.SubMatrix(1,10,1,10) = SM1; | 
|---|
| 133 |       M3.SubMatrix(1,10,11,20) = M1; | 
|---|
| 134 |       M3.SubMatrix(11,20,1,10) = M2; | 
|---|
| 135 |       M3.SubMatrix(11,20,11,20) = SM1; | 
|---|
| 136 |       Print(Matrix(M3-SM2)); | 
|---|
| 137 |  | 
|---|
| 138 |       SymmetricMatrix SM(15); SM = 0; SM.SymSubMatrix(1,10) = SM1; | 
|---|
| 139 |       M3.ReSize(15,15); M3 = 0; M3.SubMatrix(1,10,1,10) = SM1; | 
|---|
| 140 |       M3 -= SM; Print(M3); | 
|---|
| 141 |       SM = 0; SM.SymSubMatrix(6,15) = SM1; | 
|---|
| 142 |       M3.ReSize(15,15); M3 = 0; M3.SubMatrix(6,15,6,15) = SM1; | 
|---|
| 143 |       M3 = M3.t() - SM; Print(M3); | 
|---|
| 144 |    } | 
|---|
| 145 |    { | 
|---|
| 146 |       Tracer et1("Stage 4 - sort"); | 
|---|
| 147 |       TestSort(1); TestSort(2); TestSort(3); TestSort(4); | 
|---|
| 148 |       TestSort(15); TestSort(16); TestSort(17); TestSort(18); | 
|---|
| 149 |       TestSort(99); TestSort(100); TestSort(101); | 
|---|
| 150 |    } | 
|---|
| 151 |  | 
|---|
| 152 |  | 
|---|
| 153 | //   cout << "\nEnd of sixth test\n"; | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|