31 : dims(num_dims), fun_name(
func), grad_fun_name(grad_func), gradeps(h)
50 double calc(
const double *x)
const 63 val = res.
at<
double>(0);
99 if (grad_fun_name.empty()) {
106 rhs[0] =
MxArray(grad_fun_name);
152 s.
set(
"fun", fun_name);
153 s.
set(
"gradfun", grad_fun_name);
154 s.
set(
"gradeps", gradeps);
170 return makePtr<MatlabFunction>(
171 s.
at(
"dims").toInt(),
172 s.
at(
"fun").toString(),
173 s.
isField(
"gradfun") ? s.
at(
"gradfun").toString() :
"",
174 s.
isField(
"gradeps") ? s.
at(
"gradeps").toDouble() : 1e-3);
180 string grad_fun_name;
199 int id = rhs[0].toInt();
200 string method(rhs[1].toString());
203 if (method ==
"new") {
204 nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
206 TermCriteria termcrit(TermCriteria::MAX_ITER+TermCriteria::EPS, 5000, 1e-6);
207 for (
int i=2; i<nrhs; i+=2) {
208 string key(rhs[i].toString());
209 if (key ==
"Function")
211 else if (key ==
"TermCriteria")
212 termcrit = rhs[i+1].toTermCriteria();
215 "Unrecognized option %s", key.
c_str());
227 if (method ==
"delete") {
232 else if (method ==
"clear") {
236 else if (method ==
"load") {
237 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
239 bool loadFromString =
false;
240 for (
int i=3; i<nrhs; i+=2) {
241 string key(rhs[i].toString());
242 if (key ==
"ObjName")
243 objname = rhs[i+1].toString();
244 else if (key ==
"FromString")
245 loadFromString = rhs[i+1].toBool();
248 "Unrecognized option %s", key.
c_str());
250 obj_[id] = (loadFromString ?
251 Algorithm::loadFromString<ConjGradSolver>(rhs[2].toString(), objname) :
252 Algorithm::load<ConjGradSolver>(rhs[2].toString(), objname));
254 else if (method ==
"save") {
256 obj->
save(rhs[2].toString());
258 else if (method ==
"empty") {
262 else if (method ==
"getDefaultName") {
266 else if (method ==
"minimize") {
274 else if (method ==
"get") {
276 string prop(rhs[2].toString());
277 if (prop ==
"Function") {
280 (f.dynamicCast<MatlabFunction>())->toStruct();
282 else if (prop ==
"TermCriteria")
286 "Unrecognized property %s", prop.
c_str());
288 else if (method ==
"set") {
290 string prop(rhs[2].toString());
291 if (prop ==
"Function")
293 else if (prop ==
"TermCriteria")
297 "Unrecognized property %s", prop.
c_str());
301 "Unrecognized operation %s", method.
c_str());
mwSize ndims() const
Number of dimensions.
static Ptr< MatlabFunction > create(const MxArray &s)
Factory function.
mwSize numel() const
Number of elements in an array.
virtual Ptr< Function > getFunction() const=0
T at(mwIndex index) const
Template for numeric array element accessor.
virtual void setTermCriteria(const TermCriteria &termcrit)=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
MxArray toStruct() const
Convert object to MxArray.
virtual double minimize(InputOutputArray x)=0
LIBMMWMATRIX_PUBLISHED_API_EXTERN_C void mxDestroyArray(mxArray *pa)
mxArray destructor
bool isDouble() const
Determine whether mxArray represents data as double-precision, floating-point numbers.
struct mxArray_tag mxArray
Forward declaration for mxArray.
void set(mwIndex index, const T &value)
Template for numeric array element write accessor.
string func
name of MATLAB function to evaluate (custom face detector)
void getGradient(const double *x, double *grad)
Evaluates MATLAB gradient function.
double getGradientEps() const
Gradient epsilon.
virtual void getGradient(const double *x, double *grad)
map< int, Ptr< ConjGradSolver > > obj_
Object container.
bool isComplex() const
Determine whether data is complex.
virtual TermCriteria getTermCriteria() const=0
LIBMWMEX_API_EXTERN_C void mexErrMsgIdAndTxt(const char *identifier, const char *err_msg,...)
Issue formatted error message with corresponding error identifier and return to MATLAB prompt...
double calc(const double *x) const
Evaluates MATLAB objective function.
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
bool isField(const std::string &fieldName) const
Determine whether a struct array has a specified field.
mxArray object wrapper for data conversion and manipulation.
void nargchk(bool cond)
Alias for input/output arguments number check.
static MxArray Struct(const char **fields=NULL, int nfields=0, mwSize m=1, mwSize n=1)
Create a new struct array.
MatlabFunction(int num_dims, const string &func, const string &grad_func="", double h=1e-3)
Constructor.
bool isStruct() const
Determine whether input is structure array.
virtual void setFunction(const Ptr< Function > &f)=0
std::vector< T > toVector() const
Convert MxArray to std::vector<T> of primitive types.
virtual String getDefaultName() const
Global constant definitions.
Represents objective function being optimized, implemented as a MATLAB file.
int last_id
Last object id to allocate.
int getDims() const
Return number of dimensions.
virtual void save(const String &filename) const
virtual bool empty() const
LIBMWMEX_API_EXTERN_C int mexCallMATLAB(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[], const char *fcn_name)
call MATLAB function
void create(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)