Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

Tools/Math/LA_Matrix.h

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 /**
00003  * @file LA_Matrix.h
00004  * Contains class LinAlg::Matrix
00005  *
00006  * @author <a href="mailto:stefanuhrig@gmx.net">Stefan Uhrig</a>
00007  */
00008 //------------------------------------------------------------------------------
00009 #ifndef LINALG_MATRIX_H_INCLUDED
00010 #define LINALG_MATRIX_H_INCLUDED
00011 //------------------------------------------------------------------------------
00012 #include <cstdlib>
00013 #include <iosfwd>
00014 #include <string>
00015 #include "Tools/Streams/InOut.h"
00016 #include "LA_Exception.h"
00017 #include "LA_Vector.h"
00018 #include "Matrix.h"
00019 //------------------------------------------------------------------------------
00020 namespace LA
00021 {
00022 //------------------------------------------------------------------------------
00023 /**
00024  * Represents a nxm Matrix with entries of type double. 
00025  *
00026  * @author <a href="mailto:stefanuhrig@gmx.net">Stefan Uhrig</a>
00027  */
00028 class Matrix
00029 {
00030 public:
00031   /**
00032    * Standard constructor. Contructs an empty matrix.
00033    */
00034   Matrix();
00035   
00036   /**
00037    * Constructs a nxm matrix and initializes all entries to 0.0.
00038    * @param rows Number of rows (n)
00039    * @param columns Number of columns (m)
00040    */
00041   Matrix(int rows, int columns);
00042   
00043   /**
00044    * Constructs a nxm matrix and initializes the entries to the values
00045    * in the passed array (row major).
00046    * @param rows Number of rows (n)
00047    * @param columns Number of columns (m)
00048    * @param M Array with values that are used to initialize the matrix.
00049    */
00050   Matrix(int rows, int columns, const double* M);
00051   
00052   /**
00053    * Copy contructor.
00054    * @param M Matrix to copy.
00055    * @return Reference to this matrix.
00056    */
00057   Matrix(const Matrix& M);
00058   
00059   /**
00060    * Copy contructor.
00061    * @param M Matrix to copy.
00062    * @return Reference to this matrix.
00063    */
00064   Matrix(const Matrix3x3<double>& M);
00065 
00066   /**
00067    * Returns a nxn identity matrix.
00068    * @param dim Dimension of matrix (n).
00069    * @return Identity matrix.
00070    */
00071   static Matrix identity(int dim);
00072   
00073   /**
00074    * Destructor.
00075    */
00076   virtual ~Matrix();
00077 
00078 public:
00079   /**
00080    * Copy operator.
00081    * @param M Matrix to copy.
00082    * @return Reference to this matrix.
00083    */
00084   Matrix& operator=(const Matrix& M);
00085 
00086   /**
00087    * Copy operator.
00088    * @param M Matrix3x3 to copy.
00089    * @return Reference to this matrix.
00090    */
00091   Matrix& operator=(const Matrix3x3<double>& M);
00092   
00093   /**
00094    * Recreates this matrix as nxm matrix and initializes all values to 0.0.
00095    * @param rows Number of rows (n).
00096    * @param columns Number of columns (m).
00097    */
00098   void create(int rows, int columns);
00099   
00100   /**
00101    * Recreates this matrix as nxm matrix and initializes the entries to
00102    * the values in the passed array (row major).
00103    * @param rows Number of rows (n).
00104    * @param columns Number of columns (m).
00105    * @param M Array with values that are used to initialize the matrix.
00106    */
00107   void create(int rows, int columns, const double* M);
00108   
00109   /**
00110    * Constant element access operator.
00111    * @param row Index of row (zero-based).
00112    * @return Constant reference to appropriate row vector.
00113    */
00114   const Vector& operator[](int row) const;
00115   
00116   /**
00117    * Element access operator.
00118    * @param row Index of row (zero-based).
00119    * @return Reference to appropriate row vector.
00120    */
00121   Vector& operator[](int row);
00122   
00123   /**
00124    * Constant element access operator.
00125    * @param row Index of row (one-based).
00126    * @return Constant reference to appropriate row vector.
00127    */
00128   const Vector& operator()(int row) const;
00129   
00130   /**
00131    * Element access operator.
00132    * @param row Index of row (one-based).
00133    * @return Reference to appropriate row vector.
00134    */
00135   Vector& operator()(int row);
00136   
00137   /**
00138    * Constant element access operator.
00139    * @param row Index of row (one-based).
00140    * @param column Index of column (one-based).
00141    * @return Constant reference to appropriate entry.
00142    */
00143   const double& operator()(int row, int column) const;
00144   
00145   /**
00146    * Element access operator.
00147    * @param row Index of row (one-based).
00148    * @param column Index of column (one-based).
00149    * @return Reference to appropriate entry.
00150    */
00151   double& operator()(int row, int column);
00152   
00153   /**
00154    * Returns the number of rows (n).
00155    * @return Number of rows (n).
00156    */
00157   int rows() const;
00158   
00159   /**
00160    * Returns the number of columns (m).
00161    * @return Number of columns (m).
00162    */
00163   int columns() const;
00164 
00165   /**
00166    * Returns a row vector.
00167    * @param index Index of row (one-based).
00168    * @return Row vector with specified index.
00169    */
00170   Vector get_row(int index) const;
00171   
00172   /**
00173    * Sets a row vector.
00174    * @param index Index of row (one-based).
00175    * @param v Vector the row is set to.
00176    */
00177   void set_row(int index, const Vector& v);
00178   
00179   /**
00180    * Returns a column vector.
00181    * @param index Index of column (one-based).
00182    * @return Column vector with specified index.
00183    */
00184   Vector get_column(int index) const;
00185   
00186   /**
00187    * Sets a column vector.
00188    * @param index Index of column (one-based).
00189    * @param v Vector the column is set to.
00190    */
00191   void set_column(int index, const Vector& v);
00192 
00193   /**
00194    * Retrieves a submatrix.
00195    * @param rowstart The index of the row where to start (one-based).
00196    * @param rowlength The number of rows to extract.
00197    * @param columnstart The index of the column where to start (one-based).
00198    * @param columnlength The number of columns to extract.
00199    * @return The appropriate submatrix.
00200    */
00201   Matrix get_sub(int rowstart, int rowlength,
00202                  int columnstart, int columnlength) const;
00203 
00204   /**
00205    * Sets a submatrix
00206    * @param rowstart The row where to start
00207    * @param columnstart The column where to start
00208    * @param M The submatrix to set.
00209    */
00210   void set_sub(int rowstart, int columnstart, const Matrix& M);
00211 
00212   /**
00213    * Adds a matrix to this matrix.
00214    * @param M Matrix to add.
00215    * @return Reference to this matrix.
00216    */
00217   Matrix& operator+=(const Matrix& M);
00218   
00219   /**
00220    * Subtracts a matrix from this matrix.
00221    * @param M Matrix to subtract.
00222    * @return Reference to this matrix.
00223    */
00224   Matrix& operator-=(const Matrix& M);
00225   
00226   /**
00227    * Multiplies this matrix with another matrix.
00228    * @param M Matrix to multiply this matrix with.
00229    * @return Reference to this matrix.
00230    */
00231   Matrix& operator*=(const Matrix& M);
00232   
00233   /**
00234    * Multiplies all entries of this matrix with the passed scalar.
00235    * @param s Scalar to multiply this matrix with.
00236    * @return Reference to this matrix.
00237    */
00238   Matrix& operator*=(const double& s);
00239   
00240   /**
00241    * Divides this matrix by the passed matrix (multiplies this matrix with
00242    * the inverse of the passed matrix).
00243    * @param M Matrix to divide this matrix by.
00244    * @return Reference to this matrix.
00245    */
00246   Matrix& operator/=(const Matrix& M);
00247   
00248   /**
00249    * Divides all entries of this matrix by the passed scalar.
00250    * @param s Scalar to divide this matrix by.
00251    * @return Reference to this matrix.
00252    */
00253   Matrix& operator/=(const double& s);
00254 
00255   /**
00256    * Transposes the matrix.
00257    */
00258   void transpose();
00259   
00260   /**
00261    * Returns the transposition of this matrix.
00262    * @return Transposition of this matrix.
00263    */
00264   Matrix transposition() const;
00265   
00266   /**
00267    * Returns the transposition of this matrix.
00268    * @return Transposition of this matrix.
00269    */
00270   Matrix T() const;
00271   
00272   /**
00273    * Inverts this matrix.
00274    */
00275   void invert();
00276   
00277   /**
00278    * Returns the inverse of this matrix.
00279    * @return Inverse of this matrix.
00280    */
00281   Matrix inverse() const;
00282   
00283   /**
00284    * Solves the linear equation Ax=b where A is this matrix.
00285    * @param b A vector.
00286    * @return The solution vector x.
00287    */
00288   Vector solve(const Vector& b);
00289 
00290   /**
00291    * Returns the determinant of this matrix.
00292    * @return The determinant of this matrix.
00293    */
00294   double det() const;
00295 
00296   /**
00297    * Returns a human-readable representation of the matrix.
00298    * @return Human-readable representation of the matrix.
00299    */
00300   std::string asString() const;
00301 
00302 private:
00303   void destroy();
00304 
00305 private:
00306   int r;
00307   int c;
00308   Vector* content;
00309 
00310   friend Matrix operator+(const Matrix& M1, const Matrix& M2);
00311   friend Matrix operator-(const Matrix& M1, const Matrix& M2);
00312   friend Matrix operator*(const Matrix& M1, const Matrix& M2);
00313   friend Vector operator*(const Matrix& M, const Vector& v);
00314   friend Matrix operator*(const Matrix& M, const double& s);
00315   friend Matrix operator*(const double& s, const Matrix& M);
00316   friend Matrix operator/(const Matrix& M, const double& s);
00317   friend Matrix operator/(const Matrix& M1, const Matrix& M2);
00318   friend Matrix transpose(const Matrix& M);
00319   friend Matrix invert(const Matrix& M);
00320   friend Matrix inv(const Matrix& M);
00321   friend double det(const Matrix& M);
00322   friend Out& operator<<(Out& stream, const Matrix& M);
00323   friend In& operator>>(In& stream, Matrix& M);
00324 };
00325 //------------------------------------------------------------------------------
00326 
00327 /**
00328  * Adds to matrices.
00329  * @param M1 First matrix.
00330  * @param M2 Second matrix.
00331  * @return M1 + M2.
00332  */
00333 Matrix operator+(const Matrix& M1, const Matrix& M2);
00334 
00335 /**
00336  * Subtracts to matrices.
00337  * @param M1 First matrix.
00338  * @param M2 Second matrix.
00339  * @return M1 - M2.
00340  */
00341 Matrix operator-(const Matrix& M1, const Matrix& M2);
00342 
00343 /**
00344  * Multiplies to matrices.
00345  * @param M1 First matrix.
00346  * @param M2 Second matrix.
00347  * @return M1*M2.
00348  */
00349 Matrix operator*(const Matrix& M1, const Matrix& M2);
00350 
00351 /**
00352  * Multiplies a matrix with a vector.
00353  * @param M A matrix.
00354  * @param v A vector.
00355  * @return M*v.
00356  */
00357 Vector operator*(const Matrix& M, const Vector& v);
00358 
00359 /**
00360  * Multiplies a matrix with a scalar.
00361  * @param M Matrix.
00362  * @param s Scalar.
00363  * @return M*s.
00364  */
00365 Matrix operator*(const Matrix& M, const double& s);
00366 
00367 /**
00368  * Multiplies a matrix with a scalar.
00369  * @param s Scalar.
00370  * @param M Matrix.
00371  * @return s*M.
00372  */
00373 Matrix operator*(const double& s, const Matrix& M);
00374 
00375 /**
00376  * Divides a matrix by a scalar.
00377  * @param M Matrix.
00378  * @param s Scalar.
00379  * @return M/s.
00380  */
00381 Matrix operator/(const Matrix& M, const double& s);
00382 
00383 /**
00384  * Divides a matrix by another matrix (same as multiplying with the inverse).
00385  * @param M1 First matrix.
00386  * @param M2 Second matrix.
00387  * @return M1/M2 (M1*inv(M2))
00388  */
00389 Matrix operator/(const Matrix& M1, const Matrix& M2);
00390 
00391 /**
00392  * Returns the transposition of a matrix.
00393  * @param M Matrix.
00394  * @return Transposition of matrix.
00395  */
00396 Matrix transpose(const Matrix& M);
00397 
00398 /**
00399  * Returns the inverse of a matrix.
00400  * @param M Matrix.
00401  * @return Inverse of matrix.
00402  */
00403 Matrix invert(const Matrix& M);
00404 
00405 /**
00406  * Returns the inverse of a matrix.
00407  * @param M Matrix.
00408  * @return Inverse of matrix.
00409  */
00410 Matrix inv(const Matrix& M);
00411 
00412 /**
00413  * Returns the determinant of a matrix.
00414  * @param M Matrix.
00415  * @return Determinant of matrix.
00416  */
00417 double det(const Matrix& M);
00418 //------------------------------------------------------------------------------
00419 
00420 /**
00421  * Prints a user-readable representation of a matrix.
00422  * @param out An ostream.
00423  * @param M Matrix to output.
00424  * @return Reference to passed ostream.
00425  */
00426 std::ostream& operator<<(std::ostream& out, const Matrix& M);
00427 
00428 /**
00429  * Out operator for GT-streams.
00430  * @param stream Out-stream.
00431  * @param v Vector to output.
00432  * @return Reference to passed Out-stream.
00433  */
00434 Out& operator<<(Out& stream, const Matrix& M);
00435 
00436 /**
00437  * In operator for GT-streams.
00438  * @param stream In-stream.
00439  * @param v Vector to read to.
00440  * @return Reference to passed In-stream.
00441  */
00442 In& operator>>(In& stream, Matrix& M);
00443 //------------------------------------------------------------------------------
00444 }
00445 //------------------------------------------------------------------------------
00446 #endif
00447 //------------------------------------------------------------------------------

Generated on Mon Mar 20 22:00:06 2006 for GT2005 by doxygen 1.3.6