Basic support for rowwise/colwise
This commit is contained in:
parent
ea7f3fcb0b
commit
3329065ddc
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>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue