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_Matrix_Operations.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/Polynomial.ipp"
20 #include "gtest/gtest.h"
21 namespace{
22  class MatrixTest : public ::testing::Test{
23  public:
24 
25  using FieldT = int;
29 
30  }
31  virtual ~MatrixTest(){
32 
33  }
34 
35  protected:
36  //virtual void SetUp() {
37 
38  //}
39 
40  //virtual void TearDown() {
41 
42  //}
43 
44 
45  };
46 
47 
48 
49 
50  TEST_F(MatrixTest,addition){
51  entryT p1 {1,2,3};
52  entryT p2 {4,5,6};
53  entryT p3 {1};
54  entryT p4 {7,8,9,10};
55  Matrix<entryT> m (2,2);
56  m(0,0)=p1;
57  m(0,1)=p2;
58  m(1,0)=p3;
59  m(1,1)=p4;
60  entryT p5 {0,0,2};
61  entryT p6 {0};
62  entryT p7 {6,5};
63  entryT p8 {7,10};
64  Matrix<entryT> m2 (2,2);
65  m2(0,0)=p5;
66  m2(0,1)=p6;
67  m2(1,0)=p7;
68  m2(1,1)=p8;
69 
70  Matrix<entryT> m3 = m+m2;
71  entryT r1 {1,2,5};
72  entryT r2 {4,5,6};
73  entryT r3 {7,5};
74  entryT r4 {14,18,9,10};
75 
76  ASSERT_EQ(m3(0,0),r1);
77  ASSERT_EQ(m3(0,1),r2);
78  ASSERT_EQ(m3(1,0),r3);
79  ASSERT_EQ(m3(1,1),r4);
80 
81 
82  }
83  TEST_F(MatrixTest,product){
84  entryT p1 {1,2,3};
85  entryT p2 {4,5,6};
86  entryT p3 {1};
87  entryT p4 {7,8,9,10};
88  Matrix<entryT> m (2,2);
89  m(0,0)=p1;
90  m(0,1)=p2;
91  m(1,0)=p3;
92  m(1,1)=p4;
93 
94 
95  Matrix<entryT> m3 =prod(m,m);
96  entryT r1 {5,9,16,12,9};
97  entryT r2 {32,80,146,160,122,60};
98  entryT r3 {8,10,12,10};
99  entryT r4 {53,117,196,284,241,180,100};
100 
101  ASSERT_EQ(m3(0,0),r1);
102  ASSERT_EQ(m3(0,1),r2);
103  ASSERT_EQ(m3(1,0),r3);
104  ASSERT_EQ(m3(1,1),r4);
105 
106 
107  }
109  using namespace boost::numeric::ublas;
110  entryT p1 {1,2,3};
111  entryT p2 {4,5,6};
112  entryT p3 {1};
113  entryT p4 {7,8,9,10};
114  Matrix<entryT> m (2,2);
115  m(0,0)=p1;
116  m(0,1)=p2;
117  m(1,0)=p3;
118  m(1,1)=p4;
119 
120 
121  entryT t = trace(m);
122 
123  ASSERT_EQ(t, entryT ({8,10,12,10}));
124  /* Assert exception when the matrix is not square */
125  Matrix<entryT> m1 (2,3);
126  m1(0,0)=p1;
127  m1(0,1)=p2;
128  m1(0,2)=p1;
129  m1(1,0)=p3;
130  m1(1,1)=p4;
131  m1(1,2)=p3;
132  ASSERT_THROW(trace(m1),std::invalid_argument);
133 
134 
135  }
136 }
137  /* Utility Function */
138 template <typename FieldT>
139  bool operator==(const Polynomial<FieldT>& lhs, const Polynomial<FieldT>& rhs)
140 {
141  if (lhs.size() != rhs.size()) {
142  return false;
143  }
144  for(size_t i = 0; i < lhs.size(); ++i){
145  if (lhs[i] != rhs[i]) {
146  return false;
147  }
148  }
149  return true;
150 }
151 
152 int main(int argc,char **argv){
153  ::testing::InitGoogleTest(&argc,argv);
154  return RUN_ALL_TESTS();
155 }
bool operator==(const Polynomial< FieldT > &lhs, const Polynomial< FieldT > &rhs)
boost::numeric::ublas::matrix< EntryT > Matrix
Typedef for the main matrix class.
Definition: Matrix.hpp:24
int main(int argc, char **argv)
size_t size()
Definition: Polynomial.hpp:155
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