Implement unary cwiseXXX() member functions + associated free functions

See merge request oss/rotgen!17
This commit is contained in:
Joel Falcou 2025-09-03 11:50:00 +02:00
parent a2e7718a48
commit 0a3abbb58b
15 changed files with 207 additions and 6 deletions

View file

@ -36,6 +36,10 @@ namespace rotgen
using parent::prod; using parent::prod;
using parent::mean; using parent::mean;
using parent::trace; using parent::trace;
using parent::cwiseAbs;
using parent::cwiseAbs2;
using parent::cwiseInverse;
using parent::cwiseSqrt;
using parent::maxCoeff; using parent::maxCoeff;
using parent::minCoeff; using parent::minCoeff;
using parent::norm; using parent::norm;
@ -104,6 +108,10 @@ namespace rotgen
using parent::prod; using parent::prod;
using parent::mean; using parent::mean;
using parent::trace; using parent::trace;
using parent::cwiseAbs;
using parent::cwiseAbs2;
using parent::cwiseInverse;
using parent::cwiseSqrt;
using parent::maxCoeff; using parent::maxCoeff;
using parent::minCoeff; using parent::minCoeff;
using parent::norm; using parent::norm;

View file

@ -132,8 +132,12 @@ namespace rotgen
return concrete_type(static_cast<parent const&>(*this).adjoint()); return concrete_type(static_cast<parent const&>(*this).adjoint());
} }
void transposeInPlace() requires(!is_immutable) { parent::transposeInPlace(); } concrete_type cwiseAbs() const { return concrete_type(base().cwiseAbs()); }
concrete_type cwiseAbs2() const { return concrete_type(base().cwiseAbs2()); }
concrete_type cwiseInverse() const { return concrete_type(base().cwiseInverse()); }
concrete_type cwiseSqrt() const { return concrete_type(base().cwiseSqrt()); }
void transposeInPlace() requires(!is_immutable) { parent::transposeInPlace(); }
void adjointInPlace() requires(!is_immutable) { parent::adjointInPlace(); } void adjointInPlace() requires(!is_immutable) { parent::adjointInPlace(); }
friend bool operator==(block const& lhs, block const& rhs) friend bool operator==(block const& lhs, block const& rhs)

View file

@ -105,6 +105,11 @@ namespace rotgen
return concrete_type(static_cast<parent const &>(*this).adjoint()); return concrete_type(static_cast<parent const &>(*this).adjoint());
} }
concrete_type cwiseAbs() const { return concrete_type(base().cwiseAbs()); }
concrete_type cwiseAbs2() const { return concrete_type(base().cwiseAbs2()); }
concrete_type cwiseInverse() const { return concrete_type(base().cwiseInverse()); }
concrete_type cwiseSqrt() const { return concrete_type(base().cwiseSqrt()); }
void transposeInPlace() requires(!is_immutable) { parent::transposeInPlace(); } void transposeInPlace() requires(!is_immutable) { parent::transposeInPlace(); }
void adjointInPlace() requires(!is_immutable) { parent::adjointInPlace(); } void adjointInPlace() requires(!is_immutable) { parent::adjointInPlace(); }

View file

@ -98,6 +98,11 @@ namespace rotgen
void transposeInPlace() { parent::transposeInPlace(); } void transposeInPlace() { parent::transposeInPlace(); }
void adjointInPlace() { parent::adjointInPlace(); } void adjointInPlace() { parent::adjointInPlace(); }
matrix cwiseAbs() const { return matrix(base().cwiseAbs()); }
matrix cwiseAbs2() const { return matrix(base().cwiseAbs2()); }
matrix cwiseInverse() const { return matrix(base().cwiseInverse()); }
matrix cwiseSqrt() const { return matrix(base().cwiseSqrt()); }
friend bool operator==(matrix const& lhs, matrix const& rhs) friend bool operator==(matrix const& lhs, matrix const& rhs)
{ {
return static_cast<parent const&>(lhs) == static_cast<parent const&>(rhs); return static_cast<parent const&>(lhs) == static_cast<parent const&>(rhs);

View file

@ -140,6 +140,30 @@ namespace rotgen
void transposeInPlace() requires(!is_immutable) { parent::transposeInPlace(); } void transposeInPlace() requires(!is_immutable) { parent::transposeInPlace(); }
void adjointInPlace() requires(!is_immutable) { parent::adjointInPlace(); } void adjointInPlace() requires(!is_immutable) { parent::adjointInPlace(); }
auto cwiseAbs() const
{
if constexpr(!use_expression_templates) return concrete_type{parent::cwiseAbs()};
else return base().cwiseAbs();
}
auto cwiseAbs2() const
{
if constexpr(!use_expression_templates) return concrete_type{parent::cwiseAbs2()};
else return base().cwiseAbs2();
}
auto cwiseInverse() const
{
if constexpr(!use_expression_templates) return concrete_type{parent::cwiseInverse()};
else return base().cwiseInverse();
}
auto cwiseSqrt() const
{
if constexpr(!use_expression_templates) return concrete_type{parent::cwiseSqrt()};
else return base().cwiseSqrt();
}
static concrete_type Constant(value_type value) requires (Rows != -1 && Cols != -1) static concrete_type Constant(value_type value) requires (Rows != -1 && Cols != -1)
{ {
return parent::Constant(Rows, Cols, static_cast<value_type>(value)); return parent::Constant(Rows, Cols, static_cast<value_type>(value));

View file

@ -121,6 +121,30 @@ namespace rotgen
return *this; return *this;
} }
auto cwiseAbs() const
{
if constexpr(!use_expression_templates) return concrete_type{parent::cwiseAbs()};
else return base().cwiseAbs();
}
auto cwiseAbs2() const
{
if constexpr(!use_expression_templates) return concrete_type{parent::cwiseAbs2()};
else return base().cwiseAbs2();
}
auto cwiseInverse() const
{
if constexpr(!use_expression_templates) return concrete_type{parent::cwiseInverse()};
else return base().cwiseInverse();
}
auto cwiseSqrt() const
{
if constexpr(!use_expression_templates) return concrete_type{parent::cwiseSqrt()};
else return base().cwiseSqrt();
}
auto transpose() const auto transpose() const
{ {
if constexpr(use_expression_templates) return base().transpose(); if constexpr(use_expression_templates) return base().transpose();

View file

@ -139,6 +139,30 @@ namespace rotgen
void transposeInPlace() { parent::transposeInPlace(); } void transposeInPlace() { parent::transposeInPlace(); }
void adjointInPlace() { parent::adjointInPlace(); } void adjointInPlace() { parent::adjointInPlace(); }
auto cwiseAbs() const
{
if constexpr(!use_expression_templates) return matrix{parent::cwiseAbs()};
else return base().cwiseAbs();
}
auto cwiseAbs2() const
{
if constexpr(!use_expression_templates) return matrix{parent::cwiseAbs2()};
else return base().cwiseAbs2();
}
auto cwiseInverse() const
{
if constexpr(!use_expression_templates) return matrix{parent::cwiseInverse()};
else return base().cwiseInverse();
}
auto cwiseSqrt() const
{
if constexpr(!use_expression_templates) return matrix{parent::cwiseSqrt()};
else return base().cwiseSqrt();
}
void resize(int new_rows, int new_cols) requires(Rows == -1 && Cols == -1) void resize(int new_rows, int new_cols) requires(Rows == -1 && Cols == -1)
{ {
parent::resize(new_rows, new_cols); parent::resize(new_rows, new_cols);

View file

@ -34,6 +34,18 @@ namespace rotgen
void transposeInPlace(concepts::entity auto& arg) { arg.transposeInPlace(); } void transposeInPlace(concepts::entity auto& arg) { arg.transposeInPlace(); }
void adjointInPlace(concepts::entity auto& arg) { arg.adjointInPlace(); } void adjointInPlace(concepts::entity auto& arg) { arg.adjointInPlace(); }
auto abs(concepts::entity auto const& arg) { return arg.cwiseAbs(); }
auto abs2(concepts::entity auto const& arg) { return arg.cwiseAbs2(); }
auto rec(concepts::entity auto const& arg) { return arg.cwiseInverse(); }
auto sqrt(concepts::entity auto const& arg) { return arg.cwiseSqrt(); }
#if defined(ROTGEN_ENABLE_EXPRESSION_TEMPLATES)
decltype(auto) abs(concepts::eigen_compatible auto const& arg) { return arg.cwiseAbs(); }
decltype(auto) abs2(concepts::eigen_compatible auto const& arg) { return arg.cwiseAbs2(); }
decltype(auto) rec(concepts::eigen_compatible auto const& arg) { return arg.cwiseInverse(); }
decltype(auto) sqrt(concepts::eigen_compatible auto const& arg) { return arg.cwiseSqrt(); }
#endif
auto trace(concepts::entity auto const& arg) { return arg.trace(); } auto trace(concepts::entity auto const& arg) { return arg.trace(); }
auto squaredNorm(concepts::entity auto const& arg) { return arg.squaredNorm(); } auto squaredNorm(concepts::entity auto const& arg) { return arg.squaredNorm(); }
auto norm(concepts::entity auto const& arg) { return arg.norm(); } auto norm(concepts::entity auto const& arg) { return arg.norm(); }

View file

@ -50,6 +50,11 @@ class ROTGEN_EXPORT CLASSNAME
SOURCENAME conjugate() const; SOURCENAME conjugate() const;
SOURCENAME adjoint() const; SOURCENAME adjoint() const;
SOURCENAME cwiseAbs() const;
SOURCENAME cwiseAbs2() const;
SOURCENAME cwiseInverse() const;
SOURCENAME cwiseSqrt() const;
#if !defined(USE_CONST) #if !defined(USE_CONST)
void transposeInPlace(); void transposeInPlace();
void adjointInPlace(); void adjointInPlace();

View file

@ -40,6 +40,11 @@ class ROTGEN_EXPORT CLASSNAME
SOURCENAME conjugate() const; SOURCENAME conjugate() const;
SOURCENAME adjoint() const; SOURCENAME adjoint() const;
SOURCENAME cwiseAbs() const;
SOURCENAME cwiseAbs2() const;
SOURCENAME cwiseInverse() const;
SOURCENAME cwiseSqrt() const;
#if !defined(USE_CONST) #if !defined(USE_CONST)
void transposeInPlace(); void transposeInPlace();
void adjointInPlace(); void adjointInPlace();

View file

@ -38,6 +38,11 @@ class ROTGEN_EXPORT CLASSNAME
CLASSNAME conjugate() const; CLASSNAME conjugate() const;
CLASSNAME adjoint() const; CLASSNAME adjoint() const;
CLASSNAME cwiseAbs() const;
CLASSNAME cwiseAbs2() const;
CLASSNAME cwiseInverse() const;
CLASSNAME cwiseSqrt() const;
void transposeInPlace(); void transposeInPlace();
void adjointInPlace(); void adjointInPlace();

View file

@ -239,6 +239,34 @@ struct CLASSNAME::payload
return result; return result;
} }
SOURCENAME CLASSNAME::cwiseAbs() const
{
SOURCENAME result;
storage_->apply([&](const auto& blk) { result.storage()->assign(blk.cwiseAbs().eval()); });
return result;
}
SOURCENAME CLASSNAME::cwiseAbs2() const
{
SOURCENAME result;
storage_->apply([&](const auto& blk) { result.storage()->assign(blk.cwiseAbs2().eval()); });
return result;
}
SOURCENAME CLASSNAME::cwiseInverse() const
{
SOURCENAME result;
storage_->apply([&](const auto& blk) { result.storage()->assign(blk.cwiseInverse().eval()); });
return result;
}
SOURCENAME CLASSNAME::cwiseSqrt() const
{
SOURCENAME result;
storage_->apply([&](const auto& blk) { result.storage()->assign(blk.cwiseSqrt().eval()); });
return result;
}
SOURCENAME CLASSNAME::adjoint() const SOURCENAME CLASSNAME::adjoint() const
{ {
SOURCENAME result; SOURCENAME result;

View file

@ -82,6 +82,34 @@
return result; return result;
} }
SOURCENAME CLASSNAME::cwiseAbs() const
{
SOURCENAME result;
result.storage()->assign(storage_->data.cwiseAbs().eval());
return result;
}
SOURCENAME CLASSNAME::cwiseAbs2() const
{
SOURCENAME result;
result.storage()->assign(storage_->data.cwiseAbs2().eval());
return result;
}
SOURCENAME CLASSNAME::cwiseInverse() const
{
SOURCENAME result;
result.storage()->assign(storage_->data.cwiseInverse().eval());
return result;
}
SOURCENAME CLASSNAME::cwiseSqrt() const
{
SOURCENAME result;
result.storage()->assign(storage_->data.cwiseSqrt().eval());
return result;
}
#if !defined(USE_CONST) #if !defined(USE_CONST)
void CLASSNAME::transposeInPlace() void CLASSNAME::transposeInPlace()
{ {

View file

@ -106,6 +106,34 @@ void CLASSNAME::adjointInPlace()
storage_->data.adjointInPlace(); storage_->data.adjointInPlace();
} }
CLASSNAME CLASSNAME::cwiseAbs() const
{
CLASSNAME result(*this);
result.storage_->data = storage_->data.cwiseAbs();
return result;
}
CLASSNAME CLASSNAME::cwiseAbs2() const
{
CLASSNAME result(*this);
result.storage_->data = storage_->data.cwiseAbs2();
return result;
}
CLASSNAME CLASSNAME::cwiseInverse() const
{
CLASSNAME result(*this);
result.storage_->data = storage_->data.cwiseInverse();
return result;
}
CLASSNAME CLASSNAME::cwiseSqrt() const
{
CLASSNAME result(*this);
result.storage_->data = storage_->data.cwiseSqrt();
return result;
}
TYPE CLASSNAME::sum() const { return storage_->data.sum(); } TYPE CLASSNAME::sum() const { return storage_->data.sum(); }
TYPE CLASSNAME::prod() const { return storage_->data.prod(); } TYPE CLASSNAME::prod() const { return storage_->data.prod(); }
TYPE CLASSNAME::mean() const { return storage_->data.mean(); } TYPE CLASSNAME::mean() const { return storage_->data.mean(); }

View file

@ -82,7 +82,3 @@ TTS_CASE("Matrix size-related operations")
TTS_EXPECT_NOT_COMPILES(a, { rotgen::resize(a, 4, 5); }); TTS_EXPECT_NOT_COMPILES(a, { rotgen::resize(a, 4, 5); });
TTS_EXPECT_NOT_COMPILES(a, { rotgen::conservativeResize(a, 4, 5); }); TTS_EXPECT_NOT_COMPILES(a, { rotgen::conservativeResize(a, 4, 5); });
}; };