[ADVANCED INITIALISATION] implemented and tested the static methods
Co-authored-by: kallore <kkaspar@codereckons.com> See merge request oss/rotgen!4
This commit is contained in:
commit
95ae1ef1e4
4 changed files with 191 additions and 3 deletions
|
|
@ -70,6 +70,11 @@ namespace rotgen
|
||||||
|
|
||||||
const double* data() const;
|
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:
|
private:
|
||||||
struct payload;
|
struct payload;
|
||||||
std::unique_ptr<payload> storage_;
|
std::unique_ptr<payload> storage_;
|
||||||
|
|
|
||||||
|
|
@ -118,6 +118,58 @@ namespace rotgen
|
||||||
static_cast<parent&>(*this) /= rhs;
|
static_cast<parent&>(*this) /= rhs;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 Constant(Scalar value)
|
||||||
|
requires (Rows != -1 && Cols != -1)
|
||||||
|
{
|
||||||
|
return parent::Constant(Rows, Cols, static_cast<double>(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));
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename S, int R, int C, int O, int MR, int MC>
|
template<typename S, int R, int C, int O, int MR, int MC>
|
||||||
|
|
|
||||||
|
|
@ -15,12 +15,14 @@ namespace rotgen
|
||||||
//================================================================================================
|
//================================================================================================
|
||||||
struct matrix_impl64::payload
|
struct matrix_impl64::payload
|
||||||
{
|
{
|
||||||
Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> data;
|
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::size_t r=0, std::size_t c=0) : data(r, c) {}
|
||||||
payload(std::initializer_list<std::initializer_list<double>> init) : data(init) {}
|
payload(std::initializer_list<std::initializer_list<double>> init) : data(init) {}
|
||||||
|
payload(data_type&& matrix) : data(std::move(matrix)) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
//==================================================================================================
|
//==================================================================================================
|
||||||
// Constructors & Special Members
|
// Constructors & Special Members
|
||||||
//==================================================================================================
|
//==================================================================================================
|
||||||
|
|
@ -169,4 +171,37 @@ namespace rotgen
|
||||||
storage_->data /= s;
|
storage_->data /= s;
|
||||||
return *this;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
96
test/basic/static_functions.cpp
Normal file
96
test/basic/static_functions.cpp
Normal file
|
|
@ -0,0 +1,96 @@
|
||||||
|
//==================================================================================================
|
||||||
|
/*
|
||||||
|
ROTGEN - Runtime Overlay for Eigen
|
||||||
|
Copyright : CODE RECKONS
|
||||||
|
SPDX-License-Identifier: BSL-1.0
|
||||||
|
*/
|
||||||
|
//==================================================================================================
|
||||||
|
#define TTS_MAIN
|
||||||
|
#include <rotgen/matrix.hpp>
|
||||||
|
#include "tts.hpp"
|
||||||
|
|
||||||
|
template <typename MatrixType>
|
||||||
|
void test_zero(const MatrixType& matrix, std::size_t rows, std::size_t cols)
|
||||||
|
{
|
||||||
|
for(std::size_t r=0;r<rows;++r)
|
||||||
|
for(std::size_t c=0;c<cols;++c)
|
||||||
|
TTS_EQUAL(matrix(r, c), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename MatrixType>
|
||||||
|
void test_constant(const MatrixType& matrix, std::size_t rows, std::size_t cols, double constant)
|
||||||
|
{
|
||||||
|
for(std::size_t r=0;r<rows;++r)
|
||||||
|
for(std::size_t c=0;c<cols;++c)
|
||||||
|
TTS_EQUAL(matrix(r, c), constant);
|
||||||
|
|
||||||
|
constexpr double epsilon = 1e-10;
|
||||||
|
TTS_RELATIVE_EQUAL(rows * cols * constant, matrix.sum(), epsilon);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename MatrixType>
|
||||||
|
void test_random(const MatrixType& matrix, std::size_t rows, std::size_t cols)
|
||||||
|
{
|
||||||
|
for(std::size_t r=0;r<rows;++r)
|
||||||
|
for(std::size_t c=0;c<cols;++c)
|
||||||
|
{
|
||||||
|
TTS_GREATER_EQUAL(matrix(r, c), -1.0);
|
||||||
|
TTS_LESS_EQUAL(matrix(r, c), 1.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename MatrixType>
|
||||||
|
void test_identity(const MatrixType& matrix, std::size_t rows, std::size_t cols)
|
||||||
|
{
|
||||||
|
for(std::size_t r=0;r<rows;++r)
|
||||||
|
for(std::size_t c=0;c<cols;++c)
|
||||||
|
TTS_EQUAL(matrix(r, c), r==c ? 1 : 0);
|
||||||
|
|
||||||
|
|
||||||
|
std::size_t minimum = std::min(rows, cols);
|
||||||
|
TTS_EQUAL(matrix.trace(), minimum);
|
||||||
|
}
|
||||||
|
|
||||||
|
TTS_CASE("Test zero")
|
||||||
|
{
|
||||||
|
test_zero(rotgen::matrix<double, 3, 4>::Zero(), 3, 4);
|
||||||
|
test_zero(rotgen::matrix<double, 1, 1>::Zero(), 1, 1);
|
||||||
|
test_zero(rotgen::matrix<double, 10, 10>::Zero(), 10, 10);
|
||||||
|
test_zero(rotgen::matrix<double>::Zero(3, 4), 3, 4);
|
||||||
|
test_zero(rotgen::matrix<double, 7, 5>::Zero(7, 5), 7, 5);
|
||||||
|
test_zero(rotgen::matrix<double, 9>::Zero(9, 3), 9, 3);
|
||||||
|
test_zero(rotgen::matrix<double, -1, 3>::Zero(2, 3), 2, 3);
|
||||||
|
};
|
||||||
|
|
||||||
|
TTS_CASE("Test constant")
|
||||||
|
{
|
||||||
|
test_constant(rotgen::matrix<double, 3, 8>::Constant(5.12), 3, 8, 5.12);
|
||||||
|
test_constant(rotgen::matrix<double, 1, 1>::Constant(2.2), 1, 1, 2.2);
|
||||||
|
test_constant(rotgen::matrix<double, 11, 12>::Constant(13), 11, 12, 13);
|
||||||
|
test_constant(rotgen::matrix<double>::Constant(2, 7, 5.6), 2, 7, 5.6);
|
||||||
|
test_constant(rotgen::matrix<double, 2, 2>::Constant(2, 2, 2.0), 2, 2, 2.0);
|
||||||
|
test_constant(rotgen::matrix<double, 9>::Constant(9, 3, 1.1), 9, 3, 1.1);
|
||||||
|
test_constant(rotgen::matrix<double, -1, 9>::Constant(5, 9, 42), 5, 9, 42);
|
||||||
|
};
|
||||||
|
|
||||||
|
TTS_CASE("Test random")
|
||||||
|
{
|
||||||
|
test_random(rotgen::matrix<double, 2, 3>::Random(), 2, 3);
|
||||||
|
test_random(rotgen::matrix<double, 1, 1>::Random(), 1, 1);
|
||||||
|
test_random(rotgen::matrix<double, 11, 17>::Random(), 11, 17);
|
||||||
|
test_random(rotgen::matrix<double>::Random(7, 3), 7, 3);
|
||||||
|
test_random(rotgen::matrix<double, 2, 2>::Random(2, 2), 2, 2);
|
||||||
|
test_random(rotgen::matrix<double, 4, -1>::Random(4, 3), 4, 3);
|
||||||
|
test_random(rotgen::matrix<double, -1, 5>::Random(5, 5), 5, 5);
|
||||||
|
};
|
||||||
|
|
||||||
|
TTS_CASE("Test identity")
|
||||||
|
{
|
||||||
|
test_identity(rotgen::matrix<double, 4, 5>::Identity(), 4, 5);
|
||||||
|
test_identity(rotgen::matrix<double, 1, 1>::Identity(), 1, 1);
|
||||||
|
test_identity(rotgen::matrix<double, 21, 3>::Identity(), 21, 3);
|
||||||
|
test_identity(rotgen::matrix<double>::Identity(2, 7), 2, 7);
|
||||||
|
test_identity(rotgen::matrix<double, 2, 2>::Identity(2, 2), 2, 2);
|
||||||
|
test_identity(rotgen::matrix<double, 3>::Identity(3, 3), 3, 3);
|
||||||
|
test_identity(rotgen::matrix<double, -1, 11>::Identity(5, 11), 5, 11);
|
||||||
|
};
|
||||||
Loading…
Add table
Add a link
Reference in a new issue