help-octave
[Top][All Lists]
Advanced

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

Using functions from octave_base_value on class defined in .m files ?


From: John W. Eaton
Subject: Using functions from octave_base_value on class defined in .m files ?
Date: Tue, 23 Nov 2010 03:28:26 -0500

On 23-Nov-2010, CdeMills wrote:

| 
| Hello,
| the dataframe class made substantial progress, I have now an overloaded
| 'sum', 'sumsq', and 'sort' functions. This way, basic statistic functions
| like 'mean', 'median', and 'var' can be used directly on dataframes. 
| 
| Now I'm stuck with 'std', which requires to take the square root of the
| variance. As a dataframe mimics as far as it can a matrix, is it possible to
| get the set of functions from class octave_base_value (sin, sqrt, ...) to be
| applied on a dataframe without having to redefine a wrapper such as
| @dataframe/sin.m for each of them ?
| Basically, the body of the overloaded function should be like:
| function resu = sin(df)
|   resu = df;
|     for indi = 1:resu._cnt(2),
|        resu._data{indi} = builtin('sin', df._data{indi})
|    endfor
| endfunction
| Now, it is possible to make this approach generic AND to define all the
| mapping at once, instead of having a lot of small .m files ?

Not that I know of.  But if every function is exactly like the above,
you should be able to reduce it to

  @df/sin.m:

    function r = sin (df)
      r = df_mapper ("sin", df);
    endfunction

and then write

  @df/private/df_mapper:

    function r = df_mapper (fcn, df)
      r = df;
      for ii = 1:r._cnt(2),
        r._data{i} = feval (fcn, df._data{i})
      endfor
    endfunction

It would be easy enough to generate all the little wrapper functions
(that's probably true whether or not they are calling a common
function like df_mapper above).

BTW, do you really need to call builtin here, or can you use feval?
Won't df._data{i} be a numeric object so feval should be sufficient?
In Matlab, I think builtin can only call functions that are actually
"built in" as part of the interpreter, but in Octave, it can currently
also call interpreted (.m) or dynamically loaded files (.oct,
.mex). For better portability and possibly to avoid future trouble
even in Octave you might want to use feval unless builtin is really
needed here.

| I already looked at 'dispatch' doc.

Dispatch will deprecated in Octave 3.4 and removed in the next major
release after that.

| Also, how to call a specific function from the
| dataframe class, I mean a function which is not overloading something else ?

I don't understand the question.  Can you give an example of what it
is you want to do?

jwe


reply via email to

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