Go to the documentation of this file.00001
00002
00003 #define WANT_MATH
00004
00005
00006 #include "include.h"
00007
00008 #include "newmatap.h"
00009
00010 #include "tmt.h"
00011
00012 #ifdef use_namespace
00013 using namespace NEWMAT;
00014 #endif
00015
00016
00017
00018
00019
00020
00021
00022 static void SimpleSortDescending(Real* first, const int length)
00023 {
00024 int i = length;
00025 while (--i)
00026 {
00027 Real x = *first; Real* f = first; Real* g = f;
00028 int j = i;
00029 while (j--) if (x < *(++f)) { g = f; x = *g; }
00030 *g = *first; *first++ = x;
00031 }
00032 }
00033
00034 static void TestSort(int n)
00035 {
00036
00037 RowVector X(n);
00038 int i;
00039 for (i = 1; i <= n; i++)
00040 X(i) = sin((Real)i) + 0.3 * cos(i/5.0) - 0.6 * sin(i/7.0) + 0.2 * sin(2.0 * i);
00041 RowVector X_Sorted = X; SimpleSortDescending(X_Sorted.Store(), n);
00042 RowVector A = X + X.Reverse(); SimpleSortDescending(A.Store(), n);
00043
00044
00045
00046 RowVector Y = X; SortDescending(Y); Y -= X_Sorted; Print(Y);
00047 Y = X_Sorted; SortDescending(Y); Y -= X_Sorted; Print(Y);
00048 Y = X_Sorted.Reverse(); SortDescending(Y); Y -= X_Sorted; Print(Y);
00049 Y = X + X.Reverse(); SortDescending(Y); Y -= A; Print(Y);
00050
00051
00052
00053 Y = X; SortAscending(Y); Y -= X_Sorted.Reverse(); Print(Y);
00054 Y = X_Sorted; SortAscending(Y); Y -= X_Sorted.Reverse(); Print(Y);
00055 Y = X_Sorted.Reverse(); SortAscending(Y); Y -= X_Sorted.Reverse(); Print(Y);
00056 Y = X + X.Reverse(); SortAscending(Y); Y -= A.Reverse(); Print(Y);
00057 }
00058
00059
00060 void trymat6()
00061 {
00062 Tracer et("Sixth test of Matrix package");
00063 Tracer::PrintTrace();
00064
00065 int i,j;
00066
00067
00068 DiagonalMatrix D(6);
00069 UpperTriangularMatrix U(6);
00070 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; }
00071 LowerTriangularMatrix L=(U*3.0).t();
00072 SymmetricMatrix S(6);
00073 for (i=1;i<=6;i++) for (j=i;j<=6;j++) S(i,j)=i*i+2.0+j;
00074 Matrix MD=D; Matrix ML=L; Matrix MU=U; Matrix MS=S;
00075 Matrix M(6,6);
00076 for (i=1;i<=6;i++) for (j=1;j<=6;j++) M(i,j)=i*j+i*i-10.0;
00077 {
00078 Tracer et1("Stage 1");
00079 Print(Matrix(MS+(-MS)));
00080 Print(Matrix((S+M)-(MS+M)));
00081 Print(Matrix((M+U)-(M+MU)));
00082 Print(Matrix((M+L)-(M+ML)));
00083 }
00084 {
00085 Tracer et1("Stage 2");
00086 Print(Matrix((M+D)-(M+MD)));
00087 Print(Matrix((U+D)-(MU+MD)));
00088 Print(Matrix((D+L)-(ML+MD)));
00089 Print(Matrix((-U+D)+MU-MD));
00090 Print(Matrix((-L+D)+ML-MD));
00091 }
00092 {
00093 Tracer et1("Stage 3 - concatenate");
00094 RowVector A(5);
00095 A << 1 << 2 << 3 << 4 << 5;
00096 RowVector B(5);
00097 B << 3 << 1 << 4 << 1 << 5;
00098 Matrix C(3,5);
00099 C << 2 << 3 << 5 << 7 << 11
00100 << 13 << 17 << 19 << 23 << 29
00101 << 31 << 37 << 41 << 43 << 47;
00102 Matrix X1 = A & B & C;
00103 Matrix X2 = (A.t() | B.t() | C.t()).t();
00104 Matrix X3(5,5);
00105 X3.Row(1)=A; X3.Row(2)=B; X3.Rows(3,5)=C;
00106 Print(Matrix(X1-X2));
00107 Print(Matrix(X1-X3));
00108 LowerTriangularMatrix LT1; LT1 << (A & B & C);
00109 UpperTriangularMatrix UT1; UT1 << (A.t() | B.t() | C.t());
00110 Print(LowerTriangularMatrix(LT1-UT1.t()));
00111 DiagonalMatrix D1; D1 << (A.t() | B.t() | C.t());
00112 ColumnVector At = A.t();
00113 ColumnVector Bt = B.t();
00114 Matrix Ct = C.t();
00115 LowerTriangularMatrix LT2; LT2 << (At | Bt | Ct);
00116 UpperTriangularMatrix UT2; UT2 << (At.t() & Bt.t() & Ct.t());
00117 Matrix ABt = At | Bt;
00118 DiagonalMatrix D2; D2 << (ABt | Ct);
00119 Print(LowerTriangularMatrix(LT2-UT2.t()));
00120 Print(DiagonalMatrix(D1-D2));
00121 Print(Matrix(LT1+UT2-D2-X1));
00122 Matrix M1 = LT1 | UT2; Matrix M2 = UT1 & LT2;
00123 Print(Matrix(M1-M2.t()));
00124 M1 = UT2 | LT1; M2 = LT2 & UT1;
00125 Print(Matrix(M1-M2.t()));
00126 M1 = (LT1 | UT2) & (UT2 | LT1);
00127 M2 = (UT1 & LT2) | (LT2 & UT1);
00128 Print(Matrix(M1-M2.t()));
00129 SymmetricMatrix SM1; SM1 << (M1 + M1.t());
00130 SymmetricMatrix SM2; SM2 << ((SM1 | M1) & (M1.t() | SM1));
00131 Matrix M3(20,20);
00132 M3.SubMatrix(1,10,1,10) = SM1;
00133 M3.SubMatrix(1,10,11,20) = M1;
00134 M3.SubMatrix(11,20,1,10) = M2;
00135 M3.SubMatrix(11,20,11,20) = SM1;
00136 Print(Matrix(M3-SM2));
00137
00138 SymmetricMatrix SM(15); SM = 0; SM.SymSubMatrix(1,10) = SM1;
00139 M3.ReSize(15,15); M3 = 0; M3.SubMatrix(1,10,1,10) = SM1;
00140 M3 -= SM; Print(M3);
00141 SM = 0; SM.SymSubMatrix(6,15) = SM1;
00142 M3.ReSize(15,15); M3 = 0; M3.SubMatrix(6,15,6,15) = SM1;
00143 M3 = M3.t() - SM; Print(M3);
00144 }
00145 {
00146 Tracer et1("Stage 4 - sort");
00147 TestSort(1); TestSort(2); TestSort(3); TestSort(4);
00148 TestSort(15); TestSort(16); TestSort(17); TestSort(18);
00149 TestSort(99); TestSort(100); TestSort(101);
00150 }
00151
00152
00153
00154 }
00155