matrix.h
Go to the documentation of this file.
00001 /*
00002 Copyright 2011. All rights reserved.
00003 Institute of Measurement and Control Systems
00004 Karlsruhe Institute of Technology, Germany
00005 
00006 This file is part of libviso2.
00007 Authors: Andreas Geiger
00008 
00009 libviso2 is free software; you can redistribute it and/or modify it under the
00010 terms of the GNU General Public License as published by the Free Software
00011 Foundation; either version 2 of the License, or any later version.
00012 
00013 libviso2 is distributed in the hope that it will be useful, but WITHOUT ANY
00014 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
00015 PARTICULAR PURPOSE. See the GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License along with
00018 libviso2; if not, write to the Free Software Foundation, Inc., 51 Franklin
00019 Street, Fifth Floor, Boston, MA 02110-1301, USA 
00020 */
00021 
00022 #ifndef MATRIX_H
00023 #define MATRIX_H
00024 
00025 #include <stdio.h>
00026 #include <string.h>
00027 #include <stdlib.h>
00028 #include <iostream>
00029 #include <vector>
00030 
00031 #ifndef _MSC_VER
00032   #include <stdint.h>
00033 #else
00034   typedef __int8            int8_t;
00035   typedef __int16           int16_t;
00036   typedef __int32           int32_t;
00037   typedef __int64           int64_t;
00038   typedef unsigned __int8   uint8_t;
00039   typedef unsigned __int16  uint16_t;
00040   typedef unsigned __int32  uint32_t;
00041   typedef unsigned __int64  uint64_t;
00042 #endif
00043 
00044 #define endll endl << endl // double end line definition
00045 
00046 typedef double FLOAT;      // double precision
00047 //typedef float  FLOAT;    // single precision
00048 
00049 class Matrix {
00050 
00051 public:
00052 
00053   // constructor / deconstructor
00054   Matrix ();                                                  // init empty 0x0 matrix
00055   Matrix (const int32_t m,const int32_t n);                   // init empty mxn matrix
00056   Matrix (const int32_t m,const int32_t n,const FLOAT* val_); // init mxn matrix with values from array 'val'
00057   Matrix (const Matrix &M);                                   // creates deepcopy of M
00058   ~Matrix ();
00059 
00060   // assignment operator, copies contents of M
00061   Matrix& operator= (const Matrix &M);
00062 
00063   // copies submatrix of M into array 'val', default values copy whole row/column/matrix
00064   void getData(FLOAT* val_,int32_t i1=0,int32_t j1=0,int32_t i2=-1,int32_t j2=-1);
00065 
00066   // set or get submatrices of current matrix
00067   Matrix getMat(int32_t i1,int32_t j1,int32_t i2=-1,int32_t j2=-1);
00068   void   setMat(const Matrix &M,const int32_t i,const int32_t j);
00069 
00070   // set sub-matrix to scalar (default 0), -1 as end replaces whole row/column/matrix
00071   void setVal(FLOAT s,int32_t i1=0,int32_t j1=0,int32_t i2=-1,int32_t j2=-1);
00072 
00073   // set (part of) diagonal to scalar, -1 as end replaces whole diagonal
00074   void setDiag(FLOAT s,int32_t i1=0,int32_t i2=-1);
00075 
00076   // clear matrix
00077   void zero();
00078   
00079   // extract columns with given index
00080   Matrix extractCols (std::vector<int> idx);
00081 
00082   // create identity matrix
00083   static Matrix eye (const int32_t m);
00084   void          eye ();
00085 
00086   // create diagonal matrix with nx1 or 1xn matrix M as elements
00087   static Matrix diag(const Matrix &M);
00088   
00089   // returns the m-by-n matrix whose elements are taken column-wise from M
00090   static Matrix reshape(const Matrix &M,int32_t m,int32_t n);
00091 
00092   // create 3x3 rotation matrices (convention: http://en.wikipedia.org/wiki/Rotation_matrix)
00093   static Matrix rotMatX(const FLOAT &angle);
00094   static Matrix rotMatY(const FLOAT &angle);
00095   static Matrix rotMatZ(const FLOAT &angle);
00096 
00097   // simple arithmetic operations
00098   Matrix  operator+ (const Matrix &M); // add matrix
00099   Matrix  operator- (const Matrix &M); // subtract matrix
00100   Matrix  operator* (const Matrix &M); // multiply with matrix
00101   Matrix  operator* (const FLOAT &s);  // multiply with scalar
00102   Matrix  operator/ (const Matrix &M); // divide elementwise by matrix (or vector)
00103   Matrix  operator/ (const FLOAT &s);  // divide by scalar
00104   Matrix  operator- ();                // negative matrix
00105   Matrix  operator~ ();                // transpose
00106   FLOAT   l2norm ();                   // euclidean norm (vectors) / frobenius norm (matrices)
00107   FLOAT   mean ();                     // mean of all elements in matrix
00108 
00109   // complex arithmetic operations
00110   static Matrix cross (const Matrix &a, const Matrix &b);    // cross product of two vectors
00111   static Matrix inv (const Matrix &M);                       // invert matrix M
00112   bool   inv ();                                             // invert this matrix
00113   FLOAT  det ();                                             // returns determinant of matrix
00114   bool   solve (const Matrix &M,FLOAT eps=1e-20);            // solve linear system M*x=B, replaces *this and M
00115   bool   lu(int32_t *idx, FLOAT &d, FLOAT eps=1e-20);        // replace *this by lower upper decomposition
00116   void   svd(Matrix &U,Matrix &W,Matrix &V);                 // singular value decomposition *this = U*diag(W)*V^T
00117 
00118   // print matrix to stream
00119   friend std::ostream& operator<< (std::ostream& out,const Matrix& M);
00120 
00121   // direct data access
00122   FLOAT   **val;
00123   int32_t   m,n;
00124 
00125 private:
00126 
00127   void allocateMemory (const int32_t m_,const int32_t n_);
00128   void releaseMemory ();
00129   inline FLOAT pythag(FLOAT a,FLOAT b);
00130 
00131 };
00132 
00133 #endif // MATRIX_H


libviso2
Author(s): Andreas Geiger, maintained by: Stephan Wirth/stephan.wirth@uib.es
autogenerated on Tue Jan 7 2014 11:42:14