1 | |
---|
2 | #define WANT_STREAM |
---|
3 | |
---|
4 | |
---|
5 | |
---|
6 | #include "include.h" |
---|
7 | |
---|
8 | #include "newmat.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 | void trymat1() |
---|
21 | { |
---|
22 | // cout << "\nFirst test of Matrix package\n\n"; |
---|
23 | Tracer et("First test of Matrix package"); |
---|
24 | Tracer::PrintTrace(); |
---|
25 | { |
---|
26 | Tracer et1("Stage 1"); |
---|
27 | int i,j; |
---|
28 | |
---|
29 | LowerTriangularMatrix L(10); |
---|
30 | for (i=1;i<=10;i++) for (j=1;j<=i;j++) L(i,j)=2.0+i*i+j; |
---|
31 | SymmetricMatrix S(10); |
---|
32 | for (i=1;i<=10;i++) for (j=1;j<=i;j++) S(i,j)=i*j+1.0; |
---|
33 | SymmetricMatrix S1 = S / 2.0; |
---|
34 | S = S1 * 2.0; |
---|
35 | UpperTriangularMatrix U=L.t()*2.0; |
---|
36 | Print(LowerTriangularMatrix(L-U.t()*0.5)); |
---|
37 | DiagonalMatrix D(10); |
---|
38 | for (i=1;i<=10;i++) D(i,i)=(i-4)*(i-5)*(i-6); |
---|
39 | Matrix M=(S+U-D+L)*(L+U-D+S); |
---|
40 | DiagonalMatrix DD=D*D; |
---|
41 | LowerTriangularMatrix LD=L*D; |
---|
42 | // expressions split for Turbo C |
---|
43 | Matrix M1 = S*L + U*L - D*L + L*L + 10.0; |
---|
44 | { M1 = M1 + S*U + U*U - D*U + L*U - S*D; } |
---|
45 | { M1 = M1 - U*D + DD - LD + S*S; } |
---|
46 | { M1 = M1 + U*S - D*S + L*S - 10.0; } |
---|
47 | M=M1-M; |
---|
48 | Print(M); |
---|
49 | } |
---|
50 | { |
---|
51 | Tracer et1("Stage 2"); |
---|
52 | int i,j; |
---|
53 | |
---|
54 | LowerTriangularMatrix L(9); |
---|
55 | for (i=1;i<=9;i++) for (j=1;j<=i;j++) L(i,j)=1.0+j; |
---|
56 | UpperTriangularMatrix U1(9); |
---|
57 | for (j=1;j<=9;j++) for (i=1;i<=j;i++) U1(i,j)=1.0+i; |
---|
58 | LowerTriangularMatrix LX(9); |
---|
59 | for (i=1;i<=9;i++) for (j=1;j<=i;j++) LX(i,j)=1.0+i*i; |
---|
60 | UpperTriangularMatrix UX(9); |
---|
61 | for (j=1;j<=9;j++) for (i=1;i<=j;i++) UX(i,j)=1.0+j*j; |
---|
62 | { |
---|
63 | L=L+LX/0.5; L=L-LX*3.0; L=LX*2.0+L; |
---|
64 | U1=U1+UX*2.0; U1=U1-UX*3.0; U1=UX*2.0+U1; |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | SymmetricMatrix S(9); |
---|
69 | for (i=1;i<=9;i++) for (j=1;j<=i;j++) S(i,j)=i*i+j; |
---|
70 | { |
---|
71 | SymmetricMatrix S1 = S; |
---|
72 | S=S1+5.0; |
---|
73 | S=S-3.0; |
---|
74 | } |
---|
75 | |
---|
76 | DiagonalMatrix D(9); |
---|
77 | for (i=1;i<=9;i++) D(i,i)=S(i,i); |
---|
78 | UpperTriangularMatrix U=L.t()*2.0; |
---|
79 | { |
---|
80 | U1=U1*2.0 - U; Print(U1); |
---|
81 | L=L*2.0-D; U=U-D; |
---|
82 | } |
---|
83 | Matrix M=U+L; S=S*2.0; M=S-M; Print(M); |
---|
84 | } |
---|
85 | { |
---|
86 | Tracer et1("Stage 3"); |
---|
87 | int i,j; |
---|
88 | Matrix M(10,3), N(10,3); |
---|
89 | for (i = 1; i<=10; i++) for (j = 1; j<=3; j++) |
---|
90 | { M(i,j) = 2*i-j; N(i,j) = i*j + 20; } |
---|
91 | Matrix MN = M + N, M1; |
---|
92 | |
---|
93 | M1 = M; M1 += N; M1 -= MN; Print(M1); |
---|
94 | M1 = M; M1 += M1; M1 = M1 - M * 2; Print(M1); |
---|
95 | M1 = M; M1 += N * 2; M1 -= (MN + N); Print(M1); |
---|
96 | M1 = M; M1 -= M1; Print(M1); |
---|
97 | M1 = M; M1 -= MN + M1; M1 += N + M; Print(M1); |
---|
98 | M1 = M; M1 -= 5; M1 -= M; M1 *= 0.2; M1 = M1 + 1; Print(M1); |
---|
99 | Matrix NT = N.t(); |
---|
100 | M1 = M; M1 *= NT; M1 -= M * N.t(); Print(M1); |
---|
101 | M = M * M.t(); |
---|
102 | DiagonalMatrix D(10); D = 2; |
---|
103 | M1 = M; M1 += D; M1 -= M; M1 = M1 - D; Print(M1); |
---|
104 | M1 = M; M1 -= D; M1 -= M; M1 = M1 + D; Print(M1); |
---|
105 | M1 = M; M1 *= D; M1 /= 2; M1 -= M; Print(M1); |
---|
106 | SymmetricMatrix SM; SM << M; |
---|
107 | // UpperTriangularMatrix SM; SM << M; |
---|
108 | SM += 10; M1 = SM - M; M1 /=10; M1 = M1 - 1; Print(M1); |
---|
109 | } |
---|
110 | { |
---|
111 | Tracer et1("Stage 4"); |
---|
112 | int i,j; |
---|
113 | Matrix M(10,3), N(10,5); |
---|
114 | for (i = 1; i<=10; i++) for (j = 1; j<=3; j++) M(i,j) = 2*i-j; |
---|
115 | for (i = 1; i<=10; i++) for (j = 1; j<=5; j++) N(i,j) = i*j + 20; |
---|
116 | Matrix M1; |
---|
117 | M1 = M; M1 |= N; M1 &= N | M; |
---|
118 | M1 -= (M | N) & (N | M); Print(M1); |
---|
119 | M1 = M; M1 |= M1; M1 &= M1; |
---|
120 | M1 -= (M | M) & (M | M); Print(M1); |
---|
121 | |
---|
122 | } |
---|
123 | { |
---|
124 | Tracer et1("Stage 5"); |
---|
125 | int i,j; |
---|
126 | BandMatrix BM1(10,2,3), BM2(10,4,1); Matrix M1(10,10), M2(10,10); |
---|
127 | for (i=1;i<=10;i++) for (j=1;j<=10;j++) |
---|
128 | { M1(i,j) = 0.5*i+j*j-50; M2(i,j) = (i*101 + j*103) % 13; } |
---|
129 | BM1.Inject(M1); BM2.Inject(M2); |
---|
130 | BandMatrix BM = BM1; BM += BM2; |
---|
131 | Matrix M1X = BM1; Matrix M2X = BM2; Matrix MX = BM; |
---|
132 | MX -= M1X + M2X; Print(MX); |
---|
133 | MX = BM1; MX += BM2; MX -= M1X; MX -= M2X; Print(MX); |
---|
134 | SymmetricBandMatrix SM1; SM1 << BM1 * BM1.t(); |
---|
135 | SymmetricBandMatrix SM2; SM2 << BM2 * BM2.t(); |
---|
136 | SM1 *= 5.5; |
---|
137 | M1X *= M1X.t(); M1X *= 5.5; M2X *= M2X.t(); |
---|
138 | SM1 -= SM2; M1 = SM1 - M1X + M2X; Print(M1); |
---|
139 | M1 = BM1; BM1 *= SM1; M1 = M1 * SM1 - BM1; Print(M1); |
---|
140 | M1 = BM1; BM1 -= SM1; M1 = M1 - SM1 - BM1; Print(M1); |
---|
141 | M1 = BM1; BM1 += SM1; M1 = M1 + SM1 - BM1; Print(M1); |
---|
142 | |
---|
143 | } |
---|
144 | { |
---|
145 | Tracer et1("Stage 6"); |
---|
146 | int i,j; |
---|
147 | Matrix M(10,10), N(10,10); |
---|
148 | for (i = 1; i<=10; i++) for (j = 1; j<=10; j++) |
---|
149 | { M(i,j) = 2*i-j; N(i,j) = i*j + 20; } |
---|
150 | GenericMatrix GM = M; |
---|
151 | GM += N; Matrix M1 = GM - N - M; Print(M1); |
---|
152 | DiagonalMatrix D(10); D = 3; |
---|
153 | GM = D; GM += N; GM += M; GM += D; |
---|
154 | M1 = D*2 - GM + M + N; Print(M1); |
---|
155 | GM = D; GM *= 4; GM += 16; GM /= 8; GM -= 2; |
---|
156 | GM -= D / 2; M1 = GM; Print(M1); |
---|
157 | GM = D; GM *= M; GM *= N; GM /= 3; M1 = M*N - GM; Print(M1); |
---|
158 | GM = D; GM |= M; GM &= N | D; M1 = GM - ((D | M) & (N | D)); |
---|
159 | Print(M1); |
---|
160 | GM = M; M1 = M; GM += 5; GM *= 3; M *= 3; M += 15; M1 = GM - M; |
---|
161 | Print(M1); |
---|
162 | D.ReSize(10); for (i = 1; i<=10; i++) D(i) = i; |
---|
163 | M1 = D + 10; GM = D; GM += 10; M1 -= GM; Print(M1); |
---|
164 | GM = M; GM -= D; M1 = GM; GM = D; GM -= M; M1 += GM; Print(M1); |
---|
165 | GM = M; GM *= D; M1 = GM; GM = D; GM *= M.t(); |
---|
166 | M1 -= GM.t(); Print(M1); |
---|
167 | GM = M; GM += 2 * GM; GM -= 3 * M; M1 = GM; Print(M1); |
---|
168 | GM = M; GM |= GM; GM -= (M | M); M1 = GM; Print(M1); |
---|
169 | GM = M; GM &= GM; GM -= (M & M); M1 = GM; Print(M1); |
---|
170 | M1 = M; M1 = (M1.t() & M.t()) - (M | M).t(); Print(M1); |
---|
171 | M1 = M; M1 = (M1.t() | M.t()) - (M & M).t(); Print(M1); |
---|
172 | |
---|
173 | } |
---|
174 | |
---|
175 | { |
---|
176 | Tracer et1("Stage 7"); |
---|
177 | // test for bug in MS VC5 |
---|
178 | int n = 3; |
---|
179 | int k; int j; |
---|
180 | Matrix A(n,n), B(n,n); |
---|
181 | |
---|
182 | //first version - MS VC++ 5 mis-compiles if optimisation is on |
---|
183 | for (k=1; k<=n; k++) |
---|
184 | { |
---|
185 | for (j = 1; j <= n; j++) A(k,j) = ((k-1) * (2*j-1)); |
---|
186 | } |
---|
187 | |
---|
188 | //second version |
---|
189 | for (k=1; k<=n; k++) |
---|
190 | { |
---|
191 | const int k1 = k-1; // otherwise Visual C++ 5 fails |
---|
192 | for (j = 1; j <= n; j++) B(k,j) = (k1 * (2*j-1)); |
---|
193 | } |
---|
194 | |
---|
195 | if (A != B) |
---|
196 | { |
---|
197 | cout << "\nVisual C++ version 5 compiler error?"; |
---|
198 | cout << "\nTurn off optimisation"; |
---|
199 | } |
---|
200 | |
---|
201 | A -= B; Print(A); |
---|
202 | |
---|
203 | } |
---|
204 | |
---|
205 | // cout << "\nEnd of first test\n"; |
---|
206 | } |
---|
207 | |
---|