contraction compiles (untested)

This commit is contained in:
Christian Zimmermann 2017-11-04 22:49:55 +01:00
parent 83b0c427a5
commit 8dfa81a99e
5 changed files with 74 additions and 30 deletions

View file

@ -22,6 +22,20 @@ namespace MultiArrayHelper
} }
return res; return res;
} }
template <typename T, class OpFunc, class BlockClass>
BlockBinaryOpSelf<T,OpFunc,BlockClass>::BlockBinaryOpSelf(BlockResult<T>& res) : mRes(res) {}
template <typename T, class OpFunc, class BlockClass>
void BlockBinaryOpSelf<T,OpFunc,BlockClass>::operator()(const BlockClass& arg)
{
static OpFunc f;
assert(mRes.size() == arg.size());
for(size_t i = 0; i != arg.size(); ++i){
mRes[i] = f(mRes[i], arg[i]);
}
}
/***************** /*****************
* BlockBase * * BlockBase *
@ -35,7 +49,7 @@ namespace MultiArrayHelper
{ {
return mSize; return mSize;
} }
/*
template <typename T> template <typename T>
template <class OpFunction> template <class OpFunction>
BlockResult<T> BlockBase<T>::operate(const BlockBase<T>& in) BlockResult<T> BlockBase<T>::operate(const BlockBase<T>& in)
@ -43,6 +57,7 @@ namespace MultiArrayHelper
assert(mSize == in.size()); assert(mSize == in.size());
OpFunction f; OpFunction f;
BlockResult<T> res(mSize); BlockResult<T> res(mSize);
CHECK;
for(size_t i = 0; i != mSize; ++i){ for(size_t i = 0; i != mSize; ++i){
res[i] = f((*this)[i], in[i]); res[i] = f((*this)[i], in[i]);
} }
@ -73,7 +88,7 @@ namespace MultiArrayHelper
{ {
return operate<std::divides<T> >(in); return operate<std::divides<T> >(in);
} }
*/
/************************ /************************
* MutableBlockBase * * MutableBlockBase *
************************/ ************************/
@ -190,6 +205,7 @@ namespace MultiArrayHelper
template <class BlockClass> template <class BlockClass>
BlockResult<T>& BlockResult<T>::operator=(const BlockClass& in) BlockResult<T>& BlockResult<T>::operator=(const BlockClass& in)
{ {
//CHECK;
for(size_t i = 0; i != BlockBase<T>::mSize; ++i){ for(size_t i = 0; i != BlockBase<T>::mSize; ++i){
(*this)[i] = in[i]; (*this)[i] = in[i];
} }
@ -228,25 +244,33 @@ namespace MultiArrayHelper
} }
template <typename T> template <typename T>
BlockResult<T>& BlockResult<T>::operator+=(const BlockBase& in) BlockResult<T>& BlockResult<T>::assing(const T& val)
{
mRes.assing(BB::mSize, val);
return *this;
}
/*
template <typename T>
BlockResult<T>& BlockResult<T>::operator+=(const BlockBase<T>& in)
{ {
return operateSelf<std::plus<T> >(in); return operateSelf<std::plus<T> >(in);
} }
template <typename T> template <typename T>
BlockResult<T>& BlockResult<T>::operator-=(const BlockBase& in) BlockResult<T>& BlockResult<T>::operator-=(const BlockBase<T>& in)
{ {
return operateSelf<std::minus<T> >(in); return operateSelf<std::minus<T> >(in);
} }
template <typename T> template <typename T>
BlockResult<T>& BlockResult<T>::operator*=(const BlockBase& in) BlockResult<T>& BlockResult<T>::operator*=(const BlockBase<T>& in)
{ {
return operateSelf<std::multiplies<T> >(in); return operateSelf<std::multiplies<T> >(in);
} }
template <typename T> template <typename T>
BlockResult<T>& BlockResult<T>::operator/=(const BlockBase& in) BlockResult<T>& BlockResult<T>::operator/=(const BlockBase<T>& in)
{ {
return operateSelf<std::divides<T> >(in); return operateSelf<std::divides<T> >(in);
} }
@ -255,14 +279,14 @@ namespace MultiArrayHelper
template <class OpFunction> template <class OpFunction>
BlockResult<T>& BlockResult<T>::operateSelf(const BlockBase<T>& in) BlockResult<T>& BlockResult<T>::operateSelf(const BlockBase<T>& in)
{ {
assert(mSize == in.size()); assert(BB::mSize == in.size());
OpFunction f; OpFunction f;
//BlockResult<T> res(mSize); //BlockResult<T> res(mSize);
for(size_t i = 0; i != mSize; ++i){ for(size_t i = 0; i != BB::mSize; ++i){
(*this)[i] = f((*this)[i], in[i]); (*this)[i] = f((*this)[i], in[i]);
} }
return *this; return *this;
} }
*/
} // end namespace MultiArrayHelper } // end namespace MultiArrayHelper

View file

@ -29,6 +29,17 @@ namespace MultiArrayHelper
BlockResult<T> operator()(const BlockClass1& arg1, const BlockClass2& arg2); BlockResult<T> operator()(const BlockClass1& arg1, const BlockClass2& arg2);
}; };
template <typename T, class OpFunc, class BlockClass>
class BlockBinaryOpSelf
{
public:
BlockBinaryOpSelf(BlockResult<T>& res);
void operator()(const BlockClass& arg);
private:
BlockResult<T>& mRes;
};
// EVERYTHING IN HERE MUST N O T BE VITUAL !! // EVERYTHING IN HERE MUST N O T BE VITUAL !!
template <typename T> template <typename T>
@ -39,7 +50,7 @@ namespace MultiArrayHelper
BlockBase(size_t size); BlockBase(size_t size);
size_t size() const; size_t size() const;
/*
template <class OpFunction> template <class OpFunction>
BlockResult<T> operate(const BlockBase& in); BlockResult<T> operate(const BlockBase& in);
@ -47,7 +58,7 @@ namespace MultiArrayHelper
BlockResult<T> operator-(const BlockBase& in); BlockResult<T> operator-(const BlockBase& in);
BlockResult<T> operator*(const BlockBase& in); BlockResult<T> operator*(const BlockBase& in);
BlockResult<T> operator/(const BlockBase& in); BlockResult<T> operator/(const BlockBase& in);
*/
protected: protected:
size_t mSize; size_t mSize;
}; };
@ -116,12 +127,16 @@ namespace MultiArrayHelper
class BlockResult : public MutableBlockBase<T> class BlockResult : public MutableBlockBase<T>
{ {
public: public:
typedef BlockBase<T> BB;
DEFAULT_MEMBERS(BlockResult); DEFAULT_MEMBERS(BlockResult);
BlockResult(size_t size); BlockResult(size_t size);
template <class BlockClass> template <class BlockClass>
BlockResult& operator=(const BlockClass& in); BlockResult& operator=(const BlockClass& in);
BlockResult& assing(const T& val);
BlockType type() const; BlockType type() const;
const T& operator[](size_t pos) const; const T& operator[](size_t pos) const;
@ -129,13 +144,13 @@ namespace MultiArrayHelper
BlockResult& set(size_t npos); BlockResult& set(size_t npos);
size_t stepSize() const; size_t stepSize() const;
BlockResult<T>& operator+=(const BlockBase& in); //BlockResult<T>& operator+=(const BlockBase<T>& in);
BlockResult<T>& operator-=(const BlockBase& in); //BlockResult<T>& operator-=(const BlockBase<T>& in);
BlockResult<T>& operator*=(const BlockBase& in); //BlockResult<T>& operator*=(const BlockBase<T>& in);
BlockResult<T>& operator/=(const BlockBase& in); //BlockResult<T>& operator/=(const BlockBase<T>& in);
template <class OpFunction> //template <class OpFunction>
BlockResult<T>& operateSelf(const BlockBase& in); //BlockResult<T>& operateSelf(const BlockBase<T>& in);
protected: protected:
std::vector<T> mRes; std::vector<T> mRes;

View file

@ -160,7 +160,7 @@ namespace MultiArrayTools
{ {
return Operation<T,std::divides<T>,OperationClass,Second>(*mOc, in); return Operation<T,std::divides<T>,OperationClass,Second>(*mOc, in);
} }
/*
template <typename T, class OperationClass> template <typename T, class OperationClass>
template <class IndexType> template <class IndexType>
auto OperationTemplate<T,OperationClass>::c(std::shared_ptr<IndexType>& ind) const auto OperationTemplate<T,OperationClass>::c(std::shared_ptr<IndexType>& ind) const
@ -168,7 +168,7 @@ namespace MultiArrayTools
{ {
return Contraction<T,OperationClass,IndexType>(*mOc, ind); return Contraction<T,OperationClass,IndexType>(*mOc, ind);
} }
*/
/************************* /*************************
* OperationMaster * * OperationMaster *
@ -324,6 +324,7 @@ namespace MultiArrayTools
const BlockResult<T>& Operation<T,OpFunction,Ops...>::get() const const BlockResult<T>& Operation<T,OpFunction,Ops...>::get() const
{ {
mRes = std::move( PackNum<sizeof...(Ops)-1>::template unpackArgs<T,OpFunction>(mOps) ); mRes = std::move( PackNum<sizeof...(Ops)-1>::template unpackArgs<T,OpFunction>(mOps) );
//CHECK;
return mRes; return mRes;
} }
@ -347,15 +348,18 @@ namespace MultiArrayTools
*********************/ *********************/
template <typename T, class Op, class IndexType> template <typename T, class Op, class IndexType>
Contraction(const Op& op, std::shared_ptr<IndexType> ind) : Contraction<T,Op,IndexType>::Contraction(const Op& op, std::shared_ptr<IndexType> ind) :
OperationTemplate<T,Contraction<T,Op,IndexType> >(this), OperationTemplate<T,Contraction<T,Op,IndexType> >(this),
mOp(op) {} mOp(op) {}
const BlockResult<T>& get() const template <typename T, class Op, class IndexType>
const BlockResult<T>& Contraction<T,Op,IndexType>::get() const
{ {
// set mRes = 0 !!! BlockBinaryOpSelf<T,std::plus<T>,BlockResult<T> > f(mRes);
for(*mIndex = 0; mIndex->pos() != mIndex->max(); ++(*mIndex)){ mRes.assign( static_cast<T>(0) );
mRes += mOp.get(); for(*mInd = 0; mInd->pos() != mInd->max(); ++(*mInd)){
//mRes += mOp.get();
f(mOp.get());
} }
return mRes; return mRes;
} }
@ -364,12 +368,12 @@ namespace MultiArrayTools
std::vector<BTSS> Contraction<T,Op,IndexType>::block(const std::shared_ptr<IndexBase> blockIndex) const std::vector<BTSS> Contraction<T,Op,IndexType>::block(const std::shared_ptr<IndexBase> blockIndex) const
{ {
std::vector<BTSS> btv; std::vector<BTSS> btv;
PackNum<sizeof...(Ops)-1>::makeBlockTypeVec(btv, mOp, blockIndex); PackNum<0>::makeBlockTypeVec(btv, std::make_tuple( mOp ), blockIndex);
return btv; return btv;
} }
template <typename T, class Op, class IndexType> template <typename T, class Op, class IndexType>
const Contraction& block() const const Contraction<T,Op,IndexType>& Contraction<T,Op,IndexType>::block() const
{ {
return *this; return *this;
} }

View file

@ -108,11 +108,11 @@ namespace MultiArrayTools
template <class Second> template <class Second>
auto operator/(const Second& in) const auto operator/(const Second& in) const
-> Operation<T,std::divides<T>,OperationClass,Second>; -> Operation<T,std::divides<T>,OperationClass,Second>;
/*
template <class IndexType> template <class IndexType>
auto c(std::shared_ptr<IndexType>& ind) const auto c(std::shared_ptr<IndexType>& ind) const
-> Contraction<T,OperationClass,IndexType>; -> Contraction<T,OperationClass,IndexType>;
*/
private: private:
OperationClass* mOc; OperationClass* mOc;
}; };
@ -226,9 +226,9 @@ namespace MultiArrayTools
std::tuple<Ops...> mOps; std::tuple<Ops...> mOps;
mutable BlockResult<T> mRes; mutable BlockResult<T> mRes;
}; };
template <typename T, class Op, class IndexType> template <typename T, class Op, class IndexType>
class Contraction : public OperationTemplate<T,Contraction<T,Op> > class Contraction : public OperationTemplate<T,Contraction<T,Op,IndexType> >
{ {
public: public:

View file

@ -198,6 +198,7 @@ namespace {
std::cout << "ratio: " << static_cast<double>( end - begin ) / static_cast<double>( end2 - begin2 ) << std::endl; std::cout << "ratio: " << static_cast<double>( end - begin ) / static_cast<double>( end2 - begin2 ) << std::endl;
EXPECT_EQ( xround( res.at(mkt(7,9)) ), xround(res2[7*vs1 + 9]) ); EXPECT_EQ( xround( res.at(mkt(7,9)) ), xround(res2[7*vs1 + 9]) );
//EXPECT_EQ( xround( res.at(mkt(700,900)) ), xround(res2[700*vs1 + 900]) );
} }