# another question : arrays of functions ???

6 messages
Open this post in threaded view
|

## another question : arrays of functions ???

 Hello. There is another question which comes up by thinking of a solution for an exercise : Is there a way to declare arrays of functions ? I think of the possibilities of Pascal : Var a : Array[1..20] Of Function(x:Real) : Real; and then do callings like for i := 1 to 20 Do   b := b + a[i](3); Kay ---------------------------------------------------------------------------- Kay Hamacher             InterNet : [hidden email] WWW   http://www.wupper.de/sites/lucie/index.html       WINDOWS : From the people who brought you edlin
Open this post in threaded view
|

## Re: another question : arrays of functions ???

 The blessing and the curse of Octave and Matlab is that the basic data type is a matrix, a 2x2 array of numbers.  John has already expanded that somewhat by allowing for structures, and has also included a mode for dealing with matrices that are string arrays (less convenient than Matlab perhaps, but it does the job). A kludgy way to do what you're discussing here would be to create an array of strings containing function names, and then use eval to evaluate each one independently, i.e., funclist(1,:) = "bessel"; funclist(2,1:5) = "gamma"; ... for ii-1:n   x(i) = eval([setstr(funclist(i,:),'(x)']); endfor Matlab would require a similar approach, of course without the setstr function call, which is unique to Octave. Notice that the name of x must be in a string. On Thu, 13 Jun 1996, Kay Hamacher wrote: > Date: Thu, 13 Jun 1996 19:38:56 +0200 (MET DST) > From: Kay Hamacher <[hidden email]> > To: [hidden email] > Subject: another question : arrays of functions ??? > > Hello. > > There is another question which comes up by thinking > of a solution for an exercise : > > Is there a way to declare arrays of functions ? > > I think of the possibilities of Pascal : > Var a : Array[1..20] Of Function(x:Real) : Real; > > and then do callings like > for i := 1 to 20 Do   b := b + a[i](3); > > Kay > > ---------------------------------------------------------------------------- > Kay Hamacher             InterNet : [hidden email] > WWW   http://www.wupper.de/sites/lucie/index.html> >        WINDOWS : From the people who brought you edlin > A S Hodel Dept Elect Eng 200 Broun Hall, Auburn Univ., AL 36849 (334) 844-1854/fax-1809  http://www.eng.auburn.edu/~scotte
Open this post in threaded view
|

## Re: another question : arrays of functions ???

 In reply to this post by Kay Hamacher ( Re Message From: Kay Hamacher ) > > Status:   > > Hello. > > There is another question which comes up by thinking > of a solution for an exercise : > > Is there a way to declare arrays of functions ? > > I think of the possibilities of Pascal : > Var a : Array[1..20] Of Function(x:Real) : Real; > > and then do callings like > for i := 1 to 20 Do   b := b + a[i](3); > > Kay Octave does not provide for the declaration of arrays of functions in the way Pascal (or C) does. You can, however, emulate it by constructing an array of function names and using the "eval" function. But you need to make sure that you "fudge" the function names to be all the same length in the array. The function definitions would be given separately, and individually, as in P or C. For example: function y = cos_2(x)   t = cos(x); y = t.*t; endfunction function y = cos_3(x)   t = cos(x); y = t.*t.*t; endfunction fn = ["  cos"; "cos_2"; "cos_3"]; x = [ 0.25 0.5 0.75 ]; y = []; for i = 1: 3, t = eval ([ setstr( fn(i,:) ) "(x);" ]); y=[y;t] endfor Best wishes, Ted.                                    ([hidden email])
Open this post in threaded view
|

## Re: another question : arrays of functions ???

 On 13-Jun-1996, Ted Harding <[hidden email]> wrote: : Octave does not provide for the declaration of arrays of functions in the : way Pascal (or C) does. You can, however, emulate it by constructing an : array of function names and using the "eval" function. But you need to : make sure that you "fudge" the function names to be all the same length in : the array. The function definitions would be given separately, and : individually, as in P or C. : : For example: : : function y = cos_2(x) :   t = cos(x); y = t.*t; : endfunction : : function y = cos_3(x) :   t = cos(x); y = t.*t.*t; : endfunction : : fn = ["  cos"; "cos_2"; "cos_3"]; : : x = [ 0.25 0.5 0.75 ]; : y = []; : for i = 1: 3, t = eval ([ setstr( fn(i,:) ) "(x);" ]); y=[y;t] endfor You could simplify this a bit by using feval() instead of eval(). In the next release, the strings won't have be the same length, and you will also be able to omit the call to setstr():   octave:1> string_fill_char = setstr (0);  # Set fill char to ASCII nul.   octave:2> fcns = ["cos"; "acos"]          # Lengths need not match.   fcns =   sin   acos   octave:3> for i = 1:2; feval (fcns (i,:), 0), end   ans = 1   ans = 1.5708 jwe