Co-authored-by: Jules Pénuchot <jules@penuchot.com> Co-authored-by: Joel FALCOU <jfalcou@codereckons.com> See merge request oss/rotgen!41
155 lines
3.8 KiB
C++
155 lines
3.8 KiB
C++
//==================================================================================================
|
|
/*
|
|
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};
|
|
}
|
|
}
|