Protium
Math and Design Features
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
protium_unit_tests.cxx
Go to the documentation of this file.
1 
8 #include "Protium/Math/MathFunctions.h"
9 #include "Protium/Time/Time.h"
10 #include "Protium/Time/Clock.h"
11 
12 #include <iostream>
13 #include <iomanip>
14 #include <vector>
15 
16 using namespace Protium;
17 
18 template<typename T, int n>
20  std::cout<<"Vector of Length: "<<n<<std::endl;
21  for(int i=0; i<n;i++)std::cout<<" "<<vector.At(i);
22  std::cout<<std::endl;
23 }
24 
25 template<typename T, int n, int m>
27  std::cout<<"Matrix of Dimensions: "<<n<<" X "<<m<<std::endl;
28  for(int i=0; i<n;i++){
29  for(int j=0; j<m;j++)
30  std::cout<<" "<<matrix.At(i,j);
31  std::cout<<std::endl;
32  }
33  std::cout<<std::endl;
34 }
35 
36 
38  unsigned nBins = h.GetNBins();
39  std::cout<<"Getting Histogram of "<<nBins<<" bins"<<std::endl;
40  for(unsigned i=0;i<nBins;i++){
41  std::cout<<" Bin Number: "<<i<<" Lower Edge: "<<h.GetLowerEdgeByI(i)<<" Width: "<<h.GetBinWidthByI(i)<<" Content: "<<h.GetBinContentByI(i)<<std::endl;
42  }
43 
44 }
45 
46 
47 int main(int argc, char* argv[]){
48 
50 
51  std::cout<<"Starting Basic Unit Tests"<<std::endl;
52  std::cout<<"\tMath tests"<<std::setprecision(16)<<std::endl;
53  {
54  using namespace Math;
55  std::cout<<"\t\tInt factorial (6): "<<
56  factorial<unsigned>(3)<<"\t"<<factorial<int>(3)<<
57  "\t"<<factorial<long>(3)<<std::endl;
58 
59  std::cout<<"\t\tFloat/Double factorial (6): "<<
60  factorial<float>(3)<<"\t"<<factorial<double>(3)<<
61  "\t"<<factorial<long double>(3)<<std::endl;
62 
63  std::cout<<"\t\tEuler\'s Number (2.71828182845904523536028747135266249775724709369995): "<<nat<<std::endl;
64  std::cout<<"\t\t\tError: "<<2.71828182845904523536028747135266249775724709369995-nat<<std::endl;
65  std::cout<<"\t\tPowers: "<<std::endl;
66  for(double i=0; i<15;i++)for(double j=0; j<15; j++)
67  std::cout<<"\t"<<power(i,j)<<"\t";
68  std::cout<<std::endl;
69  }
70 
71  std::cout<<"\tLinear Algebra Tests"<<std::endl;
72  {
73  using namespace LinearAlgebra;
74  std::cout<<"Testing Vectors"<<std::endl;
75  {
76  TwoVector vec;
77  PrintVector(vec);
79  PrintVector(vec.GetSubVector(1));
80  }
81  {
82  ThreeVector vec;
83  PrintVector(vec);
85  PrintVector(vec.GetSubVector(1));
86  PrintVector(vec.GetSubVector(1).GetSubVector(1));
87  }
88  {
89  FourVector vec;
90  PrintVector(vec);
92  PrintVector(vec.GetSubVector(1));
93  PrintVector(vec.GetSubVector(1).GetSubVector(1));
94  PrintVector(vec.GetSubVector(1).GetSubVector(1).GetSubVector(1));
95  }
96  {
97  FiveVector vec;
98  PrintVector(vec);
100  std::cout<< (vec*FiveVector::UnitVector()) <<std::endl;
103  }
104  {
105  SixVector vec;
106  PrintVector(vec);
108  }
109  {
110  SevenVector vec;
111  PrintVector(vec);
113  }
114  {
115  EightVector vec;
116  PrintVector(vec);
118  }
119  {
120  NineVector vec;
121  PrintVector(vec);
123  }
124  {
125  TenVector vec;
126  PrintVector(vec);
128  }
129  {
130  ElevenVector vec;
131  PrintVector(vec);
133  }
134  {
135  TwelveVector vec;
136  PrintVector(vec);
138  }
139  {
140  ThirteenVector vec;
141  PrintVector(vec);
143  }
144  {
145  FourteenVector vec;
146  PrintVector(vec);
148  }
149  {
150  FifteenVector vec;
151  PrintVector(vec);
153  std::cout<< (vec==FifteenVector::UnitVector() )<<std::endl;
154  std::cout<< (vec!=FifteenVector::UnitVector() )<<std::endl;
155  std::cout<< (FifteenVector::UnitVector()==FifteenVector::UnitVector() )<<std::endl;
157  PrintVector(copy);
158  copy=vec;
159  PrintVector(copy);
160  }
161  std::cout<<"Testing Matrices"<<std::endl;
162  {
163  TwoMatrix mat;
164  PrintMatrix(mat);
166  }
167  {
168  ThreeMatrix mat;
169  PrintMatrix(mat);
171  }
172  {
173  FourMatrix mat;
174  PrintMatrix(mat);
176  }
177  {
178  FiveMatrix mat;
179  PrintMatrix(mat);
181  }
182  {
183  SixMatrix mat;
184  PrintMatrix(mat);
186  }
187  {
188  SevenMatrix mat;
189  PrintMatrix(mat);
192  PrintMatrix(copy);
193  copy=mat;
194  PrintMatrix(copy);
195  copy=SevenMatrix::Unit();
196  for(int i=0; i<7;i++)for(int j=0; j<7;j++) copy[i][j]=1+(i-j)*(i-j)*(i-j)*(i-j)*(i-j);
197  PrintMatrix( copy );
198  PrintMatrix(copy.GetSubMatrix(0,0) );
199  PrintMatrix(copy.GetSubMatrix(0,0).GetSubMatrix(0,0) );
200  PrintMatrix(copy.GetSubMatrix(0,0).GetSubMatrix(0,0).GetSubMatrix(0,0) );
201  PrintMatrix(copy.GetSubMatrix(0,0).GetSubMatrix(0,0).GetSubMatrix(0,0).GetSubMatrix(0,0) );
202  PrintMatrix(copy.GetSubMatrix(0,0).GetSubMatrix(0,0).GetSubMatrix(0,0).GetSubMatrix(0,0).GetSubMatrix(0,0) );
203  PrintMatrix(copy.GetSubMatrix(0,0).GetSubMatrix(0,0).GetSubMatrix(0,0).GetSubMatrix(0,0).GetSubMatrix(0,0).GetSubMatrix(0,0) );
204 
205  PrintMatrix( copy );
206  std::cout<<copy.GetSubMatrix(0,0).GetDeterminant()<<std::endl;
207  std::cout<<copy.GetSubMatrix(0,0).GetSubMatrix(0,0).GetDeterminant()<<std::endl;
208  std::cout<<copy.GetSubMatrix(0,0).GetSubMatrix(0,0).GetSubMatrix(0,0).GetDeterminant()<<std::endl;
209  std::cout<<copy.GetSubMatrix(0,0).GetSubMatrix(0,0).GetSubMatrix(0,0).GetSubMatrix(0,0).GetDeterminant()<<std::endl;
210  std::cout<<copy.GetSubMatrix(0,0).GetSubMatrix(0,0).GetSubMatrix(0,0).GetSubMatrix(0,0).GetSubMatrix(0,0).GetDeterminant()<<std::endl;
211  std::cout<<copy.GetSubMatrix(0,0).GetSubMatrix(0,0).GetSubMatrix(0,0).GetSubMatrix(0,0).GetSubMatrix(0,0).GetSubMatrix(0,0).GetDeterminant()<<std::endl;
212  }
213 
214  {
215  std::cout<<"Vector Times Matrix Multiplication"<<std::endl;
216  SevenVector vec;
217  for(int i=0; i<7;i++)vec[i]=i;
219  for(int i=0; i<7;i++)mat[0][i]=i;
220  PrintVector(mat*vec );
221  }
222  }
223  std::cout<<"Testing Histogram Classes"<<std::endl;
224  {
225  using namespace Containers;
226  {
227  Histo1D h(0.0, 100.0, 1000);
228  PrintHistogram(h);
229  h.SetNewBin(98.75, 1.0);
230  }
231  }
232  std::cout<<"Testing Time Functions"<<std::endl;
233  {
234  using namespace Time;
235 
236  SingleClocker clk;
237  std::cout<<"Testing Single Clock at: "<<ClockPrimitive::CPS()<<" CPS"<<std::endl;
238  clk.Stop();
239  std::cout<<"Single Clock Output"<<clk.GetTime()<<std::endl;
240  clk.Stop();
241  std::cout<<"Single Clock Output"<<clk.GetTime()<<std::endl;
242  clk.Stop();
243  std::cout<<"Single Clock Output"<<clk.GetTime()<<std::endl;
244  RepeatClocker rpt;
245  for(int i=0; i<100;i++)
246  rpt.Clock();
247  std::cout<<"Testing RepeatClocker: Start Time: "<<rpt.GetStart();
248  for(int i=0; i<rpt.GetNClocks();i++)
249  std::cout<<" Clock: "<<rpt.Get(i)<<" Offset: "<<rpt.GetOffset(i)<<std::endl;
250 
251  std::cout<<"Testing Time..."<<std::endl;
252  TimePrimitive prim = TimePrimitive::Now();
253  TimeDate t(prim);
254  std::cout<<"The Time now is: "<<t.AsFormat()<<std::endl;
255  TimeDate other = TimeDate::FromFormat("1/4/1988 19:30:15");
256  std::cout<<"Other Time is: "<<other.AsFormat()<<std::endl;
257 
258  }
259 }
260 
Vector< double, 10 > TenVector
Definition: Vector.h:268
unsigned GetNBins() const
Definition: Histogram.h:45
static Matrix< T, n, m > Unit()
Definition: Matrix.h:307
Vector< double, 13 > ThirteenVector
Definition: Vector.h:271
Vector< double, 5 > FiveVector
Definition: Vector.h:263
Vector< T, n > At(const int &row) const
Definition: Matrix.h:154
Vector< double, 12 > TwelveVector
Definition: Vector.h:270
Vector< double, 6 > SixVector
Definition: Vector.h:264
static clock_t CPS()
Definition: Clock.h:25
static SignalCatcher & Instance()
Matrix< double, 6, 6 > SixMatrix
Definition: Matrix.h:319
BinType GetBinWidthByI(const int &i) const
Definition: Histogram.h:77
Vector< double, 11 > ElevenVector
Definition: Vector.h:269
Histogram< double > Histo1D
Definition: Histogram.h:122
Vector< double, 14 > FourteenVector
Definition: Vector.h:272
int main(int argc, char *argv[])
static TimePrimitive Now()
Definition: Time.h:27
Vector< double, 7 > SevenVector
Definition: Vector.h:265
unsigned GetBinContentByI(const int &i) const
Definition: Histogram.h:86
void PrintVector(const LinearAlgebra::Vector< T, n > &vector)
Matrix< double, 7, 7 > SevenMatrix
Definition: Matrix.h:320
void PrintHistogram(const Containers::Histo1D &h)
Vector< double, 8 > EightVector
Definition: Vector.h:266
Matrix< double, 5, 5 > FiveMatrix
Definition: Matrix.h:318
Vector< double, 15 > FifteenVector
Definition: Vector.h:273
Vector< double, 9 > NineVector
Definition: Vector.h:267
static Vector< T, n > UnitVector(int dim=0)
Constructs a unit vector in the given dimension.
Definition: Vector.h:245
Matrix< double, 2, 2 > TwoMatrix
Definition: Matrix.h:315
Implements matrix addition, subtraction, multiplication.
Definition: Matrix.h:15
double Time
Definition: Quantities.h:85
Implementation of vector to be used in Linear Algebra.
Definition: Vector.h:16
Matrix< double, 4, 4 > FourMatrix
Definition: Matrix.h:317
const T & At(const int &index) const
Definition: Vector.h:155
Vector< double, 3 > ThreeVector
Convenience typedef of a double three vector.
Definition: Vector.h:260
void PrintMatrix(const LinearAlgebra::Matrix< T, n, m > &matrix)
static TimeDate FromFormat(const std::string &t, const std::string &fmt="%d/%m/%Y %H:%M:%S")
Definition: Time.h:79
Matrix< double, 3, 3 > ThreeMatrix
Definition: Matrix.h:316
Vector< double, 4 > FourVector
Convenience typedef of a double four vector.
Definition: Vector.h:262
Vector< double, 2 > TwoVector
Convenience Typedef of a double twovector.
Definition: Vector.h:258
BinType GetLowerEdgeByI(const int &i) const
Definition: Histogram.h:68