From 8a75177b3965273ab9ae789ca3efc57f4bc868ea Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sat, 26 Aug 2017 17:18:42 +0200 Subject: [PATCH] add basic operations (+-*/) --- src/multi_array_operation.cc | 28 +++++++++++++++++++++++-- src/multi_array_operation.h | 16 ++++++++++++++- src/op_unit_test.cc | 40 ++++++++++++++++++------------------ 3 files changed, 61 insertions(+), 23 deletions(-) diff --git a/src/multi_array_operation.cc b/src/multi_array_operation.cc index 4b613e5..7dfbc53 100644 --- a/src/multi_array_operation.cc +++ b/src/multi_array_operation.cc @@ -22,12 +22,36 @@ namespace MultiArrayTools template template - Operation,OperationClass,Second> - OperationTemplate::operator+(const Second& in) const + auto OperationTemplate::operator+(const Second& in) const + -> Operation,OperationClass,Second> { return Operation,OperationClass,Second>(*mOc, in); } + + template + template + auto OperationTemplate::operator-(const Second& in) const + -> Operation,OperationClass,Second> + { + return Operation,OperationClass,Second>(*mOc, in); + } + template + template + auto OperationTemplate::operator*(const Second& in) const + -> Operation,OperationClass,Second> + { + return Operation,OperationClass,Second>(*mOc, in); + } + + template + template + auto OperationTemplate::operator/(const Second& in) const + -> Operation,OperationClass,Second> + { + return Operation,OperationClass,Second>(*mOc, in); + } + /************************* * OperationMaster * *************************/ diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index 3616da1..1ddb0c6 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -56,7 +56,21 @@ namespace MultiArrayTools OperationTemplate(OperationClass* oc); template - Operation,OperationClass,Second> operator+(const Second& in) const; + auto operator+(const Second& in) const + -> Operation,OperationClass,Second>; + + template + auto operator-(const Second& in) const + -> Operation,OperationClass,Second>; + + template + auto operator*(const Second& in) const + -> Operation,OperationClass,Second>; + + template + auto operator/(const Second& in) const + -> Operation,OperationClass,Second>; + private: OperationClass* mOc; }; diff --git a/src/op_unit_test.cc b/src/op_unit_test.cc index acb9330..d75c70b 100644 --- a/src/op_unit_test.cc +++ b/src/op_unit_test.cc @@ -120,16 +120,16 @@ namespace { auto i1 = std::dynamic_pointer_cast( sr2ptr->index() ); auto i2 = std::dynamic_pointer_cast( sr4ptr->index() ); - res(i1,i2) = ma1(i1) + ma2(i2); + res(i1,i2) = ma1(i1) * ma2(i2); - EXPECT_EQ( fabs( res.at(mkt('1','A')) - (2.917 + 8.870) ) < 0.0001, true ); - EXPECT_EQ( fabs( res.at(mkt('1','B')) - (2.917 + 4.790) ) < 0.0001, true ); + EXPECT_EQ( fabs( res.at(mkt('1','A')) - (2.917 * 8.870) ) < 0.0001, true ); + EXPECT_EQ( fabs( res.at(mkt('1','B')) - (2.917 * 4.790) ) < 0.0001, true ); - EXPECT_EQ( fabs( res.at(mkt('2','A')) - (9.436 + 8.870) ) < 0.0001, true ); - EXPECT_EQ( fabs( res.at(mkt('2','B')) - (9.436 + 4.790) ) < 0.0001, true ); + EXPECT_EQ( fabs( res.at(mkt('2','A')) - (9.436 * 8.870) ) < 0.0001, true ); + EXPECT_EQ( fabs( res.at(mkt('2','B')) - (9.436 * 4.790) ) < 0.0001, true ); - EXPECT_EQ( fabs( res.at(mkt('3','A')) - (0.373 + 8.870) ) < 0.0001, true ); - EXPECT_EQ( fabs( res.at(mkt('3','B')) - (0.373 + 4.790) ) < 0.0001, true ); + EXPECT_EQ( fabs( res.at(mkt('3','A')) - (0.373 * 8.870) ) < 0.0001, true ); + EXPECT_EQ( fabs( res.at(mkt('3','B')) - (0.373 * 4.790) ) < 0.0001, true ); } @@ -143,22 +143,22 @@ namespace { auto i1 = std::dynamic_pointer_cast( mr1ptr->index() ); auto i2 = std::dynamic_pointer_cast( sr4ptr->index() ); - res(i1,i2) = ma1(i1) + ma2(i2) + ma3(i2); + res(i1,i2) = ma1(i1) + ma2(i2) - ma3(i2); - EXPECT_EQ( fabs( res.at(mkt(mkt('1','a'),'A')) - (0.353 + 8.870 + 1.470) ) < 0.0001, true ); - EXPECT_EQ( fabs( res.at(mkt(mkt('1','a'),'B')) - (0.353 + 4.790 + 2.210) ) < 0.0001, true ); - EXPECT_EQ( fabs( res.at(mkt(mkt('1','b'),'A')) - (4.005 + 8.870 + 1.470) ) < 0.0001, true ); - EXPECT_EQ( fabs( res.at(mkt(mkt('1','b'),'B')) - (4.005 + 4.790 + 2.210) ) < 0.0001, true ); + EXPECT_EQ( fabs( res.at(mkt(mkt('1','a'),'A')) - (0.353 + 8.870 - 1.470) ) < 0.0001, true ); + EXPECT_EQ( fabs( res.at(mkt(mkt('1','a'),'B')) - (0.353 + 4.790 - 2.210) ) < 0.0001, true ); + EXPECT_EQ( fabs( res.at(mkt(mkt('1','b'),'A')) - (4.005 + 8.870 - 1.470) ) < 0.0001, true ); + EXPECT_EQ( fabs( res.at(mkt(mkt('1','b'),'B')) - (4.005 + 4.790 - 2.210) ) < 0.0001, true ); - EXPECT_EQ( fabs( res.at(mkt(mkt('2','a'),'A')) - (1.070 + 8.870 + 1.470) ) < 0.0001, true ); - EXPECT_EQ( fabs( res.at(mkt(mkt('2','a'),'B')) - (1.070 + 4.790 + 2.210) ) < 0.0001, true ); - EXPECT_EQ( fabs( res.at(mkt(mkt('2','b'),'A')) - (2.310 + 8.870 + 1.470) ) < 0.0001, true ); - EXPECT_EQ( fabs( res.at(mkt(mkt('2','b'),'B')) - (2.310 + 4.790 + 2.210) ) < 0.0001, true ); + EXPECT_EQ( fabs( res.at(mkt(mkt('2','a'),'A')) - (1.070 + 8.870 - 1.470) ) < 0.0001, true ); + EXPECT_EQ( fabs( res.at(mkt(mkt('2','a'),'B')) - (1.070 + 4.790 - 2.210) ) < 0.0001, true ); + EXPECT_EQ( fabs( res.at(mkt(mkt('2','b'),'A')) - (2.310 + 8.870 - 1.470) ) < 0.0001, true ); + EXPECT_EQ( fabs( res.at(mkt(mkt('2','b'),'B')) - (2.310 + 4.790 - 2.210) ) < 0.0001, true ); - EXPECT_EQ( fabs( res.at(mkt(mkt('3','a'),'A')) - (9.243 + 8.870 + 1.470) ) < 0.0001, true ); - EXPECT_EQ( fabs( res.at(mkt(mkt('3','a'),'B')) - (9.243 + 4.790 + 2.210) ) < 0.0001, true ); - EXPECT_EQ( fabs( res.at(mkt(mkt('3','b'),'A')) - (2.911 + 8.870 + 1.470) ) < 0.0001, true ); - EXPECT_EQ( fabs( res.at(mkt(mkt('3','b'),'B')) - (2.911 + 4.790 + 2.210) ) < 0.0001, true ); + EXPECT_EQ( fabs( res.at(mkt(mkt('3','a'),'A')) - (9.243 + 8.870 - 1.470) ) < 0.0001, true ); + EXPECT_EQ( fabs( res.at(mkt(mkt('3','a'),'B')) - (9.243 + 4.790 - 2.210) ) < 0.0001, true ); + EXPECT_EQ( fabs( res.at(mkt(mkt('3','b'),'A')) - (2.911 + 8.870 - 1.470) ) < 0.0001, true ); + EXPECT_EQ( fabs( res.at(mkt(mkt('3','b'),'B')) - (2.911 + 4.790 - 2.210) ) < 0.0001, true ); } TEST_F(OpTest_MDim, ExecOp3)