Implement fixed size options for rotgen containers
See merge request oss/rotgen!11
This commit is contained in:
parent
8e545dd51a
commit
2084874b1b
39 changed files with 1247 additions and 323 deletions
246
include/rotgen/fixed/matrix.hpp
Normal file
246
include/rotgen/fixed/matrix.hpp
Normal file
|
|
@ -0,0 +1,246 @@
|
|||
//==================================================================================================
|
||||
/*
|
||||
ROTGEN - Runtime Overlay for Eigen
|
||||
Copyright : CODE RECKONS
|
||||
SPDX-License-Identifier: BSL-1.0
|
||||
*/
|
||||
//==================================================================================================
|
||||
#pragma once
|
||||
|
||||
#include <rotgen/detail/static_info.hpp>
|
||||
#include <rotgen/fixed/common.hpp>
|
||||
#include <Eigen/Dense>
|
||||
#include <iostream>
|
||||
|
||||
namespace rotgen
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
template<typename Scalar, int Rows, int Cols, int Opts,int MaxRows, int MaxCols>
|
||||
using storage_type = std::conditional_t < storage_status<Rows,Cols>
|
||||
, Eigen::Matrix<Scalar, Rows, Cols, Opts, MaxRows, MaxCols>
|
||||
, Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Opts>
|
||||
>;
|
||||
}
|
||||
|
||||
template< typename Scalar
|
||||
, int Rows = Dynamic , int Cols = Dynamic
|
||||
, int Options = ColMajor
|
||||
, int MaxRows = Rows , int MaxCols = Cols
|
||||
>
|
||||
class matrix : private detail::storage_type<Scalar, Rows, Cols, Options, MaxRows, MaxCols>
|
||||
{
|
||||
public:
|
||||
using rotgen_tag = void;
|
||||
using parent = detail::storage_type<Scalar, Rows, Cols, Options, MaxRows, MaxCols>;
|
||||
using value_type = Scalar;
|
||||
using scalar_type = Scalar;
|
||||
using Index = typename parent::Index;
|
||||
|
||||
static constexpr auto storage_order = Options & 1;
|
||||
using concrete_type = matrix;
|
||||
using concrete_dynamic_type = matrix<scalar_type>;
|
||||
|
||||
template<typename ET>
|
||||
using as_concrete_type = as_concrete_t<ET, matrix>;
|
||||
|
||||
private:
|
||||
static constexpr bool has_static_storage = storage_status<Rows,Cols>;
|
||||
|
||||
public:
|
||||
|
||||
matrix() requires(has_static_storage) {}
|
||||
matrix() requires(!has_static_storage) : parent(Rows > 0 ? Rows : 0, Cols > 0 ? Cols : 0){}
|
||||
matrix(Index r, Index c) requires(!has_static_storage) : parent(r, c) {}
|
||||
|
||||
matrix(const matrix& other) = default;
|
||||
matrix(matrix&& other) = default;
|
||||
matrix& operator=(const matrix&) = default;
|
||||
matrix& operator=(matrix&&) = default;
|
||||
|
||||
|
||||
matrix(std::initializer_list<std::initializer_list<Scalar>> init) : parent(init)
|
||||
{}
|
||||
|
||||
template<std::convertible_to<Scalar>... S>
|
||||
requires((Rows == 1 && Cols == (1+sizeof...(S))) || (Cols == 1 && Rows == (1+sizeof...(S))))
|
||||
matrix(Scalar s0,S... init)
|
||||
: parent ( [&]()
|
||||
{
|
||||
if constexpr(has_static_storage)
|
||||
return parent{static_cast<Scalar>(s0),static_cast<Scalar>(init)...};
|
||||
else return parent{Rows,Cols};
|
||||
}()
|
||||
)
|
||||
{
|
||||
if constexpr(!has_static_storage)
|
||||
{
|
||||
auto raw = {static_cast<Scalar>(s0),static_cast<Scalar>(init)...};
|
||||
auto first = raw.begin();
|
||||
for(rotgen::Index i=0; i < parent::size(); i++)
|
||||
(*this)(i) = first[i];
|
||||
}
|
||||
}
|
||||
|
||||
template<typename OtherDerived>
|
||||
matrix(const Eigen::MatrixBase<OtherDerived>& other) : parent(other)
|
||||
{}
|
||||
|
||||
template<typename OtherDerived>
|
||||
matrix& operator=(const Eigen::MatrixBase<OtherDerived>& other)
|
||||
{
|
||||
parent::operator=(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename OtherDerived>
|
||||
matrix& operator=(const Eigen::EigenBase<OtherDerived>& other)
|
||||
{
|
||||
parent::operator=(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
parent& base() { return static_cast<parent&>(*this); }
|
||||
parent const& base() const { return static_cast<const parent&>(*this); }
|
||||
|
||||
concrete_type transpose() const
|
||||
{
|
||||
return concrete_type(static_cast<parent const &>(*this).transpose());
|
||||
}
|
||||
|
||||
concrete_type conjugate() const
|
||||
{
|
||||
return concrete_type(static_cast<parent const &>(*this).conjugate());
|
||||
}
|
||||
|
||||
concrete_type adjoint() const
|
||||
{
|
||||
return concrete_type(static_cast<parent const &>(*this).adjoint());
|
||||
}
|
||||
|
||||
void transposeInPlace() { parent::transposeInPlace(); }
|
||||
void adjointInPlace() { parent::adjointInPlace(); }
|
||||
|
||||
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)
|
||||
{
|
||||
parent::conservativeResize(new_rows, new_cols);
|
||||
}
|
||||
|
||||
static matrix Constant(Scalar value) requires (Rows != -1 && Cols != -1)
|
||||
{
|
||||
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<Scalar>(value));
|
||||
}
|
||||
|
||||
static matrix Identity() requires (Rows != -1 && Cols != -1)
|
||||
{
|
||||
return parent::Identity(Rows, Cols);
|
||||
}
|
||||
|
||||
static matrix Identity(int rows, int cols)
|
||||
{
|
||||
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::Identity(rows, cols);
|
||||
}
|
||||
|
||||
static matrix Zero() requires (Rows != -1 && Cols != -1)
|
||||
{
|
||||
return parent::Zero(Rows, Cols);
|
||||
}
|
||||
|
||||
static matrix Zero(int rows, int cols)
|
||||
{
|
||||
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::Zero(rows, cols);
|
||||
}
|
||||
|
||||
static matrix Random() requires (Rows != -1 && Cols != -1)
|
||||
{
|
||||
return parent::Random(Rows, Cols);
|
||||
}
|
||||
|
||||
static matrix Random(int rows, int cols)
|
||||
{
|
||||
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::Random(rows, cols);
|
||||
}
|
||||
|
||||
template<int P>
|
||||
scalar_type lpNorm() const
|
||||
{
|
||||
static_assert(P == 1 || P == 2 || P == Infinity);
|
||||
return parent::template lpNorm<P>();
|
||||
}
|
||||
|
||||
using parent::operator();
|
||||
using parent::rows;
|
||||
using parent::cols;
|
||||
using parent::size;
|
||||
using parent::prod;
|
||||
using parent::mean;
|
||||
using parent::trace;
|
||||
using parent::maxCoeff;
|
||||
using parent::minCoeff;
|
||||
using parent::squaredNorm;
|
||||
using parent::norm;
|
||||
using parent::sum;
|
||||
using parent::data;
|
||||
|
||||
matrix& operator+=(matrix const& rhs)
|
||||
{
|
||||
static_cast<parent&>(*this) += static_cast<parent const&>(rhs);
|
||||
return *this;
|
||||
}
|
||||
|
||||
matrix& operator-=(matrix const& rhs)
|
||||
{
|
||||
static_cast<parent&>(*this) -= static_cast<parent const&>(rhs);
|
||||
return *this;
|
||||
}
|
||||
|
||||
matrix operator-() const
|
||||
{
|
||||
return matrix(static_cast<parent const&>(*this).operator-());
|
||||
}
|
||||
|
||||
matrix& operator*=(matrix const& rhs)
|
||||
{
|
||||
static_cast<parent&>(*this) *= static_cast<parent const&>(rhs);
|
||||
return *this;
|
||||
}
|
||||
|
||||
matrix& operator*=(Scalar rhs)
|
||||
{
|
||||
static_cast<parent&>(*this) *= rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
matrix& operator/=(Scalar rhs)
|
||||
{
|
||||
static_cast<parent&>(*this) /= rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
static void print_storage_info()
|
||||
{
|
||||
std::cout << "Matrix " << Rows << "x" << Cols
|
||||
<< " - Storage: " << (has_static_storage ? "Static" : "Dynamic")
|
||||
<< " (Max. size=" << max_static_size << ")"
|
||||
<< std::endl;
|
||||
}
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue