Commit d70c7148 authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

Adding a number of operators to TMatrix class.

parent cc36b01a
......@@ -42,6 +42,11 @@ Color ColorSet::next() const
void ColorSet::evaluate() const
{
//
// convert all rgb colors to cielab colors
//
// annealing algorithm
}
......
......@@ -7,6 +7,7 @@ namespace radix
{
typedef unsigned int tmatrix_index_t;
typedef Real tmatrix_value_t;
/**
* @class TMatrix
* @brief TMatix<N rows, M column> Templated N by M matrix
......@@ -16,7 +17,7 @@ class TMatrix
{
public:
typedef tmatrix_index_t index_t;
typedef Real value_t;
typedef tmatrix_value_t value_t;
typedef std::array<value_t, M> value_array_t;
typedef std::array<value_array_t, N> matrix_t;
private:
......@@ -31,6 +32,11 @@ public:
TMatrix<N,J> operator*(const TMatrix<M,J>& m) const;
matrix_t& data() {return mData;}
const matrix_t& data() const {return mData;}
TMatrix<N,M> operator/(value_t rhs) const;
TMatrix<N,M> operator+(const TMatrix<N,M>& m) const;
TMatrix<N,M> operator-(const TMatrix<N,M>& m) const;
TMatrix<M,N> t() const;
TMatrix<N,M> pow(int k) const;
}; // class TMatrix
/**
......
......@@ -51,5 +51,75 @@ TMatrix<N,J> TMatrix<N,M>::operator*(const TMatrix<M,J>& m) const
}
return r;
}
template<tmatrix_index_t N, tmatrix_index_t M>
TMatrix<N,M> TMatrix<N,M>::operator/(tmatrix_value_t rhs) const
{
// result matrix
TMatrix<N,M> r;
for(index_t ni = 0; ni < N; ++ni)
{
for(index_t mi = 0; mi < M; ++mi)
{
r[ni][mi] = mData[ni][mi] / rhs;
}
}
return r;
}
template<tmatrix_index_t N, tmatrix_index_t M>
TMatrix<M,N> TMatrix<N,M>::t() const
{
// result matrix
TMatrix<M,N> r;
for(index_t ni = 0; ni < N; ++ni)
{
for(index_t mi = 0; mi < M; ++mi)
{
r[ni][mi] = mData[mi][ni];
}
}
return r;
}
template<tmatrix_index_t N, tmatrix_index_t M>
TMatrix<N,M> TMatrix<N,M>::operator+(const TMatrix<N,M>& m) const
{
// result matrix
TMatrix<N,M> r;
for(index_t ni = 0; ni < N; ++ni)
{
for(index_t mi = 0; mi < M; ++mi)
{
r[ni][mi] = mData[ni][mi] + m[ni][mi];
}
}
return r;
}
template<tmatrix_index_t N, tmatrix_index_t M>
TMatrix<N,M> TMatrix<N,M>::operator-(const TMatrix<N,M>& m) const
{
// result matrix
TMatrix<N,M> r;
for(index_t ni = 0; ni < N; ++ni)
{
for(index_t mi = 0; mi < M; ++mi)
{
r[ni][mi] = mData[ni][mi] - m[ni][mi];
}
}
return r;
}
template<tmatrix_index_t N, tmatrix_index_t M>
TMatrix<N,M> TMatrix<N,M>::pow(int k) const
{
// result matrix
TMatrix<N,M> r(*this);
for(int i = 1; i < k; ++i)
{
r = r * r;
}
return r;
}
} // namespace radix
#include <iostream>
#include "gtest/gtest.h"
#include "radixbug/bug.hh"
#include "radixmath/matrix.hh"
using namespace radix;
......@@ -76,6 +77,81 @@ TEST(radix, TMatrix)
EXPECT_TRUE(m3.equals(solution));
EXPECT_TRUE(solution.equals(m3));
}
{// division
TMatrix<3,3> m1;
m1.data() = {{{50,80,-40}
,{60,90,-50}
,{40,70,-20}
}};
TMatrix<3,3> solution;
solution.data() = {{{25,40,-20}
,{30,45,-25}
,{20,35,-10}
}};
TMatrix<3,3> m2 = m1/2;
EXPECT_TRUE(m2.equals(solution));
}
{// transpose
TMatrix<3,3> m1;
m1.data() = {{{50,80,-40}
,{60,90,-50}
,{40,70,-20}
}};
TMatrix<3,3> solution;
solution.data() = {{{50,60,40}
,{80,90,70}
,{-40,-50,-20}
}};
TMatrix<3,3> m2 = m1.t();
EXPECT_TRUE(m2.equals(solution));
}
{// addition/subtraction
TMatrix<3,3> m1;
m1.data() = {{{50,80,-40}
,{60,90,-50}
,{40,70,-20}
}};
TMatrix<3,3> m2;
m2.data() = {{{50,60,40}
,{80,90,70}
,{-40,-50,-20}
}};
TMatrix<3,3> m3 = m1 - m2;
TMatrix<3,3> solution;
solution.data() = {{{0,20,-80}
,{-20,0,-120}
,{80,120,0}
}};
EXPECT_TRUE(m3.equals(solution));
solution = solution + m2;
EXPECT_TRUE(m1.equals(solution));
}
{// division
TMatrix<3,3> m1;
m1.data() = {{{5,8,-4}
,{6,9,-5}
,{4,7,-2}
}};
TMatrix<3,3> solution;
solution.data() = {{{57,84,-52}
,{64,94,-59}
,{54,81,-47}
}};
TMatrix<3,3> m2 = m1.pow(2);
EXPECT_TRUE(m2.equals(solution));
TMatrix<3,3>::matrix_t d = m2.data();
radix_line("{" << d[0][0] << "," << d[0][1] << "," << d[0][2] << "}" << std::endl
<< "{" << d[1][0] << "," << d[1][1] << "," << d[1][2] << "}" << std::endl
<< "{" << d[2][0] << "," << d[2][1] << "," << d[2][2] << "}" << std::endl
);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment