# newbie: function of function of function ...

6 messages
Open this post in threaded view
|

## newbie: function of function of function ...

 Maybe the subject can be misleading but expresses well my confusion. It is my n-th attempt with octave and every time I try I I get stuck with some "stupid" problem. This time I decided to go on and ask for help. I think the problem is trivial but I just don't see the solution. I need a funcioin that computes the integral (using something like quadgk or quadcc) from -Inf to +Inf of an integrand like this: f(x) * log( f(x) ) where f is the argument of the function Naively, should read as: function retval = entropy( f )   quadcc( f*log(f), -Inf, +Inf) end I know that, if I define a somewhere function, say "gauss", I can write f = @(x) -gauss(x) .* log( gauss(x) ); quadcc(f,-Inf,+Inf) and it gives the right result. The problem is that I need to include the two lines above in a function that uses another function name as argument. And I couldn't sort it out :( Alberto _______________________________________________ Help-octave mailing list [hidden email] https://mailman.cae.wisc.edu/listinfo/help-octave
Open this post in threaded view
|

## Re: newbie: function of function of function ...

 On Thu, Sep 27, 2012 at 9:25 PM, Dot Deb <[hidden email]> wrote: > Maybe the subject can be misleading but expresses well my confusion. > It is my n-th attempt with octave and every time I try I I get stuck > with some "stupid" problem. > This time I decided to go on and ask for help. > > I think the problem is trivial but I just don't see the solution. > > I need a funcioin that computes the integral (using something like > quadgk or quadcc) from -Inf to +Inf of an integrand like this: > > f(x) * log( f(x) ) > > where f is the argument of the function > > Naively, should read as: > > function retval = entropy( f ) >   quadcc( f*log(f), -Inf, +Inf) > end > > I know that, if I define a somewhere function, say "gauss", I can write > > f = @(x) -gauss(x) .* log( gauss(x) ); > quadcc(f,-Inf,+Inf) > > and it gives the right result. > > The problem is that I need to include the two lines above in a > function that uses another function name as argument. > And I couldn't sort it out :( > > Alberto > _______________________________________________ > Help-octave mailing list > [hidden email] > https://mailman.cae.wisc.edu/listinfo/help-octaveIf you want to use function handles, the operation * and log are not defined for them so it is not possible. If you really want to improve GNU Octave, please ask the developers (in IRC or here) what are the chances to overload the operator and the function log for function handles. For example function f_handle2 = log(f_handle)   f_handle2 = @(x) log (f_handle(x)); end This may be highly non efficient! Another possibility is that, if you have defined the function in a file, that you create the handle inside the function entropy function retval = entropy( f_txt )   eval(sprintf("f = @(x) -%s(x) .* log( %s(x) );",f_txt,f_txt))   quadcc( f, -Inf, +Inf) end This doesn't look to elegant but is a solution. -- M. Sc. Juan Pablo Carbajal ----- PhD Student University of Zürich http://ailab.ifi.uzh.ch/carbajal/_______________________________________________ Help-octave mailing list [hidden email] https://mailman.cae.wisc.edu/listinfo/help-octave
Open this post in threaded view
|

## RE: newbie: function of function of function ...

 In reply to this post by Dot Deb > From: [hidden email] [mailto:help-octave- > [hidden email]] On Behalf Of Dot Deb > Sent: Thursday, September 27, 2012 2:26 PM > To: [hidden email] > Subject: newbie: function of function of function ... > > Maybe the subject can be misleading but expresses well my confusion. > It is my n-th attempt with octave and every time I try I I get stuck with some > "stupid" problem. > This time I decided to go on and ask for help. > > I think the problem is trivial but I just don't see the solution. > > I need a funcioin that computes the integral (using something like quadgk or > quadcc) from -Inf to +Inf of an integrand like this: > > f(x) * log( f(x) ) > > where f is the argument of the function > > Naively, should read as: > > function retval = entropy( f ) >   quadcc( f*log(f), -Inf, +Inf) > end > > I know that, if I define a somewhere function, say "gauss", I can write > > f = @(x) -gauss(x) .* log( gauss(x) ); > quadcc(f,-Inf,+Inf) > > and it gives the right result. > > The problem is that I need to include the two lines above in a function that > uses another function name as argument. > And I couldn't sort it out :( > > Alberto Try defining the entropy function like this: function retval = entropy( f )    quadcc( @(x) f(x).*log(f(x)), -Inf, +Inf) end and call it using:  entropy(@gauss) Tony Richardson   _______________________________________________ Help-octave mailing list [hidden email] https://mailman.cae.wisc.edu/listinfo/help-octave
Open this post in threaded view
|

## Re: newbie: function of function of function ...

 On Thu, Sep 27, 2012 at 11:25 PM, Richardson, Anthony <[hidden email]> wrote: > > Try defining the entropy function like this: > > function retval = entropy( f ) >    quadcc( @(x) f(x).*log(f(x)), -Inf, +Inf) > end > > and call it using:  entropy(@gauss) > Oh, it works! Now I understand where the problem was: actually I did try something similar, but without using the "@" at call level. Thank you. Alberto _______________________________________________ Help-octave mailing list [hidden email] https://mailman.cae.wisc.edu/listinfo/help-octave
Open this post in threaded view
|

## Re: newbie: function of function of function ...

 In reply to this post by Juan Pablo Carbajal On Thu, Sep 27, 2012 at 11:22 PM, Juan Pablo Carbajal <[hidden email]> wrote: > If you want to use function handles, the operation * and log are not > defined for them so it is not possible. If you really want to improve > GNU Octave, please ask the developers (in IRC or here) what are the > chances to overload the operator and the function log for function > handles. For example > > function f_handle2 = log(f_handle) >   f_handle2 = @(x) log (f_handle(x)); > end > > This may be highly non efficient! What you are saying is that this is one of the possible solution but it is not efficient, right? More efficient solutions would require the possibility to overload the operators "*" and "log" (and similar, I guess). When I will be more familiar with octave, I will send developers the request.Juan Pablo Carbajal <[hidden email]> > Another possibility is that, if you have defined the function in a > file, that you create the handle inside the function entropy > function retval = entropy( f_txt ) >   eval(sprintf("f = @(x) -%s(x) .* log( %s(x) );",f_txt,f_txt)) >   quadcc( f, -Inf, +Inf) > end > > This doesn't look to elegant but is a solution. In you opinion, is this solution more efficient or it shares the same problem? In any case, thank you for the info: I'm learning and anything can be useful. Alberto _______________________________________________ Help-octave mailing list [hidden email] https://mailman.cae.wisc.edu/listinfo/help-octave
Open this post in threaded view
|

## Re: newbie: function of function of function ...

 On Fri, Sep 28, 2012 at 5:09 PM, Dot Deb <[hidden email]> wrote: > On Thu, Sep 27, 2012 at 11:22 PM, Juan Pablo Carbajal > <[hidden email]> wrote: >> If you want to use function handles, the operation * and log are not >> defined for them so it is not possible. If you really want to improve >> GNU Octave, please ask the developers (in IRC or here) what are the >> chances to overload the operator and the function log for function >> handles. For example >> >> function f_handle2 = log(f_handle) >>   f_handle2 = @(x) log (f_handle(x)); >> end >> >> This may be highly non efficient! > > What you are saying is that this is one of the possible solution but > it is not efficient, right? > More efficient solutions would require the possibility to overload the > operators "*" and "log" (and similar, I guess). > When I will be more familiar with octave, I will send developers the > request.Juan Pablo Carbajal <[hidden email]> > > >> Another possibility is that, if you have defined the function in a >> file, that you create the handle inside the function entropy >> function retval = entropy( f_txt ) >>   eval(sprintf("f = @(x) -%s(x) .* log( %s(x) );",f_txt,f_txt)) >>   quadcc( f, -Inf, +Inf) >> end >> >> This doesn't look to elegant but is a solution. > > In you opinion, is this solution more efficient or it shares the same problem? > > In any case, thank you for the info: I'm learning and anything can be useful. > > Alberto > _______________________________________________ > Help-octave mailing list > [hidden email] > https://mailman.cae.wisc.edu/listinfo/help-octaveThere is an overhead when calling anonymous functions. I do not know wat causes it, but you can see the effect. f =@(x) x+1; g =@(x) f(x)*2; % This is the nesting of anonymous functions, maybe as well function handles. h = @(x) (x+1)*2; % This is the explicit construction h2 = @(x) plus(x,1)*2; % This is the construction sing the eval + printf x=0; n=1e4; t0=cputime(); for i=1:n  g(x); end cputime()-t0 t0=cputime(); for i=1:n  h(x);\$ end cputime()-t0 t0=cputime(); for i=1:n  h2(x); end cputime()-t0 I get ans =  0.60004 ans =  0.31602 ans =  0.43203 -- M. Sc. Juan Pablo Carbajal ----- PhD Student University of Zürich http://ailab.ifi.uzh.ch/carbajal/_______________________________________________ Help-octave mailing list [hidden email] https://mailman.cae.wisc.edu/listinfo/help-octave