

Currently I try to use nonlin_curvefit() from octave optim package in my
standalone c++ code.
Unfortunatelly, I can't find any information how to declare octavetype
anonymous function in standalone application to prepare my model function.
Directly in octave I can do f = @(p,x)1exp(p(1)*x), but in c++ I need to
declare a type of f; here function or octave::function doesn't work.
I can only use the feval to call nonlin_curvefit, and I don't know how put
there my model function, f:
octave_value_list result = octave::feval("nonlin_curvefit", ovl("@(p,x)
1exp(p(1)*x)", init, t_points, y_val, opt(0)), 4); # also doesn't work
Could you help me?

ola

Sent from: http://octave.1599824.n4.nabble.com/OctaveGeneralf1599825.html
ola


Currently I try to use nonlin_curvefit() from octave optim package in my
standalone c++ code.
Unfortunatelly, I can't find any information how to declare octavetype
anonymous function in standalone application to prepare my model function.
Directly in octave I can do f = @(p,x)1exp(p(1)*x), but in c++ I need to
declare a type of f; here function or octave::function doesn't work.
I can only use the feval to call nonlin_curvefit, and I don't know how put
there my model function, f:
octave_value_list result = octave::feval("nonlin_curvefit", ovl("@(p,x)
1exp(p(1)*x)", init, t_points, y_val, opt(0)), 4); # also doesn't work
Could you help me?

ola
Maybe this is not a very trivial task. Anonymous function creation looks deeply connected to the parser. The "octave_value" class that manages anonymous functions is "octave_user_function" [1]. Perhaps you can get something working when looking at how Octave creates those functions while parsing [2].
Best, Kai


I have tried to declare:
octave_user_function f = @(p,x) 1exp(p*x);
but I've got 1) error: stray ‘@’ in program and 2) error: ‘p, x’ was not
declared in this scope
Why the octave interpreter doesn't understand @ sign?
How to properly declare an anonymous function or how to write a function
that will return an object octave_user_function?
Should I provide any additional headers (include <???>) when declaring
anonymous functions?
Maybe there is another way to transfer user function (@(p,x) 1exp(p*x)) as
an argument for octave::feval?
It would be really great if I could use this function. My ideas for a
solution are exhausted. I need your help.

ola

Sent from: http://octave.1599824.n4.nabble.com/OctaveGeneralf1599825.html
ola


I have tried to declare:
octave_user_function f = @(p,x) 1exp(p*x);
but I've got 1) error: stray ‘@’ in program and 2) error: ‘p, x’ was not
declared in this scope
Why the octave interpreter doesn't understand @ sign?
How to properly declare an anonymous function or how to write a function
that will return an object octave_user_function?
Should I provide any additional headers (include <???>) when declaring
anonymous functions?
Maybe there is another way to transfer user function (@(p,x) 1exp(p*x)) as
an argument for octave::feval?
It would be really great if I could use this function. My ideas for a
solution are exhausted. I need your help.

ola

Sent from: http://octave.1599824.n4.nabble.com/OctaveGeneralf1599825.html
does this help
>>str1='@(p,x) 1exp(p*x);' str1 = @(p,x) 1exp(p*x); >>fh=str2func(str1) fh = @(p, x) 1  exp (p * x) >> fh(2,4)
ans = 2980.0
start with a string use octave to convert it to a function handle and the use feval from c++
 DAS


I have tried to declare:
octave_user_function f = @(p,x) 1exp(p*x);
but I've got 1) error: stray ‘@’ in program and 2) error: ‘p, x’ was not
declared in this scope
Why the octave interpreter doesn't understand @ sign?
How to properly declare an anonymous function or how to write a function
that will return an object octave_user_function?
Should I provide any additional headers (include <???>) when declaring
anonymous functions?
Maybe there is another way to transfer user function (@(p,x) 1exp(p*x)) as
an argument for octave::feval?
It would be really great if I could use this function. My ideas for a
solution are exhausted. I need your help.

ola

does this help
>>str1='@(p,x) 1exp(p*x);' str1 = @(p,x) 1exp(p*x); >>fh=str2func(str1) fh = @(p, x) 1  exp (p * x) >> fh(2,4)
ans = 2980.0
start with a string use octave to convert it to a function handle and the use feval from c++
 DAS
To my understanding, ola wants to entirely work on C++level to avoid any usage of the Octave interpreter. Maybe you need to give us (at least me) a more complete example to help you. Can you for example upload your C++standaloneapplication to https://pastebin.com/ or alike?
Kai


Yes, it does. Thank you Marcus. :)
So, I have the anonymous function in the nonlin_curvefit() and the code is
compilable, but the execution returns an error:
/error: f(0.1,_): subscripts must be either integers 1 to (2^63)1 or
logicals/
I've checked that directly in the graphical octave interface all parameters
and matrices defined the same as in my code works well. The sizes of
matrices are also good. Does this problem depend on the difference in
indexing between octave and c++?
All solution which I found in the internet doesn't fit to my case.
Do you know how to fix that? How to pass the correct indexing?
Here is the code: test_curve_fit.cc
< http://octave.1599824.n4.nabble.com/file/t373160/test_curve_fit.cc>

ola

Sent from: http://octave.1599824.n4.nabble.com/OctaveGeneralf1599825.html
ola


I didn't try this myself. But that part of your code:
std::string f = "@(p,x) 1exp(p(1)*x)";
extern octave_value make_fcn_handle(const std::string& f, bool
local_funcs = true);
octave_value_list result = octave::feval("nonlin_curvefit", ovl(f, init,
x_val, y_val, opt(0)), 4);
should probably better be:
std::string f = "@(p,x) 1exp(p(1)*x)";
octave_value_list result = octave::feval("nonlin_curvefit",
ovl(make_fcn_handle(f), init, x_val, y_val, opt(0)), 4);
Markus

Sent from: http://octave.1599824.n4.nabble.com/OctaveGeneralf1599825.html


Sorry for the confusion. "make_fcn_handle" does something different than what
I suspected.
Doug's approach seems to work however:
octave_value_list fh = octave::feval("str2func", ovl("@(p,x)
1exp(p(1)*x)"), 1);
octave_value_list result = octave::feval("nonlin_curvefit", ovl(fh(0),
init, x_val, y_val, opt(0)), 4);
Markus

Sent from: http://octave.1599824.n4.nabble.com/OctaveGeneralf1599825.html


This post was updated on .
Hi,
I have one more question.
What if my function does not have a simple form (@(p,x) 1exp(p(1)*x)), but is calculated in the inner loop, as here:
Matrix my_fun(Matrix& p, Matrix& x, Matrix& y, Matrix& z) {
octave_value_list out = octave::feval("zeros", ovl(y.rows(), x.cols()),1);
Matrix k(out(0).matrix_value());
for (unsigned int i = 0; i < x.cols(); ++i) {
for (unsigned int ii = 0; ii < y.rows(); ++ii) {
for (unsigned int iii = z(i,0); iii <= z(i,1); ++iii) {
k(ii, i) = k(ii, i) + (1(exp(  p(iii1) * x(0,i)))) * z(ii,iii1);
}}}
return k; //here an error occure, when I call octave::feval("lsqcurvefit", ovl(my_fun(p,x,y,z), ...);
}
Is it correct if I use this function in this way?
octave_value_list result = octave::feval("lsqcurvefit", ovl(my_fun(p,x,y,z), init, xdata, ydata, L, U, opt(0)), 6);
Unfortunately, I've got an error, when I returned k: error: f(0,_): subscripts must be either integers 1 to (2^63)1 or logicals
so again, I don't know is it problem with my data or maybe I should do this in other way?
Is there another function (similar to str2func) that allows my_fun to be given as an argument to lsqcurvefit?
I hope you will help. Thank you.

ola

Sent from: http://octave.1599824.n4.nabble.com/OctaveGeneralf1599825.html
ola


ola wrote
> Hi,
> I have one more question.
> What if my function does not have a simple form (@(p,x) 1exp(p(1)*x)),
> but is calculated in the inner loop, as here:
>
> Matrix my_fun(Matrix& p, Matrix& x, Matrix& y, Matrix& z) {
> octave_value_list out = octave::feval("zeros", ovl(y.rows(),
> x.cols()),1);
> Matrix k(out(0).matrix_value());
> for (unsigned int i = 0; i < x.cols(); ++i) {
> for (unsigned int ii = 0; ii < y.rows(); ++ii) {
> for (unsigned int iii = z(i,0); iii <= z(i,1); ++iii) {
> k(ii, i) = k(ii, i) + (1(exp(  p(iii1) * x(0,i)))) *
> z(ii,iii1);
> }}}
> return k; //here an error occure, when I call
> octave::feval("lsqcurvefit", ovl(my_fun(p,x,y,z), ...);
> }
>
> Is it correct if I use this function in this way?
> octave_value_list result = octave::feval("lsqcurvefit",
> ovl(my_fun(p,x,y,z), init, xdata, ydata, L, U, opt(0)), 6);
You have to pass a function handle of an Octave function when you call
"lsqcurvefit" from the interpreter. The Octave interpreter doesn't know
about your C++ function my_fun.
You could probably use .oct file functions if you want to write an Octave
function in C++. Please read e.g. "doc mkoctfile" or the Appendix A of the
manual for instructions.
Markus

Sent from: http://octave.1599824.n4.nabble.com/OctaveGeneralf1599825.html

