61 template <
class Type,
int DIM = 4>
65 typedef Type CStyleMatrix[DIM][DIM];
124 inline void Clear() { memset(
_data, 0,
sizeof(Type)*DIM*DIM); }
283 Type& operator () (
unsigned row,
unsigned col);
284 Type operator () (
unsigned row,
unsigned col)
const;
296 GlsMatrix<Type, DIM> operator - ()
const;
299 GlsMatrix<Type, DIM>& operator += (
const GlsMatrix<Type,DIM>& rhs);
300 GlsMatrix<Type, DIM>& operator -= (
const GlsMatrix<Type,DIM>& rhs);
301 GlsMatrix<Type, DIM>& operator *= (
const GlsMatrix<Type,DIM>& rhs);
302 GlsMatrix<Type, DIM>& operator *= (Type scalar);
303 GlsMatrix<Type, DIM>& operator /= (Type scalar);
306 GlsMatrix<Type, DIM> operator + (
const GlsMatrix<Type,DIM>& rhs)
const;
307 GlsMatrix<Type, DIM> operator - (
const GlsMatrix<Type,DIM>& rhs)
const;
308 GlsMatrix<Type, DIM> operator * (
const GlsMatrix<Type,DIM>& rhs)
const;
309 GlsMatrix<Type, DIM> operator * (Type scalar)
const;
310 GlsMatrix<Type, DIM> operator / (Type scalar)
const;
364 int Pivot(
unsigned row);
388 template<
class Type,
int DIM>
392 template<
class Type,
int DIM>
399 template<
class Type,
int DIM>
403 for (
int row = 0; row < DIM; ++row)
405 for (
int col = 0; col < DIM; ++col)
407 _matrix[col][row] = m[row][col];
412 template<
class Type,
int DIM>
416 memcpy(_data, m,
sizeof(Type)*DIM*DIM);
419 template<
class Type,
int DIM>
423 memcpy(_data, m.
_data,
sizeof(Type)*DIM*DIM);
426 template<
class Type,
int DIM>
462 GlsMatrix<float, 4>::operator * (
const GlsMatrix<float, 4>& rhs)
const
464 GlsMatrix<float, 4> result;
467 result.
_matrix[0][0] = _matrix[0][0] * rhs._matrix[0][0] + _matrix[1][0] * rhs._matrix[0][1] + _matrix[2][0] * rhs._matrix[0][2] + _matrix[3][0] * rhs._matrix[0][3];
468 result._matrix[1][0] = _matrix[0][0] * rhs._matrix[1][0] + _matrix[1][0] * rhs._matrix[1][1] + _matrix[2][0] * rhs._matrix[1][2] + _matrix[3][0] * rhs._matrix[1][3];
469 result._matrix[2][0] = _matrix[0][0] * rhs._matrix[2][0] + _matrix[1][0] * rhs._matrix[2][1] + _matrix[2][0] * rhs._matrix[2][2] + _matrix[3][0] * rhs._matrix[2][3];
470 result._matrix[3][0] = _matrix[0][0] * rhs._matrix[3][0] + _matrix[1][0] * rhs._matrix[3][1] + _matrix[2][0] * rhs._matrix[3][2] + _matrix[3][0] * rhs._matrix[3][3];
471 result._matrix[0][1] = _matrix[0][1] * rhs._matrix[0][0] + _matrix[1][1] * rhs._matrix[0][1] + _matrix[2][1] * rhs._matrix[0][2] + _matrix[3][1] * rhs._matrix[0][3];
472 result._matrix[1][1] = _matrix[0][1] * rhs._matrix[1][0] + _matrix[1][1] * rhs._matrix[1][1] + _matrix[2][1] * rhs._matrix[1][2] + _matrix[3][1] * rhs._matrix[1][3];
473 result._matrix[2][1] = _matrix[0][1] * rhs._matrix[2][0] + _matrix[1][1] * rhs._matrix[2][1] + _matrix[2][1] * rhs._matrix[2][2] + _matrix[3][1] * rhs._matrix[2][3];
474 result._matrix[3][1] = _matrix[0][1] * rhs._matrix[3][0] + _matrix[1][1] * rhs._matrix[3][1] + _matrix[2][1] * rhs._matrix[3][2] + _matrix[3][1] * rhs._matrix[3][3];
475 result._matrix[0][2] = _matrix[0][2] * rhs._matrix[0][0] + _matrix[1][2] * rhs._matrix[0][1] + _matrix[2][2] * rhs._matrix[0][2] + _matrix[3][2] * rhs._matrix[0][3];
476 result._matrix[1][2] = _matrix[0][2] * rhs._matrix[1][0] + _matrix[1][2] * rhs._matrix[1][1] + _matrix[2][2] * rhs._matrix[1][2] + _matrix[3][2] * rhs._matrix[1][3];
477 result._matrix[2][2] = _matrix[0][2] * rhs._matrix[2][0] + _matrix[1][2] * rhs._matrix[2][1] + _matrix[2][2] * rhs._matrix[2][2] + _matrix[3][2] * rhs._matrix[2][3];
478 result._matrix[3][2] = _matrix[0][2] * rhs._matrix[3][0] + _matrix[1][2] * rhs._matrix[3][1] + _matrix[2][2] * rhs._matrix[3][2] + _matrix[3][2] * rhs._matrix[3][3];
479 result._matrix[0][3] = _matrix[0][3] * rhs._matrix[0][0] + _matrix[1][3] * rhs._matrix[0][1] + _matrix[2][3] * rhs._matrix[0][2] + _matrix[3][3] * rhs._matrix[0][3];
480 result._matrix[1][3] = _matrix[0][3] * rhs._matrix[1][0] + _matrix[1][3] * rhs._matrix[1][1] + _matrix[2][3] * rhs._matrix[1][2] + _matrix[3][3] * rhs._matrix[1][3];
481 result._matrix[2][3] = _matrix[0][3] * rhs._matrix[2][0] + _matrix[1][3] * rhs._matrix[2][1] + _matrix[2][3] * rhs._matrix[2][2] + _matrix[3][3] * rhs._matrix[2][3];
482 result._matrix[3][3] = _matrix[0][3] * rhs._matrix[3][0] + _matrix[1][3] * rhs._matrix[3][1] + _matrix[2][3] * rhs._matrix[3][2] + _matrix[3][3] * rhs._matrix[3][3];
487 template<
class Type,
int DIM>
490 GlsMatrix<Type, DIM>::operator * (
const GlsMatrix<Type, DIM>& rhs)
const
492 GlsMatrix<Type,DIM> result;
493 memset(result._data, 0,
sizeof(Type) * DIM * DIM);
494 for (
int row = 0; row < DIM; ++row)
496 for (
int col = 0; col < DIM; ++col)
498 for (
int i = 0; i < DIM; ++i)
500 result._matrix[col][row] += _matrix[i][row] * rhs._matrix[col][i];
507 template<
class Type,
int DIM>
509 GlsMatrix<Type, DIM> GlsMatrix<Type, DIM>::operator * (Type scalar)
const
511 GlsMatrix<Type, DIM> result;
512 for (
int row = 0; row < DIM; ++row)
514 for (
int col = 0; col < DIM; ++col)
516 result._matrix[col][row] = _matrix[col][row] * scalar;
522 template<
class Type,
int DIM>
525 for (
int row = 0; row < DIM; ++row)
527 for (
int col = 0; col < DIM; ++col)
530 std::cout << _matrix[col][row] <<
' ';
532 std::cout << std::endl;
534 std::cout << std::endl;
537 template<
class Type,
int DIM>
542 for (
int i = 0; i < DIM; ++i)
544 _matrix[i][i] = Type(1);
548 template<
class Type,
int DIM>
571 for (
int k = 0; k < DIM; ++k)
574 int index = temp.
Pivot(k);
596 for (
int i = k+1; i < DIM; ++i)
607 for (
int j = k+1; j < DIM; ++j)
616 template<
class Type,
int DIM>
650 for (
int k = 0; k < DIM; ++k)
653 int index = temp.
Pivot(k);
666 size_t byteCount =
sizeof(Type) * DIM;
667 memcpy(rowptr, inverse.
_matrix[k], byteCount);
669 memcpy(inverse.
_matrix[index], rowptr, byteCount);
675 a1_inv = ((Type)1.0)/temp.
_matrix[k][k];
676 for (
int j = 0; j < DIM; ++j)
679 inverse.
_matrix[k][j] *= a1_inv;
689 for (
int i = 0; i < DIM; ++i)
694 for (
int j = 0; j < DIM; ++j)
705 template<
class Type,
int DIM>
712 template<
class Type,
int DIM>
718 for (
int row = 0; row < DIM; ++row)
720 for (
int col = 0; col < DIM; ++col)
722 result.
_matrix[col][row] = _matrix[row][col];
728 template<
class Type,
int DIM>
733 result = Transposition();
737 template<
class Type,
int DIM>
743 for (
int row = 0; row < DIM; ++row)
745 for (
int col = 0; col < DIM; ++col)
747 if (col != row && !closeValues(_matrix[col][row],Type(0)))
754 template<
class Type,
int DIM>
761 static const GlsMatrix referenceIdentity;
762 if (0 == memcmp(_data, referenceIdentity.
_data,
sizeof(Type) * DIM * DIM))
768 if (IsScalar() && closeValues(_matrix[0][0],Type(1)))
774 template<
class Type,
int DIM>
783 Type val = _matrix[0][0];
784 for (
int i = 0; i < DIM; ++i)
786 if (!closeValues(_matrix[i][i],val))
792 template<
class Type,
int DIM>
796 return Determinant() == Type(0);
799 template<
class Type,
int DIM>
803 return Transposition() == *
this;
806 template<
class Type,
int DIM>
810 return Transposition() == -(*this);
813 template<
class Type,
int DIM>
818 for (
int row = 0; row < DIM-1; ++row)
820 for (
int col = row+1; col < DIM; ++col)
822 if (!closeValues(_matrix[col][row],Type(0)))
829 template<
class Type,
int DIM>
834 for (
int row = 1; row < DIM; ++row)
836 for (
int col = 0; col < row; ++col)
838 if (!closeValues(_matrix[col][row],Type(0)))
845 template<
class Type,
int DIM>
851 return _matrix[col][row];
854 template<
class Type,
int DIM>
856 Type GlsMatrix<Type, DIM>::operator () (
unsigned row,
unsigned col)
const
860 return _matrix[col][row];
863 template<
class Type,
int DIM>
865 bool GlsMatrix<Type, DIM>::operator == (
const GlsMatrix<Type, DIM>& rhs)
const
867 static const int SIZE = DIM*DIM;
868 for (
int i = 0; i < SIZE; ++i)
870 if (!closeValues(_data[i],rhs._data[i]))
876 template<
class Type,
int DIM>
878 bool GlsMatrix<Type, DIM>::operator != (
const GlsMatrix<Type, DIM>& rhs)
const
880 return !(*
this == rhs);
883 template<
class Type,
int DIM>
884 inline GlsMatrix<Type, DIM>&
885 GlsMatrix<Type, DIM>::operator = (
const GlsMatrix<Type, DIM>& m)
887 if (
this == &m)
return *
this;
889 memcpy(_data, m._data,
sizeof(Type)*DIM*DIM);
894 template<
class Type,
int DIM>
895 inline GlsMatrix<Type, DIM>&
896 GlsMatrix<Type, DIM>::operator = (
const CStyleMatrix m)
898 for (
int row = 0; row < DIM; ++row)
900 for (
int col = 0; col < DIM; ++col)
902 _matrix[col][row] = m[row][col];
909 template<
class Type,
int DIM>
911 GlsMatrix<Type, DIM> GlsMatrix<Type, DIM>::operator - ()
const
913 GlsMatrix<Type,DIM> temp;
915 for (
int row = 0; row < DIM; ++row)
917 for (
int col = 0; col < DIM; ++col)
919 temp._matrix[col][row] = -_matrix[col][row];
926 template<
class Type,
int DIM>
928 GlsMatrix<Type, DIM>&
929 GlsMatrix<Type, DIM>::operator += (
const GlsMatrix<Type, DIM>& rhs)
931 for (
int row = 0; row < DIM; ++row)
933 for (
int col = 0; col < DIM; ++col)
935 _matrix[col][row] += rhs._matrix[col][row];
941 template<
class Type,
int DIM>
943 GlsMatrix<Type, DIM>&
944 GlsMatrix<Type, DIM>::operator -= (
const GlsMatrix<Type, DIM>& rhs)
946 for (
int row = 0; row < DIM; ++row)
948 for (
int col = 0; col < DIM; ++col)
950 _matrix[col][row] -= rhs._matrix[col][row];
958 GlsMatrix<double, 4>&
959 GlsMatrix<double, 4>::operator *= (
const GlsMatrix<double,4>& rhs)
961 return *
this = *
this * rhs;
967 GlsMatrix<float, 4>::operator *= (
const GlsMatrix<float,4>& rhs)
969 return *
this = *
this * rhs;
972 template<
class Type,
int DIM>
974 GlsMatrix<Type, DIM>&
975 GlsMatrix<Type, DIM>::operator *= (
const GlsMatrix<Type, DIM>& rhs)
977 GlsMatrix<Type,DIM> result;
978 memset(result._data, 0,
sizeof(Type) * DIM * DIM);
979 for (
int row = 0; row < DIM; ++row)
981 for (
int col = 0; col < DIM; ++col)
983 for (
int i = 0; i < DIM; ++i)
985 result._matrix[col][row] += _matrix[i][row] * rhs._matrix[col][i];
993 template<
class Type,
int DIM>
995 GlsMatrix<Type, DIM>&
996 GlsMatrix<Type, DIM>::operator *= (Type scalar)
998 for (
int row = 0; row < DIM; ++row)
1000 for (
int col = 0; col < DIM; ++col)
1002 _matrix[col][row] *= scalar;
1008 template<
class Type,
int DIM>
1010 GlsMatrix<Type, DIM>&
1011 GlsMatrix<Type, DIM>::operator /= (Type scalar)
1013 for (
int row = 0; row < DIM; ++row)
1015 for (
int col = 0; col < DIM; ++col)
1017 _matrix[col][row] /= scalar;
1023 template<
class Type,
int DIM>
1025 GlsMatrix<Type, DIM>
1026 GlsMatrix<Type, DIM>::operator + (
const GlsMatrix<Type, DIM>& rhs)
const
1028 GlsMatrix<Type,DIM> result;
1029 for (
int row = 0; row < DIM; ++row)
1031 for (
int col = 0; col < DIM; ++col)
1033 result._matrix[col][row] = _matrix[col][row] + rhs._matrix[col][row];
1039 template<
class Type,
int DIM>
1041 GlsMatrix<Type, DIM>
1042 GlsMatrix<Type, DIM>::operator - (
const GlsMatrix<Type, DIM>& rhs)
const
1044 GlsMatrix<Type,DIM> result;
1045 for (
int row = 0; row < DIM; ++row)
1047 for (
int col = 0; col < DIM; ++col)
1049 result._matrix[col][row] = _matrix[col][row] - rhs._matrix[col][row];
1055 template<
class Type,
int DIM>
1057 GlsMatrix<Type, DIM> GlsMatrix<Type, DIM>::operator / (Type scalar)
const
1059 GlsMatrix<Type, DIM> result;
1060 for (
int row = 0; row < DIM; ++row)
1062 for (
int col = 0; col < DIM; ++col)
1064 result._matrix[col][row] = _matrix[col][row] / scalar;
1070 template <
class Type,
int DIM>
1072 std::ostream& operator << (std::ostream& ostrm, const GlsMatrix<Type, DIM>& m)
1074 for (
int row = 0; row < DIM; ++row)
1076 for (
int col = 0; col < DIM; ++col)
1079 ostrm << m(row,col) <<
' ';
1085 template <
class Type,
int DIM>
1087 std::istream& operator >> (std::istream& istrm, GlsMatrix<Type, DIM>& m)
1090 for (
int row = 0; row < DIM; ++row)
1092 for (
int col = 0; col < DIM; ++col)
1101 template<
class Type,
int DIM>
1107 for (
int i = 0, j = 0; i < DIM; ++i,j+=DIM)
1109 _matrix[i] = &(_data[j]);
1113 template<
class Type,
int DIM>
1124 for (
unsigned i = row; i < unsigned(DIM); ++i)
1126 if ((temp = fabs(_matrix[i][row])) > amax && temp != 0.0)
1133 if (_matrix[k][row] == Type(0))
1139 else if (k !=
int(row))
1145 size_t byteCount =
sizeof(Type) * DIM;
1146 memcpy(rowptr, _matrix[k], byteCount);
1147 memcpy(_matrix[k], _matrix[row], byteCount);
1148 memcpy(_matrix[row], rowptr, byteCount);
1162 template<
class Type,
int DIM>
1166 return (lv - PRECISION) <= rv && rv <= (lv + PRECISION);
bool IsScalar() const
Definition: gls_matrix.h:776
bool IsDiagonal() const
Definition: gls_matrix.h:739
GlsMatrix< Type, DIM > Transposition() const
Definition: gls_matrix.h:714
Definition: gls_matrix.h:62
bool IsLowerTriangular() const
Definition: gls_matrix.h:815
void Initialize()
Definition: gls_matrix.h:1103
static const double PRECISION
Definition: gls_matrix.h:314
GlsMatrix< Type, DIM > Inverse() const
Definition: gls_matrix.h:617
Type * _matrix[DIM]
Definition: gls_matrix.h:335
bool IsSkewSymmetric() const
Definition: gls_matrix.h:808
GlsMatrix()
Definition: gls_matrix.h:393
Type _data[DIM *DIM]
Definition: gls_matrix.h:323
Type Determinant() const
Definition: gls_matrix.h:549
bool IsIdentity() const
Definition: gls_matrix.h:756
void Clear()
Definition: gls_matrix.h:124
Type * Data()
Definition: gls_matrix.h:132
bool closeValues(Type lv, Type rv) const
Definition: gls_matrix.h:1164
const Type * Data() const
Definition: gls_matrix.h:140
void Dump() const
Definition: gls_matrix.h:523
bool IsSingular() const
Definition: gls_matrix.h:794
void Invert()
Definition: gls_matrix.h:707
int Pivot(unsigned row)
Definition: gls_matrix.h:1114
void MakeIdentity()
Definition: gls_matrix.h:539
void Transpose()
Definition: gls_matrix.h:730
bool IsUpperTriangular() const
Definition: gls_matrix.h:831
Definition: bmpimage.h:46
virtual ~GlsMatrix()
Definition: gls_matrix.h:427
bool IsSymmetric() const
Definition: gls_matrix.h:801