matlabmatrix.h
Go to the documentation of this file.
1 // Copyright (C) 2007 Peter Carbonetto. All Rights Reserved.
2 // This code is published under the Eclipse Public License.
3 //
4 // Author: Peter Carbonetto
5 // Dept. of Computer Science
6 // University of British Columbia
7 // May 22, 2007
8 
9 #ifndef INCLUDE_MATLABMATRIX
10 #define INCLUDE_MATLABMATRIX
11 
12 #include "array.h"
13 #include "mex.h"
14 
15 // Class Matrix
16 // ---------------------------------------------------------------
17 // A matrix object stores its elements in column-major format, as in
18 // Fortran and Matlab. This means that columns are stored one after
19 // another. For example, the matrix
20 //
21 // 1 2 3
22 // 4 5 6
23 //
24 // is stored in memory as
25 //
26 // 1 4 2 5 3 6
27 //
28 // Like Array objects, Matrix objects are not necessarily
29 // encapsulated. They exhibit analogous behaviour.
30 class Matrix : public Array<double> {
31 public:
32 
33  // This constructor allocates memory for matrix of the specified
34  // heigth and width.
35  Matrix (int height, int width);
36 
37  // This constructor basically follows the lead of the analagous
38  // constructor for the Array class.
39  Matrix (double* data, int height, int width);
40 
41  // This constructor retrieves a matrix from a Matlab array. This
42  // particular constructor is only defined for Matrix<double>.
43  // Since Matlab handles storage, the object created by this
44  // constructor is not encapsulated.
45  explicit Matrix (const mxArray* ptr);
46 
47  // This constructor creates a new Matlab array as a side effect.
48  // Since Matlab handles storage, the object created by this
49  // constructor is not encapsulated.
50  Matrix (mxArray*& ptr, int height, int width);
51 
52  // The copy constructor makes a shallow copy of the data.
53  Matrix (const Matrix& source);
54 
55  // The destructor.
56  ~Matrix() { };
57 
58  // Copy assignment operator that observes the same behaviour as
59  // the Array copy assignment operator.
60  Matrix& operator= (const Matrix& source);
61 
62  // Get the height and width of the matrix.
63  int height() const { return h; };
64  int width () const { return w; };
65 
66  // Returns true if the two matrices have the same dimensions
67  // (i.e. the same height and width).
68  bool operator== (const Matrix& X) const;
69  bool operator!= (const Matrix& X) const { return !(*this == X); };
70 
71  // If X is an object of type Matrix, X.entry(r,c) accesses the
72  // entry of the rth row and cth column.
73  double entry (int r, int c) const;
74  double& entry (int r, int c);
75  double operator() (int r, int c) const { return entry(r,c); };
76  double& operator() (int r, int c) { return entry(r,c); };
77 
78 protected:
79  int h; // The height of the matrix.
80  int w; // The width of the matrix.
81 };
82 
83 #endif