1 | |
---|
2 | //#define WANT_STREAM |
---|
3 | |
---|
4 | |
---|
5 | #include "include.h" |
---|
6 | |
---|
7 | #include "newmat.h" |
---|
8 | |
---|
9 | #include "tmt.h" |
---|
10 | |
---|
11 | #ifdef use_namespace |
---|
12 | using namespace NEWMAT; |
---|
13 | #endif |
---|
14 | |
---|
15 | |
---|
16 | /**************************** test program ******************************/ |
---|
17 | |
---|
18 | |
---|
19 | void trymat4() |
---|
20 | { |
---|
21 | // cout << "\nFourth test of Matrix package\n"; |
---|
22 | Tracer et("Fourth test of Matrix package"); |
---|
23 | Tracer::PrintTrace(); |
---|
24 | |
---|
25 | int i,j; |
---|
26 | |
---|
27 | { |
---|
28 | Tracer et1("Stage 1"); |
---|
29 | Matrix M(10,10); |
---|
30 | UpperTriangularMatrix U(10); |
---|
31 | for (i=1;i<=10;i++) for (j=1;j<=10;j++) M(i,j) = 100*i+j; |
---|
32 | U << -M; |
---|
33 | Matrix X1 = M.Rows(2,4); |
---|
34 | Matrix Y1 = U.t().Rows(2,4); |
---|
35 | Matrix X = U; { Print(Matrix(X.Columns(2,4).t()-Y1)); } |
---|
36 | RowVector RV = M.Row(5); |
---|
37 | { |
---|
38 | X.ReSize(3,10); |
---|
39 | X.Row(1) << M.Row(2); X.Row(2) << M.Row(3); X.Row(3) << M.Row(4); |
---|
40 | Print(Matrix(X-X1)); |
---|
41 | } |
---|
42 | { |
---|
43 | UpperTriangularMatrix V = U.SymSubMatrix(3,5); |
---|
44 | Matrix MV = U.SubMatrix(3,5,3,5); { Print(Matrix(MV-V)); } |
---|
45 | Matrix X2 = M.t().Columns(2,4); { Print(Matrix(X2-X1.t())); } |
---|
46 | Matrix Y2 = U.Columns(2,4); { Print(Matrix(Y2-Y1.t())); } |
---|
47 | ColumnVector CV = M.t().Column(5); { Print(ColumnVector(CV-RV.t())); } |
---|
48 | X.ReSize(10,3); M = M.t(); |
---|
49 | X.Column(1) << M.Column(2); X.Column(2) << M.Column(3); |
---|
50 | X.Column(3) << M.Column(4); |
---|
51 | Print(Matrix(X-X2)); |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | { |
---|
56 | Tracer et1("Stage 2"); |
---|
57 | Matrix M; Matrix X; M.ReSize(5,8); |
---|
58 | for (i=1;i<=5;i++) for (j=1;j<=8;j++) M(i,j) = 100*i+j; |
---|
59 | { |
---|
60 | X = M.Columns(5,8); M.Columns(5,8) << M.Columns(1,4); |
---|
61 | M.Columns(1,4) << X; |
---|
62 | X = M.Columns(3,4); M.Columns(3,4) << M.Columns(1,2); |
---|
63 | M.Columns(1,2) << X; |
---|
64 | X = M.Columns(7,8); M.Columns(7,8) << M.Columns(5,6); |
---|
65 | M.Columns(5,6) << X; |
---|
66 | } |
---|
67 | { |
---|
68 | X = M.Column(2); M.Column(2) = M.Column(1); M.Column(1) = X; |
---|
69 | X = M.Column(4); M.Column(4) = M.Column(3); M.Column(3) = X; |
---|
70 | X = M.Column(6); M.Column(6) = M.Column(5); M.Column(5) = X; |
---|
71 | X = M.Column(8); M.Column(8) = M.Column(7); M.Column(7) = X; |
---|
72 | X.ReSize(5,8); |
---|
73 | } |
---|
74 | for (i=1;i<=5;i++) for (j=1;j<=8;j++) X(i,9-j) = 100*i+j; |
---|
75 | Print(Matrix(X-M)); |
---|
76 | } |
---|
77 | { |
---|
78 | Tracer et1("Stage 3"); |
---|
79 | // try submatrices of zero dimension |
---|
80 | Matrix A(4,5); Matrix B, C; |
---|
81 | for (i=1; i<=4; i++) for (j=1; j<=5; j++) |
---|
82 | A(i,j) = 100+i*10+j; |
---|
83 | B = A + 100; |
---|
84 | C = A | B.Columns(4,3); Print(Matrix(A - C)); |
---|
85 | C = A | B.Columns(1,0); Print(Matrix(A - C)); |
---|
86 | C = A | B.Columns(6,5); Print(Matrix(A - C)); |
---|
87 | C = A & B.Rows(2,1); Print(Matrix(A - C)); |
---|
88 | } |
---|
89 | { |
---|
90 | Tracer et1("Stage 4"); |
---|
91 | BandMatrix BM(5,3,2); |
---|
92 | BM(1,1) = 1; BM(1,2) = 2; BM(1,3) = 3; |
---|
93 | BM(2,1) = 4; BM(2,2) = 5; BM(2,3) = 6; BM(2,4) = 7; |
---|
94 | BM(3,1) = 8; BM(3,2) = 9; BM(3,3) =10; BM(3,4) =11; BM(3,5) =12; |
---|
95 | BM(4,1) =13; BM(4,2) =14; BM(4,3) =15; BM(4,4) =16; BM(4,5) =17; |
---|
96 | BM(5,2) =18; BM(5,3) =19; BM(5,4) =20; BM(5,5) =21; |
---|
97 | SymmetricBandMatrix SM(5,3); |
---|
98 | SM.Inject(BandMatrix(BM + BM.t())); |
---|
99 | Matrix A = BM + 1; |
---|
100 | Matrix M = A + A.t() - 2; |
---|
101 | Matrix C = A.i() * BM; |
---|
102 | C = A * C - BM; Clean(C, 0.000000001); Print(C); |
---|
103 | C = A.i() * SM; |
---|
104 | C = A * C - M; Clean(C, 0.000000001); Print(C); |
---|
105 | |
---|
106 | // check row-wise load |
---|
107 | BandMatrix BM1(5,3,2); |
---|
108 | BM1.Row(1) << 1 << 2 << 3; |
---|
109 | BM1.Row(2) << 4 << 5 << 6 << 7; |
---|
110 | BM1.Row(3) << 8 << 9 << 10 << 11 << 12; |
---|
111 | BM1.Row(4) << 13 << 14 << 15 << 16 << 17; |
---|
112 | BM1.Row(5) << 18 << 19 << 20 << 21; |
---|
113 | Matrix M1 = BM1 - BM; Print(M1); |
---|
114 | } |
---|
115 | { |
---|
116 | Tracer et1("Stage 5"); |
---|
117 | Matrix X(4,4); |
---|
118 | X << 1 << 2 << 3 << 4 |
---|
119 | << 5 << 6 << 7 << 8 |
---|
120 | << 9 <<10 <<11 <<12 |
---|
121 | <<13 <<14 <<15 <<16; |
---|
122 | Matrix Y(4,0); |
---|
123 | Y = X | Y; |
---|
124 | X -= Y; Print(X); |
---|
125 | |
---|
126 | DiagonalMatrix D(1); |
---|
127 | D << 23; // matrix input with just one value |
---|
128 | D(1) -= 23; Print(D); |
---|
129 | |
---|
130 | } |
---|
131 | { |
---|
132 | Tracer et1("Stage 6"); |
---|
133 | Matrix h (2,2); |
---|
134 | h << 1.0 << 2.0 << 0.0 << 1.0 ; |
---|
135 | RowVector c(2); |
---|
136 | c << 0.0 << 1.0; |
---|
137 | h -= c & c; |
---|
138 | h -= c.t().Reverse() | c.Reverse().t(); |
---|
139 | Print(h); |
---|
140 | } |
---|
141 | { |
---|
142 | Tracer et1("Stage 7"); |
---|
143 | // Check row-wise input for diagonal matrix |
---|
144 | DiagonalMatrix D(4); |
---|
145 | D << 18 << 23 << 31 << 17; |
---|
146 | DiagonalMatrix D1(4); |
---|
147 | D1.Row(1) << 18; D1.Row(2) << 23; D1.Row(3) << 31; D1.Row(4) << 17; |
---|
148 | D1 -= D; Print(D1); |
---|
149 | D1(1) = 18; D1(2) = 23; D1(3) = 31; D1(4) = 17; |
---|
150 | D1 -= D; Print(D1); |
---|
151 | } |
---|
152 | |
---|
153 | // cout << "\nEnd of fourth test\n"; |
---|
154 | } |
---|
155 | |
---|