help-octave
[Top][All Lists]
Advanced

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

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


From: Juan Pablo Carbajal
Subject: Re: newbie: function of function of function ...
Date: Thu, 27 Sep 2012 23:22:25 +0200

On Thu, Sep 27, 2012 at 9:25 PM, Dot Deb <address@hidden> 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
> address@hidden
> https://mailman.cae.wisc.edu/listinfo/help-octave

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!

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/


reply via email to

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