Passing real function to a subroutine as argument

Consider we have subroutine below that gets a real function as its first argument:


I have wrapped this subroutine in python using f2py.
The question is, How can we pass that real function to it in python?

Some other notes:

  • You should declare all the dummy arguments and other variables.

  • Also, do not use goto, but use a do loop.

I don’t know how f2py does it, or if it allows it.

1 Like

Are you looking to pass a python function as your f argument? If so, is this a callback issue? See https://numpy.org/devdocs/f2py/python-usage.html#call-back-arguments

2 Likes

Yeah, thanks :pray: :pray:

I refactored subroutine above as follows:


and i tried to wrap it , but i got this error:

/tmp/tmp1rr3n3ta/src.linux-x86_64-3.7/sec_bimodule.c:335:56: error: ‘nofoptargs’ undeclared (first use in this function)
if (create_cb_arglist(f_capi,f_xa_capi,#maxnofargs#,#nofoptargs#,&f_nofargs,&f_args_capi,“failed in processing argument list for call-back f.”)) {
^
/tmp/tmp1rr3n3ta/src.linux-x86_64-3.7/sec_bimodule.c:356:22: warning: passing argument 1 of ‘f2py_func’ makes pointer from integer without a cast [-Wint-conversion]
(*f2py_func)(f_cptr,&a,&b,&x_tol,&root);
^
/tmp/tmp1rr3n3ta/src.linux-x86_64-3.7/sec_bimodule.c:356:22: note: expected ‘float *’ but argument is of type ‘int’

If f is a python function, you need to inform f2py that this is a intent(callback) argument in the .pyf file. This means the c pointer to the python function gets correctly assigned when wrapping. Is it possible for you to build a MWE and point me to it? I might be able to help via PR.

1 Like

I have created a separate callback branch: