Added more operators + tests

This commit is contained in:
Joel Falcou 2025-05-12 11:33:38 +02:00
parent 5f1d070547
commit 682202825e
4 changed files with 68 additions and 10 deletions

View file

@ -35,6 +35,7 @@ namespace rotgen
matrix_impl64& operator*=(double d); matrix_impl64& operator*=(double d);
friend std::ostream& operator<<(std::ostream&,matrix_impl64 const&); friend std::ostream& operator<<(std::ostream&,matrix_impl64 const&);
friend bool operator==(matrix_impl64 const& lhs, matrix_impl64 const& rhs);
private: private:
struct payload; struct payload;

View file

@ -11,7 +11,9 @@
namespace rotgen namespace rotgen
{ {
template<typename Scalar, int Rows = -1, int Cols = -1> template< typename Scalar, int Rows = -1 , int Cols = -1
, int Options = 0, int MaxRows = Rows, int MaxCols = Cols
>
class matrix : public matrix_impl64 class matrix : public matrix_impl64
{ {
using parent = matrix_impl64; using parent = matrix_impl64;
@ -20,6 +22,11 @@ namespace rotgen
matrix() : parent(Rows==-1?0:Rows,Cols==-1?0:Cols) {} matrix() : parent(Rows==-1?0:Rows,Cols==-1?0:Cols) {}
matrix(std::size_t r, std::size_t c) : parent(r,c) {} matrix(std::size_t r, std::size_t c) : parent(r,c) {}
friend bool operator==(matrix const& lhs, matrix const& rhs)
{
return static_cast<parent const&>(lhs) == static_cast<parent const&>(rhs);
}
matrix& operator+=(matrix const& rhs) matrix& operator+=(matrix const& rhs)
{ {
static_cast<parent&>(*this) += static_cast<parent const&>(rhs); static_cast<parent&>(*this) += static_cast<parent const&>(rhs);
@ -31,19 +38,38 @@ namespace rotgen
static_cast<parent&>(*this) *= static_cast<parent const&>(rhs); static_cast<parent&>(*this) *= static_cast<parent const&>(rhs);
return *this; return *this;
} }
matrix& operator*=(double rhs)
{
static_cast<parent&>(*this) *= rhs;
return *this;
}
}; };
template<typename S, int R, int C> template<typename S, int R, int C, int O, int MR, int MC>
matrix<S,R,C> operator+(matrix<S,R,C> const& lhs, matrix<S,R,C> const& rhs) matrix<S,R,C,O,MR,MC> operator+(matrix<S,R,C,O,MR,MC> const& lhs, matrix<S,R,C,O,MR,MC> const& rhs)
{ {
matrix<S,R,C> that(lhs); matrix<S,R,C,O,MR,MC> that(lhs);
return that += rhs; return that += rhs;
} }
template<typename S, int R, int C> template<typename S, int R, int C, int O, int MR, int MC>
matrix<S,R,C> operator*(matrix<S,R,C> const& lhs, matrix<S,R,C> const& rhs) matrix<S,R,C,O,MR,MC> operator*(matrix<S,R,C,O,MR,MC> const& lhs, matrix<S,R,C,O,MR,MC> const& rhs)
{ {
matrix<S,R,C> that(lhs); matrix<S,R,C,O,MR,MC> that(lhs);
return that *= rhs; return that *= rhs;
} }
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> 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)
{
return rhs * lhs;
}
} }

31
test/basic/operators.cpp Normal file
View file

@ -0,0 +1,31 @@
//==================================================================================================
/*
ROTGEN - Runtime Overlay for Eigen
Copyright : CODE RECKONS
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#define TTS_MAIN
#include <rotgen/matrix.hpp>
#include "tts.hpp"
TTS_CASE("Check operator*")
{
rotgen::matrix<double> a(2,2);
rotgen::matrix<double> ref(2,2);
for(int r=0;r<a.rows();r++)
{
for(int c=0;c<a.cols();c++)
{
a(r,c) = (1+c) + 10*(1+r);
ref(r,c) = ((1+c) + 10*(1+r)) * 10.5;
}
}
TTS_EQUAL(a * 10.5, ref);
TTS_EQUAL(10.5 * a, ref);
a *= 10.5;
TTS_EQUAL(a, ref);
};