Ipopt  3.12.12
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
sparsematrix.hpp
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 "mex.h"
13 
14 // Type definitions.
15 // -----------------------------------------------------------------
16 // This line is needed for versions of MATLAB prior to 7.3.
17 #ifdef MWINDEXISINT
18 typedef int mwIndex;
19 #endif
20 
21 // class SparseMatrix
22 // ---------------------------------------------------------------
23 // An object of class SparseMatrixStructure stores information about
24 // the structure of a sparse matrix. It does not store the actual
25 // values of the matrix entries.
26 //
27 // WARNING: Starting with version 7.3, MATLAB can handle 64-bit
28 // addressing, and the authors of MATLAB have modified the
29 // implementation of sparse matrices to reflect this change. However,
30 // I convert all the row and column indices in the sparse matrix to
31 // signed integers, and this could potentially cause problems when
32 // dealing with large, sparse matrices on 64-bit platforms with MATLAB
33 // version 7.3 or greater.
34 class SparseMatrix {
35 public:
36 
37  // This constructor takes as input a Matlab array. It it points to a
38  // valid sparse matrix in double precision, it will store all the
39  // information pertaining to the sparse matrix structure. It is up
40  // to the user to ensure that the MATLAB array is a sparse,
41  // symmetric matrix with row indices in increasing order as the
42  // nonzero elements appear in the matrix. Note that a SparseMatrix
43  // object retains a completely independent copy of the sparse matrix
44  // information by duplicating the data from the specified MATLAB
45  // array.
46  explicit SparseMatrix (const mxArray* ptr);
47 
48  // The destructor.
49  ~SparseMatrix();
50 
51  // Get the height and width of the matrix.
52  friend int height (const SparseMatrix& A) { return A.h; };
53  friend int width (const SparseMatrix& A) { return A.w; };
54 
55  // The first function returns the total number of non-zero entries.
56  // The second function returns the number of non-zero entries in the
57  // cth column.
58  int numelems () const { return nnz; };
59  int numelems (int c) const;
60 
61  // Upon completion of this function, cols[i] contains the column
62  // index for the ith element, and rows[i] contains the row index for
63  // the ith element. It is assumed that "cols" and "rows" have
64  // sufficient space to store this information. This routine is most
65  // useful for converting the Matlab sparse matrix format into the
66  // IPOPT format.
67  void getColsAndRows (int* cols, int* rows) const;
68 
69  // Copy the matrix entries in a sensible manner while preserving the
70  // structure of the destination. In order to preserve the structure
71  // of the destination, it is required that the source set of
72  // non-zero entries be a subset of the destination non-zero
73  // entries. On success, the value true is returned.
74  bool copyto (SparseMatrix& dest) const;
75 
76  // Copy the values of the nonzero elements to the destination array
77  // which of course must be of the proper length.
78  void copyto (double* dest) const;
79 
80  // Returns the number of nonzeros in the sparse matrix.
81  static int getSizeOfSparseMatrix (const mxArray* ptr);
82 
83  // Returns true if and only if the sparse matrix is symmetric and
84  // lower triangular.
85  static bool isLowerTri (const mxArray* ptr);
86 
87  // For the proper functioning of a sparse matrix object, it is
88  // necessary that the row indices be in increasing order.
89  static bool inIncOrder (const mxArray* ptr);
90 
91 protected:
92  int h; // The height of the matrix.
93  int w; // The width of the matrix.
94  int nnz; // The number of non-zero elements.
95  mwIndex* jc; // See mxSetJc in the MATLAB documentation.
96  mwIndex* ir; // See mxSetIr in the MATLAB documentation.
97  double* x; // The values of the non-zero entries.
98 };
99 
100 #endif
static bool inIncOrder(const mxArray *ptr)
friend int width(const SparseMatrix &A)
int numelems() const
static int getSizeOfSparseMatrix(const mxArray *ptr)
static bool isLowerTri(const mxArray *ptr)
mwIndex * ir
void getColsAndRows(int *cols, int *rows) const
friend int height(const SparseMatrix &A)
bool copyto(SparseMatrix &dest) const
SparseMatrix(const mxArray *ptr)
mwIndex * jc