From maintainers-request at octave dot org Thu Mar 30 10:03:47 2006 Subject: Re: Bi variate to mono variate functions From: David Bateman To: "John W. Eaton" Cc: Anglade Pierre-Matthieu , maintainers@octave.org Date: Thu, 30 Mar 2006 17:59:17 +0200 John W. Eaton wrote: >On 30-Mar-2006, David Bateman wrote: > >| I defined the function >| >| function y = myfun2(x,c) >| global p; >| p = 10; >| y = 1./(x.^3-2*x-c); >| end >| >| and then did the following in matlab >| >| >> global p >| >> p = 5; >| >> Q = quad( at (x)myfun2(x,p),0,2) >| >| Q = >| >| -0.4605 >| >| >> p = 10; >| >> Q = quad( at (x)myfun2(x,p),0,2) >| >| Q = >| >| -0.2043 >| >| >> >| >| I think this makes it pretty clear that the anonymous variable is >| evaluated when the anonymous function is declared... > >I think your test is not quite right. You are not using P in the >expression inside of myfun2, and you are redefining the anonymous >function in each case (so how can you tell whether the parameter is >evaluated when the function is defined or evaluated?). > >Maybe a better test would be > > >> type myfun2 > > function y = myfun2(x,c) > y = 1./(x.^3-2*x-c); > > >> p = 5; > >> f = at (x) myfun2 (x, p); > >> quad (f, 0, 2) > > ans = > > -0.4605 > > >> p = 10; > >> quad (f, 0, 2) > > ans = > > -0.4605 > >so the function is defined just once, and it keeps the value >available at the time of the definition instead of picking up the >modified value that is defined later, or > > >> clear all > >> f = at (x) myfun2 (x, p); > >> quad (f, 0, 2) > ??? Undefined function or variable 'p'. > > Error in ==> at (x) myfun2 (x, p) > > > Error in ==> quad at 63 > y = f(x, varargin{:}); > > >> p = 10 > > p = > > 10 > > >> quad (f, 0, 2) > ??? Undefined function or variable 'p'. > > Error in ==> at (x) myfun2 (x, p) > > > Error in ==> quad at 63 > y = f(x, varargin{:}); > > >I suppose the way to do this is to modify the function >make_anon_fcn_handle in parse.y to perform some magic necessary to >assign initial values to any symbols that are present in the anonymous >function symbol table and are defined as variables in the current >symbol table, skipping those symbols that appear in the argument list >of the anonymous function. Would that work? Would it cause trobule >for inline functions (which are implemented using an anonymous >function). > >jwe > > > I don't see why it would cause trouble for inline functions as long as the octave_fcn_inline class doesn't call make_anon_fcn_handle, but rather the octave_fcn_handle class directly.. On another point I don't think you responded to the proposal of syntax like function y = myfun(x), y = ...; endfunction function y = myjac(x), y = ...; endfunction y = fsolve({ at (x)myfun(x),@(x)myjac(x)}, ...); to allow function handles and inline functions for fsolve and family, without the pain of caching the values of the user function I previously had. Jacobian and constraint functions would be strings in a cell array, which would be fairly easy to implement. D. -- David Bateman David dot Bateman at motorola dot com Motorola Labs - Paris +33 1 69 35 48 04 (Ph) Parc Les Algorithmes, Commune de St Aubin +33 6 72 01 06 33 (Mob) 91193 Gif-Sur-Yvette FRANCE +33 1 69 35 77 01 (Fax) The information contained in this communication has been classified as: [x] General Business Information [ ] Motorola Internal Use Only [ ] Motorola Confidential Proprietary