sparsematrix.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 19, 2007
8 
9 #ifndef INCLUDE_SPARSEMATRIX
10 #define INCLUDE_SPARSEMATRIX
11 
12 #include "array.h"
13 #include "mex.h"
14 
15 // Type definitions.
16 // -----------------------------------------------------------------
17 #ifdef MWINDEXISINT
18 // This line is needed for versions of MATLAB prior to 7.3.
19 typedef int mwIndex;
20 #endif
21 
22 // Function declarations.
23 // ---------------------------------------------------------------
24 int getSparseMatrixSize (const mxArray* ptr);
25 int isSparseLowerTriangular (const mxArray* ptr);
26 
27 // class SparseMatrixStructure
28 // ---------------------------------------------------------------
29 // An object of class SparseMatrixStructure stores information about
30 // the structure of a sparse matrix. It does not store the actual
31 // values of the matrix entries.
32 //
33 // WARNING: Starting with version 7.3, MATLAB can handle 64-bit
34 // addressing, and the authors of MATLAB have modified the
35 // implementation of sparse matrices to reflect this change. However,
36 // I convert all the row and column indices in the sparse matrix to
37 // signed integers, and this could potentially cause problems when
38 // dealing with large, sparse matrices on 64-bit platforms with MATLAB
39 // version 7.3 or greater.
41 public:
42 
43  // This constructor takes as input a Matlab array. It it points to a
44  // valid sparse matrix, it will store all the information pertaining
45  // to the sparse matrix structure. If "makeCopy" is true, then the
46  // object will obtain an independent copy of the sparse matrix
47  // structure. If not, the object will be dependent on the data in
48  // memory.
49  explicit SparseMatrixStructure (const mxArray* ptr,
50  bool makeCopy = false);
51 
52  // The copy constructor makes a shallow copy of the source object.
54 
55  // The destructor.
57 
58  // Get the height and width of the matrix.
59  int height() const { return h; };
60  int width () const { return w; };
61 
62  // Return the number of non-zero entries.
63  int size() const { return nnz; };
64 
65  // Return the number of non-zero entries in the cth column.
66  int size (int c) const;
67 
68  // Upon completion of this function, cols[i] contains the column
69  // index for the ith element, and rows[i] contains the row index for
70  // the ith element. It is assumed that "cols" and "rows" have
71  // sufficient space to store this information. This routine is most
72  // useful for converting the Matlab sparse matrix format into the
73  // IPOPT format.
74  void getColsAndRows (int* cols, int* rows) const;
75 
76  // Copy the matrix entries in a sensible manner while preserving the
77  // structure of the destination. In order to preserve the structure
78  // of the destination, it is required that its set of non-zero
79  // entries be a (non-strict) superset of the non-zero entries of the
80  // source.
81  friend void copyElems (const SparseMatrixStructure& sourceStructure,
82  const SparseMatrixStructure& destStructure,
83  const double* sourceValues, double* destValues);
84 
85 protected:
86  mwIndex* jc; // See mxSetJc in the MATLAB documentation.
87  mwIndex* ir; // See mxSetIr in the MATLAB documentation.
88  int nnz; // The number of non-zero elements.
89  int h; // The height of the matrix.
90  int w; // The width of the matrix.
91  bool owner; // Whether or not the object has ownership of the
92  // "jc" and "ir" matrices.
93 
94  // The copy assignment operator is kept hidden because we don't
95  // want it to be used.
97  { return *this; };
98 };
99 
100 #endif