2013-10-03 23:19:28 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2012 Pavel Krajcevski
|
|
|
|
*
|
|
|
|
* This software is provided 'as-is', without any express or implied
|
|
|
|
* warranty. In no event will the authors be held liable for any damages
|
|
|
|
* arising from the use of this software.
|
|
|
|
*
|
|
|
|
* Permission is granted to anyone to use this software for any purpose,
|
|
|
|
* including commercial applications, and to alter it and redistribute it
|
|
|
|
* freely, subject to the following restrictions:
|
|
|
|
*
|
|
|
|
* 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
* claim that you wrote the original software. If you use this software
|
|
|
|
* in a product, an acknowledgment in the product documentation would be
|
|
|
|
* appreciated but is not required.
|
|
|
|
*
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
* misrepresented as being the original software.
|
|
|
|
*
|
|
|
|
* 3. This notice may not be removed or altered from any source
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#ifndef BASE_INCLUDE_MATRIXBASE_H__
|
|
|
|
#define BASE_INCLUDE_MATRIXBASE_H__
|
|
|
|
|
|
|
|
#include "VectorBase.h"
|
|
|
|
|
|
|
|
namespace FasTC {
|
|
|
|
|
|
|
|
template <typename T, const int nRows, const int nCols>
|
2014-02-21 22:18:00 +01:00
|
|
|
class MatrixBase {
|
|
|
|
protected:
|
|
|
|
|
|
|
|
// Vector representation
|
|
|
|
T mat[nRows * nCols];
|
|
|
|
|
2013-10-03 23:19:28 +02:00
|
|
|
public:
|
2014-02-21 22:18:00 +01:00
|
|
|
typedef T ScalarType;
|
|
|
|
static const int kNumRows = nRows;
|
|
|
|
static const int kNumCols = nCols;
|
|
|
|
static const int Size = kNumCols * kNumRows;
|
2014-02-20 21:34:12 +01:00
|
|
|
|
2013-10-03 23:19:28 +02:00
|
|
|
// Constructors
|
|
|
|
MatrixBase() { }
|
|
|
|
MatrixBase(const MatrixBase<T, nRows, nCols> &other) {
|
2014-02-20 21:34:12 +01:00
|
|
|
for(int i = 0; i < Size; i++) {
|
|
|
|
(*this)[i] = other[i];
|
2013-10-03 23:19:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accessors
|
2014-02-21 22:18:00 +01:00
|
|
|
T &operator()(int idx) { return mat[idx]; }
|
|
|
|
T &operator[](int idx) { return mat[idx]; }
|
|
|
|
const T &operator()(int idx) const { return mat[idx]; }
|
|
|
|
const T &operator[](int idx) const { return mat[idx]; }
|
2014-02-20 21:34:12 +01:00
|
|
|
|
|
|
|
T &operator()(int r, int c) { return (*this)[r * nCols + c]; }
|
|
|
|
const T &operator() (int r, int c) const { return (*this)[r * nCols + c]; }
|
|
|
|
|
|
|
|
// Allow casts to the respective array representation...
|
2014-02-21 22:18:00 +01:00
|
|
|
operator const T *() const { return this->mat; }
|
2014-02-20 21:34:12 +01:00
|
|
|
MatrixBase<T, nRows, nCols> &operator=(const T *v) {
|
|
|
|
for(int i = 0; i < Size; i++)
|
|
|
|
(*this)[i] = v[i];
|
2013-10-03 23:19:28 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-02-20 21:34:12 +01:00
|
|
|
// Allows casting to other vector types if the underlying type system does as well...
|
2013-10-03 23:19:28 +02:00
|
|
|
template<typename _T>
|
2014-02-20 21:34:12 +01:00
|
|
|
operator MatrixBase<_T, nRows, nCols>() const {
|
|
|
|
MatrixBase<_T, nRows, nCols> ret;
|
|
|
|
for(int i = 0; i < Size; i++) {
|
2014-02-21 22:18:00 +01:00
|
|
|
ret[i] = static_cast<_T>(mat[i]);
|
2013-10-03 23:19:28 +02:00
|
|
|
}
|
2014-02-20 21:34:12 +01:00
|
|
|
return ret;
|
2013-10-03 23:19:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Matrix multiplication
|
|
|
|
template<typename _T, const int nTarget>
|
2014-02-21 22:11:49 +01:00
|
|
|
MatrixBase<T, nRows, nTarget> MultiplyMatrix(const MatrixBase<_T, nCols, nTarget> &m) const {
|
2013-10-03 23:19:28 +02:00
|
|
|
MatrixBase<T, nRows, nTarget> result;
|
|
|
|
for(int r = 0; r < nRows; r++)
|
|
|
|
for(int c = 0; c < nTarget; c++) {
|
|
|
|
result(r, c) = 0;
|
|
|
|
for(int j = 0; j < nCols; j++) {
|
|
|
|
result(r, c) += (*this)(r, j) * m(j, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vector multiplication -- treat vectors as Nx1 matrices...
|
|
|
|
template<typename _T>
|
2014-02-21 22:18:00 +01:00
|
|
|
VectorBase<T, nCols> MultiplyVectorLeft(const VectorBase<_T, nRows> &v) const {
|
2013-10-03 23:19:28 +02:00
|
|
|
VectorBase<T, nCols> result;
|
2014-02-21 22:18:00 +01:00
|
|
|
for(int j = 0; j < nCols; j++) {
|
|
|
|
result(j) = 0;
|
|
|
|
for(int r = 0; r < nRows; r++) {
|
|
|
|
result(j) += (*this)(r, j) * v(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename _T>
|
|
|
|
VectorBase<T, nRows> MultiplyVectorRight(const VectorBase<_T, nCols> &v) const {
|
|
|
|
VectorBase<T, nRows> result;
|
2013-10-03 23:19:28 +02:00
|
|
|
for(int r = 0; r < nRows; r++) {
|
|
|
|
result(r) = 0;
|
|
|
|
for(int j = 0; j < nCols; j++) {
|
|
|
|
result(r) += (*this)(r, j) * v(j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-02-20 22:15:13 +01:00
|
|
|
// Transposition
|
|
|
|
MatrixBase<T, nCols, nRows> Transpose() const {
|
|
|
|
MatrixBase<T, nCols, nRows> result;
|
|
|
|
for(int r = 0; r < nRows; r++) {
|
|
|
|
for(int c = 0; c < nCols; c++) {
|
|
|
|
result(c, r) = (*this)(r, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-10-03 23:19:28 +02:00
|
|
|
// Double dot product
|
|
|
|
template<typename _T>
|
2014-02-21 22:18:00 +01:00
|
|
|
T DDot(const MatrixBase<_T, nRows, nCols> &m) const {
|
2013-10-03 23:19:28 +02:00
|
|
|
T result = 0;
|
2014-02-20 21:34:12 +01:00
|
|
|
for(int i = 0; i < Size; i++) {
|
|
|
|
result += (*this)[i] * m[i];
|
2013-10-03 23:19:28 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2014-02-21 22:18:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, const int N, const int M>
|
|
|
|
class VectorTraits<MatrixBase<T, N, M> > {
|
|
|
|
public:
|
|
|
|
static const EVectorType kVectorType = eVectorType_Matrix;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define REGISTER_MATRIX_TYPE(TYPE) \
|
|
|
|
template<> \
|
|
|
|
class VectorTraits< TYPE > { \
|
|
|
|
public: \
|
|
|
|
static const EVectorType kVectorType = eVectorType_Matrix; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define REGISTER_ONE_TEMPLATE_MATRIX_TYPE(TYPE) \
|
|
|
|
template<typename T> \
|
|
|
|
class VectorTraits< TYPE <T> > { \
|
|
|
|
public: \
|
|
|
|
static const EVectorType kVectorType = eVectorType_Matrix; \
|
2014-02-21 23:45:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define REGISTER_ONE_TEMPLATE_MATRIX_SIZED_TYPE(TYPE) \
|
|
|
|
template<typename T, const int SIZE> \
|
|
|
|
class VectorTraits< TYPE <T, SIZE> > { \
|
|
|
|
public: \
|
|
|
|
static const EVectorType kVectorType = eVectorType_Matrix; \
|
2014-02-21 22:18:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Define matrix multiplication for * operator
|
|
|
|
template<typename TypeOne, typename TypeTwo>
|
|
|
|
class MultSwitch<
|
|
|
|
eVectorType_Matrix,
|
|
|
|
eVectorType_Vector,
|
|
|
|
TypeOne, TypeTwo> {
|
|
|
|
private:
|
|
|
|
const TypeOne &m_A;
|
|
|
|
const TypeTwo &m_B;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef VectorBase<typename TypeTwo::ScalarType, TypeOne::kNumRows> ResultType;
|
|
|
|
|
|
|
|
MultSwitch(const TypeOne &a, const TypeTwo &b)
|
|
|
|
: m_A(a), m_B(b) { }
|
|
|
|
|
|
|
|
ResultType GetMultiplication() const { return m_A.MultiplyVectorRight(m_B); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename TypeOne, typename TypeTwo>
|
|
|
|
class MultSwitch<
|
|
|
|
eVectorType_Vector,
|
|
|
|
eVectorType_Matrix,
|
|
|
|
TypeOne, TypeTwo> {
|
|
|
|
private:
|
|
|
|
const TypeOne &m_A;
|
|
|
|
const TypeTwo &m_B;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef VectorBase<typename TypeOne::ScalarType, TypeTwo::kNumCols> ResultType;
|
|
|
|
|
|
|
|
MultSwitch(const TypeOne &a, const TypeTwo &b)
|
|
|
|
: m_A(a), m_B(b) { }
|
|
|
|
|
|
|
|
ResultType GetMultiplication() const { return m_B.MultiplyVectorLeft(m_A); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename TypeOne, typename TypeTwo>
|
|
|
|
class MultSwitch<
|
|
|
|
eVectorType_Matrix,
|
|
|
|
eVectorType_Matrix,
|
|
|
|
TypeOne, TypeTwo> {
|
|
|
|
private:
|
|
|
|
const TypeOne &m_A;
|
|
|
|
const TypeTwo &m_B;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef MatrixBase<typename TypeOne::ScalarType, TypeOne::kNumRows, TypeTwo::kNumCols> ResultType;
|
|
|
|
|
|
|
|
MultSwitch(const TypeOne &a, const TypeTwo &b)
|
|
|
|
: m_A(a), m_B(b) { }
|
2013-10-03 23:19:28 +02:00
|
|
|
|
2014-02-21 22:18:00 +01:00
|
|
|
ResultType GetMultiplication() const { return m_A.MultiplyMatrix(m_B); }
|
2013-10-03 23:19:28 +02:00
|
|
|
};
|
2014-02-20 21:34:12 +01:00
|
|
|
|
|
|
|
// Outer product...
|
|
|
|
template<typename _T, typename _U, const int N, const int M>
|
|
|
|
MatrixBase<_T, N, M> operator^(
|
|
|
|
const VectorBase<_T, N> &a,
|
|
|
|
const VectorBase<_U, M> &b
|
|
|
|
) {
|
|
|
|
MatrixBase<_T, N, M> result;
|
|
|
|
|
|
|
|
for(int i = 0; i < N; i++)
|
|
|
|
for(int j = 0; j < M; j++)
|
|
|
|
result(i, j) = a[i] * b[j];
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename _T, typename _U, const int N, const int M>
|
|
|
|
MatrixBase<_T, N, M> OuterProduct(
|
|
|
|
const VectorBase<_T, N> &a,
|
|
|
|
const VectorBase<_U, M> &b
|
|
|
|
) {
|
|
|
|
return a ^ b;
|
|
|
|
}
|
|
|
|
|
2013-10-03 23:19:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // BASE_INCLUDE_MATRIXBASE_H_
|