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:
parent
a76020e274
commit
8647639c0d
24 changed files with 1176 additions and 1111 deletions
49
src/matrix.cpp
Normal file
49
src/matrix.cpp
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
//==================================================================================================
|
||||
/*
|
||||
ROTGEN - Runtime Overlay for Eigen
|
||||
Copyright : CODE RECKONS
|
||||
SPDX-License-Identifier: BSL-1.0
|
||||
*/
|
||||
//==================================================================================================
|
||||
#include <rotgen/detail/generators.hpp>
|
||||
#include <rotgen/impl/matrix.hpp>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
namespace rotgen
|
||||
{
|
||||
#define SIZE 64
|
||||
#define TYPE double
|
||||
#define STORAGE_ORDER Eigen::ColMajor
|
||||
|
||||
#define MATRIX ROTGEN_MATRIX_NAME(matrix_impl,SIZE,_col)
|
||||
#include "matrix_model.cpp"
|
||||
#undef MATRIX
|
||||
#undef STORAGE_ORDER
|
||||
|
||||
#define STORAGE_ORDER Eigen::RowMajor
|
||||
#define MATRIX ROTGEN_MATRIX_NAME(matrix_impl,SIZE,_row)
|
||||
#include "matrix_model.cpp"
|
||||
#undef MATRIX
|
||||
#undef STORAGE_ORDER
|
||||
|
||||
#undef SIZE
|
||||
#undef TYPE
|
||||
|
||||
#define SIZE 32
|
||||
#define TYPE float
|
||||
#define STORAGE_ORDER Eigen::ColMajor
|
||||
|
||||
#define MATRIX ROTGEN_MATRIX_NAME(matrix_impl,SIZE,_col)
|
||||
#include "matrix_model.cpp"
|
||||
#undef MATRIX
|
||||
#undef STORAGE_ORDER
|
||||
|
||||
#define STORAGE_ORDER Eigen::RowMajor
|
||||
#define MATRIX ROTGEN_MATRIX_NAME(matrix_impl,SIZE,_row)
|
||||
#include "matrix_model.cpp"
|
||||
#undef MATRIX
|
||||
#undef STORAGE_ORDER
|
||||
|
||||
#undef SIZE
|
||||
#undef TYPE
|
||||
}
|
||||
|
|
@ -1,219 +0,0 @@
|
|||
//==================================================================================================
|
||||
/*
|
||||
ROTGEN - Runtime Overlay for Eigen
|
||||
Copyright : CODE RECKONS
|
||||
SPDX-License-Identifier: BSL-1.0
|
||||
*/
|
||||
//==================================================================================================
|
||||
#include <rotgen/matrix.hpp>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
namespace rotgen
|
||||
{
|
||||
//================================================================================================
|
||||
// Internal paylod
|
||||
//================================================================================================
|
||||
struct matrix_impl64::payload
|
||||
{
|
||||
using data_type = Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic>;
|
||||
|
||||
data_type data;
|
||||
payload(std::size_t r=0, std::size_t c=0) : data(r, c) {}
|
||||
payload(std::initializer_list<std::initializer_list<double>> init) : data(init) {}
|
||||
payload(data_type&& matrix) : data(std::move(matrix)) {}
|
||||
};
|
||||
|
||||
//==================================================================================================
|
||||
// Constructors & Special Members
|
||||
//==================================================================================================
|
||||
matrix_impl64::matrix_impl64(std::size_t r, std::size_t c) : storage_(std::make_unique<payload>(r,c)) {}
|
||||
|
||||
matrix_impl64::matrix_impl64(std::initializer_list<std::initializer_list<double>> init)
|
||||
: storage_(std::make_unique<payload>(init))
|
||||
{}
|
||||
|
||||
matrix_impl64::matrix_impl64(std::size_t r, std::size_t c,std::initializer_list<double> init)
|
||||
: matrix_impl64(r,c)
|
||||
{
|
||||
auto first = init.begin();
|
||||
for(std::size_t i=0; i < init.size(); i++)
|
||||
(*this)(i) = first[i];
|
||||
}
|
||||
|
||||
matrix_impl64::matrix_impl64(matrix_impl64 const& o)
|
||||
: matrix_impl64(o.rows(),o.cols())
|
||||
{
|
||||
storage_->data = o.storage_->data;
|
||||
}
|
||||
|
||||
matrix_impl64::matrix_impl64(matrix_impl64&&) noexcept = default;
|
||||
|
||||
|
||||
matrix_impl64& matrix_impl64::operator=(matrix_impl64 const& o)
|
||||
{
|
||||
if (this != &o) storage_->data = o.storage_->data;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
matrix_impl64& matrix_impl64::operator=(matrix_impl64&&) noexcept = default;
|
||||
|
||||
matrix_impl64::~matrix_impl64() = default;
|
||||
|
||||
//==================================================================================================
|
||||
// Matrix API
|
||||
//==================================================================================================
|
||||
std::size_t matrix_impl64::rows() const { return static_cast<std::size_t>(storage_->data.rows()); }
|
||||
std::size_t matrix_impl64::cols() const { return static_cast<std::size_t>(storage_->data.cols()); }
|
||||
|
||||
std::size_t matrix_impl64::size() const { return static_cast<std::size_t>(storage_->data.size()); }
|
||||
void matrix_impl64::resize(std::size_t new_rows, std::size_t new_cols) { storage_->data.resize(new_rows, new_cols); }
|
||||
void matrix_impl64::conservativeResize(std::size_t new_rows, std::size_t new_cols) { storage_->data.conservativeResize(new_rows, new_cols); }
|
||||
|
||||
double& matrix_impl64::operator()(std::size_t i, std::size_t j) { return storage_->data(i,j); }
|
||||
double const& matrix_impl64::operator()(std::size_t i, std::size_t j) const { return storage_->data(i,j); }
|
||||
|
||||
double& matrix_impl64::operator()(std::size_t index) { return storage_->data(index); }
|
||||
double const& matrix_impl64::operator()(std::size_t index) const { return storage_->data(index); }
|
||||
|
||||
const double* matrix_impl64::data() const { return storage_->data.data(); }
|
||||
|
||||
matrix_impl64 matrix_impl64::transpose() const
|
||||
{
|
||||
matrix_impl64 result(*this);
|
||||
result.storage_->data.transposeInPlace();
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
matrix_impl64 matrix_impl64::conjugate() const
|
||||
{
|
||||
matrix_impl64 result(*this);
|
||||
result.storage_->data = storage_->data.conjugate();
|
||||
return result;
|
||||
}
|
||||
|
||||
matrix_impl64 matrix_impl64::adjoint() const
|
||||
{
|
||||
matrix_impl64 result(*this);
|
||||
result.storage_->data.adjointInPlace();
|
||||
return result;
|
||||
}
|
||||
|
||||
void matrix_impl64::transposeInPlace()
|
||||
{
|
||||
storage_->data.transposeInPlace();
|
||||
}
|
||||
|
||||
void matrix_impl64::adjointInPlace()
|
||||
{
|
||||
storage_->data.adjointInPlace();
|
||||
}
|
||||
|
||||
double matrix_impl64::sum() const { return storage_->data.sum(); }
|
||||
double matrix_impl64::prod() const { return storage_->data.prod(); }
|
||||
double matrix_impl64::mean() const { return storage_->data.mean(); }
|
||||
|
||||
double matrix_impl64::minCoeff() const { return storage_->data.minCoeff(); }
|
||||
double matrix_impl64::minCoeff(std::ptrdiff_t* row, std::ptrdiff_t* col) const { return storage_->data.minCoeff(row, col); }
|
||||
|
||||
double matrix_impl64::maxCoeff() const { return storage_->data.maxCoeff(); }
|
||||
double matrix_impl64::maxCoeff(std::ptrdiff_t* row, std::ptrdiff_t* col) const { return storage_->data.maxCoeff(row, col); }
|
||||
|
||||
double matrix_impl64::trace() const { return storage_->data.trace(); }
|
||||
|
||||
double matrix_impl64::squaredNorm() const { return storage_->data.squaredNorm(); }
|
||||
double matrix_impl64::norm() const { return storage_->data.norm(); }
|
||||
|
||||
double matrix_impl64::lpNorm(int p) const {
|
||||
if (p == 1)
|
||||
return storage_->data.lpNorm<1>();
|
||||
else if (p == 2)
|
||||
return storage_->data.lpNorm<2>();
|
||||
else
|
||||
return storage_->data.lpNorm<Eigen::Infinity>();
|
||||
}
|
||||
|
||||
//==================================================================================================
|
||||
// Operators
|
||||
//==================================================================================================
|
||||
std::ostream& operator<<(std::ostream& os,matrix_impl64 const& m)
|
||||
{
|
||||
return os << m.storage_->data;
|
||||
}
|
||||
|
||||
bool operator==(matrix_impl64 const& lhs, matrix_impl64 const& rhs)
|
||||
{
|
||||
return lhs.storage_->data == rhs.storage_->data;
|
||||
}
|
||||
|
||||
matrix_impl64& matrix_impl64::operator+=(matrix_impl64 const& rhs)
|
||||
{
|
||||
storage_->data += rhs.storage_->data;
|
||||
return *this;
|
||||
}
|
||||
|
||||
matrix_impl64& matrix_impl64::operator-=(matrix_impl64 const& rhs)
|
||||
{
|
||||
storage_->data -= rhs.storage_->data;
|
||||
return *this;
|
||||
}
|
||||
|
||||
matrix_impl64 matrix_impl64::operator-() const
|
||||
{
|
||||
matrix_impl64 result(*this);
|
||||
result.storage_->data = -result.storage_->data;
|
||||
return result;
|
||||
}
|
||||
|
||||
matrix_impl64& matrix_impl64::operator*=(matrix_impl64 const& rhs)
|
||||
{
|
||||
storage_->data *= rhs.storage_->data;
|
||||
return *this;
|
||||
}
|
||||
|
||||
matrix_impl64& matrix_impl64::operator*=(double s)
|
||||
{
|
||||
storage_->data *= s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
matrix_impl64& matrix_impl64::operator/=(double s)
|
||||
{
|
||||
storage_->data /= s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
//==================================================================================================
|
||||
//==================================================================================================
|
||||
// Static functions
|
||||
//==================================================================================================
|
||||
|
||||
matrix_impl64 matrix_impl64::Zero(std::size_t rows, std::size_t cols) {
|
||||
matrix_impl64 m;
|
||||
m.storage_ = std::make_unique<payload>(payload::data_type::Zero(rows, cols));
|
||||
return m;
|
||||
}
|
||||
|
||||
matrix_impl64 matrix_impl64::Constant(std::size_t rows, std::size_t cols, double value)
|
||||
{
|
||||
matrix_impl64 m;
|
||||
m.storage_ = std::make_unique<payload>(payload::data_type::Constant(rows, cols, value));
|
||||
return m;
|
||||
}
|
||||
|
||||
matrix_impl64 matrix_impl64::Random(std::size_t rows, std::size_t cols)
|
||||
{
|
||||
matrix_impl64 m;
|
||||
m.storage_ = std::make_unique<payload>(payload::data_type::Random(rows, cols));
|
||||
return m;
|
||||
}
|
||||
|
||||
matrix_impl64 matrix_impl64::Identity(std::size_t rows, std::size_t cols)
|
||||
{
|
||||
matrix_impl64 m;
|
||||
m.storage_ = std::make_unique<payload>(payload::data_type::Identity(rows, cols));
|
||||
return m;
|
||||
}
|
||||
|
||||
}
|
||||
222
src/matrix_model.cpp
Normal file
222
src/matrix_model.cpp
Normal file
|
|
@ -0,0 +1,222 @@
|
|||
//==================================================================================================
|
||||
/*
|
||||
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
|
||||
*/
|
||||
//==================================================================================================
|
||||
|
||||
//==================================================================================================
|
||||
// Internal payload
|
||||
//==================================================================================================
|
||||
struct MATRIX::payload
|
||||
{
|
||||
using data_type = Eigen::Matrix<TYPE,Eigen::Dynamic,Eigen::Dynamic,STORAGE_ORDER>;
|
||||
|
||||
data_type data;
|
||||
payload(std::size_t r=0, std::size_t c=0) : data(r, c) {}
|
||||
payload(std::initializer_list<std::initializer_list<TYPE>> init) : data(init) {}
|
||||
payload(data_type&& matrix) : data(std::move(matrix)) {}
|
||||
};
|
||||
|
||||
//==================================================================================================
|
||||
// Constructors & Special Members
|
||||
//==================================================================================================
|
||||
MATRIX::MATRIX(std::size_t r, std::size_t c) : storage_(std::make_unique<payload>(r,c)) {}
|
||||
|
||||
MATRIX::MATRIX(std::initializer_list<std::initializer_list<TYPE>> init)
|
||||
: storage_(std::make_unique<payload>(init))
|
||||
{}
|
||||
|
||||
MATRIX::MATRIX(std::size_t r, std::size_t c,std::initializer_list<TYPE> init)
|
||||
: MATRIX(r,c)
|
||||
{
|
||||
auto first = init.begin();
|
||||
for(std::size_t i=0; i < init.size(); i++)
|
||||
(*this)(i) = first[i];
|
||||
}
|
||||
|
||||
MATRIX::MATRIX(MATRIX const& o)
|
||||
: MATRIX(o.rows(),o.cols())
|
||||
{
|
||||
storage_->data = o.storage_->data;
|
||||
}
|
||||
|
||||
MATRIX::MATRIX(MATRIX&&) noexcept = default;
|
||||
|
||||
|
||||
MATRIX& MATRIX::operator=(MATRIX const& o)
|
||||
{
|
||||
if (this != &o) storage_->data = o.storage_->data;
|
||||
return *this;
|
||||
}
|
||||
|
||||
MATRIX& MATRIX::operator=(MATRIX&&) noexcept = default;
|
||||
|
||||
MATRIX::~MATRIX() = default;
|
||||
|
||||
//==================================================================================================
|
||||
// Matrix API
|
||||
//==================================================================================================
|
||||
std::size_t MATRIX::rows() const { return static_cast<std::size_t>(storage_->data.rows()); }
|
||||
std::size_t MATRIX::cols() const { return static_cast<std::size_t>(storage_->data.cols()); }
|
||||
std::size_t MATRIX::size() const { return static_cast<std::size_t>(storage_->data.size()); }
|
||||
|
||||
void MATRIX::resize(std::size_t new_rows, std::size_t new_cols)
|
||||
{
|
||||
storage_->data.resize(new_rows, new_cols);
|
||||
}
|
||||
|
||||
void MATRIX::conservativeResize(std::size_t new_rows, std::size_t new_cols)
|
||||
{
|
||||
storage_->data.conservativeResize(new_rows, new_cols);
|
||||
}
|
||||
|
||||
TYPE& MATRIX::operator()(std::size_t i, std::size_t j) { return storage_->data(i,j); }
|
||||
TYPE const& MATRIX::operator()(std::size_t i, std::size_t j) const { return storage_->data(i,j); }
|
||||
|
||||
TYPE& MATRIX::operator()(std::size_t index) { return storage_->data(index); }
|
||||
TYPE const& MATRIX::operator()(std::size_t index) const { return storage_->data(index); }
|
||||
|
||||
const TYPE* MATRIX::data() const { return storage_->data.data(); }
|
||||
|
||||
MATRIX MATRIX::transpose() const
|
||||
{
|
||||
MATRIX result(*this);
|
||||
result.storage_->data.transposeInPlace();
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
MATRIX MATRIX::conjugate() const
|
||||
{
|
||||
MATRIX result(*this);
|
||||
result.storage_->data = storage_->data.conjugate();
|
||||
return result;
|
||||
}
|
||||
|
||||
MATRIX MATRIX::adjoint() const
|
||||
{
|
||||
MATRIX result(*this);
|
||||
result.storage_->data.adjointInPlace();
|
||||
return result;
|
||||
}
|
||||
|
||||
void MATRIX::transposeInPlace()
|
||||
{
|
||||
storage_->data.transposeInPlace();
|
||||
}
|
||||
|
||||
void MATRIX::adjointInPlace()
|
||||
{
|
||||
storage_->data.adjointInPlace();
|
||||
}
|
||||
|
||||
TYPE MATRIX::sum() const { return storage_->data.sum(); }
|
||||
TYPE MATRIX::prod() const { return storage_->data.prod(); }
|
||||
TYPE MATRIX::mean() const { return storage_->data.mean(); }
|
||||
TYPE MATRIX::trace() const { return storage_->data.trace(); }
|
||||
|
||||
TYPE MATRIX::minCoeff() const { return storage_->data.minCoeff(); }
|
||||
TYPE MATRIX::maxCoeff() const { return storage_->data.maxCoeff(); }
|
||||
|
||||
TYPE MATRIX::minCoeff(std::ptrdiff_t* row, std::ptrdiff_t* col) const { return storage_->data.minCoeff(row, col); }
|
||||
TYPE MATRIX::maxCoeff(std::ptrdiff_t* row, std::ptrdiff_t* col) const { return storage_->data.maxCoeff(row, col); }
|
||||
|
||||
TYPE MATRIX::squaredNorm() const { return storage_->data.squaredNorm(); }
|
||||
TYPE MATRIX::norm() const { return storage_->data.norm(); }
|
||||
|
||||
TYPE MATRIX::lpNorm(int p) const
|
||||
{
|
||||
if (p == 1) return storage_->data.lpNorm<1>();
|
||||
else if (p == 2) return storage_->data.lpNorm<2>();
|
||||
else return storage_->data.lpNorm<Eigen::Infinity>();
|
||||
}
|
||||
|
||||
//==================================================================================================
|
||||
// Operators
|
||||
//==================================================================================================
|
||||
std::ostream& operator<<(std::ostream& os,MATRIX const& m)
|
||||
{
|
||||
return os << m.storage_->data;
|
||||
}
|
||||
|
||||
bool operator==(MATRIX const& lhs, MATRIX const& rhs)
|
||||
{
|
||||
return lhs.storage_->data == rhs.storage_->data;
|
||||
}
|
||||
|
||||
MATRIX& MATRIX::operator+=(MATRIX const& rhs)
|
||||
{
|
||||
storage_->data += rhs.storage_->data;
|
||||
return *this;
|
||||
}
|
||||
|
||||
MATRIX& MATRIX::operator-=(MATRIX const& rhs)
|
||||
{
|
||||
storage_->data -= rhs.storage_->data;
|
||||
return *this;
|
||||
}
|
||||
|
||||
MATRIX MATRIX::operator-() const
|
||||
{
|
||||
MATRIX result(*this);
|
||||
result.storage_->data = -result.storage_->data;
|
||||
return result;
|
||||
}
|
||||
|
||||
MATRIX& MATRIX::operator*=(MATRIX const& rhs)
|
||||
{
|
||||
storage_->data *= rhs.storage_->data;
|
||||
return *this;
|
||||
}
|
||||
|
||||
MATRIX& MATRIX::operator*=(TYPE s)
|
||||
{
|
||||
storage_->data *= s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
MATRIX& MATRIX::operator/=(TYPE s)
|
||||
{
|
||||
storage_->data /= s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
//==================================================================================================
|
||||
// Static functions
|
||||
//==================================================================================================
|
||||
|
||||
MATRIX MATRIX::Zero(std::size_t rows, std::size_t cols)
|
||||
{
|
||||
MATRIX m;
|
||||
m.storage_ = std::make_unique<payload>(payload::data_type::Zero(rows, cols));
|
||||
return m;
|
||||
}
|
||||
|
||||
MATRIX MATRIX::Constant(std::size_t rows, std::size_t cols, TYPE value)
|
||||
{
|
||||
MATRIX m;
|
||||
m.storage_ = std::make_unique<payload>(payload::data_type::Constant(rows, cols, value));
|
||||
return m;
|
||||
}
|
||||
|
||||
MATRIX MATRIX::Random(std::size_t rows, std::size_t cols)
|
||||
{
|
||||
MATRIX m;
|
||||
m.storage_ = std::make_unique<payload>(payload::data_type::Random(rows, cols));
|
||||
return m;
|
||||
}
|
||||
|
||||
MATRIX MATRIX::Identity(std::size_t rows, std::size_t cols)
|
||||
{
|
||||
MATRIX m;
|
||||
m.storage_ = std::make_unique<payload>(payload::data_type::Identity(rows, cols));
|
||||
return m;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue