help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: evaluating octave_user_functions


From: Paul Thomas
Subject: Re: evaluating octave_user_functions
Date: Tue, 6 Jul 2004 19:26:24 +0200

I think that this is what you want:

9 Communicating with Octave



It is quite often the case that a dynamically linked function needs to call
an octave function.  For example, you might have some world-shattering new
integration method that can be offered up to user defined functions for the
differentials.  This is done using feval, here.  It is assumed that the
reader wishes to implement things in a bomb-proof fashion, even if it is not
the most efficient.  For more efficient, if less transparent methods, see
LSODE, for example.



Octave provides the means for reading and setting global variables.  This
can be useful if the computational background is stored in global, rather
than in structures or shells.  This demonstration does nothing more than
read the value from one global variable and set another one with the same
value.



Thus:



octave:16> function a=myfunc(x);a=x.^x;endfunction

octave:17> fevaldemo("myfunc",10)

error: get_global_by_name: undefined symbol `g'

octave:17> ind global variable g



octave:17> global g=41.9999

octave:18> fevaldemo("myfunc",10)

ans =  1.0000e+10

octave:19> global a;disp(a)

42.000



#include <octave/oct.h>

#include <octave/pager.h>

#include <octave/parse.h>



DEFUN_DLD (fevaldemo, args, ,

   "The feval demo.")

{

   octave_value_list retval;

   std::string myfunc = args( 0 ).string_value();
// #1

   octave_value arg1 = args( 1 );

   octave_value_list fargs ( arg1 );

   octave_value_list fret=feval( myfunc , fargs , 1 );
// #2

   if ( error_state )

   {

      octave_stdout  <<  "error in MYSUB";

      return octave_value( -1 );

   }

   octave_value gtmp = get_global_value(  "g" ) ;
// #3

   set_global_value(  "a"  ,  gtmp  );
// #4

   return fret;

}



#1 To call feval, the name of the function is required.  This is supplied in
args(0).  The argument for the function is provided in args(1) and this is
done next.



#2 The function call is done with feval.  The name of the function is the
first argument.  This can be an octave function or a user-supplied function.
The second argument is an octave_value_list, containing all the octave_value
arguments required by myfunc.  Finally, the function is told how many input
arguments to expect.  The results are output in an octave_value_list, from
which values can be extracted in the usual way.  It should be said that the
trapping of error_state is nealy pointless, since feval seems to the right
things itself; ie. If myfunc does not exist, it says so.



#3 To read an octave value from an octave global variable, use
get_global_value(name).  Again, this function seems to do all the right
things, if for example name does not exist or has not been declared to be
global.



#4 set_global_function does exactly what its name suggest, given the name in
the first argument and an ocatave_value in the second.



Regards



Paul T



----- Original Message ----- 
From: "Prof. Schnitzlein" <address@hidden>
To: <address@hidden>
Sent: Tuesday, July 06, 2004 5:07 PM
Subject: evaluating octave_user_functions


> Hi
>
> in dld function (c++), given the name of a user-defined-function as
> string, e.g. as argument from the arguments list, I would like to
> evaluate this function directly as octave_user_function after checking
> its validity with is_valid_user_function() instead using
> feval(function_name). Is this possible?
> Any help or hint is appreciated.
>
> Thank you very much
>
> Klaus
>
>
>
> -------------------------------------------------------------
> Octave is freely available under the terms of the GNU GPL.
>
> Octave's home on the web:  http://www.octave.org
> How to fund new projects:  http://www.octave.org/funding.html
> Subscription information:  http://www.octave.org/archive.html
> -------------------------------------------------------------
>



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]