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 19, 2007 00008 00009 #ifndef INCLUDE_SPARSEMATRIX 00010 #define INCLUDE_SPARSEMATRIX 00011 00012 #include "array.h" 00013 #include "mex.h" 00014 00015 // Type definitions. 00016 // ----------------------------------------------------------------- 00017 #ifdef MWINDEXISINT 00018 // This line is needed for versions of MATLAB prior to 7.3. 00019 typedef int mwIndex; 00020 #endif 00021 00022 // Function declarations. 00023 // --------------------------------------------------------------- 00024 int getSparseMatrixSize (const mxArray* ptr); 00025 int isSparseLowerTriangular (const mxArray* ptr); 00026 00027 // class SparseMatrixStructure 00028 // --------------------------------------------------------------- 00029 // An object of class SparseMatrixStructure stores information about 00030 // the structure of a sparse matrix. It does not store the actual 00031 // values of the matrix entries. 00032 class SparseMatrixStructure { 00033 public: 00034 00035 // This constructor takes as input a Matlab array. It it points to a 00036 // valid sparse matrix, it will store all the information pertaining 00037 // to the sparse matrix structure. If "makeCopy" is true, then the 00038 // object will obtain an independent copy of the sparse matrix 00039 // structure. If not, the object will be dependent on the data in 00040 // memory. 00041 explicit SparseMatrixStructure (const mxArray* ptr, 00042 bool makeCopy = false); 00043 00044 // The copy constructor makes a shallow copy of the source object. 00045 SparseMatrixStructure (const SparseMatrixStructure& source); 00046 00047 // The destructor. 00048 ~SparseMatrixStructure(); 00049 00050 // Get the height and width of the matrix. 00051 mwIndex height() const { return h; }; 00052 mwIndex width () const { return w; }; 00053 00054 // Return the number of non-zero entries. 00055 mwIndex size() const { return nnz; }; 00056 00057 // Return the number of non-zero entries in the cth column. 00058 mwIndex size (mwIndex c) const; 00059 00060 // Upon completion of this function, cols[i] contains the column 00061 // index for the ith element, and rows[i] contains the row index for 00062 // the ith element. It is assumed that "cols" and "rows" have 00063 // sufficient space to store this information. This routine is most 00064 // useful for converting the Matlab sparse matrix format into the 00065 // IPOPT format. 00066 void getColsAndRows (int* cols, int* rows) const; 00067 00068 // Copy the matrix entries in a sensible manner while preserving the 00069 // structure of the destination. In order to preserve the structure 00070 // of the destination, it is required that its set of non-zero 00071 // entries be a (non-strict) superset of the non-zero entries of the 00072 // source. 00073 friend void copyElems (const SparseMatrixStructure& sourceStructure, 00074 const SparseMatrixStructure& destStructure, 00075 const double* sourceValues, double* destValues); 00076 00077 protected: 00078 mwIndex nnz; // The number of non-zero elements. 00079 mwIndex h; // The height of the matrix. 00080 mwIndex w; // The width of the matrix. 00081 mwIndex* jc; // See mxSetJc in the MATLAB documentation. 00082 mwIndex* ir; // See mxSetIr in the MATLAB documentation. 00083 bool owner; // Whether or not the object has ownership of the 00084 // "jc" and "ir" matrices. 00085 00086 // The copy assignment operator is kept hidden because we don't 00087 // want it to be used. 00088 SparseMatrixStructure& operator= (const SparseMatrixStructure& source) 00089 { return *this; }; 00090 }; 00091 00092 #endif