21 #ifndef MIRTK_Matrix_H 22 #define MIRTK_Matrix_H 24 #include "mirtk/Object.h" 26 #include "mirtk/Math.h" 27 #include "mirtk/Array.h" 28 #include "mirtk/Pair.h" 29 #include "mirtk/Indent.h" 30 #include "mirtk/Cfstream.h" 31 #include "mirtk/Matrix3x3.h" 56 typedef double ElementType;
82 Matrix(
int,
int = -1,
double * = NULL);
96 explicit Matrix(
const Matrix3x3 &);
111 void Initialize(
int,
int = -1,
double * = NULL);
114 void Resize(
int,
int = -1);
129 Pair<int, int>
Size()
const;
138 int Index(
int,
int)
const;
147 void SubIndex(
int,
int &,
int &)
const;
159 const double *
RawPointer(
int r = 0,
int c = 0)
const;
173 const double *
Col(
int c)
const;
188 void Put(
int,
double);
191 double Get(
int)
const;
194 void Put(
int,
int,
double);
197 double Get(
int,
int)
const;
314 void RowRange(
int,
double &,
double &)
const;
335 void ColRange(
int,
double &,
double &)
const;
363 double Trace()
const;
366 double InfinityNorm()
const;
375 Matrix3x3
To3x3()
const;
477 void Read(
const char *);
480 void Write(
const char *)
const;
483 void Import(
const char *,
int,
int);
488 bool WriteMAT(
const char *,
const char * =
"A")
const;
499 #include "mirtk/Point.h" 516 for (
int c = 0; c < 3; ++c)
517 for (
int r = 0; r < 3; ++r) {
531 for (
int c = 0; c < 3; ++c)
532 for (
int r = 0; r < 3; ++r) {
569 return c *
_cols + r;
646 return const_cast<const double &
>(
const_cast<Matrix *
>(
this)->
operator()(i));
658 return const_cast<const double &
>(
const_cast<Matrix *
>(
this)->
operator()(r, c));
689 for (
int r = 0; r < 3; ++r) {
691 for (
int c = 0; c < 3; ++c) {
699 memset(m[r], 0, 3 *
sizeof(
double));
742 for (
int i = 0; i < n; ++i, ++p) (*p) = s;
751 for (
int i = 0; i < n; ++i, ++p) (*p) -= x;
760 for (
int i = 0; i < n; ++i, ++p) (*p) += x;
769 for (
int i = 0; i < n; ++i, ++p) (*p) *= x;
778 for (
int i = 0; i < n; ++i, ++p) (*p) /= x;
785 return Matrix(*
this) -= x;
791 return Matrix(*
this) += x;
797 return Matrix(*
this) *= x;
803 return Matrix(*
this) /= x;
817 for (
int i = 0; i < n; ++i, ++ptr1, ++ptr2) {
818 if (!
fequal(*ptr2, *ptr1))
return false;
826 return !(*
this == m);
835 for (
int i = 0; i < n; ++i, ++p) {
836 if (*p == x) idx.push_back(i);
847 for (
int i = 0; i < n; ++i, ++p) {
848 if (*p != x) idx.push_back(i);
859 for (
int i = 0; i < n; ++i, ++p) {
860 if (*p < x) idx.push_back(i);
871 for (
int i = 0; i < n; ++i, ++p) {
872 if (*p <= x) idx.push_back(i);
883 for (
int i = 0; i < n; ++i, ++p) {
884 if (*p > x) idx.push_back(i);
895 for (
int i = 0; i < n; ++i, ++p) {
896 if (*p >= x) idx.push_back(i);
908 if (
_rows == 0)
return .0;
910 for (
int c = 1; c <
_cols; ++c) v = min(v,
_matrix[c][r]);
917 if (
_rows == 0)
return .0;
919 for (
int c = 1; c <
_cols; ++c) v = max(v,
_matrix[c][r]);
928 for (
int c = 1; c <
_cols; ++c) {
929 const double &v =
_matrix[c][r];
930 v1 = min(v1, v), v2 = max(v2, v);
940 if (
_cols == 0)
return .0;
942 for (
int r = 1; r <
_rows; ++r) v = min(v,
_matrix[c][r]);
949 if (
_cols == 0)
return .0;
951 for (
int r = 1; r <
_rows; ++r) v = min(v,
_matrix[c][r]);
960 for (
int r = 1; r <
_rows; ++r) {
961 const double &v =
_matrix[c][r];
962 v1 = min(v1, v), v2 = max(v2, v);
974 for (
int j = 0; j <
_cols; ++j)
975 for (
int i = 0; i <
_rows; ++i) {
980 cerr <<
"Matrix::Trace() matrix number of col != row" << endl;
992 for (j = 0; j <
_cols; j++) {
993 for (i = 0; i <
_rows; i++) {
1001 inline double Matrix::InfinityNorm()
const 1004 double normInf = -1.0 * DBL_MAX;
1007 for (i = 0; i <
_rows; ++i) {
1009 for (j = 0; j <
_cols; ++j) {
1023 + _matrix[0][2] * (_matrix[1][0] * _matrix[2][1] - _matrix[1][1] * _matrix[2][0]);
1029 return Matrix(*this).Invert(use_svd_if_singular);
1035 return Matrix(*this).SVDInvert();
1041 return Matrix(*this).PseudoInvert();
1053 return Matrix(*this).Transpose();
1059 return Matrix(*this).Transpose();
1093 f.a = make_float3(m(0, 0), m(0, 1), m(0, 2));
1094 f.b = make_float3(m(1, 0), m(1, 1), m(1, 2));
1095 f.c = make_float3(m(2, 0), m(2, 1), m(2, 2));
1104 f.a = make_float4(m(0, 0), m(0, 1), m(0, 2), m(0, 3));
1105 f.b = make_float4(m(1, 0), m(1, 1), m(1, 2), m(1, 3));
1106 f.c = make_float4(m(2, 0), m(2, 1), m(2, 2), m(2, 3));
1115 d.a = make_float4(m(0, 0), m(0, 1), m(0, 2), m(0, 3));
1116 d.b = make_float4(m(1, 0), m(1, 1), m(1, 2), m(1, 3));
1117 d.c = make_float4(m(2, 0), m(2, 1), m(2, 2), m(2, 3));
1118 d.d = make_float4(m(3, 0), m(3, 1), m(3, 2), m(3, 3));
1127 d.a = make_double3(m(0, 0), m(0, 1), m(0, 2));
1128 d.b = make_double3(m(1, 0), m(1, 1), m(1, 2));
1129 d.c = make_double3(m(2, 0), m(2, 1), m(2, 2));
1138 d.a = make_double4(m(0, 0), m(0, 1), m(0, 2), m(0, 3));
1139 d.b = make_double4(m(1, 0), m(1, 1), m(1, 2), m(1, 3));
1140 d.c = make_double4(m(2, 0), m(2, 1), m(2, 2), m(2, 3));
1149 d.a = make_double4(m(0, 0), m(0, 1), m(0, 2), m(0, 3));
1150 d.b = make_double4(m(1, 0), m(1, 1), m(1, 2), m(1, 3));
1151 d.c = make_double4(m(2, 0), m(2, 1), m(2, 2), m(2, 3));
1152 d.d = make_double4(m(3, 0), m(3, 1), m(3, 2), m(3, 3));
1199 const double *weights = NULL,
1200 int niter = 20,
double tol = 1e-12,
1201 const Matrix *mu0 = NULL);
1206 int niter = 20,
double tol = 1e-12,
1207 const Matrix *mu0 = NULL)
1215 int niter = 20,
double tol = 1e-12,
1216 const Matrix *mu0 = NULL)
1227 double &mx,
double &my,
double &mz)
1229 mx = m(0, 0) * x + m(0, 1) * y + m(0, 2) * z + m(0, 3);
1230 my = m(1, 0) * x + m(1, 1) * y + m(1, 2) * z + m(1, 3);
1231 mz = m(2, 0) * x + m(2, 1) * y + m(2, 2) * z + m(2, 3);
1250 double &mx,
double &my,
double &mz)
1252 mx = m(0, 0) * x + m(0, 1) * y + m(0, 2) * z;
1253 my = m(1, 0) * x + m(1, 1) * y + m(1, 2) * z;
1254 mz = m(2, 0) * x + m(2, 1) * y + m(2, 2) * z;
1298 double cosrx,
double cosry,
double cosrz,
1299 double sinrx,
double sinry,
double sinrz,
1316 double rx,
double ry,
double rz,
Matrix &m);
1333 double rx,
double ry,
double rz)
1351 double &rx,
double &ry,
double &rz);
1367 double &tx,
double &ty,
double &tz,
1368 double &rx,
double &ry,
double &rz);
1391 double rx,
double ry,
double rz,
1392 double sx,
double sy,
double sz,
1393 double sxy,
double sxz,
double syz,
Matrix &m);
1416 double rx,
double ry,
double rz,
1417 double sx,
double sy,
double sz,
1418 double sxy,
double sxz,
double syz)
1421 AffineParametersToMatrix(tx, ty, tz, rx, ry, rz, sx, sy, sz, sxy, sxz, syz, m);
1442 double rx,
double ry,
double rz,
1443 double sx,
double sy,
double sz,
Matrix &m)
1445 AffineParametersToMatrix(tx, ty, tz, rx, ry, rz, sx, sy, sz, .0, .0, .0, m);
1466 double rx,
double ry,
double rz,
1467 double sx,
double sy,
double sz)
1470 AffineParametersToMatrix(tx, ty, tz, rx, ry, rz, sx, sy, sz, .0, .0, .0, m);
1498 double &tx,
double &ty,
double &tz,
1499 double &rx,
double &ry,
double &rz,
1500 double &sx,
double &sy,
double &sz,
1501 double &sxy,
double &sxz,
double &syz);
1532 #endif // MIRTK_Matrix_H void RowRange(int, double &, double &) const
Minimum and maximum value in specified row.
double & operator()(int)
Get reference to element with specified linear index.
void Resize(int, int=-1)
Resize matrix preserving existing entries.
bool Eigenvalues(Matrix &, Vector &, Matrix &) const
bool IsSquare() const
Whether matrix is square.
bool operator!=(const Matrix &) const
Matrix inequality.
int NumberOfElements() const
Get number of elements.
Matrix operator/(double) const
Return result of division by a double.
Matrix & Adjugate(double &)
Adjugate matrix and return determinant.
double Get(int) const
Get value of element with specified linear index.
Matrix & ScaleRow(int, double)
Scale row by a scalar.
Matrix operator!()
Matrix inversion operator.
double Trace() const
Calculate trace of matrix.
void SubIndex(int, int &, int &) const
Get row and column index of element given its linear index.
void MatrixToEulerAngles(const Matrix &m, double &rx, double &ry, double &rz)
bool IsIdentity() const
Returns true if the matrix is an identity matrix.
Matrix PseudoInverse() const
Get pseudo inverse of matrix.
double Det() const
Calculate determinant of matrix.
void LeastSquaresFit(const Vector &, Vector &) const
Calculate least square fit via SVD.
void MatrixToRigidParameters(const Matrix &m, double &tx, double &ty, double &tz, double &rx, double &ry, double &rz)
3x4 single-precision coordinate transformation matrix
4x4 single-precision matrix
Matrix Sqrt() const
Matrix square root.
double Det3x3() const
Calculate determinant of a 3x3 matrix.
Matrix LogEuclideanMean(int n, const Matrix *matrices, const double *weights=NULL)
double * Col(int c)
Get pointer to matrix entries in specified column.
void MatrixToAffineParameters(const Matrix &m, double &tx, double &ty, double &tz, double &rx, double &ry, double &rz, double &sx, double &sy, double &sz, double &sxy, double &sxz, double &syz)
void Read(const char *)
Read matrix from file.
void Zero()
Set elements to zero.
double ColMax(int) const
Maximum value in specified column.
double RowMax(int) const
Maximum value in specified row.
bool IsSymmetric() const
Whether matrix is symmetric.
double _x
x coordinate of Point
3x3 double-precision matrix
Matrix ApproximateAffineMatrix(const PointSet &target, const PointSet &source, const Vector &weight)
void SymmetricEigen(Matrix &, Vector &) const
Calculate eigendecomposition of symmetric matrix.
void Allocate(Type *&matrix, int n)
Allocate 1D array.
void RigidParametersToMatrix(double tx, double ty, double tz, double cosrx, double cosry, double cosrz, double sinrx, double sinry, double sinrz, Matrix &m)
double Norm() const
Calculate norm of matrix.
Matrix & PermuteCols(Array< int >)
Permute columns.
bool WriteMAT(const char *, const char *="A") const
int ColIndex(int) const
Get column index of element given its linear index.
Matrix & Ident()
Set to identity matrix.
Matrix & Transpose()
Transpose matrix.
3x3 single-precision matrix
void LU(Matrix &, Matrix &, double &) const
Calculate LU decomposition of square matrix.
friend ostream & operator<<(ostream &, const Matrix &)
Interface to output stream.
int RowIndex(int) const
Get row index of element given its linear index.
Matrix & operator/=(double)
Division by a double.
void Import(const char *, int, int)
Import matrix from text file (requires no. of expected rows and cols)
int Cols() const
Get number of columns.
double ColStd(int) const
Standard deviation of row values in specified column.
MIRTKCU_API bool fequal(double a, double b, double tol=1e-12)
Matrix Inverse(bool use_svd_if_singular=true) const
Get inverse matrix.
void Initialize(int, int=-1, double *=NULL)
Initialize matrix with number of rows and columns.
double ColVar(int) const
Variance of row values in specified column.
void MakeSymmetric()
Make square matrix symmetric by adding its transpose and divide by 2.
Matrix & Invert(bool use_svd_if_singular=true)
Invert matrix.
void Print(Indent=0) const
Print matrix.
Matrix & ScaleCol(int, double)
Scale column by a scalar.
3x4 double-precision coordinate transformation matrix
4x4 double-precision matrix
Matrix operator-(double) const
Return result of subtraction of a double.
bool _owner
Whether this instance owns the memory of the matrix elements.
Matrix & operator=(double)
Assign scalar value to all elements.
friend istream & operator>>(istream &, Matrix &)
Interface to input stream.
Matrix SVDInverse() const
Get inverse of (singular) matrix using SVD.
double ** _matrix
Matrix values.
void Put(int, double)
Set value of element with specified linear index.
Matrix operator~()
Matrix transpose operator.
double RowSum(int) const
Sum of column values in specified row.
Matrix Transposed() const
Get transposed matrix.
Matrix()
Default constructor.
Matrix & AddToRow(int, double)
Add scalar to values in specified row.
Array< int > operator>(double) const
Matrix3x3 To3x3() const
Get upper left 3x3 sub-matrix.
double * RawPointer(int r=0, int c=0)
Get pointer to linear memory which stores matrix elements in column-major order.
Matrix & PermuteRows(Array< int >)
Permute rows.
double ColMean(int) const
Mean of row values in specified column.
void Write(const char *) const
Write matrix to file.
Matrix BiInvariantMean(int n, const Matrix *matrices, const double *weights=NULL, int niter=20, double tol=1e-12, const Matrix *mu0=NULL)
Matrix FrechetMean(const Matrix *matrices, const double *weights, int n, int niter=20, double tol=1e-12, const Matrix *mu0=NULL)
Array< int > operator<(double) const
Matrix Log() const
Matrix logarithm.
void TransformVector(const Matrix &m, double x, double y, double z, double &mx, double &my, double &mz)
Muliply vector by 3x3 transformation matrix.
Matrix & SVDInvert()
Invert (singular) matrix using SVD.
bool IsDiagonalizable() const
Whether matrix is diagonalizable.
double ColSum(int) const
Sum of row values in specified column.
int Index(int, int) const
Get linear index of element given its row and column indices.
Matrix & operator*=(double)
Multiplication with a double.
Matrix operator*(double) const
Return result of multiplication with a double.
Array< int > operator<=(double) const
Matrix & AddToCol(int, double)
Add scalar to values in specified column.
double RowMean(int) const
Mean of column values in specified row.
Matrix operator+(double) const
Return result of addition of a double.
void ColRange(int, double &, double &) const
Minimum and maximum value in specified column.
Array< int > operator>=(double) const
double * GetPointerToElements(int r=0, int c=0)
double _z
z coordinate of Point
void AffineParametersToMatrix(double tx, double ty, double tz, double rx, double ry, double rz, double sx, double sy, double sz, double sxy, double sxz, double syz, Matrix &m)
bool operator==(const Matrix &) const
Matrix equality.
double RowMin(int) const
Minimum value in specified row.
Matrix & operator+=(double)
Addition of a double.
double _y
y coordinate of Point
int Rows() const
Get number of rows.
void SVD(Matrix &, Vector &, Matrix &) const
Calculate singular value decomposition.
double RowVar(int) const
Variance of column values in specified row.
void OrthoNormalize3x3(Matrix &)
Orthonormalize upper 3x3 matrix using stabilized Gram-Schmidt.
Matrix & PseudoInvert()
Invert matrix using pseudo inverse.
double ColMin(int) const
Minimum value in specified column.
int _cols
Number of colums.
void Transform(Array< T > &values, UnaryOperation op)
Apply unary operation for each array element in-place.
Matrix & operator-=(double)
Subtraction of a double.
double RowStd(int) const
Standard deviation of column values in specified row.
Pair< int, int > Size() const
Get matrix size in each dimension.