PandaRoot
matrix.h
Go to the documentation of this file.
1 /*
2 Copyright 2011. All rights reserved.
3 Institute of Measurement and Control Systems
4 Karlsruhe Institute of Technology, Germany
5 
6 Authors: Andreas Geiger
7 
8 matrix is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 2 of the License, or any later version.
11 
12 matrix is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14 PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License along with
17 matrix; if not, write to the Free Software Foundation, Inc., 51 Franklin
18 Street, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 #ifndef MATRIX_H
22 #define MATRIX_H
23 
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <iostream>
28 #include <vector>
29 
30 #ifndef _MSC_VER
31 //#include <stdint.h>
32 typedef int int32_t;
33 #else
34 typedef __int8 int8_t;
35 typedef __int16 int16_t;
36 typedef __int32 int32_t;
37 typedef __int64 int64_t;
38 typedef unsigned __int8 uint8_t;
39 typedef unsigned __int16 uint16_t;
40 typedef unsigned __int32 uint32_t;
41 typedef unsigned __int64 uint64_t;
42 #endif
43 
44 //#define endll endl << endl // double end line definition
45 
46 // typedef long double FLOAT; //quad precision
47 typedef double FLOAT; // double precision
48 // typedef float FLOAT; // single precision
49 
50 class Matrix {
51 
52  public:
53  // constructor / deconstructor
54  Matrix(); // init empty 0x0 matrix
55  Matrix(const int32_t m, const int32_t n); // init empty mxn matrix
56  Matrix(const int32_t m, const int32_t n, const FLOAT *val_); // init mxn matrix with values from array 'val'
57  Matrix(const Matrix &M); // creates deepcopy of M
58  ~Matrix();
59 
60  // assignment operator, copies contents of M
61  Matrix &operator=(const Matrix &M);
62 
63  // copies submatrix of M into array 'val', default values copy whole row/column/matrix
64  void getData(FLOAT *val_, int32_t i1 = 0, int32_t j1 = 0, int32_t i2 = -1, int32_t j2 = -1);
65 
66  // set or get submatrices of current matrix
67  Matrix getMat(int32_t i1, int32_t j1, int32_t i2 = -1, int32_t j2 = -1);
68  void setMat(const Matrix &M, const int32_t i, const int32_t j);
69 
70  // set mxn matrix with values from array 'val'
71  void setVal(const int32_t m, const int32_t n, const FLOAT *val_);
72 
73  // set sub-matrix to scalar (default 0), -1 as end replaces whole row/column/matrix
74  void setVal(FLOAT s, int32_t i1 = 0, int32_t j1 = 0, int32_t i2 = -1, int32_t j2 = -1);
75 
76  // set (part of) diagonal to scalar, -1 as end replaces whole diagonal
77  void setDiag(FLOAT s, int32_t i1 = 0, int32_t i2 = -1);
78 
79  // clear matrix
80  void zero();
81 
82  // extract columns with given index
83  Matrix extractCols(std::vector<int> idx);
84 
85  // create identity matrix
86  static Matrix eye(const int32_t m);
87  void eye();
88 
89  // create matrix with ones
90  static Matrix ones(const int32_t m, const int32_t n);
91 
92  // create diagonal matrix with nx1 or 1xn matrix M as elements
93  static Matrix diag(const Matrix &M);
94 
95  // returns the m-by-n matrix whose elements are taken column-wise from M
96  static Matrix reshape(const Matrix &M, int32_t m, int32_t n);
97 
98  // create 3x3 rotation matrices (convention: http://en.wikipedia.org/wiki/Rotation_matrix)
99  static Matrix rotMatX(const FLOAT &angle);
100  static Matrix rotMatY(const FLOAT &angle);
101  static Matrix rotMatZ(const FLOAT &angle);
102 
103  // homogenize 3x3 matrix or 3 vector to 4x4 matrix or 4 vector
104  static Matrix homogenize(const Matrix &M);
105  // de-homogenize a 4 vector
106  static Matrix dehomogenize(const Matrix &M);
107  // homogenize 3 vector to 4x4 translation matrix
108  static Matrix homogenizeTranslation(const Matrix &M);
109  // homogenize rotation and translation simultaniously
110  static Matrix homogenizeRotTrans(const Matrix &R, const Matrix &t);
111 
112  // simple arithmetic operations
113  Matrix operator+(const Matrix &M); // add matrix
114  Matrix operator-(const Matrix &M); // subtract matrix
115  Matrix operator*(const Matrix &M); // multiply with matrix
116  Matrix operator*(const FLOAT &s); // multiply with scalar
117  Matrix operator/(const Matrix &M); // divide elementwise by matrix (or vector)
118  Matrix operator/(const FLOAT &s); // divide by scalar
119  Matrix operator-(); // negative matrix
120  Matrix operator~(); // transpose
121  FLOAT l2norm(); // euclidean norm (vectors) / frobenius norm (matrices)
122  FLOAT mean(); // mean of all elements in matrix
123 
124  // complex arithmetic operations
125  static Matrix cross(const Matrix &a, const Matrix &b); // cross product of two vectors
126  static Matrix inv(const Matrix &M); // invert matrix M
127  bool inv(); // invert this matrix
128  FLOAT det(); // returns determinant of matrix
129  bool solve(const Matrix &M, FLOAT eps = 1e-20); // solve linear system M*x=B, replaces *this and M
130  bool lu(int32_t *idx, FLOAT &d, FLOAT eps = 1e-20); // replace *this by lower upper decomposition
131  void svd(Matrix &U, Matrix &W, Matrix &V); // singular value decomposition *this = U*diag(W)*V^T
132 
133  // print matrix to stream
134  friend std::ostream &operator<<(std::ostream &out, const Matrix &M);
135 
136  // direct data access
139 
140  private:
141  void allocateMemory(const int32_t m_, const int32_t n_);
142  void releaseMemory();
143  inline FLOAT pythag(FLOAT a, FLOAT b);
144 };
145 
146 #endif // MATRIX_H
bool solve(const Matrix &M, FLOAT eps=1e-20)
void setMat(const Matrix &M, const int32_t i, const int32_t j)
void zero()
Matrix operator-()
static Matrix ones(const int32_t m, const int32_t n)
bool lu(int32_t *idx, FLOAT &d, FLOAT eps=1e-20)
static Matrix diag(const Matrix &M)
FLOAT det()
Matrix operator~()
static Matrix homogenizeTranslation(const Matrix &M)
static Matrix homogenize(const Matrix &M)
Matrix operator*(const Matrix &M)
double FLOAT
Definition: matrix.h:47
Matrix operator/(const Matrix &M)
Matrix & operator=(const Matrix &M)
unsigned int i
Definition: P4_F32vec4.h:21
static Matrix reshape(const Matrix &M, int32_t m, int32_t n)
void setVal(const int32_t m, const int32_t n, const FLOAT *val_)
int int32_t
Definition: matrix.h:32
void setDiag(FLOAT s, int32_t i1=0, int32_t i2=-1)
static Matrix cross(const Matrix &a, const Matrix &b)
FLOAT mean()
static Matrix rotMatX(const FLOAT &angle)
int32_t m
Definition: matrix.h:138
Definition: matrix.h:50
bool inv()
int32_t n
Definition: matrix.h:138
static Matrix homogenizeRotTrans(const Matrix &R, const Matrix &t)
void getData(FLOAT *val_, int32_t i1=0, int32_t j1=0, int32_t i2=-1, int32_t j2=-1)
friend std::ostream & operator<<(std::ostream &out, const Matrix &M)
Matrix operator+(const Matrix &M)
static Matrix rotMatZ(const FLOAT &angle)
static Matrix dehomogenize(const Matrix &M)
void svd(Matrix &U, Matrix &W, Matrix &V)
FLOAT l2norm()
Matrix extractCols(std::vector< int > idx)
void eye()
Matrix getMat(int32_t i1, int32_t j1, int32_t i2=-1, int32_t j2=-1)
FLOAT ** val
Definition: matrix.h:137
static Matrix rotMatY(const FLOAT &angle)