Basic support for rowwise/colwise
This commit is contained in:
commit
f285251a52
3 changed files with 214 additions and 0 deletions
159
include/rotgen/common/reshaper.hpp
Normal file
159
include/rotgen/common/reshaper.hpp
Normal file
|
|
@ -0,0 +1,159 @@
|
|||
//==================================================================================================
|
||||
/*
|
||||
ROTGEN - Runtime Overlay for Eigen
|
||||
Copyright : CODE RECKONS
|
||||
SPDX-License-Identifier: BSL-1.0
|
||||
*/
|
||||
//==================================================================================================
|
||||
#pragma once
|
||||
|
||||
namespace rotgen
|
||||
{
|
||||
template<typename Ref> struct rowwise_adaptor
|
||||
{
|
||||
using concrete_type = typename std::remove_cvref_t<Ref>::concrete_type;
|
||||
Ref& target_;
|
||||
|
||||
concrete_type sum() const
|
||||
{
|
||||
concrete_type res(target_.rows(),1);
|
||||
apply([&](auto r, auto i){ res(i) = r.sum(); });
|
||||
return res;
|
||||
}
|
||||
|
||||
concrete_type mean() const
|
||||
{
|
||||
concrete_type res(target_.rows(),1);
|
||||
apply([&](auto r, auto i){ res(i) = r.mean(); });
|
||||
return res;
|
||||
}
|
||||
|
||||
concrete_type prod() const
|
||||
{
|
||||
concrete_type res(target_.rows(),1);
|
||||
apply([&](auto r, auto i){ res(i) = r.prod(); });
|
||||
return res;
|
||||
}
|
||||
|
||||
concrete_type maxCoeff() const
|
||||
{
|
||||
concrete_type res(target_.rows(),1);
|
||||
apply([&](auto r, auto i){ res(i) = r.maxCoeff(); });
|
||||
return res;
|
||||
}
|
||||
|
||||
concrete_type minCoeff() const
|
||||
{
|
||||
concrete_type res(target_.rows(),1);
|
||||
apply([&](auto r, auto i){ res(i) = r.minCoeff(); });
|
||||
return res;
|
||||
}
|
||||
|
||||
concrete_type squaredNorm() const
|
||||
{
|
||||
concrete_type res(target_.rows(),1);
|
||||
apply([&](auto r, auto i){ res(i) = r.squaredNorm(); });
|
||||
return res;
|
||||
}
|
||||
|
||||
concrete_type norm() const
|
||||
{
|
||||
concrete_type res(target_.rows(),1);
|
||||
apply([&](auto r, auto i){ res(i) = r.norm(); });
|
||||
return res;
|
||||
}
|
||||
|
||||
private:
|
||||
template<typename Func> void apply(Func f)
|
||||
{
|
||||
for(Index i = 0; i < target_.rows(); ++i)
|
||||
f(row(target_,i), i);
|
||||
}
|
||||
|
||||
template<typename Func> void apply(Func f) const
|
||||
{
|
||||
for(Index i = 0; i < target_.rows(); ++i)
|
||||
f(row(target_,i), i);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Ref> struct colwise_adaptor
|
||||
{
|
||||
using concrete_type = typename std::remove_cvref_t<Ref>::concrete_type;
|
||||
Ref& target_;
|
||||
|
||||
concrete_type sum() const
|
||||
{
|
||||
concrete_type res(1, target_.cols());
|
||||
apply([&](auto r, auto i){ res(i) = r.sum(); });
|
||||
return res;
|
||||
}
|
||||
|
||||
concrete_type mean() const
|
||||
{
|
||||
concrete_type res(1, target_.cols());
|
||||
apply([&](auto r, auto i){ res(i) = r.mean(); });
|
||||
return res;
|
||||
}
|
||||
|
||||
concrete_type prod() const
|
||||
{
|
||||
concrete_type res(1, target_.cols());
|
||||
apply([&](auto r, auto i){ res(i) = r.prod(); });
|
||||
return res;
|
||||
}
|
||||
|
||||
concrete_type maxCoeff() const
|
||||
{
|
||||
concrete_type res(1, target_.cols());
|
||||
apply([&](auto r, auto i){ res(i) = r.maxCoeff(); });
|
||||
return res;
|
||||
}
|
||||
|
||||
concrete_type minCoeff() const
|
||||
{
|
||||
concrete_type res(1, target_.cols());
|
||||
apply([&](auto r, auto i){ res(i) = r.minCoeff(); });
|
||||
return res;
|
||||
}
|
||||
|
||||
concrete_type squaredNorm() const
|
||||
{
|
||||
concrete_type res(1, target_.cols());
|
||||
apply([&](auto r, auto i){ res(i) = r.squaredNorm(); });
|
||||
return res;
|
||||
}
|
||||
|
||||
concrete_type norm() const
|
||||
{
|
||||
concrete_type res(1, target_.cols());
|
||||
apply([&](auto r, auto i){ res(i) = r.norm(); });
|
||||
return res;
|
||||
}
|
||||
|
||||
private:
|
||||
template<typename Func> void apply(Func f)
|
||||
{
|
||||
for(Index i = 0; i < target_.cols(); ++i)
|
||||
f(col(target_,i), i);
|
||||
}
|
||||
|
||||
template<typename Func> void apply(Func f) const
|
||||
{
|
||||
for(Index i = 0; i < target_.cols(); ++i)
|
||||
f(col(target_,i), i);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T> auto rowwise(T&& t)
|
||||
{
|
||||
if constexpr(use_expression_templates) return t.base().rowwise();
|
||||
else return rowwise_adaptor<T>{t};
|
||||
}
|
||||
|
||||
template<typename T> auto colwise(T&& t)
|
||||
{
|
||||
if constexpr(use_expression_templates) return t.base().colwise();
|
||||
else return colwise_adaptor<T>{t};
|
||||
}
|
||||
}
|
||||
|
|
@ -26,5 +26,6 @@
|
|||
#include <rotgen/extract.hpp>
|
||||
#include <rotgen/functions.hpp>
|
||||
#include <rotgen/operators.hpp>
|
||||
#include <rotgen/common/reshaper.hpp>
|
||||
#include <rotgen/solver.hpp>
|
||||
#include <rotgen/alias.hpp>
|
||||
|
|
|
|||
54
test/unit/functions/rowwise.cpp
Normal file
54
test/unit/functions/rowwise.cpp
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
//==================================================================================================
|
||||
/*
|
||||
ROTGEN - Runtime Overlay for Eigen
|
||||
Copyright : CODE RECKONS
|
||||
SPDX-License-Identifier: BSL-1.0
|
||||
*/
|
||||
//==================================================================================================
|
||||
#include "unit/tests.hpp"
|
||||
#include <rotgen/rotgen.hpp>
|
||||
|
||||
TTS_CASE_TPL("rowwise API", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
using e_t = Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic,O::value>;
|
||||
e_t ref = e_t::Random(4,4);
|
||||
auto ref_rw = ref.rowwise();
|
||||
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> mat(4,4);
|
||||
rotgen::tests::prepare([&](auto r, auto c) { return ref(r,c); }, mat);
|
||||
|
||||
auto rw = rotgen::rowwise(mat);
|
||||
|
||||
for(rotgen::Index i=0;i<mat.rows();++i) TTS_EQUAL(rw.sum()(i) , ref_rw.sum()(i) );
|
||||
for(rotgen::Index i=0;i<mat.rows();++i) TTS_EQUAL(rw.mean()(i) , ref_rw.mean()(i) );
|
||||
for(rotgen::Index i=0;i<mat.rows();++i) TTS_EQUAL(rw.prod()(i) , ref_rw.prod()(i) );
|
||||
for(rotgen::Index i=0;i<mat.rows();++i) TTS_EQUAL(rw.maxCoeff()(i) , ref_rw.maxCoeff()(i) );
|
||||
for(rotgen::Index i=0;i<mat.rows();++i) TTS_EQUAL(rw.minCoeff()(i) , ref_rw.minCoeff()(i) );
|
||||
for(rotgen::Index i=0;i<mat.rows();++i) TTS_EQUAL(rw.squaredNorm()(i) , ref_rw.squaredNorm()(i) );
|
||||
for(rotgen::Index i=0;i<mat.rows();++i) TTS_EQUAL(rw.norm()(i) , ref_rw.norm()(i) );
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("colwise API", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
using e_t = Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic,O::value>;
|
||||
e_t ref = e_t::Random(4,4);
|
||||
auto ref_rw = ref.colwise();
|
||||
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> mat(4,4);
|
||||
rotgen::tests::prepare([&](auto r, auto c) { return ref(r,c); }, mat);
|
||||
|
||||
auto rw = rotgen::colwise(mat);
|
||||
|
||||
for(rotgen::Index i=0;i<mat.cols();++i) TTS_EQUAL(rw.sum()(i) , ref_rw.sum()(i) );
|
||||
for(rotgen::Index i=0;i<mat.cols();++i) TTS_EQUAL(rw.mean()(i) , ref_rw.mean()(i) );
|
||||
for(rotgen::Index i=0;i<mat.cols();++i) TTS_EQUAL(rw.prod()(i) , ref_rw.prod()(i) );
|
||||
for(rotgen::Index i=0;i<mat.cols();++i) TTS_EQUAL(rw.maxCoeff()(i) , ref_rw.maxCoeff()(i) );
|
||||
for(rotgen::Index i=0;i<mat.cols();++i) TTS_EQUAL(rw.minCoeff()(i) , ref_rw.minCoeff()(i) );
|
||||
for(rotgen::Index i=0;i<mat.cols();++i) TTS_EQUAL(rw.squaredNorm()(i) , ref_rw.squaredNorm()(i) );
|
||||
for(rotgen::Index i=0;i<mat.cols();++i) TTS_EQUAL(rw.norm()(i) , ref_rw.norm()(i) );
|
||||
};
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue