Ipopt
trunk
|
00001 // Copyright (C) 2008 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 // September 15, 2008 00008 00009 #ifndef INCLUDE_ITERATE 00010 #define INCLUDE_ITERATE 00011 00012 #include "mex.h" 00013 #include <string.h> 00014 00015 // Function definitions. 00016 // --------------------------------------------------------------- 00017 template <class Type> void copymemory (const Type* source, Type* dest, int n) { 00018 memcpy(dest,source,sizeof(Type)*n); 00019 } 00020 00021 // Class Iterate. 00022 // ----------------------------------------------------------------- 00023 // An object of this class stores all the information we would like to 00024 // keep track of regarding the variables in our optimization 00025 // problem. An Iterate object is constructed from a MATLAB array, 00026 // which is either an array in double precision, or a cell array with 00027 // elements that are arrays in double precision. There are two methods 00028 // of interest: a function that copies the elements from a source 00029 // array to the Iterate object (inject), and a function that copies 00030 // the Itereate to a destination array (copyto). 00031 class Iterate { 00032 public: 00033 00034 // This constructor initializes the object basically by creating a 00035 // duplicate of the specified MATLAB array. It is important to note 00036 // that the Iterate object does *not* create an independent copy of 00037 // the MATLAB array. As such, it will not destroy the MATLAB arrays 00038 // when the object is destroyed. 00039 explicit Iterate (mxArray* ptr); 00040 00041 // The destructor. 00042 ~Iterate() { }; 00043 00044 // Return the number of variables. 00045 friend int numvars (const Iterate& x) { return x.nv; }; 00046 00047 // Copy the elements from the source array. 00048 void inject (const double* x); 00049 00050 // Copy the elements to the location in memory pointed to by "dest". 00051 // It is assumed that sufficient memory is allocated for the 00052 // destination. 00053 void copyto (double* x) const; 00054 00055 // Convert the Iterate object to a MATLAB array. 00056 operator mxArray*() { return ptr; }; 00057 operator const mxArray*() const { return ptr; }; 00058 00059 // This function dynamically creates a new array containing all the 00060 // information about the iterate as specified by the MATLAB 00061 // array. It is up to the user to deallocate the memory associated 00062 // with the newly allocated data array. The return value is the size 00063 // of the array. 00064 static int getMatlabData (const mxArray* ptr, double*& data); 00065 00066 protected: 00067 int nv; // The number of optimization variables. 00068 mxArray* ptr; // All the information is stored in a MATLAB array. 00069 }; 00070 00071 #endif