From a2b936c4dae09d95b1383b2fe3ac15bd30bb06b0 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sun, 26 Feb 2023 20:15:05 +0100 Subject: [PATCH] add basic_operations + first corresponding test --- src/include/operation/basic_operations.cc.h | 99 +++++++++++++++++++++ src/include/operation/basic_operations.h | 55 ++++++++++++ src/include/operation/op_types.cc.h | 2 + src/include/operation/op_types.h | 1 + src/include/operation/operation.cc.h | 1 + src/include/operation/operation.h | 1 + src/include/ranges/range_base.h | 3 +- src/tests/operation_unit_test.cc | 84 ++++++++++------- 8 files changed, 214 insertions(+), 32 deletions(-) create mode 100644 src/include/operation/basic_operations.cc.h create mode 100644 src/include/operation/basic_operations.h diff --git a/src/include/operation/basic_operations.cc.h b/src/include/operation/basic_operations.cc.h new file mode 100644 index 0000000..9e8d3b5 --- /dev/null +++ b/src/include/operation/basic_operations.cc.h @@ -0,0 +1,99 @@ + +#ifndef __cxz_basic_operations_cc_h__ +#define __cxz_basic_operations_cc_h__ + +#include "basic_operations.h" + +namespace CNORXZ +{ + /************************************ + * standard operatrions (unary) * + ************************************/ + + template + constexpr decltype(auto) minus(const COpInterface& op) + { + return operation( [](const auto& a) { return -a; }, op.THIS() ); + } + + /************************************* + * standard operatrions (binary) * + *************************************/ + + template + constexpr decltype(auto) plus(const COpInterface& op1, const COpInterface& op2) + { + return operation( [](const auto& a, const auto& b) { return a + b; }, + op1.THIS(), op2.THIS() ); + } + + template + constexpr decltype(auto) minus(const COpInterface& op1, const COpInterface& op2) + { + return operation( [](const auto& a, const auto& b) { return a - b; }, + op1.THIS(), op2.THIS() ); + } + + template + constexpr decltype(auto) multiplies(const COpInterface& op1, const COpInterface& op2) + { + return operation( [](const auto& a, const auto& b) { return a * b; }, + op1.THIS(), op2.THIS() ); + } + + template + constexpr decltype(auto) divides(const COpInterface& op1, const COpInterface& op2) + { + return operation( [](const auto& a, const auto& b) { return a / b; }, + op1.THIS(), op2.THIS() ); + } + + template + constexpr decltype(auto) modulo(const COpInterface& op1, const COpInterface& op2) + { + return operation( [](const auto& a, const auto& b) { return a % b; }, + op1.THIS(), op2.THIS() ); + } + + /***************************************** + * operators for standard operations * + *****************************************/ + + template + constexpr decltype(auto) operator-(const COpInterface& op) + { + return minus(op); + } + + template + constexpr decltype(auto) operator+(const COpInterface& op1, const COpInterface& op2) + { + return plus(op1, op2); + } + + template + constexpr decltype(auto) operator-(const COpInterface& op1, const COpInterface& op2) + { + return minus(op1, op2); + } + + template + constexpr decltype(auto) operator*(const COpInterface& op1, const COpInterface& op2) + { + return multiplies(op1, op2); + } + + template + constexpr decltype(auto) operator/(const COpInterface& op1, const COpInterface& op2) + { + return divides(op1, op2); + } + + template + constexpr decltype(auto) operator%(const COpInterface& op1, const COpInterface& op2) + { + return modulo(op1, op2); + } +} + +#endif diff --git a/src/include/operation/basic_operations.h b/src/include/operation/basic_operations.h new file mode 100644 index 0000000..150e183 --- /dev/null +++ b/src/include/operation/basic_operations.h @@ -0,0 +1,55 @@ + +#ifndef __cxz_basic_operations_h__ +#define __cxz_basic_operations_h__ + +#include "base/base.h" +#include "operation.h" + +namespace CNORXZ +{ + // standard operations: + // unary: + + template + constexpr decltype(auto) minus(const COpInterface& op); + + // binary: + + template + constexpr decltype(auto) plus(const COpInterface& op1, const COpInterface& op2); + + template + constexpr decltype(auto) minus(const COpInterface& op1, const COpInterface& op2); + + template + constexpr decltype(auto) multiplies(const COpInterface& op1, const COpInterface& op2); + + template + constexpr decltype(auto) divides(const COpInterface& op1, const COpInterface& op2); + + template + constexpr decltype(auto) modulo(const COpInterface& op1, const COpInterface& op2); + + // operators for standard operations: + + template + constexpr decltype(auto) operator-(const COpInterface& op); + + template + constexpr decltype(auto) operator+(const COpInterface& op1, const COpInterface& op2); + + template + constexpr decltype(auto) operator-(const COpInterface& op1, const COpInterface& op2); + + template + constexpr decltype(auto) operator*(const COpInterface& op1, const COpInterface& op2); + + template + constexpr decltype(auto) operator/(const COpInterface& op1, const COpInterface& op2); + + template + constexpr decltype(auto) operator%(const COpInterface& op1, const COpInterface& op2); + +} + +#endif diff --git a/src/include/operation/op_types.cc.h b/src/include/operation/op_types.cc.h index 800855b..330fa4a 100644 --- a/src/include/operation/op_types.cc.h +++ b/src/include/operation/op_types.cc.h @@ -421,6 +421,8 @@ namespace CNORXZ { return i->xpr(i); } + + } #endif diff --git a/src/include/operation/op_types.h b/src/include/operation/op_types.h index 827f4ad..ef380d6 100644 --- a/src/include/operation/op_types.h +++ b/src/include/operation/op_types.h @@ -243,6 +243,7 @@ namespace CNORXZ template constexpr decltype(auto) indexOp(const Sptr& i); + } #endif diff --git a/src/include/operation/operation.cc.h b/src/include/operation/operation.cc.h index fb6746d..b1dd93c 100644 --- a/src/include/operation/operation.cc.h +++ b/src/include/operation/operation.cc.h @@ -1,3 +1,4 @@ #include "op_types.cc.h" #include "op_utility.cc.h" +#include "basic_operations.cc.h" diff --git a/src/include/operation/operation.h b/src/include/operation/operation.h index 5117d04..1e560d9 100644 --- a/src/include/operation/operation.h +++ b/src/include/operation/operation.h @@ -1,5 +1,6 @@ #include "op_types.h" #include "op_utility.h" +#include "basic_operations.h" #include "operation.cc.h" diff --git a/src/include/ranges/range_base.h b/src/include/ranges/range_base.h index 1dfbdf2..264c623 100644 --- a/src/include/ranges/range_base.h +++ b/src/include/ranges/range_base.h @@ -45,7 +45,8 @@ namespace CNORXZ typedef DIndex IndexType; virtual ~RangeBase() = default; - + + // virtual RangePtr sub() const; // Sptr> ; range of subranges (TODO!!!) virtual RangePtr sub(SizeT num) const; virtual SizeT size() const = 0; virtual SizeT dim() const = 0; diff --git a/src/tests/operation_unit_test.cc b/src/tests/operation_unit_test.cc index 43b2a85..2e2bb4c 100644 --- a/src/tests/operation_unit_test.cc +++ b/src/tests/operation_unit_test.cc @@ -53,35 +53,45 @@ namespace { mSize1 = 7; mSize2 = 11; - const SizeT off = 20; + SizeT off = 20; mData1 = Numbers::get(off, mSize1); - mData2 = Numbers::get(off+mSize1, mSize2); - mData12 = Numbers::get(off+mSize1+mSize2, mSize1*mSize2); - auto cra = CRangeFactory(mSize1).create(); - auto crb = CRangeFactory(mSize2).create(); - mCIa1 = std::make_shared(cra); - mCIa2 = std::make_shared(cra); - mCIb1 = std::make_shared(crb); - mCIb2 = std::make_shared(crb); - mCCa1a2 = mindexPtr(mCIa1*mCIa2); - mCCa2a1 = mindexPtr(mCIa2*mCIa1); - mOCa1a2.init(mCCa1a2); - mORa2a1.init(mData12.data(), mCCa2a1); + mData2 = Numbers::get(off += mSize1 , mSize2); + mData11 = Numbers::get(off += mSize2, mSize1*mSize1); + mData12 = Numbers::get(off += mSize1*mSize1, mSize1*mSize2); + auto cr1 = CRangeFactory(mSize1).create(); + auto cr2 = CRangeFactory(mSize2).create(); + mCI1i = std::make_shared(cr1); + mCI1j = std::make_shared(cr1); + mCI2i = std::make_shared(cr2); + mCI2j = std::make_shared(cr2); + mCC1i1j = mindexPtr(mCI1i*mCI1j); + mCC1j1i = mindexPtr(mCI1j*mCI1i); + //mCC1i2i = mindexPtr(mCI1i*mCI2i); + //mCC1j2i = mindexPtr(mCI1j*mCI2i); + mOC1i1j.init(mCC1i1j); + mOR1j1i.init(mData11.data(), mCC1j1i); + mOR1i1j.init(mData11.data(), mCC1i1j); } SizeT mSize1; SizeT mSize2; Vector mData1; Vector mData2; + Vector mData11; Vector mData12; - Sptr mCIa1; - Sptr mCIa2; - Sptr mCIb1; - Sptr mCIb2; - Sptr mCCa1a2; - Sptr mCCa2a1; - OpCont mOCa1a2; - COpRoot mORa2a1; + Sptr mCI1i; + Sptr mCI1j; + Sptr mCI2i; + Sptr mCI2j; + Sptr mCC1i1j; + Sptr mCC1j1i; + //Sptr mCC1i2i; + //Sptr mCC1j2i; + OpCont mOC1i1j; + COpRoot mOR1j1i; + COpRoot mOR1i1j; + //COpRoot mOR1j2i; + //COpRoot mOR1i2i; }; TEST_F(OpCont_CR_Test, Basics) @@ -145,20 +155,32 @@ namespace TEST_F(OpCont_CR_CR_Test, Basics) { - EXPECT_EQ(mOCa1a2.rootSteps(mCIa1->id()).val(), mCIa2->pmax().val()); - EXPECT_EQ(mOCa1a2.rootSteps(mCIa2->id()).val(), 1u); - EXPECT_EQ(mORa2a1.rootSteps(mCIa1->id()).val(), 1u); - EXPECT_EQ(mORa2a1.rootSteps(mCIa2->id()).val(), mCIa1->pmax().val()); + EXPECT_EQ(mOC1i1j.rootSteps(mCI1i->id()).val(), mCI1j->pmax().val()); + EXPECT_EQ(mOC1i1j.rootSteps(mCI1j->id()).val(), 1u); + EXPECT_EQ(mOR1j1i.rootSteps(mCI1i->id()).val(), 1u); + EXPECT_EQ(mOR1j1i.rootSteps(mCI1j->id()).val(), mCI1i->pmax().val()); } TEST_F(OpCont_CR_CR_Test, Assignment) { - mOCa1a2 = mORa2a1; - for(SizeT i = 0; i != mCIa1->pmax().val(); ++i){ - for(SizeT j = 0; j != mCIa2->pmax().val(); ++j){ - const SizeT jS = mCIa2->pmax().val(); - const SizeT iS = mCIa1->pmax().val(); - EXPECT_EQ(mOCa1a2.data()[i*jS+j], mORa2a1.data()[j*iS+i]); + mOC1i1j = mOR1j1i; + for(SizeT i = 0; i != mCI1i->pmax().val(); ++i){ + for(SizeT j = 0; j != mCI1j->pmax().val(); ++j){ + const SizeT jS = mCI1j->pmax().val(); + const SizeT iS = mCI1i->pmax().val(); + EXPECT_EQ(mOC1i1j.data()[i*jS+j], mOR1j1i.data()[j*iS+i]); + } + } + } + + TEST_F(OpCont_CR_CR_Test, Multiply) + { + mOC1i1j = mOR1j1i * mOR1i1j; + for(SizeT i = 0; i != mCI1i->pmax().val(); ++i){ + for(SizeT j = 0; j != mCI1j->pmax().val(); ++j){ + const SizeT jS = mCI1j->pmax().val(); + const SizeT iS = mCI1i->pmax().val(); + EXPECT_EQ(mOC1i1j.data()[i*jS+j], mOR1j1i.data()[j*iS+i] * mOR1i1j.data()[i*jS+j]); } } }