Merge branch 'feat/x-files' into 'main'

Use X-macros to generate all combinations of supported Eigen Matrix types

Co-authored-by: Joel Falcou <joel.falcou@lri.fr>

See merge request oss/rotgen!8
This commit is contained in:
Joel Falcou 2025-05-26 14:49:20 +02:00
parent a76020e274
commit 8647639c0d
24 changed files with 1176 additions and 1111 deletions

View file

@ -0,0 +1,12 @@
//==================================================================================================
/*
ROTGEN - Runtime Overlay for Eigen
Copyright : CODE RECKONS
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#pragma once
#define ROTGEN_CAT(a, ...) ROTGEN_PRIMITIVE_CAT(a, __VA_ARGS__)
#define ROTGEN_PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__
#define ROTGEN_MATRIX_NAME(BASE,ID,SUFFIX) ROTGEN_CAT(BASE,ROTGEN_CAT(ID,SUFFIX))

View file

@ -0,0 +1,27 @@
//==================================================================================================
/*
ROTGEN - Runtime Overlay for Eigen
Copyright : CODE RECKONS
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#pragma once
namespace rotgen
{
struct matrix_static_info
{
int rows = -1;
int cols = -1;
int options = 0;
int max_rows = -1;
int max_cols = -1;
};
inline constexpr int Dynamic = -1;
inline constexpr int Infinity = -1;
inline constexpr int AutoAlign = 0;
inline constexpr int ColMajor = 0;
inline constexpr int RowMajor = 1;
inline constexpr int DotnAlgin = 2;
}

View file

@ -0,0 +1,55 @@
//==================================================================================================
/*
ROTGEN - Runtime Overlay for Eigen
Copyright : CODE RECKONS
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#pragma once
#include <rotgen/detail/generators.hpp>
#include <rotgen/detail/static_info.hpp>
#include <initializer_list>
#include <cstddef>
#include <memory>
namespace rotgen
{
#define SIZE 64
#define TYPE double
#define MATRIX ROTGEN_MATRIX_NAME(matrix_impl,SIZE,_col)
#include <rotgen/impl/matrix_model.hpp>
#undef MATRIX
#define MATRIX ROTGEN_MATRIX_NAME(matrix_impl,SIZE,_row)
#include <rotgen/impl/matrix_model.hpp>
#undef MATRIX
#undef SIZE
#undef TYPE
#define SIZE 32
#define TYPE float
#define MATRIX ROTGEN_MATRIX_NAME(matrix_impl,SIZE,_col)
#include <rotgen/impl/matrix_model.hpp>
#undef MATRIX
#define MATRIX ROTGEN_MATRIX_NAME(matrix_impl,SIZE,_row)
#include <rotgen/impl/matrix_model.hpp>
#undef MATRIX
#undef SIZE
#undef TYPE
template<typename Scalar,int Options> struct find_matrix_impl;
template<> struct find_matrix_impl<float , ColMajor> { using type = matrix_impl32_col; };
template<> struct find_matrix_impl<float , RowMajor> { using type = matrix_impl32_row; };
template<> struct find_matrix_impl<double, ColMajor> { using type = matrix_impl64_col; };
template<> struct find_matrix_impl<double, RowMajor> { using type = matrix_impl64_row; };
template<typename Scalar,int Options>
using find_matrix = typename find_matrix_impl<Scalar,(Options & 1)>::type;
}

View file

@ -1,86 +0,0 @@
//==================================================================================================
/*
ROTGEN - Runtime Overlay for Eigen
Copyright : CODE RECKONS
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#pragma once
#include <memory>
#include <cstddef>
#include <initializer_list>
namespace rotgen
{
class matrix_impl64
{
public:
matrix_impl64(std::size_t rows = 0, std::size_t cols = 0);
matrix_impl64(std::size_t rows, std::size_t cols,std::initializer_list<double> init);
matrix_impl64(std::initializer_list<std::initializer_list<double>> init);
matrix_impl64(matrix_impl64 const& other);
matrix_impl64(matrix_impl64&&) noexcept;
matrix_impl64& operator=(matrix_impl64 const& other);
matrix_impl64& operator=(matrix_impl64&&) noexcept;
~matrix_impl64();
std::size_t rows() const;
std::size_t cols() const;
std::size_t size() const;
void resize(std::size_t new_rows, std::size_t new_cols);
void conservativeResize(std::size_t new_rows, std::size_t new_cols);
matrix_impl64 transpose() const;
matrix_impl64 conjugate() const;
matrix_impl64 adjoint() const;
void transposeInPlace();
void adjointInPlace();
double sum() const;
double prod() const;
double mean() const;
double maxCoeff() const;
double maxCoeff(std::ptrdiff_t* row, std::ptrdiff_t* col) const;
double minCoeff() const;
double minCoeff(std::ptrdiff_t* row, std::ptrdiff_t* col) const;
double trace() const;
double squaredNorm() const;
double norm() const;
double lpNorm(int p) const;
double& operator()(std::size_t i, std::size_t j);
double const& operator()(std::size_t i, std::size_t j) const;
double& operator()(std::size_t index);
double const& operator()(std::size_t index) const;
matrix_impl64& operator+=(matrix_impl64 const& rhs);
matrix_impl64& operator-=(matrix_impl64 const& rhs);
matrix_impl64 operator-() const;
matrix_impl64& operator*=(matrix_impl64 const& rhs);
matrix_impl64& operator*=(double d);
matrix_impl64& operator/=(double d);
friend std::ostream& operator<<(std::ostream&,matrix_impl64 const&);
friend bool operator==(matrix_impl64 const& lhs, matrix_impl64 const& rhs);
const double* data() const;
static matrix_impl64 Zero(std::size_t rows, std::size_t cols);
static matrix_impl64 Constant(std::size_t rows, std::size_t cols, double value);
static matrix_impl64 Random(std::size_t rows, std::size_t cols);
static matrix_impl64 Identity(std::size_t rows, std::size_t cols);
private:
struct payload;
std::unique_ptr<payload> storage_;
};
}

View file

@ -0,0 +1,83 @@
//==================================================================================================
/*
ROTGEN - Runtime Overlay for Eigen
Copyright : CODE RECKONS
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
//==================================================================================================
/*
This file is a X-File to generate various matrix_impl_* definitions variant
*/
//==================================================================================================
class MATRIX
{
public:
MATRIX(std::size_t rows = 0, std::size_t cols = 0);
MATRIX(std::size_t rows, std::size_t cols,std::initializer_list<TYPE> init);
MATRIX(std::initializer_list<std::initializer_list<TYPE>> init);
MATRIX(MATRIX const& other);
MATRIX(MATRIX&&) noexcept;
MATRIX& operator=(MATRIX const& other);
MATRIX& operator=(MATRIX&&) noexcept;
~MATRIX();
std::size_t rows() const;
std::size_t cols() const;
std::size_t size() const;
void resize(std::size_t new_rows, std::size_t new_cols);
void conservativeResize(std::size_t new_rows, std::size_t new_cols);
MATRIX transpose() const;
MATRIX conjugate() const;
MATRIX adjoint() const;
void transposeInPlace();
void adjointInPlace();
TYPE sum() const;
TYPE prod() const;
TYPE mean() const;
TYPE trace() const;
TYPE maxCoeff() const;
TYPE minCoeff() const;
TYPE maxCoeff(std::ptrdiff_t* row, std::ptrdiff_t* col) const;
TYPE minCoeff(std::ptrdiff_t* row, std::ptrdiff_t* col) const;
TYPE squaredNorm() const;
TYPE norm() const;
TYPE lpNorm(int p) const;
TYPE& operator()(std::size_t i, std::size_t j);
TYPE const& operator()(std::size_t i, std::size_t j) const;
TYPE& operator()(std::size_t index);
TYPE const& operator()(std::size_t index) const;
MATRIX& operator+=(MATRIX const& rhs);
MATRIX& operator-=(MATRIX const& rhs);
MATRIX operator-() const;
MATRIX& operator*=(MATRIX const& rhs);
MATRIX& operator*=(TYPE d);
MATRIX& operator/=(TYPE d);
friend std::ostream& operator<<(std::ostream&,MATRIX const&);
friend bool operator==(MATRIX const& lhs, MATRIX const& rhs);
const TYPE* data() const;
static MATRIX Zero(std::size_t rows, std::size_t cols);
static MATRIX Constant(std::size_t rows, std::size_t cols, TYPE value);
static MATRIX Random(std::size_t rows, std::size_t cols);
static MATRIX Identity(std::size_t rows, std::size_t cols);
private:
struct payload;
std::unique_ptr<payload> storage_;
};

View file

@ -7,28 +7,30 @@
//==================================================================================================
#pragma once
#include <rotgen/impl/matrix_impl64.hpp>
#include <rotgen/impl/matrix.hpp>
#include <initializer_list>
#include <cassert>
namespace rotgen
{
inline constexpr int Infinity = -1;
template< typename Scalar, int Rows = -1 , int Cols = -1
, int Options = 0, int MaxRows = Rows, int MaxCols = Cols
template< typename Scalar
, int Rows = Dynamic , int Cols = Dynamic
, int Options = ColMajor
, int MaxRows = Rows , int MaxCols = Cols
>
class matrix : public matrix_impl64
class matrix : public find_matrix<Scalar,Options>
{
using parent = matrix_impl64;
using parent = find_matrix<Scalar,Options>;
public:
using value_type = Scalar;
matrix() : parent(Rows==-1?0:Rows,Cols==-1?0:Cols) {}
matrix(int r, int c) : parent(r, c)
{
if constexpr(Rows != -1) assert(r == Rows && "Mismatched between dynamic and static row size");
if constexpr(Cols != -1) assert(c == Cols && "Mismatched between dynamic and static column size");
if constexpr(Rows != -1) assert(r == Rows && "Mismatched between dynamic and static row size");
if constexpr(Cols != -1) assert(c == Cols && "Mismatched between dynamic and static column size");
}
matrix(parent const& base) : parent(base) {}
@ -50,14 +52,12 @@ namespace rotgen
: parent(Rows, Cols, {s0,static_cast<Scalar>(init)...})
{}
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);
}
void conservativeResize(int new_rows, int new_cols)
requires(Rows == -1 && Cols == -1)
void conservativeResize(int new_rows, int new_cols) requires(Rows == -1 && Cols == -1)
{
parent::conservativeResize(new_rows, new_cols);
}
@ -78,8 +78,7 @@ namespace rotgen
}
void transposeInPlace() { parent::transposeInPlace(); }
void adjointInPlace() { parent::adjointInPlace(); }
void adjointInPlace() { parent::adjointInPlace(); }
friend bool operator==(matrix const& lhs, matrix const& rhs)
{
@ -109,20 +108,19 @@ namespace rotgen
return *this;
}
matrix& operator*=(double rhs)
matrix& operator*=(Scalar rhs)
{
static_cast<parent&>(*this) *= rhs;
return *this;
}
matrix& operator/=(double rhs)
matrix& operator/=(Scalar rhs)
{
static_cast<parent&>(*this) /= rhs;
return *this;
}
static matrix Zero()
requires (Rows != -1 && Cols != -1)
static matrix Zero() requires (Rows != -1 && Cols != -1)
{
return parent::Zero(Rows, Cols);
}
@ -134,21 +132,19 @@ namespace rotgen
return parent::Zero(rows, cols);
}
static matrix Constant(Scalar value)
requires (Rows != -1 && Cols != -1)
static matrix Constant(Scalar value) requires (Rows != -1 && Cols != -1)
{
return parent::Constant(Rows, Cols, static_cast<double>(value));
return parent::Constant(Rows, Cols, static_cast<Scalar>(value));
}
static matrix Constant(int rows, int cols, Scalar value)
{
if constexpr(Rows != -1) assert(rows == Rows && "Mismatched between dynamic and static row size");
if constexpr(Cols != -1) assert(cols == Cols && "Mismatched between dynamic and static column size");
return parent::Constant(rows, cols, static_cast<double>(value));
return parent::Constant(rows, cols, static_cast<Scalar>(value));
}
static matrix Random()
requires (Rows != -1 && Cols != -1)
static matrix Random() requires (Rows != -1 && Cols != -1)
{
return parent::Random(Rows, Cols);
}
@ -160,8 +156,7 @@ namespace rotgen
return parent::Random(rows, cols);
}
static matrix Identity()
requires (Rows != -1 && Cols != -1)
static matrix Identity() requires (Rows != -1 && Cols != -1)
{
return parent::Identity(Rows, Cols);
}
@ -174,7 +169,8 @@ namespace rotgen
}
template<int P>
double lpNorm() const {
Scalar lpNorm() const
{
assert(P == 1 || P == 2 || P == Infinity);
return parent::lpNorm(P);
}
@ -203,20 +199,20 @@ namespace rotgen
}
template<typename S, int R, int C, int O, int MR, int MC>
matrix<S,R,C,O,MR,MC> operator*(matrix<S,R,C,O,MR,MC> const& lhs, double rhs)
matrix<S,R,C,O,MR,MC> operator*(matrix<S,R,C,O,MR,MC> const& lhs, S rhs)
{
matrix<S,R,C,O,MR,MC> that(lhs);
return that *= rhs;
}
template<typename S, int R, int C, int O, int MR, int MC>
matrix<S,R,C,O,MR,MC> operator*(double lhs, matrix<S,R,C,O,MR,MC> const& rhs)
matrix<S,R,C,O,MR,MC> operator*(S lhs, matrix<S,R,C,O,MR,MC> const& rhs)
{
return rhs * lhs;
}
template<typename S, int R, int C, int O, int MR, int MC>
matrix<S,R,C,O,MR,MC> operator/(matrix<S,R,C,O,MR,MC> const& lhs, double rhs)
matrix<S,R,C,O,MR,MC> operator/(matrix<S,R,C,O,MR,MC> const& lhs, S rhs)
{
matrix<S,R,C,O,MR,MC> that(lhs);
return that /= rhs;