tmt.cpp
Go to the documentation of this file.
1 
6 
7 #define WANT_STREAM
8 #define WANT_TIME
9 
10 #include "include.h"
11 
12 #include "newmat.h"
13 
14 #include "tmt.h"
15 
16 #ifdef use_namespace
17 //using namespace NEWMAT;
18 namespace NEWMAT {
19 #endif
20 
21 
22 /**************************** test program ******************************/
23 
24 
26 {
27  int count;
28  const char* s;
29 public:
30  ~PrintCounter();
31  PrintCounter(const char * sx) : count(0), s(sx) {}
32  void operator++() { count++; }
33 };
34 
35 PrintCounter PCZ("Number of non-zero matrices (should be 1) = ");
36 PrintCounter PCN("Number of matrices tested = ");
37 
39 { cout << s << count << "\n"; }
40 
41 
42 void Print(const Matrix& X)
43 {
44  ++PCN;
45  cout << "\nMatrix type: " << X.Type().Value() << " (";
46  cout << X.Nrows() << ", ";
47  cout << X.Ncols() << ")\n\n";
48  if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; }
49  int nr=X.Nrows(); int nc=X.Ncols();
50  for (int i=1; i<=nr; i++)
51  {
52  for (int j=1; j<=nc; j++) cout << X(i,j) << "\t";
53  cout << "\n";
54  }
55  cout << flush; ++PCZ;
56 }
57 
59 {
60  ++PCN;
61  cout << "\nMatrix type: " << X.Type().Value() << " (";
62  cout << X.Nrows() << ", ";
63  cout << X.Ncols() << ")\n\n";
64  if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; }
65  int nr=X.Nrows(); int nc=X.Ncols();
66  for (int i=1; i<=nr; i++)
67  {
68  int j;
69  for (j=1; j<i; j++) cout << "\t";
70  for (j=i; j<=nc; j++) cout << X(i,j) << "\t";
71  cout << "\n";
72  }
73  cout << flush; ++PCZ;
74 }
75 
76 void Print(const DiagonalMatrix& X)
77 {
78  ++PCN;
79  cout << "\nMatrix type: " << X.Type().Value() << " (";
80  cout << X.Nrows() << ", ";
81  cout << X.Ncols() << ")\n\n";
82  if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; }
83  int nr=X.Nrows(); int nc=X.Ncols();
84  for (int i=1; i<=nr; i++)
85  {
86  for (int j=1; j<i; j++) cout << "\t";
87  if (i<=nc) cout << X(i,i) << "\t";
88  cout << "\n";
89  }
90  cout << flush; ++PCZ;
91 }
92 
93 void Print(const SymmetricMatrix& X)
94 {
95  ++PCN;
96  cout << "\nMatrix type: " << X.Type().Value() << " (";
97  cout << X.Nrows() << ", ";
98  cout << X.Ncols() << ")\n\n";
99  if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; }
100  int nr=X.Nrows(); int nc=X.Ncols();
101  for (int i=1; i<=nr; i++)
102  {
103  int j;
104  for (j=1; j<i; j++) cout << X(j,i) << "\t";
105  for (j=i; j<=nc; j++) cout << X(i,j) << "\t";
106  cout << "\n";
107  }
108  cout << flush; ++PCZ;
109 }
110 
112 {
113  ++PCN;
114  cout << "\nMatrix type: " << X.Type().Value() << " (";
115  cout << X.Nrows() << ", ";
116  cout << X.Ncols() << ")\n\n";
117  if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; }
118  int nr=X.Nrows();
119  for (int i=1; i<=nr; i++)
120  {
121  for (int j=1; j<=i; j++) cout << X(i,j) << "\t";
122  cout << "\n";
123  }
124  cout << flush; ++PCZ;
125 }
126 
127 
128 void Clean(Matrix& A, Real c)
129 {
130  int nr = A.Nrows(); int nc = A.Ncols();
131  for (int i=1; i<=nr; i++)
132  {
133  for (int j=1; j<=nc; j++)
134  { Real a = A(i,j); if ((a < c) && (a > -c)) A(i,j) = 0.0; }
135  }
136 }
137 
139 {
140  int nr = A.Nrows();
141  for (int i=1; i<=nr; i++)
142  { Real a = A(i,i); if ((a < c) && (a > -c)) A(i,i) = 0.0; }
143 }
144 
146 {
147  Real R = N / D;
148  R = R * D - N;
149  if ( R > 1 || R < -1)
150  cout << "Pentium error detected: % error = " << 100 * R / N << "\n";
151 }
152 
153 // random number generator class
154 // See newran03 documentation for details
155 
157 {
158  if (s>=1.0 || s<=0.0)
159  Throw(Logic_error("MultWithCarry: seed out of range"));
160  x = (unsigned long)(s * 4294967296.0);
161  crry = 1234567;
162 }
163 
164 
165 // carry out 32bit * 32bit multiply in software
166 
168 {
169  unsigned long mult = 2083801278;
170  unsigned long m_hi = mult >> 16;
171  unsigned long m_lo = mult & 0xFFFF;
172 
173  unsigned long x_hi = x >> 16;
174  unsigned long x_lo = x & 0xFFFF;
175 
176  unsigned long c_hi = crry >> 16;
177  unsigned long c_lo = crry & 0xFFFF;
178 
179  x = x_lo * m_lo + c_lo;
180  unsigned long axc = x_lo * m_hi + x_hi * m_lo + c_hi + (x >> 16);
181  crry = x_hi * m_hi + (axc >> 16);
182 
183  x = (x & 0xFFFF) + (axc << 16);
184 
185 }
186 
187 Real MultWithCarry::Next() { NextValue(); return ((double)x + 0.5) / 4294967296.0; }
188 
189 // fill a matrix with values from the MultWithCarry random number generator
191 {
192  int nr = M.nrows();
193  int nc = M.ncols();
194  for (int i = 1; i <= nr; ++i) for (int j = 1; j <= nc; ++ j)
195  M(i, j) = MWC.Next();
196 }
197 
198 
199 #ifdef use_namespace
200 }
201 using namespace NEWMAT;
202 #endif
203 
204 
205 //*************************** main program **********************************
206 
207 void TestTypeAdd(); // test +
208 void TestTypeMult(); // test *
209 void TestTypeConcat(); // test |
210 void TestTypeSP(); // test SP
211 void TestTypeKP(); // test KP
212 void TestTypeOrder(); // test >=
213 
214 
215 int main()
216 {
217  time_lapse tl; // measure program run time
218  Real* s1; Real* s2; Real* s3; Real* s4;
219  cout << "\nBegin test\n"; // Forces cout to allocate memory at beginning
220  cout << "Now print a real number: " << 3.14159265 << endl;
221  // Throw exception to set up exception buffer
222 #ifndef DisableExceptions
223  Try { Throw(BaseException("Just a dummy\n")); }
224  CatchAll {}
225 #else
226  cout << "Not doing exceptions\n";
227 #endif
228  { Matrix A1(40,200); s1 = A1.Store(); }
229  { Matrix A1(1,1); s3 = A1.Store(); }
230  {
231  Tracer et("Matrix test program");
232 
233  Matrix A(25,150);
234  {
235  int i;
236  RowVector A(8);
237  for (i=1;i<=7;i++) A(i)=0.0; A(8)=1.0;
238  Print(A);
239  }
240  cout << "\n";
241 
244 
245  Try {
246  trymat1();
247  trymat2();
248  trymat3();
249  trymat4();
250  trymat5();
251  trymat6();
252  trymat7();
253  trymat8();
254  trymat9();
255  trymata();
256  trymatb();
257  trymatc();
258  trymatd();
259  trymate();
260  trymatf();
261  trymatg();
262  trymath();
263  trymati();
264  trymatj();
265  trymatk();
266  trymatl();
267  trymatm();
268 
269  cout << "\nEnd of tests\n";
270  }
271  CatchAll
272  {
273  cout << "\nTest program fails - exception generated\n\n";
274  cout << BaseException::what();
275  }
276 
277 
278  }
279 
280  { Matrix A1(40,200); s2 = A1.Store(); }
281  cout << "\n(The following memory checks are probably not valid with all\n";
282  cout << "compilers - see documentation)\n";
283  cout << "\nChecking for lost memory (large block): "
284  << (unsigned long)s1 << " " << (unsigned long)s2 << " ";
285  if (s1 != s2) cout << " - see section 2.8\n\n"; else cout << " - ok\n\n";
286  { Matrix A1(1,1); s4 = A1.Store(); }
287  cout << "\nChecking for lost memory (small block): "
288  << (unsigned long)s3 << " " << (unsigned long)s4 << " ";
289  if (s3 != s4) cout << " - see section 2.8\n\n"; else cout << " - ok\n\n";
290 
291  // check for Pentium bug
292  PentiumCheck(4195835L,3145727L);
293  PentiumCheck(5244795L,3932159L);
294 
295 #ifdef DO_FREE_CHECK
296  FreeCheck::Status();
297 #endif
298  return 0;
299 }
300 
301 
302 
303 
304 //************************ test type manipulation **************************/
305 
306 
307 // These functions may cause problems for Glockenspiel 2.0c; they are used
308 // only for testing so you can delete them
309 
310 
312 {
313  MatrixType list[13];
314  list[0] = MatrixType::UT;
315  list[1] = MatrixType::LT;
316  list[2] = MatrixType::Rt;
317  list[3] = MatrixType::Sq;
318  list[4] = MatrixType::Sm;
319  list[5] = MatrixType::Sk;
320  list[6] = MatrixType::Dg;
321  list[7] = MatrixType::Id;
322  list[8] = MatrixType::BM;
323  list[9] = MatrixType::UB;
324  list[10] = MatrixType::LB;
325  list[11] = MatrixType::SB;
326  list[12] = MatrixType::KB;
327 
328  cout << "+ ";
329  int i;
330  for (i=0; i<MatrixType::nTypes(); i++) cout << list[i].Value() << " ";
331  cout << "\n";
332  for (i=0; i<MatrixType::nTypes(); i++)
333  {
334  cout << list[i].Value() << " ";
335  for (int j=0; j<MatrixType::nTypes(); j++)
336  cout << (list[j]+list[i]).Value() << " ";
337  cout << "\n";
338  }
339  cout << "\n";
340 }
341 
343 {
344  MatrixType list[13];
345  list[0] = MatrixType::UT;
346  list[1] = MatrixType::LT;
347  list[2] = MatrixType::Rt;
348  list[3] = MatrixType::Sq;
349  list[4] = MatrixType::Sm;
350  list[5] = MatrixType::Sk;
351  list[6] = MatrixType::Dg;
352  list[7] = MatrixType::Id;
353  list[8] = MatrixType::BM;
354  list[9] = MatrixType::UB;
355  list[10] = MatrixType::LB;
356  list[11] = MatrixType::SB;
357  list[12] = MatrixType::KB;
358 
359  cout << "* ";
360  int i;
361  for (i=0; i<MatrixType::nTypes(); i++)
362  cout << list[i].Value() << " ";
363  cout << "\n";
364  for (i=0; i<MatrixType::nTypes(); i++)
365  {
366  cout << list[i].Value() << " ";
367  for (int j=0; j<MatrixType::nTypes(); j++)
368  cout << (list[j]*list[i]).Value() << " ";
369  cout << "\n";
370  }
371  cout << "\n";
372 }
373 
375 {
376  MatrixType list[13];
377  list[0] = MatrixType::UT;
378  list[1] = MatrixType::LT;
379  list[2] = MatrixType::Rt;
380  list[3] = MatrixType::Sq;
381  list[4] = MatrixType::Sm;
382  list[5] = MatrixType::Sk;
383  list[6] = MatrixType::Dg;
384  list[7] = MatrixType::Id;
385  list[8] = MatrixType::BM;
386  list[9] = MatrixType::UB;
387  list[10] = MatrixType::LB;
388  list[11] = MatrixType::SB;
389  list[12] = MatrixType::KB;
390 
391  cout << "| ";
392  int i;
393  for (i=0; i<MatrixType::nTypes(); i++)
394  cout << list[i].Value() << " ";
395  cout << "\n";
396  for (i=0; i<MatrixType::nTypes(); i++)
397  {
398  cout << list[i].Value() << " ";
399  for (int j=0; j<MatrixType::nTypes(); j++)
400  cout << (list[j] | list[i]).Value() << " ";
401  cout << "\n";
402  }
403  cout << "\n";
404 }
405 
407 {
408  MatrixType list[13];
409  list[0] = MatrixType::UT;
410  list[1] = MatrixType::LT;
411  list[2] = MatrixType::Rt;
412  list[3] = MatrixType::Sq;
413  list[4] = MatrixType::Sm;
414  list[5] = MatrixType::Sk;
415  list[6] = MatrixType::Dg;
416  list[7] = MatrixType::Id;
417  list[8] = MatrixType::BM;
418  list[9] = MatrixType::UB;
419  list[10] = MatrixType::LB;
420  list[11] = MatrixType::SB;
421  list[12] = MatrixType::KB;
422 
423  cout << "SP ";
424  int i;
425  for (i=0; i<MatrixType::nTypes(); i++)
426  cout << list[i].Value() << " ";
427  cout << "\n";
428  for (i=0; i<MatrixType::nTypes(); i++)
429  {
430  cout << list[i].Value() << " ";
431  for (int j=0; j<MatrixType::nTypes(); j++)
432  cout << (list[j].SP(list[i])).Value() << " ";
433  cout << "\n";
434  }
435  cout << "\n";
436 }
437 
439 {
440  MatrixType list[13];
441  list[0] = MatrixType::UT;
442  list[1] = MatrixType::LT;
443  list[2] = MatrixType::Rt;
444  list[3] = MatrixType::Sq;
445  list[4] = MatrixType::Sm;
446  list[5] = MatrixType::Sk;
447  list[6] = MatrixType::Dg;
448  list[7] = MatrixType::Id;
449  list[8] = MatrixType::BM;
450  list[9] = MatrixType::UB;
451  list[10] = MatrixType::LB;
452  list[11] = MatrixType::SB;
453  list[12] = MatrixType::KB;
454 
455  cout << "KP ";
456  int i;
457  for (i=0; i<MatrixType::nTypes(); i++)
458  cout << list[i].Value() << " ";
459  cout << "\n";
460  for (i=0; i<MatrixType::nTypes(); i++)
461  {
462  cout << list[i].Value() << " ";
463  for (int j=0; j<MatrixType::nTypes(); j++)
464  cout << (list[j].KP(list[i])).Value() << " ";
465  cout << "\n";
466  }
467  cout << "\n";
468 }
469 
471 {
472  MatrixType list[13];
473  list[0] = MatrixType::UT;
474  list[1] = MatrixType::LT;
475  list[2] = MatrixType::Rt;
476  list[3] = MatrixType::Sq;
477  list[4] = MatrixType::Sm;
478  list[5] = MatrixType::Sk;
479  list[6] = MatrixType::Dg;
480  list[7] = MatrixType::Id;
481  list[8] = MatrixType::BM;
482  list[9] = MatrixType::UB;
483  list[10] = MatrixType::LB;
484  list[11] = MatrixType::SB;
485  list[12] = MatrixType::KB;
486 
487  cout << ">= ";
488  int i;
489  for (i = 0; i<MatrixType::nTypes(); i++)
490  cout << list[i].Value() << " ";
491  cout << "\n";
492  for (i=0; i<MatrixType::nTypes(); i++)
493  {
494  cout << list[i].Value() << " ";
495  for (int j=0; j<MatrixType::nTypes(); j++)
496  cout << ((list[j]>=list[i]) ? "Yes " : "No ");
497  cout << "\n";
498  }
499  cout << "\n";
500 }
501 
502 
503 //************** elapsed time class ****************
504 
506 {
507  start_time = ((double)clock())/(double)CLOCKS_PER_SEC;
508 }
509 
511 {
512  double time = ((double)clock())/(double)CLOCKS_PER_SEC - start_time;
513  cout << "Elapsed (processor) time = " << setprecision(2) << time << " seconds" << endl;
514  cout << endl;
515 }
516 
517 
518 
519 
521 
522 
523 
MatrixType Type() const
Definition: newmat.h:493
Definition: tmt.h:15
#define Try
Definition: myexcept.h:190
void trymat3()
Definition: tmt3.cpp:23
void PentiumCheck(Real N, Real D)
Definition: tmt.cpp:145
PrintCounter PCZ("Number of non-zero matrices (should be 1) = ")
void TestTypeSP()
Definition: tmt.cpp:406
void TestTypeConcat()
Definition: tmt.cpp:374
void trymatl()
Definition: tmtl.cpp:138
void trymat8()
Definition: tmt8.cpp:41
void trymath()
Definition: tmth.cpp:371
SPMatrix SP(const BaseMatrix &, const BaseMatrix &)
Definition: newmat6.cpp:278
void trymati()
Definition: tmti.cpp:33
void trymatc()
Definition: tmtc.cpp:23
double Real
Definition: include.h:307
void trymat1()
Definition: tmt1.cpp:26
~PrintCounter()
Definition: tmt.cpp:38
int Nrows() const
Definition: newmat.h:494
KPMatrix KP(const BaseMatrix &, const BaseMatrix &)
Definition: newmat6.cpp:281
void trymatf()
Definition: tmtf.cpp:312
void TestTypeKP()
Definition: tmt.cpp:438
PrintCounter(const char *sx)
Definition: tmt.cpp:31
void trymatk()
Definition: tmtk.cpp:197
void NextValue()
Definition: tmt.cpp:167
#define CatchAll
Definition: myexcept.h:194
int count
Definition: tmt.cpp:27
time_lapse()
Definition: tmt.cpp:505
Upper triangular matrix.
Definition: newmat.h:799
Real Next()
Definition: tmt.cpp:187
void Clean(Matrix &A, Real c)
Definition: tmt.cpp:128
void trymate()
Definition: tmte.cpp:32
void TestTypeMult()
Definition: tmt.cpp:342
bool IsZero() const
Definition: newmat.h:513
Real * Store() const
Definition: newmat.h:497
#define Throw(E)
Definition: myexcept.h:191
The usual rectangular matrix.
Definition: newmat.h:625
void trymatg()
Definition: tmtg.cpp:24
FloatVector FloatVector * a
int Ncols() const
Definition: newmat.h:495
void trymat6()
Definition: tmt6.cpp:66
static const char * what()
Definition: myexcept.h:93
~time_lapse()
Definition: tmt.cpp:510
Diagonal matrix.
Definition: newmat.h:896
const char * Value() const
Definition: newmat.h:183
Lower triangular matrix.
Definition: newmat.h:848
void TestTypeOrder()
Definition: tmt.cpp:470
const char * s
Definition: tmt.cpp:28
void trymat9()
Definition: tmt9.cpp:22
void trymatd()
Definition: tmtd.cpp:133
int ncols() const
Definition: newmat.h:500
void trymata()
Definition: tmta.cpp:38
int main()
Definition: tmt.cpp:215
void operator++()
Definition: tmt.cpp:32
void trymatm()
Definition: tmtm.cpp:26
int nrows() const
Definition: newmat.h:499
void TestTypeAdd()
Definition: tmt.cpp:311
Row vector.
Definition: newmat.h:953
static int nTypes()
Definition: newmat.h:136
void trymatb()
Definition: tmtb.cpp:49
void trymat5()
Definition: tmt5.cpp:49
void trymat2()
Definition: tmt2.cpp:25
void Print(const Matrix &X)
Definition: tmt.cpp:42
void trymat7()
Definition: tmt7.cpp:35
MultWithCarry(double s=0.46875)
Definition: tmt.cpp:156
PrintCounter PCN("Number of matrices tested = ")
void trymatj()
Definition: tmtj.cpp:22
void trymat4()
Definition: tmt4.cpp:25
void FillWithValues(MultWithCarry &MWC, Matrix &M)
Definition: tmt.cpp:190
Symmetric matrix.
Definition: newmat.h:753


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