tmt2.cpp
Go to the documentation of this file.
1 
6 
7 
8 //#define WANT_STREAM
9 
10 
11 #include "include.h"
12 
13 #include "newmat.h"
14 
15 #include "tmt.h"
16 
17 #ifdef use_namespace
18 using namespace NEWMAT;
19 #endif
20 
21 
22 /**************************** test program ******************************/
23 
24 
25 void trymat2()
26 {
27 // cout << "\nSecond test of Matrix package\n\n";
28  Tracer et("Second test of Matrix package");
30 
31  int i,j;
32 
33  Matrix M(3,5);
34  for (i=1; i<=3; i++) for (j=1; j<=5; j++) M(i,j) = 100*i + j;
35  Matrix X(8,10);
36  for (i=1; i<=8; i++) for (j=1; j<=10; j++) X(i,j) = 1000*i + 10*j;
37  Matrix Y = X; Matrix Z = X;
38  { X.SubMatrix(2,4,3,7) << M; }
39  for (i=1; i<=3; i++) for (j=1; j<=5; j++) Y(i+1,j+2) = 100*i + j;
40  Print(Matrix(X-Y));
41 
42 
43  Real a[15]; Real* r = a;
44  for (i=1; i<=3; i++) for (j=1; j<=5; j++) *r++ = 100*i + j;
45  { Z.SubMatrix(2,4,3,7) << a; }
46  Print(Matrix(Z-Y));
47 
48  { M=33; X.SubMatrix(2,4,3,7) << M; }
49  { Z.SubMatrix(2,4,3,7) = 33; }
50  Print(Matrix(Z-X));
51 
52  for (i=1; i<=8; i++) for (j=1; j<=10; j++) X(i,j) = 1000*i + 10*j;
53  Y = X;
55  for (i=1; i<=5; i++) for (j=i; j<=5; j++) U(i,j) = 100*i + j;
56  { X.SubMatrix(3,7,5,9) << U; }
57  for (i=1; i<=5; i++) for (j=i; j<=5; j++) Y(i+2,j+4) = 100*i + j;
58  for (i=1; i<=5; i++) for (j=1; j<i; j++) Y(i+2,j+4) = 0.0;
59  Print(Matrix(X-Y));
60  for (i=1; i<=8; i++) for (j=1; j<=10; j++) X(i,j) = 1000*i + 10*j;
61  Y = X;
62  for (i=1; i<=5; i++) for (j=i; j<=5; j++) U(i,j) = 100*i + j;
63  { X.SubMatrix(3,7,5,9).Inject(U); }
64  for (i=1; i<=5; i++) for (j=i; j<=5; j++) Y(i+2,j+4) = 100*i + j;
65  Print(Matrix(X-Y));
66 
67 
68  // test growing and shrinking a vector
69  {
70  ColumnVector V(100);
71  for (i=1;i<=100;i++) V(i) = i*i+i;
72  V = V.Rows(1,50); // to get first 50 vlaues.
73 
74  {
75  V.Release(); ColumnVector VX=V;
76  V.ReSize(100); V = 0.0; V.Rows(1,50)=VX;
77  } // V now length 100
78 
79  M=V; M=100; // to make sure V will hold its values
80  for (i=1;i<=50;i++) V(i) -= i*i+i;
81  Print(V);
82 
83 
84  // test redimensioning vectors with two dimensions given
85  ColumnVector CV1(10); CV1 = 10;
86  ColumnVector CV2(5); CV2.ReSize(10,1); CV2 = 10;
87  V = CV1-CV2; Print(V);
88 
89  RowVector RV1(20); RV1 = 100;
90  RowVector RV2; RV2.ReSize(1,20); RV2 = 100;
91  V = (RV1-RV2).t(); Print(V);
92 
93  X.ReSize(4,7);
94  for (i=1; i<=4; i++) for (j=1; j<=7; j++) X(i,j) = 1000*i + 10*j;
95  Y = 10.5 * X;
96  Z = 7.25 - Y;
97  M = Z + X * 10.5 - 7.25;
98  Print(M);
99  Y = 2.5 * X;
100  Z = 9.25 + Y;
101  M = Z - X * 2.5 - 9.25;
102  Print(M);
103  U.ReSize(8);
104  for (i=1; i<=8; i++) for (j=i; j<=8; j++) U(i,j) = 100*i + j;
105  Y = 100 - U;
106  M = Y + U - 100;
107  Print(M);
108  }
109 
110  {
111  SymmetricMatrix S,T;
112 
113  S << (U + U.t());
114  T = 100 - S; M = T + S - 100; Print(M);
115  T = 100 - 2 * S; M = T + S * 2 - 100; Print(M);
116  X = 100 - 2 * S; M = X + S * 2 - 100; Print(M);
117  T = S; T = 100 - T; M = T + S - 100; Print(M);
118  }
119 
120  // test new
121  {
122  ColumnVector CV1; RowVector RV1;
123  Matrix* MX; MX = new Matrix; if (!MX) Throw(Bad_alloc("New fails "));
124  MX->ReSize(10,20);
125  for (i = 1; i <= 10; i++) for (j = 1; j <= 20; j++)
126  (*MX)(i,j) = 100 * i + j;
127  ColumnVector* CV = new ColumnVector(10);
128  if (!CV) Throw(Bad_alloc("New fails "));
129  *CV << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10;
130  RowVector* RV = new RowVector(CV->t() | (*CV + 10).t());
131  if (!RV) Throw(Bad_alloc("New fails "));
132  CV1 = ColumnVector(10); CV1 = 1; RV1 = RowVector(20); RV1 = 1;
133  *MX -= 100 * *CV * RV1 + CV1 * *RV;
134  Print(*MX);
135  delete MX; delete CV; delete RV;
136  }
137 
138 
139  // test copying of vectors and matrices with no elements
140  {
141  ColumnVector dims(16);
142  Matrix M1; Matrix M2 = M1; Print(M2);
143  dims(1) = M2.Nrows(); dims(2) = M2.Ncols();
144  dims(3) = (Real)(unsigned long)M2.Store(); dims(4) = M2.Storage();
145  M2 = M1;
146  dims(5) = M2.Nrows(); dims(6) = M2.Ncols();
147  dims(7) = (Real)(unsigned long)M2.Store(); dims(8) = M2.Storage();
148  M2.ReSize(10,20); M2.CleanUp();
149  dims(9) = M2.Nrows(); dims(10) = M2.Ncols();
150  dims(11) = (Real)(unsigned long)M2.Store(); dims(12) = M2.Storage();
151  M2.ReSize(20,10); M2.ReSize(0,0);
152  dims(13) = M2.Nrows(); dims(14) = M2.Ncols();
153  dims(15) = (Real)(unsigned long)M2.Store(); dims(16) = M2.Storage();
154  Print(dims);
155  }
156 
157  {
158  ColumnVector dims(16);
159  ColumnVector M1; ColumnVector M2 = M1; Print(M2);
160  dims(1) = M2.Nrows(); dims(2) = M2.Ncols()-1;
161  dims(3) = (Real)(unsigned long)M2.Store(); dims(4) = M2.Storage();
162  M2 = M1;
163  dims(5) = M2.Nrows(); dims(6) = M2.Ncols()-1;
164  dims(7) = (Real)(unsigned long)M2.Store(); dims(8) = M2.Storage();
165  M2.ReSize(10); M2.CleanUp();
166  dims(9) = M2.Nrows(); dims(10) = M2.Ncols()-1;
167  dims(11) = (Real)(unsigned long)M2.Store(); dims(12) = M2.Storage();
168  M2.ReSize(10); M2.ReSize(0);
169  dims(13) = M2.Nrows(); dims(14) = M2.Ncols()-1;
170  dims(15) = (Real)(unsigned long)M2.Store(); dims(16) = M2.Storage();
171  Print(dims);
172  }
173 
174  {
175  ColumnVector dims(16);
176  RowVector M1; RowVector M2 = M1; Print(M2);
177  dims(1) = M2.Nrows()-1; dims(2) = M2.Ncols();
178  dims(3) = (Real)(unsigned long)M2.Store(); dims(4) = M2.Storage();
179  M2 = M1;
180  dims(5) = M2.Nrows()-1; dims(6) = M2.Ncols();
181  dims(7) = (Real)(unsigned long)M2.Store(); dims(8) = M2.Storage();
182  M2.ReSize(10); M2.CleanUp();
183  dims(9) = M2.Nrows()-1; dims(10) = M2.Ncols();
184  dims(11) = (Real)(unsigned long)M2.Store(); dims(12) = M2.Storage();
185  M2.ReSize(10); M2.ReSize(0);
186  dims(13) = M2.Nrows()-1; dims(14) = M2.Ncols();
187  dims(15) = (Real)(unsigned long)M2.Store(); dims(16) = M2.Storage();
188  Print(dims);
189  }
190 
191  // test identity matrix
192  {
193  Matrix M;
194  IdentityMatrix I(10); DiagonalMatrix D(10); D = 1;
195  M = I; M -= D; Print(M);
196  D -= I; Print(D);
197  ColumnVector X(8);
198  D = 1;
199  X(1) = Sum(D) - Sum(I);
200  X(2) = SumAbsoluteValue(D) - SumAbsoluteValue(I);
201  X(3) = SumSquare(D) - SumSquare(I);
202  X(4) = Trace(D) - Trace(I);
203  X(5) = Maximum(D) - Maximum(I);
204  X(6) = Minimum(D) - Minimum(I);
205  X(7) = LogDeterminant(D).LogValue() - LogDeterminant(I).LogValue();
206  X(8) = LogDeterminant(D).Sign() - LogDeterminant(I).Sign();
207  Clean(X,0.00000001); Print(X);
208 
209  for (i = 1; i <= 10; i++) for (j = 1; j <= 10; j++)
210  M(i,j) = 100 * i + j;
211  Matrix N;
212  N = M * I - M; Print(N);
213  N = I * M - M; Print(N);
214  N = M * I.i() - M; Print(N);
215  N = I.i() * M - M; Print(N);
216  N = I.i(); N -= I; Print(N);
217  N = I.t(); N -= I; Print(N);
218  N = I.t(); N += (-I); Print(N); // <----------------
219  D = I; N = D; D = 1; N -= D; Print(N);
220  N = I; D = 1; N -= D; Print(N);
221  N = M + 2 * IdentityMatrix(10); N -= (M + 2 * D); Print(N);
222 
223  I *= 4;
224 
225  D = 4;
226 
227  X.ReSize(14);
228  X(1) = Sum(D) - Sum(I);
229  X(2) = SumAbsoluteValue(D) - SumAbsoluteValue(I);
230  X(3) = SumSquare(D) - SumSquare(I);
231  X(4) = Trace(D) - Trace(I);
232  X(5) = Maximum(D) - Maximum(I);
233  X(6) = Minimum(D) - Minimum(I);
234  X(7) = LogDeterminant(D).LogValue() - LogDeterminant(I).LogValue(); // <--
235  X(8) = LogDeterminant(D).Sign() - LogDeterminant(I).Sign();
236  int i,j;
237  X(9) = I.Maximum1(i) - 4; X(10) = i-1;
238  X(11) = I.Maximum2(i,j) - 4; X(12) = i-10; X(13) = j-10;
239  X(14) = I.Nrows() - 10;
240  Clean(X,0.00000001); Print(X);
241 
242 
243  N = D.i();
244  N += I / (-16);
245  Print(N);
246  N = M * I - 4 * M; Print(N);
247  N = I * M - 4 * M; Print(N);
248  N = M * I.i() - 0.25 * M; Print(N);
249  N = I.i() * M - 0.25 * M; Print(N);
250  N = I.i(); N -= I * 0.0625; Print(N);
251  N = I.i(); N = N - 0.0625 * I; Print(N);
252  N = I.t(); N -= I; Print(N);
253  D = I * 2; N = D; D = 1; N -= 8 * D; Print(N);
254  N = I * 2; N -= 8 * D; Print(N);
255  N = 0.5 * I + M; N -= M; N -= 2.0 * D; Print(N);
256 
257  IdentityMatrix J(10); J = 8;
258  D = 4;
259  DiagonalMatrix E(10); E = 8;
260  N = (I + J) - (D + E); Print(N);
261  N = (5*I + 3*J) - (5*D + 3*E); Print(N);
262  N = (-I + J) - (-D + E); Print(N);
263  N = (I - J) - (D - E); Print(N);
264  N = (I | J) - (D | E); Print(N);
265  N = (I & J) - (D & E); Print(N);
266  N = SP(I,J) - SP(D,E); Print(N);
267  N = D.SubMatrix(2,5,3,8) - I.SubMatrix(2,5,3,8); Print(N);
268 
269  N = M; N.Inject(I); D << M; N -= (M + I); N += D; Print(N);
270  D = 4;
271 
272  IdentityMatrix K = I.i()*7 - J.t()/4;
273  N = D.i() * 7 - E / 4 - K; Print(N);
274  K = I * J; N = K - D * E; Print(N);
275  N = I * J; N -= D * E; Print(N);
276  K = 5*I - 3*J;
277  N = K - (5*D - 3*E); Print(N);
278  K = I.i(); N = K - 0.0625 * I; Print(N);
279  K = I.t(); N = K - I; Print(N);
280 
281 
282  K.ReSize(20); D.ReSize(20); D = 1;
283  D -= K; Print(D);
284 
285  I.ReSize(3); J.ReSize(3); K = I * J; N = K - I; Print(N);
286  K << D; N = K - D; Print(N);
287  }
288 
289  // test add integer
290  {
291  Matrix X(2,3);
292  X << 5.25 << 7.75 << 1.25
293  << 9.00 << 1.00 << 2.50;
294  Matrix Y = X;
295  X = 10 + X;
296  X += (-10);
297  X -= Y;
298  Print(X);
299 
300  // also test f suffix
301  X << 5.25f << 7.75f << 1.25f
302  << 9.00f << 1.00f << 2.50f;
303  X -= Y; Print(X);
304 
305  }
306 
307 
308 
309 
310 // cout << "\nEnd of second test\n";
311 }
312 
313 
LogAndSign LogDeterminant(const BaseMatrix &B)
Definition: newmat.h:2088
void Release()
Definition: newmat.h:514
Real Sum(const BaseMatrix &B)
Definition: newmat.h:2107
void ReSize(int m)
Definition: newmat.h:1034
int Sign() const
Definition: newmat.h:61
Real Maximum1(int &i) const
Definition: newmat.h:373
Real Minimum(const BaseMatrix &B)
Definition: newmat.h:2120
SPMatrix SP(const BaseMatrix &, const BaseMatrix &)
Definition: newmat6.cpp:278
Real Maximum(const BaseMatrix &B)
Definition: newmat.h:2118
virtual void ReSize(int m, int n)
Definition: newmat.h:662
Matrix K
Definition: demo.cpp:228
void Inject(const GeneralMatrix &GM)
Definition: newmat.h:526
Real Maximum2(int &i, int &j) const
Definition: newmat.h:375
void ReSize(int m)
Definition: newmat.h:985
double Real
Definition: include.h:307
void ReSize(int m)
Definition: newmat.h:935
Real SumSquare(const BaseMatrix &B)
Definition: newmat.h:2095
int Nrows() const
Definition: newmat.h:494
void Inject(const GeneralMatrix &GM)
Definition: newmat.h:1798
int Storage() const
Definition: newmat.h:496
Upper triangular matrix.
Definition: newmat.h:799
Real LogValue() const
Definition: newmat.h:59
void Clean(Matrix &A, Real c)
Definition: tmt.cpp:128
TransposedMatrix t() const
Definition: newmat6.cpp:320
Real * Store() const
Definition: newmat.h:497
Real Trace(const BaseMatrix &B)
Definition: newmat.h:2100
#define Throw(E)
Definition: myexcept.h:191
static void PrintTrace()
Definition: myexcept.cpp:109
void trymat2()
Definition: tmt2.cpp:25
The usual rectangular matrix.
Definition: newmat.h:625
InvertedMatrix i() const
Definition: newmat6.cpp:329
Real SumAbsoluteValue(const BaseMatrix &B)
Definition: newmat.h:2103
FloatVector FloatVector * a
int Ncols() const
Definition: newmat.h:495
void CleanUp()
Definition: newmat.h:396
Diagonal matrix.
Definition: newmat.h:896
GetSubMatrix SubMatrix(int fr, int lr, int fc, int lc) const
Definition: newmat.h:2146
Row vector.
Definition: newmat.h:953
void Print(const Matrix &X)
Definition: tmt.cpp:42
Column vector.
Definition: newmat.h:1008
void ReSize(int m)
Definition: newmat.h:833
GetSubMatrix Rows(int f, int l) const
Definition: newmat.h:2151
Identity matrix.
Definition: newmat.h:1350
void ReSize(int n)
Definition: newmat.h:1381
Symmetric matrix.
Definition: newmat.h:753


kni
Author(s): Martin Günther
autogenerated on Fri Jan 3 2020 04:01:17