How to tell a function from a string

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

How to tell a function from a string

John W. Eaton-6
On 17-Jan-1998, Lorenzo M. Catucci <[hidden email]> wrote:

| Dear readers,
| here I am with my first question:
| In the comments after definition of newtroot, in Octave's manual, I read:
|
| <  Note that this is only meant to be an example of calling
| < user-supplied functions and should not be taken too seriously.  In
| < addition to using a more robust algorithm, any serious code would check
| < the number and type of all the arguments, ensure that the supplied
| < function really was a function, etc.
|
| Now, since I didn't find an is_function() function, I'd like to know how
| must I act to make sure what was passed as a function name is really
| registered with the parser (or can be registered) as a function.

Unfortunately, there isn't a good way to do this from Octave, though
you could probably wrap is_valid function from src/variables.cc in a
DEFUN easily enough.

| While I'm on this, how do I do the same from c++?

In 2.0.x, you can do something like this (from src/lsode.cc):

  tree_fvc *lsode_fcn = is_valid_function (args(0), "lsode", 1);

  if (! lsode_fcn)
    error ...

In 2.1.x and later the interface will change.  You will have to use
octave_function* instead of tree_fvc*.

jwe


Reply | Threaded
Open this post in threaded view
|

Re: How to tell a function from a string

Lorenzo M. Catucci-2


On Tue, 20 Jan 1998, John W. Eaton wrote:

> On 17-Jan-1998, Lorenzo M. Catucci <[hidden email]> wrote:
>
> Unfortunately, there isn't a good way to do this from Octave, though
> you could probably wrap is_valid function from src/variables.cc in a
> DEFUN easily enough.
>
> In 2.0.x, you can do something like this (from src/lsode.cc):
>
>   tree_fvc *lsode_fcn = is_valid_function (args(0), "lsode", 1);
>
>   if (! lsode_fcn)
>     error ...
>
> In 2.1.x and later the interface will change.  You will have to use
> octave_function* instead of tree_fvc*.
>
Now comes another question: does there exist some reason not to have such
a wrapper defined into octave's main?

Yours,


lorenzo



Reply | Threaded
Open this post in threaded view
|

Re: How to tell a function from a string

John W. Eaton-6
On 20-Jan-1998, Lorenzo M. Catucci <[hidden email]> wrote:

| On Tue, 20 Jan 1998, John W. Eaton wrote:
|
| > On 17-Jan-1998, Lorenzo M. Catucci <[hidden email]> wrote:
| >
| > Unfortunately, there isn't a good way to do this from Octave, though
| > you could probably wrap is_valid function from src/variables.cc in a
| > DEFUN easily enough.
| >
| > In 2.0.x, you can do something like this (from src/lsode.cc):
| >
| >   tree_fvc *lsode_fcn = is_valid_function (args(0), "lsode", 1);
| >
| >   if (! lsode_fcn)
| >     error ...
| >
| > In 2.1.x and later the interface will change.  You will have to use
| > octave_function* instead of tree_fvc*.
| >
| Now comes another question: does there exist some reason not to have such
| a wrapper defined into octave's main?

If you mean why haven't I written it, probably because I never needed
it and I don't recall anyone else ever complaining that it was missing.

jwe


Reply | Threaded
Open this post in threaded view
|

Re: How to tell a function from a string

Jim Van Zandt
In reply to this post by John W. Eaton-6


On 17-Jan-1998, Lorenzo M. Catucci <[hidden email]> wrote:

| In the comments after definition of newtroot, in Octave's manual, I read:
|
| < any serious code would check
| < the number and type of all the arguments, ensure that the supplied
| < function really was a function, etc.
|
| Now, since I didn't find an is_function() function, I'd like to know how
| must I act to make sure what was passed as a function name is really
| registered with the parser (or can be registered) as a function.

I also thought the documentation could have been more helpful.
Eventually I found this:

 - Built-in Function:  exist (NAME)
     Return 1 if the name exists as a variable, 2 if the name (after
     appending `.m') is a function file in the path, 3 if the name is a
     `.oct' file in the path, or 5 if the name is a built-in function.
     Otherwise, return 0.

and decided that "exist(NAME) > 1" was a reasonable test.  (Can it
ever return 4?)

I'm now surprised to read JWE's reply:

> Unfortunately, there isn't a good way to do this from Octave, though
> you could probably wrap is_valid function from src/variables.cc in a
> DEFUN easily enough.

What's wrong with the above test?

Also, please add a discussion to the above passage in the manual.
Maybe something like this:
   
      if (exist(fname)<2)
    error("newtroot: expecting function argument");
      endif
      ...
      ensure that the supplied function really was a function, etc.  
      The test in the example is helpful, but fails if...

...or explain why this test is hopeless, or whatever.

                                - Jim Van Zandt


Reply | Threaded
Open this post in threaded view
|

Re: How to tell a function from a string

John W. Eaton-6
On 20-Jan-1998, Jim Van Zandt <[hidden email]> wrote:

| On 17-Jan-1998, Lorenzo M. Catucci <[hidden email]> wrote:
|
| I also thought the documentation could have been more helpful.
| Eventually I found this:
|
|  - Built-in Function:  exist (NAME)
|      Return 1 if the name exists as a variable, 2 if the name (after
|      appending `.m') is a function file in the path, 3 if the name is a
|      `.oct' file in the path, or 5 if the name is a built-in function.
|      Otherwise, return 0.
|
| and decided that "exist(NAME) > 1" was a reasonable test.  (Can it
| ever return 4?)

No, Octave's exist function doesn't currently return 4, but I'm not
sure what Matlab uses that return code for, so if you want to write
portable code, you might want to check that first.

| I'm now surprised to read JWE's reply:
|
| > Unfortunately, there isn't a good way to do this from Octave, though
| > you could probably wrap is_valid function from src/variables.cc in a
| > DEFUN easily enough.
|
| What's wrong with the above test?

Uh, I just completely forgot about it.

| Also, please add a discussion to the above passage in the manual.
| Maybe something like this:
|    
|       if (exist(fname)<2)
|     error("newtroot: expecting function argument");
|       endif
|       ...
|       ensure that the supplied function really was a function, etc.  
|       The test in the example is helpful, but fails if...

Done, thanks.

| ...or explain why this test is hopeless, or whatever.

No, I think it is my memory that is hopeless...

Thanks,

jwe


Reply | Threaded
Open this post in threaded view
|

Re: How to tell a function from a string

Jim Van Zandt
In message <[hidden email]>, "John W. Eaton
" writes:
>On 20-Jan-1998, Jim Van Zandt <[hidden email]> wrote:

>| What's wrong with the above test?
>
>Uh, I just completely forgot about it.

After six months, even the author needs the documentation.

>| I...decided that "exist(NAME) > 1" was a reasonable test.  (Can it
>| ever return 4?)
>
>No, Octave's exist function doesn't currently return 4, but I'm not
>sure what Matlab uses that return code for, so if you want to write
>portable code, you might want to check that first.

Here are the MATLAB return codes:

      0  undefined
      1  variable
      2  M-file or a file of unknown type
      3  MEX-file
      4  MDL-file
      5  built-in MATLAB function
      6  P-file
      7  directory

I guess the acceptable codes are 2-6.  (I don't know what MDL-files
and P-files are.)  The test would fail for "a file of unknown type".

                                  - Jim Van Zandt