standalone application (octave to c++): define anonymous function for nonlin_curvefit()

classic Classic list List threaded Threaded
16 messages Options
ola
Reply | Threaded
Open this post in threaded view
|

standalone application (octave to c++): define anonymous function for nonlin_curvefit()

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 octave-type
anonymous function in standalone application to prepare my model function.
Directly in octave I can do f = @(p,x)1-exp(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)
1-exp(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/Octave-General-f1599825.html


ola
Reply | Threaded
Open this post in threaded view
|

Re: standalone application (octave to c++): define anonymous function for nonlin_curvefit()

siko1056
On Mon, Jan 28, 2019 at 10:32 AM ola <[hidden email]> wrote:
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 octave-type
anonymous function in standalone application to prepare my model function.
Directly in octave I can do f = @(p,x)1-exp(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)
1-exp(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



ola
Reply | Threaded
Open this post in threaded view
|

Re: standalone application (octave to c++): define anonymous function for nonlin_curvefit()

ola
I have tried to declare:
octave_user_function f = @(p,x) 1-exp(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) 1-exp(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/Octave-General-f1599825.html


ola
Reply | Threaded
Open this post in threaded view
|

Re: standalone application (octave to c++): define anonymous function for nonlin_curvefit()

Doug Stewart-4


On Mon, Jan 28, 2019 at 7:53 AM ola <[hidden email]> wrote:
I have tried to declare:
octave_user_function f = @(p,x) 1-exp(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) 1-exp(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/Octave-General-f1599825.html



does this help

>>str1='@(p,x) 1-exp(p*x);'
     str1 = @(p,x) 1-exp(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++





--
DASCertificate for 206392



Reply | Threaded
Open this post in threaded view
|

Re: standalone application (octave to c++): define anonymous function for nonlin_curvefit()

siko1056
On Mon, Jan 28, 2019 at 2:16 PM Doug Stewart <[hidden email]> wrote:
On Mon, Jan 28, 2019 at 7:53 AM ola <[hidden email]> wrote:
I have tried to declare:
octave_user_function f = @(p,x) 1-exp(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) 1-exp(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) 1-exp(p*x);'
     str1 = @(p,x) 1-exp(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++





--
DASCertificate for 206392

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++-standalone-application to https://pastebin.com/ or alike?

Kai


ola
Reply | Threaded
Open this post in threaded view
|

Re: standalone application (octave to c++): define anonymous function for nonlin_curvefit()

ola
My program is too large so, I have prepared the smallest, but complete c++
example to use octave function nonlin_curvefit() with an anonymous function
as an argument.

Here is the code:  test_curve_fit.cc
<http://octave.1599824.n4.nabble.com/file/t373160/test_curve_fit.cc>  

Thanks so much for helping me.



-----
ola
--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


ola
Reply | Threaded
Open this post in threaded view
|

Re: standalone application (octave to c++): define anonymous function for nonlin_curvefit()

mmuetzel
ov-fcn-handle.h declares:
extern octave_value make_fcn_handle (const std::string& nm,
                                     bool local_funcs = true);

Does that help?

Markus



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


ola
Reply | Threaded
Open this post in threaded view
|

Re: standalone application (octave to c++): define anonymous function for nonlin_curvefit()

ola
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/Octave-General-f1599825.html


ola
Reply | Threaded
Open this post in threaded view
|

Re: standalone application (octave to c++): define anonymous function for nonlin_curvefit()

mmuetzel
I didn't try this myself. But that part of your code:
    std::string f = "@(p,x) 1-exp(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) 1-exp(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/Octave-General-f1599825.html


ola
Reply | Threaded
Open this post in threaded view
|

Re: standalone application (octave to c++): define anonymous function for nonlin_curvefit()

ola
Unfortunately, your code variant return an error:
error: @(p,x) 1-exp(p(1)*x): no function and no method found

I still need your help, please.



-----
ola
--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


ola
Reply | Threaded
Open this post in threaded view
|

Re: standalone application (octave to c++): define anonymous function for nonlin_curvefit()

mmuetzel
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)
1-exp(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/Octave-General-f1599825.html


ola
Reply | Threaded
Open this post in threaded view
|

Re: standalone application (octave to c++): define anonymous function for nonlin_curvefit()

ola
#status: problem solved

Great, it works for me as well.
Thank you very much guys. You are the best. You have contributed to the
development of academic science.



-----
ola
--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


ola
ola
Reply | Threaded
Open this post in threaded view
|

Re: standalone application (octave to c++): define anonymous function for nonlin_curvefit()

ola
This post was updated on .
Hi,
I have one more question.
What if my function does not have a simple form (@(p,x) 1-exp(-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(iii-1) * x(0,i)))) * z(ii,iii-1);
      }}}
  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/Octave-General-f1599825.html


ola
ola
Reply | Threaded
Open this post in threaded view
|

Re: standalone application (octave to c++): define anonymous function for nonlin_curvefit()

ola
Hi,
I have prepared the smallest possible example of lsqcurvefit with c++
intern_function.
I still have the same problem with subscripts error.
Could you help me to solve this, please?

Here is the code:
test_lsqcurvefit.cc
<http://octave.1599824.n4.nabble.com/file/t373160/test_lsqcurvefit.cc>  



-----
ola
--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


ola
Reply | Threaded
Open this post in threaded view
|

Re: standalone application (octave to c++): define anonymous function for nonlin_curvefit()

mmuetzel
In reply to this post by ola
ola wrote

> Hi,
> I have one more question.
> What if my function does not have a simple form (@(p,x) 1-exp(-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(iii-1) * x(0,i)))) *
> z(ii,iii-1);
>       }}}
>   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/Octave-General-f1599825.html


ola
Reply | Threaded
Open this post in threaded view
|

Re: standalone application (octave to c++): define anonymous function for nonlin_curvefit()

ola
Thank you for your suggestion.
I solved the problem using external .oct functions.

Ola



-----
ola
--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


ola