libelas
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 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 <iostream>
29 #include <vector>
30 
31 #ifndef _MSC_VER
32  #include <stdint.h>
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 double FLOAT; // double precision
47 //typedef float FLOAT; // single precision
48 
49 class Matrix {
50 
51 public:
52 
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 sub-matrix to scalar (default 0), -1 as end replaces whole row/column/matrix
71  void setVal(FLOAT s,int32_t i1=0,int32_t j1=0,int32_t i2=-1,int32_t j2=-1);
72 
73  // set (part of) diagonal to scalar, -1 as end replaces whole diagonal
74  void setDiag(FLOAT s,int32_t i1=0,int32_t i2=-1);
75 
76  // clear matrix
77  void zero();
78 
79  // extract columns with given index
80  Matrix extractCols (std::vector<int> idx);
81 
82  // create identity matrix
83  static Matrix eye (const int32_t m);
84  void eye ();
85 
86  // create diagonal matrix with nx1 or 1xn matrix M as elements
87  static Matrix diag(const Matrix &M);
88 
89  // returns the m-by-n matrix whose elements are taken column-wise from M
90  static Matrix reshape(const Matrix &M,int32_t m,int32_t n);
91 
92  // create 3x3 rotation matrices (convention: http://en.wikipedia.org/wiki/Rotation_matrix)
93  static Matrix rotMatX(const FLOAT &angle);
94  static Matrix rotMatY(const FLOAT &angle);
95  static Matrix rotMatZ(const FLOAT &angle);
96 
97  // simple arithmetic operations
98  Matrix operator+ (const Matrix &M); // add matrix
99  Matrix operator- (const Matrix &M); // subtract matrix
100  Matrix operator* (const Matrix &M); // multiply with matrix
101  Matrix operator* (const FLOAT &s); // multiply with scalar
102  Matrix operator/ (const Matrix &M); // divide elementwise by matrix (or vector)
103  Matrix operator/ (const FLOAT &s); // divide by scalar
104  Matrix operator- (); // negative matrix
105  Matrix operator~ (); // transpose
106  FLOAT l2norm (); // euclidean norm (vectors) / frobenius norm (matrices)
107  FLOAT mean (); // mean of all elements in matrix
108 
109  // complex arithmetic operations
110  static Matrix cross (const Matrix &a, const Matrix &b); // cross product of two vectors
111  static Matrix inv (const Matrix &M); // invert matrix M
112  bool inv (); // invert this matrix
113  FLOAT det (); // returns determinant of matrix
114  bool solve (const Matrix &M,FLOAT eps=1e-20); // solve linear system M*x=B, replaces *this and M
115  bool lu(int32_t *idx, FLOAT &d, FLOAT eps=1e-20); // replace *this by lower upper decomposition
116  void svd(Matrix &U,Matrix &W,Matrix &V); // singular value decomposition *this = U*diag(W)*V^T
117 
118  // print matrix to stream
119  friend std::ostream& operator<< (std::ostream& out,const Matrix& M);
120 
121  // direct data access
123  int32_t m,n;
124 
125 private:
126 
127  void allocateMemory (const int32_t m_,const int32_t n_);
128  void releaseMemory ();
129  inline FLOAT pythag(FLOAT a,FLOAT b);
130 
131 };
132 
133 #endif // MATRIX_H
bool solve(const Matrix &M, FLOAT eps=1e-20)
Definition: matrix.cpp:413
void setMat(const Matrix &M, const int32_t i, const int32_t j)
Definition: matrix.cpp:103
void zero()
Definition: matrix.cpp:133
Matrix operator-()
Definition: matrix.cpp:331
bool lu(int32_t *idx, FLOAT &d, FLOAT eps=1e-20)
Definition: matrix.cpp:510
static Matrix rotMatY(const FLOAT &angle)
Definition: matrix.cpp:206
FLOAT det()
Definition: matrix.cpp:397
Matrix()
Definition: matrix.cpp:37
Matrix operator~()
Definition: matrix.cpp:339
Matrix operator*(const Matrix &M)
Definition: matrix.cpp:260
double FLOAT
Definition: matrix.h:46
Matrix operator/(const Matrix &M)
Definition: matrix.cpp:284
static Matrix cross(const Matrix &a, const Matrix &b)
Definition: matrix.cpp:363
static Matrix rotMatZ(const FLOAT &angle)
Definition: matrix.cpp:218
void setDiag(FLOAT s, int32_t i1=0, int32_t i2=-1)
Definition: matrix.cpp:127
FLOAT mean()
Definition: matrix.cpp:355
static Matrix reshape(const Matrix &M, int32_t m, int32_t n)
Definition: matrix.cpp:177
static Matrix diag(const Matrix &M)
Definition: matrix.cpp:161
int32_t m
Definition: matrix.h:123
Definition: matrix.h:49
bool inv()
Definition: matrix.cpp:386
int32_t n
Definition: matrix.h:123
void getData(FLOAT *val_, int32_t i1=0, int32_t j1=0, int32_t i2=-1, int32_t j2=-1)
Definition: matrix.cpp:78
static Matrix rotMatX(const FLOAT &angle)
Definition: matrix.cpp:194
friend std::ostream & operator<<(std::ostream &out, const Matrix &M)
Matrix operator+(const Matrix &M)
Definition: matrix.cpp:230
Matrix & operator=(const Matrix &M)
Definition: matrix.cpp:65
void svd(Matrix &U, Matrix &W, Matrix &V)
Definition: matrix.cpp:575
FLOAT l2norm()
Definition: matrix.cpp:347
void setVal(FLOAT s, int32_t i1=0, int32_t j1=0, int32_t i2=-1, int32_t j2=-1)
Definition: matrix.cpp:115
Matrix extractCols(std::vector< int > idx)
Definition: matrix.cpp:137
void eye()
Definition: matrix.cpp:153
Matrix getMat(int32_t i1, int32_t j1, int32_t i2=-1, int32_t j2=-1)
Definition: matrix.cpp:87
FLOAT ** val
Definition: matrix.h:122
~Matrix()
Definition: matrix.cpp:61