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