10#ifndef MATH_MATRIX_HEADER
11#define MATH_MATRIX_HEADER
28template <
typename T,
int N,
int M>
class Matrix;
52template <
typename T,
int N,
int M>
58 static int constexpr rows = N;
59 static int constexpr cols = M;
146 T
const*
end (
void)
const;
153 T
const* operator* (
void)
const;
156 T& operator() (
int row,
int col);
158 T
const& operator() (
int row,
int col)
const;
161 T& operator[] (
unsigned int i);
163 T
const& operator[] (
unsigned int i)
const;
173 template <
typename O>
223template <
typename T,
int N,
int M>
226template <
typename T,
int N,
int M>
229template <
typename T,
int N,
int M>
235template <
typename T,
int N,
int M>
239 std::copy(values, values + N * M, m);
242template <
typename T,
int N,
int M>
249template <
typename T,
int N,
int M>
253 std::copy(*other, *other + M * N, m);
256template <
typename T,
int N,
int M>
261 std::copy(*other, *other + M * N, m);
266template <
typename T,
int N,
int M>
273template <
typename T,
int N>
280template <
typename T,
int N>
284 for (
int i = 0; i < matrix.
rows; ++i)
285 for (
int j = i + 1; j < matrix.
cols; ++j)
292template <
typename T,
int N,
int M>
296 std::fill(m, m + N * M, value);
300template <
typename T,
int N,
int M>
307template <
typename T,
int N,
int M>
311 return *std::min_element(m, m + N * M);
314template <
typename T,
int N,
int M>
318 return *std::max_element(m, m + N * M);
321template <
typename T,
int N,
int M>
328template <
typename T,
int N,
int M>
334 std::copy_n(RowIter(m + index), N, *ret);
338template <
typename T,
int N,
int M>
345 T
const* in2 = *other;
346 for (T* out = *ret; in1 < m + M*N; in1 += M, in2 += O, out += O+M)
348 std::copy(in1, in1 + M, out);
349 std::copy(in2, in2 + O, out + M);
354template <
typename T,
int N,
int M>
360 std::copy(m, m + M*N, *ret);
361 std::copy(*other, *other + O*M, *ret + M*N);
365template <
typename T,
int N,
int M>
371 T
const* in2 = *other;
372 for (T* out = *ret; in1 < m + M*N; in1 += M, in2 += 1, out += M+1)
374 std::copy(in1, in1 + M, out);
375 std::copy(in2, in2 + 1, out + M);
380template <
typename T,
int N,
int M>
385 std::copy(m, m + M*N, *ret);
386 std::copy(*other, *other + M, *ret + M*N);
390template <
typename T,
int N,
int M>
395 T
const* in_ptr = this->begin();
396 T* out_ptr = ret.
begin();
397 for (
int i = 0; i < N; ++i, in_ptr += M)
400 std::copy(in_ptr, in_ptr + M, out_ptr);
406template <
typename T,
int N,
int M>
411 T
const* in_ptr = this->begin();
412 T* out_ptr = ret.
begin();
413 for (
int i = 0; i < M*N; ++i, ++in_ptr)
424template <
typename T,
int N,
int M>
428 std::for_each(m, m + N*M, &algo::foreach_negate_value<T>);
432template <
typename T,
int N,
int M>
439template <
typename T,
int N,
int M>
446template <
typename T,
int N,
int M>
451 for (
int i = 0; i < N; ++i)
452 for (
int j = 0; j < M; ++j)
453 ret(j,i) = (*this)(i,j);
459template <
typename T,
int N,
int M>
466 for (
int i = 0; i < ret.
rows; ++i)
467 for (
int j = 0; j < ret.
cols; ++j)
468 ret(i,j) = std::inner_product(m + M * i,
469 m + M * i + M, RowIter(*rhs + j), T(0));
473template <
typename T,
int N,
int M>
478 for (
int i = 0; i < N; ++i)
479 ret[i] = std::inner_product(m + M * i, m + M * i + M, *rhs, T(0));
483template <
typename T,
int N,
int M>
488 for (
int i = 0; i < N-1; ++i)
489 ret[i] = std::inner_product(m + M * i, m + M * i + M - 1, *rhs, T(0))
490 + v * m[M * i + M - 1];
494template <
typename T,
int N,
int M>
498 return std::equal(m, m + N * M, *other,
504template <
typename T,
int N,
int M>
511template <
typename T,
int N,
int M>
518template <
typename T,
int N,
int M>
525template <
typename T,
int N,
int M>
534template <
typename T,
int N,
int M>
541template <
typename T,
int N,
int M>
548template <
typename T,
int N,
int M>
552 return m[row * M + col];
555template <
typename T,
int N,
int M>
559 return m[row * M + col];
561template <
typename T,
int N,
int M>
568template <
typename T,
int N,
int M>
575template <
typename T,
int N,
int M>
579 return std::equal(m, m + N * M, *rhs);
582template <
typename T,
int N,
int M>
586 return !std::equal(m, m + N * M, *rhs);
589template <
typename T,
int N,
int M>
593 std::copy(*rhs, *rhs + N * M, m);
597template <
typename T,
int N,
int M>
602 std::copy(*rhs, *rhs + N * M, m);
606template <
typename T,
int N,
int M>
613template <
typename T,
int N,
int M>
617 std::transform(m, m + N * M, *rhs, m, std::minus<T>());
621template <
typename T,
int N,
int M>
628template <
typename T,
int N,
int M>
636template <
typename T,
int N,
int M>
643template <
typename T,
int N,
int M>
647 std::transform(m, m + N * M, *rhs, m, std::plus<T>());
651template <
typename T,
int N,
int M>
658template <
typename T,
int N,
int M>
666template <
typename T,
int N,
int M>
673template <
typename T,
int N,
int M>
681template <
typename T,
int N,
int M>
688template <
typename T,
int N,
int M>
696template <
typename T,
int N,
int M>
703template <
typename T,
int N,
int M>
711template <
typename T,
int N,
int M>
727template <
typename T,
int N,
int M>
731 for (
int i = 0; i < m.
rows; ++i)
732 for (
int j = 0; j < m.
cols; ++j)
733 os << m(i,j) << (j == m.
cols - 1 ?
"\n" :
" ");
Matrix class for arbitrary dimensions and types.
Matrix< T, N, M > negated(void) const
Returns a component-wise negation on copy of self.
Matrix(void)
Default ctor that leaves values uninitialized.
T minimum(void) const
Returns the smallest element in the matrix.
Matrix< T, N, U > mult(Matrix< T, M, U > const &rhs) const
Matrix with matrix multiplication.
static int constexpr rows
Vector< T, N > mult(Vector< T, M > const &rhs) const
Matrix with vector multiplication.
Matrix< T, N+1, M > vstack(Vector< T, M > const &other) const
Stacks this matrix (top) and another vector (bottom) vertically.
Matrix(Matrix< T, N, M > const &other)
Copy ctor from matrix of same type.
T const * begin(void) const
Matrix(T const &value)
Ctor that initializes ALL elements.
Vector< T, M > row(int index) const
Returns a row of the matrix as vector.
bool is_similar(Matrix< T, N, M > const &other, T const &epsilon) const
Component-wise similarity using epsilon checks.
Matrix< T, N-1, M > delete_row(int index) const
Returns a new matrix with the specified row deleted.
bool is_square(void) const
Tests if the matrix is square.
Vector< T, N > col(int index) const
Returns a column of the matrix as vector.
Vector< T, N-1 > mult(Vector< T, M-1 > const &rhs, T const &v) const
Matrix with smaller vector multiplication.
Matrix< T, N, M > & negate(void)
Component-wise negation on self, returns self.
Matrix< T, N, M-1 > delete_col(int index) const
Returns a new matrix with the specified column deleted.
Matrix(T const *values)
Ctor taking a pointer to initialize values.
T const * end(void) const
Matrix< T, N, M+O > hstack(Matrix< T, N, O > const &other) const
Stacks this (left) and another matrix (right) horizontally.
Matrix< T, M, N > transposed(void) const
Returns a transposed copy of self by treating rows as columns.
Matrix(Matrix< O, N, M > const &other)
Copy ctor from matrix of different type.
Matrix< T, N+O, M > vstack(Matrix< T, O, M > const &other) const
Stacks this (top) and another matrix (bottom) vertically.
static int constexpr cols
Matrix< T, N, M > & fill(T const &value)
Fills all vector elements with the given value.
Matrix< T, M, N > & transpose(void)
Transpose the current matrix.
T maximum(void) const
Returns the largest element in the matrix.
Matrix< T, N, M+1 > hstack(Vector< T, N > const &other) const
Stacks this matrix (left) and another vector (right) horizontally.
Vector class for arbitrary dimensions and types.
#define MATH_NAMESPACE_BEGIN
#define MATH_NAMESPACE_END
Matrix< T, N, N > & matrix_inplace_transpose(Matrix< T, N, N > &matrix)
bool matrix_is_square(Matrix< T, N, M > const &)
void swap(mve::Image< T > &a, mve::Image< T > &b)
Specialization of std::swap for efficient image swapping.
Iterator that advances 'S' elements of type T.
for-each functor: adds a constant value to operand.
for-each functor: divides operand by constant divisor.
for-each functor: multiplies operand with constant factor.
for-each functor: substracts a constant value to operand.
Epsilon comparator predicate.