Substitute out anonymous function
Afficher commentaires plus anciens
To make my code readable, I use lots of anonymous function and nest them together. Imagine a simple example like this:
demand=@(p) a-b*p;
supply = @(p)c+d*p;
at this point a,b,c and d can be changed.
overhang = @(p) demand(p)-supply(p);
crit_f = @(p) overhang(p)^2;
and then use a solver on crit_f to find the price which clears the market.
However, I have now discovered that significant time is spent in "Self time". Using inline functions should help to speed the code up.
My question is whether there is a way to convert a complicated nested anonymous function into an inline function? I found func2str function, which I could use and "substitute out" the individual anonymous functions, but I do not know how to get access to the the objects stored within anonymous functions (this this example parameters a,b,c and d).
4 commentaires
OCDER
le 4 Oct 2018
"To make my code readable, I use lots of anonymous function and nest them together."
I recommend avoiding anonymous functions, as it could get confusing and slow.
demand = @(p) a-b*p;
supply = @(p)c+d*p;
"at this point a,b,c and d can be changed."
Actually, a, b, c, d can NOT be changed to other values. These values retain the value BEFORE the anonymous function was declared. Try this to confirm:
a = 1;
b = 2;
F = @(x) a*x + b;
F(1) =
3
%Changing a and b
a = 2;
b = 3;
F(1) =
3 %a and b were not changed! Answer expected was 5.
Can you show us the solve code so that someone in the forum can easily test the solution?
Filip Rozsypal
le 4 Oct 2018
Philip Borghesani
le 4 Oct 2018
What version of matlab are you using? Versions after 2015b have significantly better performance with anonymous functions (Which perform better then inline functions... see my comment below) It could be that upgrading matlab would solve your issues
Walter Roberson
le 5 Oct 2018
Multiple levels of anonymous functions are slow, seemingly unreasonably so. In releases before R2018b you should avoid them for code that needs to be high performance.
Philip, you might want to look at my case 02665303 where I submitted code that proved this against R2017b, with Mathworks having acknowledged a bug; I was told the problem was fixed for R2018b. There was a follow-up case 03222935 against R2018b which Mathworks said was due to CPU cache effects, but when I submitted code that should not have had meaningful cache effects to demonstrate the problem still existed, I did not receive any reply. The cache part focused on the nested functions case but I did not seem to see any timing improvement for anonymous functions.
Réponse acceptée
Plus de réponses (2)
James Tursa
le 4 Oct 2018
E.g.,
funs = functions(demand);
funs.workspace{1} <-- gets the embedded data in the demand function handle
Walter Roberson
le 4 Oct 2018
0 votes
In some cases a viable route is to use the symbolic toolbox, declaring a symbolic variable or vector, calling the last level function on it, getting back the expression, and using matlabFunction on it, especially with the 'file' option with common sub expression optimization turned on.
Catégories
En savoir plus sur Function Creation dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!