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 // 00033 // WARNING: Starting with version 7.3, MATLAB can handle 64-bit 00034 // addressing, and the authors of MATLAB have modified the 00035 // implementation of sparse matrices to reflect this change. However, 00036 // I convert all the row and column indices in the sparse matrix to 00037 // signed integers, and this could potentially cause problems when 00038 // dealing with large, sparse matrices on 64-bit platforms with MATLAB 00039 // version 7.3 or greater. 00040 class SparseMatrixStructure { 00041 public: 00042 00043 // This constructor takes as input a Matlab array. It it points to a 00044 // valid sparse matrix, it will store all the information pertaining 00045 // to the sparse matrix structure. If "makeCopy" is true, then the 00046 // object will obtain an independent copy of the sparse matrix 00047 // structure. If not, the object will be dependent on the data in 00048 // memory. 00049 explicit SparseMatrixStructure (const mxArray* ptr, 00050 bool makeCopy = false); 00051 00052 // The copy constructor makes a shallow copy of the source object. 00053 SparseMatrixStructure (const SparseMatrixStructure& source); 00054 00055 // The destructor. 00056 ~SparseMatrixStructure(); 00057 00058 // Get the height and width of the matrix. 00059 int height() const { return h; }; 00060 int width () const { return w; }; 00061 00062 // Return the number of non-zero entries. 00063 int size() const { return nnz; }; 00064 00065 // Return the number of non-zero entries in the cth column. 00066 int size (int c) const; 00067 00068 // Upon completion of this function, cols[i] contains the column 00069 // index for the ith element, and rows[i] contains the row index for 00070 // the ith element. It is assumed that "cols" and "rows" have 00071 // sufficient space to store this information. This routine is most 00072 // useful for converting the Matlab sparse matrix format into the 00073 // IPOPT format. 00074 void getColsAndRows (int* cols, int* rows) const; 00075 00076 // Copy the matrix entries in a sensible manner while preserving the 00077 // structure of the destination. In order to preserve the structure 00078 // of the destination, it is required that its set of non-zero 00079 // entries be a (non-strict) superset of the non-zero entries of the 00080 // source. 00081 friend void copyElems (const SparseMatrixStructure& sourceStructure, 00082 const SparseMatrixStructure& destStructure, 00083 const double* sourceValues, double* destValues); 00084 00085 protected: 00086 mwIndex* jc; // See mxSetJc in the MATLAB documentation. 00087 mwIndex* ir; // See mxSetIr in the MATLAB documentation. 00088 int nnz; // The number of non-zero elements. 00089 int h; // The height of the matrix. 00090 int w; // The width of the matrix. 00091 bool owner; // Whether or not the object has ownership of the 00092 // "jc" and "ir" matrices. 00093 00094 // The copy assignment operator is kept hidden because we don't 00095 // want it to be used. 00096 SparseMatrixStructure& operator= (const SparseMatrixStructure& source) 00097 { return *this; }; 00098 }; 00099 00100 #endif