Ipopt
trunk
|
00001 // Copyright (C) 2007 Peter Carbonetto. All Rights Reserved. 00002 // This code is published under the Eclipse 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 "mex.h" 00013 00014 // Type definitions. 00015 // ----------------------------------------------------------------- 00016 // This line is needed for versions of MATLAB prior to 7.3. 00017 #ifdef MWINDEXISINT 00018 typedef int mwIndex; 00019 #endif 00020 00021 // class SparseMatrix 00022 // --------------------------------------------------------------- 00023 // An object of class SparseMatrixStructure stores information about 00024 // the structure of a sparse matrix. It does not store the actual 00025 // values of the matrix entries. 00026 // 00027 // WARNING: Starting with version 7.3, MATLAB can handle 64-bit 00028 // addressing, and the authors of MATLAB have modified the 00029 // implementation of sparse matrices to reflect this change. However, 00030 // I convert all the row and column indices in the sparse matrix to 00031 // signed integers, and this could potentially cause problems when 00032 // dealing with large, sparse matrices on 64-bit platforms with MATLAB 00033 // version 7.3 or greater. 00034 class SparseMatrix { 00035 public: 00036 00037 // This constructor takes as input a Matlab array. It it points to a 00038 // valid sparse matrix in double precision, it will store all the 00039 // information pertaining to the sparse matrix structure. It is up 00040 // to the user to ensure that the MATLAB array is a sparse, 00041 // symmetric matrix with row indices in increasing order as the 00042 // nonzero elements appear in the matrix. Note that a SparseMatrix 00043 // object retains a completely independent copy of the sparse matrix 00044 // information by duplicating the data from the specified MATLAB 00045 // array. 00046 explicit SparseMatrix (const mxArray* ptr); 00047 00048 // The destructor. 00049 ~SparseMatrix(); 00050 00051 // Get the height and width of the matrix. 00052 friend int height (const SparseMatrix& A) { return A.h; }; 00053 friend int width (const SparseMatrix& A) { return A.w; }; 00054 00055 // The first function returns the total number of non-zero entries. 00056 // The second function returns the number of non-zero entries in the 00057 // cth column. 00058 int numelems () const { return nnz; }; 00059 int numelems (int c) const; 00060 00061 // Upon completion of this function, cols[i] contains the column 00062 // index for the ith element, and rows[i] contains the row index for 00063 // the ith element. It is assumed that "cols" and "rows" have 00064 // sufficient space to store this information. This routine is most 00065 // useful for converting the Matlab sparse matrix format into the 00066 // IPOPT format. 00067 void getColsAndRows (int* cols, int* rows) const; 00068 00069 // Copy the matrix entries in a sensible manner while preserving the 00070 // structure of the destination. In order to preserve the structure 00071 // of the destination, it is required that the source set of 00072 // non-zero entries be a subset of the destination non-zero 00073 // entries. On success, the value true is returned. 00074 bool copyto (SparseMatrix& dest) const; 00075 00076 // Copy the values of the nonzero elements to the destination array 00077 // which of course must be of the proper length. 00078 void copyto (double* dest) const; 00079 00080 // Returns the number of nonzeros in the sparse matrix. 00081 static int getSizeOfSparseMatrix (const mxArray* ptr); 00082 00083 // Returns true if and only if the sparse matrix is symmetric and 00084 // lower triangular. 00085 static bool isLowerTri (const mxArray* ptr); 00086 00087 // For the proper functioning of a sparse matrix object, it is 00088 // necessary that the row indices be in increasing order. 00089 static bool inIncOrder (const mxArray* ptr); 00090 00091 protected: 00092 int h; // The height of the matrix. 00093 int w; // The width of the matrix. 00094 int nnz; // The number of non-zero elements. 00095 mwIndex* jc; // See mxSetJc in the MATLAB documentation. 00096 mwIndex* ir; // See mxSetIr in the MATLAB documentation. 00097 double* x; // The values of the non-zero entries. 00098 }; 00099 00100 #endif