Polymetic  1.1
A c++ library for polynomial and matrix arithmetic, focused on applications in Kinematics.
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
Test_MatrixAlgorithms_polynomial_double.cpp
Go to the documentation of this file.
1 // Copyright 2018 Dhruvesh Nikhilkumar Patel
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
18 #include "../include/Matrix.ipp"
19 #include "../include/MatrixAlgorithms.ipp"
20 #include "../include/Polynomial.ipp"
21 #include "Polynomial_testutils.ipp"
22 #include "gtest/gtest.h"
23 namespace{
24  class MatrixTest : public ::testing::Test{
25  public:
26  using FieldT = double;
30 
31  }
32  virtual ~MatrixTest(){
33 
34  }
35 
36  protected:
37  //virtual void SetUp() {
38 
39  //}
40 
41  //virtual void TearDown() {
42 
43  //}
44 
45 
46  };
47 
48 
49 
50 
51  TEST_F(MatrixTest,trace_with_power){
52 
53  Matrix<entryT> mi (3,3);
54  mi(0,0)=1;
55  mi(0,1)=0;
56  mi(0,2)=0;
57  mi(1,0)=0;
58  mi(1,1)=1;
59  mi(1,2)=0;
60  mi(2,0)=0;
61  mi(2,1)=0;
62  mi(2,2)=1;
63 
64  entryT tri = trace(mi,5);
65  ASSERT_EQ(tri.size(),1);
66  ASSERT_DOUBLE_EQ(tri[0],3);
67 
68  Matrix<entryT> m2 (3,3);
69  m2(0,0)=1;
70  m2(0,1)=2;
71  m2(0,2)=3;
72  m2(1,0)=4;
73  m2(1,1)=5;
74  m2(1,2)=6;
75  m2(2,0)=7;
76  m2(2,1)=8;
77  m2(2,2)=9;
78 
79  entryT tr2 = trace(m2,3);
80  ASSERT_EQ(tr2,entryT(4185));
81 
82  Matrix<entryT> m (3,3);
83  entryT p11 {2,0,10};//10x^2+2
84  entryT p12 {5.2};
85  entryT p13 {0,0,2,0,0,0.1};
86  entryT p21 {0};
87  entryT p22 {1,2.5};
88  entryT p23 {0,100,0,23};
89  entryT p31 {45};
90  entryT p32 {0,0,44};
91  entryT p33 {0};
92  m(0,0)=p11;
93  m(0,1)=p12;
94  m(0,2)=p13;
95  m(1,0)=p21;
96  m(1,1)=p22;
97  m(1,2)=p23;
98  m(2,0)=p31;
99  m(2,1)=p32;
100  m(2,2)=p33;
101 
102 
103  entryT tr = trace(m,3);
104  ASSERT_EQ(tr,entryT ({9.0,70207.5000000000024,678.7500,29361.62500000000055,36300.0,3063.000000000000001,8590.0,135.0000000000000075}));
105 
106 
107 
108  }
109  TEST_F(MatrixTest,determinant){
110 
111  Matrix<entryT> mi (3,3);
112  mi(0,0)=1;
113  mi(0,1)=0;
114  mi(0,2)=0;
115  mi(1,0)=0;
116  mi(1,1)=1;
117  mi(1,2)=0;
118  mi(2,0)=0;
119  mi(2,1)=0;
120  mi(2,2)=1;
121 
122  entryT di = det(mi);
123  ASSERT_EQ(di,entryT(1));
124 
125  Matrix<entryT> m2 (3,3);
126  m2(0,0)=1;
127  m2(0,1)=2;
128  m2(0,2)=3;
129  m2(1,0)=4;
130  m2(1,1)=5;
131  m2(1,2)=6;
132  m2(2,0)=7;
133  m2(2,1)=8;
134  m2(2,2)=9;
135 
136  entryT d2 = det(m2);
137  ASSERT_EQ(d2,entryT(0));
138 
139  Matrix<entryT> m (3,3);
140  entryT p11 {2,0,10};//10x^2+2
141  entryT p12 {5.2};
142  entryT p13 {0,0,2,0,0,0.1};
143  entryT p21 {0};
144  entryT p22 {1,2.5};
145  entryT p23 {0,100,0,23};
146  entryT p31 {45};
147  entryT p32 {0,0,44};
148  entryT p33 {0};
149  m(0,0)=p11;
150  m(0,1)=p12;
151  m(0,2)=p13;
152  m(1,0)=p21;
153  m(1,1)=p22;
154  m(1,2)=p23;
155  m(2,0)=p31;
156  m(2,1)=p32;
157  m(2,2)=p33;
158 
159  entryT d = det(m);
160  ASSERT_EQ(d,entryT({0,23400.0,-90.0,-3642.99999999999982,0,-46028.5,-11.250,-10120.00}));
161 
162  }
163  TEST_F(MatrixTest,determinant_large_5_5){
164  /* read in the files created by mathematica */
165  std::string input_file {"test/inputs_MatDim5_PolyDegree5.csv"};
166  std::string result_file {"test/result_MatDim5_PolyDegree5.csv"};
167 
168  /* Create the input matrix*/
169  Matrix<entryT> M (5,5);
170  createMatrixWithPolynomialEntries<FieldT>(input_file,&M);
171 
172  /* read the expected result from file */
173  entryT expectedResultp= readPolynomialFromFile<FieldT>(result_file);
174 
175  /* calculate the det */
176  entryT resultp= det(M);
177  ASSERT_EQ(resultp,expectedResultp);
178 
179  }
180  TEST_F(MatrixTest,determinant_large_5_10){
181  /* read in the files created by mathematica */
182  std::string input_file {"test/inputs_MatDim5_PolyDegree10.csv"};
183  std::string result_file {"test/result_MatDim5_PolyDegree10.csv"};
184 
185  /* Create the input matrix*/
186  Matrix<entryT> M (5,5);
187  createMatrixWithPolynomialEntries<FieldT>(input_file,&M);
188 
189  /* read the expected result from file */
190  entryT expectedResultp= readPolynomialFromFile<FieldT>(result_file);
191 
192  /* calculate the det */
193  entryT resultp= det(M);
194  ASSERT_EQ(resultp,expectedResultp);
195 
196  }
197  TEST_F(MatrixTest,determinant_large_10_10){
198  /* read in the files created by mathematica */
199  std::string input_file {"test/inputs_MatDim10_PolyDegree10.csv"};
200  std::string result_file {"test/result_MatDim10_PolyDegree10.csv"};
201 
202  /* Create the input matrix*/
203  using f=long double;
204  using e = Polynomial<f>
205  Matrix<entryT> M (10,10);
206  createMatrixWithPolynomialEntries<FieldT>(input_file,&M);
207 
208  /* read the expected result from file */
209  entryT expectedResultp= readPolynomialFromFile<FieldT>(result_file);
210 
211  /* calculate the det */
212  entryT resultp= det(M);
213  ASSERT_EQ(resultp,expectedResultp);
214 
215  }
216  }
217 int main(int argc,char **argv){
218  ::testing::InitGoogleTest(&argc,argv);
219  return RUN_ALL_TESTS();
220 }
int main(int argc, char **argv)
boost::numeric::ublas::matrix< EntryT > Matrix
Typedef for the main matrix class.
Definition: Matrix.hpp:24
MatrixT::value_type det(const MatrixT &M)
Finds the determinant of a square matrix.
EntryT trace(const Matrix< EntryT > &M)
Finds trace of a square matrix.
Definition: Matrix_impl.ipp:25
Contains the definition for the abstract base class which will be used by different multiplication al...
Definition: Polynomial.hpp:40
Header for the utility functions like == operator for polynomials.