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: Fri, 28 Sep 2012 18:51:32 +0200

On Fri, Sep 28, 2012 at 5:09 PM, Dot Deb <address@hidden> wrote:
> On Thu, Sep 27, 2012 at 11:22 PM, Juan Pablo Carbajal
> <address@hidden> 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 <address@hidden>
>
>
>> 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
> address@hidden
> https://mailman.cae.wisc.edu/listinfo/help-octave

There 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/


reply via email to

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