00001 // Copyright (C) 2007 Peter Carbonetto. All Rights Reserved. 00002 // This code is published under the Common Public License. 00003 // 00004 // Author: Peter Carbonetto 00005 // Dept. of Computer Science 00006 // University of British Columbia 00007 // May 22, 2007 00008 00009 #ifndef INCLUDE_MATLABMATRIX 00010 #define INCLUDE_MATLABMATRIX 00011 00012 #include "array.h" 00013 #include "mex.h" 00014 00015 // Class Matrix 00016 // --------------------------------------------------------------- 00017 // A matrix object stores its elements in column-major format, as in 00018 // Fortran and Matlab. This means that columns are stored one after 00019 // another. For example, the matrix 00020 // 00021 // 1 2 3 00022 // 4 5 6 00023 // 00024 // is stored in memory as 00025 // 00026 // 1 4 2 5 3 6 00027 // 00028 // Like Array objects, Matrix objects are not necessarily 00029 // encapsulated. They exhibit analogous behaviour. 00030 class Matrix : public Array<double> { 00031 public: 00032 00033 // This constructor allocates memory for matrix of the specified 00034 // heigth and width. 00035 Matrix (int height, int width); 00036 00037 // This constructor basically follows the lead of the analagous 00038 // constructor for the Array class. 00039 Matrix (double* data, int height, int width); 00040 00041 // This constructor retrieves a matrix from a Matlab array. This 00042 // particular constructor is only defined for Matrix<double>. 00043 // Since Matlab handles storage, the object created by this 00044 // constructor is not encapsulated. 00045 explicit Matrix (const mxArray* ptr); 00046 00047 // This constructor creates a new Matlab array as a side effect. 00048 // Since Matlab handles storage, the object created by this 00049 // constructor is not encapsulated. 00050 Matrix (mxArray*& ptr, int height, int width); 00051 00052 // The copy constructor makes a shallow copy of the data. 00053 Matrix (const Matrix& source); 00054 00055 // The destructor. 00056 ~Matrix() { }; 00057 00058 // Copy assignment operator that observes the same behaviour as 00059 // the Array copy assignment operator. 00060 Matrix& operator= (const Matrix& source); 00061 00062 // Get the height and width of the matrix. 00063 int height() const { return h; }; 00064 int width () const { return w; }; 00065 00066 // Returns true if the two matrices have the same dimensions 00067 // (i.e. the same height and width). 00068 bool operator== (const Matrix& X) const; 00069 bool operator!= (const Matrix& X) const { return !(*this == X); }; 00070 00071 // If X is an object of type Matrix, X.entry(r,c) accesses the 00072 // entry of the rth row and cth column. 00073 double entry (int r, int c) const; 00074 double& entry (int r, int c); 00075 double operator() (int r, int c) const { return entry(r,c); }; 00076 double& operator() (int r, int c) { return entry(r,c); }; 00077 00078 protected: 00079 int h; // The height of the matrix. 00080 int w; // The width of the matrix. 00081 }; 00082 00083 #endif