Matrix.h

Go to the documentation of this file.
00001 /************************************************************************/
00002 /*                                                                      */
00003 /*   M A T R I X   C O N T A I N E R                                    */
00004 /*                                                                      */
00005 /*   Roberto Lopez                                                      */
00006 /*   International Center for Numerical Methods in Engineering (CIMNE)  */
00007 /*   Technical University of Catalonia (UPC)                            */
00008 /*   Barcelona, Spain                                                   */
00009 /*   E-mail: rlopez@cimne.upc.edu                                       */
00010 /*                                                                      */
00011 /************************************************************************/
00012 
00013 
00014 #ifndef __MATRIX_H__
00015 #define __MATRIX_H__
00016 
00017 namespace Purple
00018 {
00019 
00020 /// This template class defines a matrix for general purpose use.
00021 ///
00022 /// @see Vector.
00023 
00024 template <class Type>
00025 class Matrix 
00026 {
00027 
00028 private:
00029 
00030    /// Number of rows in matrix.
00031 
00032    int numberOfRows;
00033 
00034    /// Number of columns in matrix.
00035 
00036    int numberOfColumns;
00037 
00038    /// Double pointer to a Type.
00039 
00040    Type** matrix;
00041 
00042 public:
00043 
00044    // CONSTRUCTORS
00045 
00046    Matrix();
00047 
00048    Matrix(int, int);
00049 
00050    Matrix(int, int, const Type&);
00051 
00052    Matrix(int, int, const Type*);
00053 
00054    Matrix(const Matrix&);
00055 
00056 
00057    // ASSIGNMENT OPERATOR
00058 
00059    Matrix& operator=(const Matrix&);
00060 
00061 
00062    // REFERENCE OPERATORS
00063 
00064    inline Type* operator[](const int);
00065 
00066    inline const Type* operator[](const int) const;
00067 
00068 
00069    // METHODS
00070 
00071    inline int getNumberOfRows() const;
00072    inline int getNumberOfColumns() const;
00073 
00074 
00075    // DESTRUCTOR
00076 
00077    ~Matrix();
00078 };
00079 
00080 
00081 // CONSTRUCTORS
00082 
00083 
00084 /// Default constructor. It creates a matrix with zero rows and zero 
00085 /// columns.
00086 
00087 template <class Type>
00088 Matrix<Type>::Matrix() : numberOfRows(0), numberOfColumns(0), matrix(0) 
00089 {
00090 
00091 }
00092 
00093 
00094 /// Constructor. It creates a matrix with n rows and m columns, containing 
00095 /// n*m copies of the default value for Type. 
00096 ///
00097 /// @param newNumberOfRows: Number of rows in Matrix.
00098 /// @param newNumberOfColumns: Number of columns in Matrix.
00099 
00100 template <class Type>
00101 Matrix<Type>::Matrix(int newNumberOfRows, int newNumberOfColumns) 
00102 : numberOfRows(newNumberOfRows), numberOfColumns(newNumberOfColumns), matrix(new Type*[newNumberOfRows])
00103 {
00104    matrix[0] = new Type[numberOfColumns*numberOfRows];
00105 
00106    for (int i = 1; i < numberOfRows; i++)
00107    {
00108       matrix[i] = matrix[i-1] + numberOfColumns;
00109    }
00110 }
00111 
00112 
00113 /// Constructor. It creates a matrix with n rows and m columns, containing n*m copies of 
00114 /// the type value of Type. 
00115 ///
00116 /// @param newNumberOfRows: Number of rows in Matrix.
00117 /// @param newNumberOfColumns: Number of columns in Matrix.
00118 /// @param type: Value of Type.
00119 
00120 template <class Type>
00121 Matrix<Type>::Matrix(int newNumberOfRows, int newNumberOfColumns, const Type& type) 
00122 : numberOfRows(newNumberOfRows), numberOfColumns(newNumberOfColumns), matrix(new Type*[newNumberOfRows])
00123 {
00124    matrix[0] = new Type[numberOfColumns*numberOfRows];
00125 
00126    for (int i = 1; i < numberOfRows; i++)
00127    {
00128       matrix[i] = matrix[i-1] + numberOfColumns;
00129    }
00130 
00131    for (int i = 0; i < numberOfRows; i++)
00132    {
00133       for (int j = 0; j < numberOfColumns; j++)
00134       {
00135          matrix[i][j] = type;
00136       }
00137    }
00138 }
00139 
00140 
00141 /// Constructor. It creates a matrix with n rows and m columns, containing n*m copies of 
00142 /// the type value of Type. 
00143 ///
00144 /// @param newNumberOfRows: Number of rows in Matrix.
00145 /// @param newNumberOfColumns: Number of columns in Matrix.
00146 /// @param type: Value of Type.
00147 
00148 template <class Type>
00149 Matrix<Type>::Matrix(int newNumberOfRows, int newNumberOfColumns, const Type* type) 
00150 : numberOfRows(newNumberOfRows), numberOfColumns(newNumberOfColumns), matrix(new Type*[newNumberOfRows])
00151 {
00152    // Construct matrix
00153 
00154    matrix[0] = new Type[numberOfColumns*numberOfRows];
00155 
00156    for (int i = 1; i < numberOfRows; i++)
00157    {
00158       matrix[i] = matrix[i-1] + numberOfColumns;
00159    }
00160 
00161    // Set all elements of matrix to the type value of Type
00162 
00163    for (int i = 0; i < numberOfRows; i++)
00164    {
00165       for (int j = 0; j < numberOfColumns; j++)
00166       {
00167          matrix[i][j] = *type++;
00168      }
00169    }
00170 }
00171 
00172 
00173 /// Copy constructor. It creates a copy of an existing Matrix. 
00174 ///
00175 /// @param oldMatrix: Matrix to be copied.
00176 
00177 template <class Type>
00178 Matrix<Type>::Matrix(const Matrix& oldMatrix) 
00179 : numberOfRows(oldMatrix.numberOfRows), numberOfColumns(oldMatrix.numberOfColumns), matrix(new Type*[numberOfRows])
00180 {
00181    // Construct matrix
00182 
00183    matrix[0] = new Type[numberOfColumns*numberOfRows];
00184 
00185    for (int i = 1; i < numberOfRows; i++)
00186    {
00187       matrix[i] = matrix[i-1] + numberOfColumns;
00188    }
00189 
00190    // Set all elements of matrix to the old matrix type values
00191 
00192    for (int i = 0; i < numberOfRows; i++)
00193    {
00194       for (int j = 0; j < numberOfColumns; j++)
00195       {
00196          matrix[i][j] = oldMatrix[i][j];
00197       }
00198    }
00199 }
00200 
00201 
00202 // ASSIGNMENT OPERATORS
00203 
00204 /// Assignment operator. It assigns to self a copy of an existing Matrix.
00205 ///
00206 /// @param oldMatrix: Matrix to be assigned.
00207 
00208 template <class Type>
00209 Matrix<Type>& Matrix<Type>::operator=(const Matrix<Type>& oldMatrix)
00210 {
00211    if (this != &oldMatrix) 
00212    {
00213       if (numberOfRows != oldMatrix.numberOfRows || numberOfColumns != oldMatrix.numberOfColumns) 
00214       {
00215          if (matrix != 0) 
00216          {
00217             delete[] (matrix[0]);
00218 
00219             delete[] (matrix);
00220          }
00221 
00222          numberOfRows = oldMatrix.numberOfRows;
00223 
00224          numberOfColumns = oldMatrix.numberOfColumns;
00225 
00226          matrix = new Type*[numberOfRows];
00227 
00228          matrix[0] = new Type[numberOfColumns*numberOfRows];
00229       }
00230 
00231       for (int i = 1; i < numberOfRows; i++)
00232       {
00233          matrix[i] = matrix[i-1] + numberOfColumns;
00234       }
00235 
00236       // Set all elements of matrix to the old matrix type values
00237 
00238       for (int i = 0; i < numberOfRows; i++)
00239       {
00240          for (int j = 0; j < numberOfColumns; j++)
00241          {
00242             matrix[i][j] = oldMatrix[i][j];
00243          }
00244       }
00245    }
00246 
00247    return(*this);
00248 }
00249 
00250 
00251 // REFERENCE OPERATORS
00252 
00253 /// Reference operator.  
00254 
00255 template <class Type>
00256 inline Type* Matrix<Type>::operator[](const int i) 
00257 {
00258    return(matrix[i]);
00259 }
00260 
00261 
00262 /// Reference operator.  
00263 
00264 template <class Type>
00265 inline const Type* Matrix<Type>::operator[](const int i) const
00266 {
00267    return(matrix[i]);
00268 }
00269 
00270 
00271 // METHODS
00272 
00273 // int getNumberOfRows(void) method
00274 
00275 /// This method returns the number of rows in the matrix. 
00276 
00277 template <class Type>
00278 inline int Matrix<Type>::getNumberOfRows() const
00279 {
00280    return(numberOfRows);
00281 }
00282 
00283 
00284 // int getNumberOfColumns(void) method
00285 
00286 /// This method returns the number of columns in the matrix. 
00287 
00288 template <class Type>
00289 inline int Matrix<Type>::getNumberOfColumns() const
00290 {
00291    return(numberOfColumns);
00292 }
00293 
00294 
00295 // DESTRUCTOR
00296 
00297 /// Destructor. 
00298 
00299 template <class Type>
00300 Matrix<Type>::~Matrix()
00301 {
00302    if (matrix != 0) 
00303    {
00304       delete[] (matrix[0]);
00305 
00306       delete[] (matrix);
00307    }
00308 }
00309 
00310 }
00311 
00312 #endif
00313 
00314 
00315 // Purple: An Open Source Numerical Optimization C++ Library.
00316 // Copyright (C) 2006 Roberto Lopez 
00317 //
00318 // This library is free software; you can redistribute it and/or
00319 // modify it under the terms of the GNU Lesser General Public
00320 // License as published by the Free Software Foundation; either
00321 // version 2.1 of the License, or any later version.
00322 //
00323 // This library is distributed in the hope that it will be useful,
00324 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00325 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00326 // Lesser General Public License for more details.
00327 
00328 // You should have received a copy of the GNU Lesser General Public
00329 // License along with this library; if not, write to the Free Software
00330 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

Generated on Wed Jun 21 13:10:38 2006 for Purple by  doxygen 1.4.7