Adapt generator static functions as free functions.

This commit is contained in:
Joel Falcou 2025-09-20 16:44:45 +02:00
parent fe68663ab9
commit 62802273e4
3 changed files with 223 additions and 87 deletions

View file

@ -282,7 +282,7 @@ namespace rotgen
matrix& setOnes()
{
*this = parent::Ones(Rows, Cols);
*this = parent::Ones(parent::rows(), parent::cols());
return *this;
}
@ -294,7 +294,7 @@ namespace rotgen
matrix& setZero()
{
*this = parent::Zero(Rows, Cols);
*this = parent::Zero(parent::rows(), parent::cols());
return *this;
}
@ -306,7 +306,7 @@ namespace rotgen
matrix& setConstant(Scalar value)
{
*this = parent::Constant(Rows, Cols, static_cast<Scalar>(value));
*this = parent::Constant(parent::rows(), parent::cols(), static_cast<Scalar>(value));
return *this;
}
@ -318,7 +318,7 @@ namespace rotgen
matrix& setRandom()
{
*this = parent::Random(Rows, Cols);
*this = parent::Random(parent::rows(), parent::cols());
return *this;
}
@ -330,7 +330,7 @@ namespace rotgen
matrix& setIdentity()
{
*this = parent::Identity(Rows, Cols);
*this = parent::Identity(parent::rows(), parent::cols());
return *this;
}

View file

@ -9,6 +9,9 @@
namespace rotgen
{
//-----------------------------------------------------------------------------------------------
// Infos & Shape
//-----------------------------------------------------------------------------------------------
std::size_t rows(concepts::entity auto const& arg) { return arg.rows(); }
std::size_t cols(concepts::entity auto const& arg) { return arg.cols(); }
std::size_t size(concepts::entity auto const& arg) { return arg.size(); }
@ -39,6 +42,9 @@ namespace rotgen
arg.conservativeResize(new_rows, new_cols);
}
//-----------------------------------------------------------------------------------------------
// Global operations
//-----------------------------------------------------------------------------------------------
decltype(auto) normalized(concepts::entity auto const& arg) { return arg.normalized(); }
decltype(auto) transpose (concepts::entity auto const& arg) { return arg.transpose(); }
decltype(auto) conjugate (concepts::entity auto const& arg) { return arg.conjugate(); }
@ -48,18 +54,17 @@ namespace rotgen
void transposeInPlace(concepts::entity auto& arg) { arg.transposeInPlace(); }
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
//-----------------------------------------------------------------------------------------------
// Component-wise functions
//-----------------------------------------------------------------------------------------------
auto abs (auto const& arg) requires(requires{arg.cwiseAbs();} ) { return arg.cwiseAbs(); }
auto abs2(auto const& arg) requires(requires{arg.cwiseAbs2();} ) { return arg.cwiseAbs2(); }
auto rec (auto const& arg) requires(requires{arg.cwiseInverse();}) { return arg.cwiseInverse(); }
auto sqrt(auto const& arg) requires(requires{arg.cwiseSqrt();} ) { return arg.cwiseSqrt(); }
//-----------------------------------------------------------------------------------------------
// Reductions
//-----------------------------------------------------------------------------------------------
auto trace(concepts::entity auto const& arg) { return arg.trace(); }
auto squaredNorm(concepts::entity auto const& arg) { return arg.squaredNorm(); }
auto norm(concepts::entity auto const& arg) { return arg.norm(); }
@ -94,6 +99,9 @@ namespace rotgen
return arg.template lpNorm<P>();
}
//-----------------------------------------------------------------------------------------------
// Expression handling
//-----------------------------------------------------------------------------------------------
template<typename T>
decltype(auto) noalias(T&& t) requires( requires{std::forward<T>(t).noalias();} )
{
@ -111,4 +119,127 @@ namespace rotgen
{
return std::forward<T>(t).eval();
}
//-----------------------------------------------------------------------------------------------
// Generators
//-----------------------------------------------------------------------------------------------
template<typename T>
auto setZero(T&& t) requires( requires{std::forward<T>(t).setZero();} )
{
return std::forward<T>(t).setZero();
}
template<typename T>
auto setZero() requires( requires{T::Zero();} )
{
return T::Zero();
}
template<typename T>
auto setZero(std::integral auto n) requires( requires{T::Zero(n);} )
{
return T::Zero(n);
}
template<typename T>
auto setZero(std::integral auto r,std::integral auto c) requires( requires{T::Zero(r,c);} )
{
return T::Zero(r,c);
}
template<typename T>
auto setOnes(T&& t) requires( requires{std::forward<T>(t).setOnes();} )
{
return std::forward<T>(t).setOnes();
}
template<typename T>
auto setOnes() requires( requires{T::Ones();} )
{
return T::Ones();
}
template<typename T>
auto setOnes(std::integral auto n) requires( requires{T::Ones(n);} )
{
return T::Ones(n);
}
template<typename T>
auto setOnes(std::integral auto r,std::integral auto c) requires( requires{T::Ones(r,c);} )
{
return T::Ones(r,c);
}
template<typename T>
auto setIdentity(T&& t) requires( requires{std::forward<T>(t).setIdentity();} )
{
return std::forward<T>(t).setIdentity();
}
template<typename T>
auto setIdentity() requires( requires{T::Identity();} )
{
return T::Identity();
}
template<typename T>
auto setIdentity(std::integral auto n) requires( requires{T::Identity(n);} )
{
return T::Identity(n);
}
template<typename T>
auto setIdentity(std::integral auto r,std::integral auto c) requires( requires{T::Identity(r,c);} )
{
return T::Identity(r,c);
}
template<typename T>
auto setRandom(T&& t) requires( requires{std::forward<T>(t).setRandom();} )
{
return std::forward<T>(t).setRandom();
}
template<typename T>
auto setRandom() requires( requires{T::Random();} )
{
return T::Random();
}
template<typename T>
auto setRandom(std::integral auto n) requires( requires{T::Random(n);} )
{
return T::Random(n);
}
template<typename T>
auto setRandom(std::integral auto r,std::integral auto c) requires( requires{T::Random(r,c);} )
{
return T::Random(r,c);
}
template<typename T>
auto setConstant(T&& t, auto v) requires( requires{std::forward<T>(t).setConstant(v);} )
{
return std::forward<T>(t).setConstant(v);
}
template<typename T>
auto setConstant(auto v) requires( requires{T::Constant(v);} )
{
return T::Constant(v);
}
template<typename T>
auto setConstant(std::integral auto n, auto v) requires( requires{T::Constant(n,v);} )
{
return T::Constant(n,v);
}
template<typename T>
auto setConstant(std::integral auto r,std::integral auto c, auto v) requires( requires{T::Constant(r,c,v);} )
{
return T::Constant(r,c,v);
}
}

View file

@ -38,99 +38,104 @@ void test_identity(const auto& matrix, std::size_t rows, std::size_t cols)
TTS_CASE_TPL("Test zero", rotgen::tests::types)
<typename T, typename O>( tts::type< tts::types<T,O>> )
{
test_value(rotgen::matrix<T, 3, 4, O::value>{}.setZero(), 3, 4, 0);
test_value(rotgen::matrix<T, 1, 1, O::value>{}.setZero(), 1, 1, 0);
test_value(rotgen::matrix<T, 10, 10, O::value>{}.setZero(), 10, 10, 0);
test_value(rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic, O::value>(1,1).setZero(3,4), 3, 4, 0);
test_value(rotgen::matrix<T, 7, 5, O::value>().setZero(7, 5), 7, 5, 0);
test_value(rotgen::matrix<T, 9,rotgen::Dynamic, O::value>(9,1).setZero(9, 3), 9, 3, 0);
test_value(rotgen::matrix<T,rotgen::Dynamic, 3, O::value>(1,3).setZero(2, 3), 2, 3, 0);
using namespace rotgen;
test_value(setZero<matrix<T, 3, 4, O::value> >(), 3, 4, 0);
test_value(setZero<matrix<T, 1, 1, O::value> >(), 1, 1, 0);
test_value(setZero<matrix<T, 10, 10, O::value> >(), 10, 10, 0);
test_value(setZero<matrix<T,Dynamic,Dynamic, O::value>>(3, 4), 3, 4, 0);
test_value(setZero<matrix<T, 7, 5, O::value> >(7, 5), 7, 5, 0);
test_value(setZero<matrix<T, 9,Dynamic> >(9, 3), 9, 3, 0);
test_value(setZero<matrix<T,Dynamic, 3> >(2, 3), 2, 3, 0);
test_value(rotgen::matrix<T, 3, 4, O::value>::Zero(), 3, 4, 0);
test_value(rotgen::matrix<T, 1, 1, O::value>::Zero(), 1, 1, 0);
test_value(rotgen::matrix<T, 10, 10, O::value>::Zero(), 10, 10, 0);
test_value(rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic, O::value>::Zero(3, 4), 3, 4, 0);
test_value(rotgen::matrix<T, 7, 5, O::value>::Zero(7, 5), 7, 5, 0);
test_value(rotgen::matrix<T, 9,rotgen::Dynamic, O::value>::Zero(9, 3), 9, 3, 0);
test_value(rotgen::matrix<T,rotgen::Dynamic, 3, O::value>::Zero(2, 3), 2, 3, 0);
test_value(setZero(matrix<T, 3, 4, O::value>{} ), 3, 4, 0);
test_value(setZero(matrix<T, 1, 1, O::value>{} ), 1, 1, 0);
test_value(setZero(matrix<T, 10, 10, O::value>{} ), 10, 10, 0);
test_value(setZero(matrix<T,Dynamic,Dynamic, O::value>{3, 4}), 3, 4, 0);
test_value(setZero(matrix<T, 7, 5, O::value>{7, 5} ), 7, 5, 0);
test_value(setZero(matrix<T, 9,Dynamic>{9, 3} ), 9, 3, 0);
test_value(setZero(matrix<T,Dynamic, 3>{2, 3} ), 2, 3, 0);
};
TTS_CASE_TPL("Test ones", rotgen::tests::types)
<typename T, typename O>( tts::type< tts::types<T,O>> )
{
test_value(rotgen::matrix<T, 3, 4, O::value>{}.setOnes(), 3, 4, 1);
test_value(rotgen::matrix<T, 1, 1, O::value>{}.setOnes(), 1, 1, 1);
test_value(rotgen::matrix<T, 10, 10, O::value>{}.setOnes(), 10, 10, 1);
test_value(rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic, O::value>(1,1).setOnes(3, 4), 3, 4, 1);
test_value(rotgen::matrix<T, 7, 5, O::value>{}.setOnes(7, 5), 7, 5, 1);
test_value(rotgen::matrix<T, 9,rotgen::Dynamic, O::value>(9,1).setOnes(9, 3), 9, 3, 1);
test_value(rotgen::matrix<T,rotgen::Dynamic, 3, O::value>(1,3).setOnes(2, 3), 2, 3, 1);
using namespace rotgen;
test_value(setOnes<matrix<T, 3, 4, O::value> >(), 3, 4, 1);
test_value(setOnes<matrix<T, 1, 1, O::value> >(), 1, 1, 1);
test_value(setOnes<matrix<T, 10, 10, O::value> >(), 10, 10, 1);
test_value(setOnes<matrix<T,Dynamic,Dynamic, O::value>>(3, 4), 3, 4, 1);
test_value(setOnes<matrix<T, 7, 5, O::value> >(7, 5), 7, 5, 1);
test_value(setOnes<matrix<T, 9,Dynamic> >(9, 3), 9, 3, 1);
test_value(setOnes<matrix<T,Dynamic, 3> >(2, 3), 2, 3, 1);
test_value(rotgen::matrix<T, 3, 4, O::value>::Ones(), 3, 4, 1);
test_value(rotgen::matrix<T, 1, 1, O::value>::Ones(), 1, 1, 1);
test_value(rotgen::matrix<T, 10, 10, O::value>::Ones(), 10, 10, 1);
test_value(rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic, O::value>::Ones(3, 4), 3, 4, 1);
test_value(rotgen::matrix<T, 7, 5, O::value>::Ones(7, 5), 7, 5, 1);
test_value(rotgen::matrix<T, 9,rotgen::Dynamic, O::value>::Ones(9, 3), 9, 3, 1);
test_value(rotgen::matrix<T,rotgen::Dynamic, 3, O::value>::Ones(2, 3), 2, 3, 1);
test_value(setOnes(matrix<T, 3, 4, O::value>{} ), 3, 4, 1);
test_value(setOnes(matrix<T, 1, 1, O::value>{} ), 1, 1, 1);
test_value(setOnes(matrix<T, 10, 10, O::value>{} ), 10, 10, 1);
test_value(setOnes(matrix<T,Dynamic,Dynamic, O::value>{3, 4}), 3, 4, 1);
test_value(setOnes(matrix<T, 7, 5, O::value>{7, 5} ), 7, 5, 1);
test_value(setOnes(matrix<T, 9,Dynamic>{9, 3} ), 9, 3, 1);
test_value(setOnes(matrix<T,Dynamic, 3>{2, 3} ), 2, 3, 1);
};
TTS_CASE_TPL("Test constant", rotgen::tests::types)
<typename T, typename O>( tts::type< tts::types<T,O>> )
{
test_value(rotgen::matrix<T, 3, 8, O::value>{}.setConstant(5.12), 3, 8, T(5.12));
test_value(rotgen::matrix<T, 1, 1, O::value>{}.setConstant(2.2), 1, 1, T(2.2));
test_value(rotgen::matrix<T, 11, 12, O::value>{}.setConstant(13), 11, 12, T(13));
test_value(rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic, O::value>(1,1).setConstant(2, 7, 5.6), 2, 7, T(5.6));
test_value(rotgen::matrix<T, 2, 2, O::value>{}.setConstant(2, 2, 2.0), 2, 2, T(2.0));
test_value(rotgen::matrix<T, 9,rotgen::Dynamic, O::value>(9,1).setConstant(9, 3, 1.1), 9, 3, T(1.1));
test_value(rotgen::matrix<T,rotgen::Dynamic, 9, O::value>(1,9).setConstant(5, 9, 42), 5, 9,T(42));
using namespace rotgen;
test_value(setConstant<matrix<T, 3, 4, O::value> >(T(5.12)), 3, 4, T(5.12));
test_value(setConstant<matrix<T, 1, 1, O::value> >(T(5.12)), 1, 1, T(5.12));
test_value(setConstant<matrix<T, 10, 10, O::value> >(T(5.12)), 10, 10, T(5.12));
test_value(setConstant<matrix<T,Dynamic,Dynamic, O::value>>(3, 4, T(5.12)), 3, 4, T(5.12));
test_value(setConstant<matrix<T, 7, 5, O::value> >(7, 5, T(5.12)), 7, 5, T(5.12));
test_value(setConstant<matrix<T, 9,Dynamic> >(9, 3, T(5.12)), 9, 3, T(5.12));
test_value(setConstant<matrix<T,Dynamic, 3> >(2, 3, T(5.12)), 2, 3, T(5.12));
test_value(rotgen::matrix<T, 3, 8, O::value>::Constant(5.12), 3, 8, T(5.12));
test_value(rotgen::matrix<T, 1, 1, O::value>::Constant(2.2), 1, 1, T(2.2));
test_value(rotgen::matrix<T, 11, 12, O::value>::Constant(13), 11, 12, T(13));
test_value(rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic, O::value>::Constant(2, 7, 5.6), 2, 7, T(5.6));
test_value(rotgen::matrix<T, 2, 2, O::value>::Constant(2, 2, 2.0), 2, 2, T(2.0));
test_value(rotgen::matrix<T, 9,rotgen::Dynamic, O::value>::Constant(9, 3, 1.1), 9, 3, T(1.1));
test_value(rotgen::matrix<T,rotgen::Dynamic, 9, O::value>::Constant(5, 9, 42), 5, 9,T(42));
test_value(setConstant(matrix<T, 3, 4, O::value>{} , T(5.12)), 3, 4, T(5.12));
test_value(setConstant(matrix<T, 1, 1, O::value>{} , T(5.12)), 1, 1, T(5.12));
test_value(setConstant(matrix<T, 10, 10, O::value>{} , T(5.12)), 10, 10, T(5.12));
test_value(setConstant(matrix<T,Dynamic,Dynamic, O::value>{3, 4}, T(5.12)), 3, 4, T(5.12));
test_value(setConstant(matrix<T, 7, 5, O::value>{7, 5} , T(5.12)), 7, 5, T(5.12));
test_value(setConstant(matrix<T, 9,Dynamic>{9, 3} , T(5.12)), 9, 3, T(5.12));
test_value(setConstant(matrix<T,Dynamic, 3>{2, 3} , T(5.12)), 2, 3, T(5.12));
};
TTS_CASE_TPL("Test random", rotgen::tests::types)
<typename T, typename O>( tts::type< tts::types<T,O>> )
{
test_random(rotgen::matrix<T, 2, 3, O::value>{}.setRandom(), 2, 3);
test_random(rotgen::matrix<T, 1, 1, O::value>{}.setRandom(), 1, 1);
test_random(rotgen::matrix<T, 11, 17, O::value>{}.setRandom(), 11, 17);
test_random(rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic, O::value>{1,1}.setRandom(7, 3), 7, 3);
test_random(rotgen::matrix<T, 2, 2, O::value>{}.setRandom(2, 2), 2, 2);
test_random(rotgen::matrix<T, 4,rotgen::Dynamic, O::value>{4,1}.setRandom(4, 3), 4, 3);
test_random(rotgen::matrix<T,rotgen::Dynamic, 5, O::value>{1,5}.setRandom(5, 5), 5, 5);
using namespace rotgen;
test_random(setRandom<matrix<T, 3, 4, O::value> >(), 3, 4);
test_random(setRandom<matrix<T, 1, 1, O::value> >(), 1, 1);
test_random(setRandom<matrix<T, 10, 10, O::value> >(), 10, 10);
test_random(setRandom<matrix<T,Dynamic,Dynamic, O::value>>(3, 4), 3, 4);
test_random(setRandom<matrix<T, 7, 5, O::value> >(7, 5), 7, 5);
test_random(setRandom<matrix<T, 9,Dynamic> >(9, 3), 9, 3);
test_random(setRandom<matrix<T,Dynamic, 3> >(2, 3), 2, 3);
test_random(rotgen::matrix<T, 2, 3, O::value>::Random(), 2, 3);
test_random(rotgen::matrix<T, 1, 1, O::value>::Random(), 1, 1);
test_random(rotgen::matrix<T, 11, 17, O::value>::Random(), 11, 17);
test_random(rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic, O::value>::Random(7, 3), 7, 3);
test_random(rotgen::matrix<T, 2, 2, O::value>::Random(2, 2), 2, 2);
test_random(rotgen::matrix<T, 4,rotgen::Dynamic, O::value>::Random(4, 3), 4, 3);
test_random(rotgen::matrix<T,rotgen::Dynamic, 5, O::value>::Random(5, 5), 5, 5);
test_random(setRandom(matrix<T, 3, 4, O::value>{} ), 3, 4);
test_random(setRandom(matrix<T, 1, 1, O::value>{} ), 1, 1);
test_random(setRandom(matrix<T, 10, 10, O::value>{} ), 10, 10);
test_random(setRandom(matrix<T,Dynamic,Dynamic, O::value>{3, 4}), 3, 4);
test_random(setRandom(matrix<T, 7, 5, O::value>{7, 5} ), 7, 5);
test_random(setRandom(matrix<T, 9,Dynamic>{9, 3} ), 9, 3);
test_random(setRandom(matrix<T,Dynamic, 3>{2, 3} ), 2, 3);
};
TTS_CASE_TPL("Test identity", rotgen::tests::types)
<typename T, typename O>( tts::type< tts::types<T,O>> )
{
test_identity(rotgen::matrix<T, 4, 5, O::value>{}.setIdentity(), 4, 5);
test_identity(rotgen::matrix<T, 1, 1, O::value>{}.setIdentity(), 1, 1);
test_identity(rotgen::matrix<T, 21, 3, O::value>{}.setIdentity(), 21, 3);
test_identity(rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic, O::value>{1,1}.setIdentity(2, 7), 2, 7);
test_identity(rotgen::matrix<T, 2, 2, O::value>{}.setIdentity(2, 2), 2, 2);
test_identity(rotgen::matrix<T, 3,rotgen::Dynamic, O::value>{3,1}.setIdentity(3, 3), 3, 3);
test_identity(rotgen::matrix<T,rotgen::Dynamic, 11, O::value>{1,11}.setIdentity(5, 11), 5, 11);
using namespace rotgen;
test_identity(setIdentity<matrix<T, 3, 4, O::value> >(), 3, 4);
test_identity(setIdentity<matrix<T, 1, 1, O::value> >(), 1, 1);
test_identity(setIdentity<matrix<T, 10, 10, O::value> >(), 10, 10);
test_identity(setIdentity<matrix<T,Dynamic,Dynamic, O::value>>(3, 4), 3, 4);
test_identity(setIdentity<matrix<T, 7, 5, O::value> >(7, 5), 7, 5);
test_identity(setIdentity<matrix<T, 9,Dynamic> >(9, 3), 9, 3);
test_identity(setIdentity<matrix<T,Dynamic, 3> >(2, 3), 2, 3);
test_identity(rotgen::matrix<T, 4, 5, O::value>::Identity(), 4, 5);
test_identity(rotgen::matrix<T, 1, 1, O::value>::Identity(), 1, 1);
test_identity(rotgen::matrix<T, 21, 3, O::value>::Identity(), 21, 3);
test_identity(rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic, O::value>::Identity(2, 7), 2, 7);
test_identity(rotgen::matrix<T, 2, 2, O::value>::Identity(2, 2), 2, 2);
test_identity(rotgen::matrix<T, 3,rotgen::Dynamic, O::value>::Identity(3, 3), 3, 3);
test_identity(rotgen::matrix<T,rotgen::Dynamic, 11, O::value>::Identity(5, 11), 5, 11);
test_identity(setIdentity(matrix<T, 3, 4, O::value>{} ), 3, 4);
test_identity(setIdentity(matrix<T, 1, 1, O::value>{} ), 1, 1);
test_identity(setIdentity(matrix<T, 10, 10, O::value>{} ), 10, 10);
test_identity(setIdentity(matrix<T,Dynamic,Dynamic, O::value>{3, 4}), 3, 4);
test_identity(setIdentity(matrix<T, 7, 5, O::value>{7, 5} ), 7, 5);
test_identity(setIdentity(matrix<T, 9,Dynamic>{9, 3} ), 9, 3);
test_identity(setIdentity(matrix<T,Dynamic, 3>{2, 3} ), 2, 3);
};