Substitute out anonymous function

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 个评论

"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?
I was not clear enough. The point of the questions is exactly how to recover the values of a,b,c and d from the time when the corresponding anonymous function was declared.
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
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.

请先登录,再进行评论。

 采纳的回答

It is possible to obtain information about the values used to create the anonymous function for debugging purposes, but you should not use the tool that does this as part of your normal workflow. There's even a note to that effect in its documentation.
Create an anonymous function then destroy the data used to make it
>> a = 42;
>> f = @(x) a+x;
>> clear a
You can see that the variable a no longer exists in the workspace.
>> whos
Name Size Bytes Class Attributes
f 1x1 32 function_handle
Use the functions function to obtain information about f.
>> S = functions(f);
The workspace information stored in S contains information about the value the anonymous function f "remembers".
>> S.workspace{1}
ans =
struct with fields:
a: 42
If by "inline function" you mean you want to create an inline object, I strongly recommend against doing that. I'm not sure your belief that "Using inline functions should help to speed the code up." is correct.
Note that "self time" is not necessarily a bad thing; from the documentation it is "Total time in seconds spent in a function, excluding time spent in any child functions. Self time also includes some overhead resulting from the process of profiling." Given the simple functions you've posted, the time required to perform addition, subtraction, and multiplication probably all show up as "self time". You can't avoid that by moving to the very old inline object.

7 个评论

There is Mathworks documentation that inline functions have the fastest variable access of all, so someone who had read that detail could be left wanting to use inline functions.
I remember reading that recently and hoping it was not confusing. I assume you understood the difference between the old and obsolete "inline" function (see help inline) and inlining your code (function/script) into the calling loop. If you think this is confusing customers I will request the documentation be clarified.
I understood it as referring to the old inline() facility. It did not make any sense in context to think it referred to in-line.
It might have made sense if it had been intended to refer to finding variables declared in the same function, but in that case it would disturb my understanding of what the section was saying, as I was fairly confident the document was also saying that parameters to the function are faster than variables declared in the function because parameters are replaced with references whereas local variables have to be searched for. So in context inline() seemed to be the only reasonable interpretation.
Anyhow, over the years, I have consistently interpreted the document as referring to inline().
Walter, could you please post a link to the specific documentation page(s) to which you're referring? I'd like to take a look at it.
"...inlining your code (function/script) into the calling loop"
I also find the terminology in that thread confusing. It should be clarified.
Yes, I see your point about the wording of that Answers post. I'll ask the Support Team to update it.

请先登录,再进行评论。

更多回答(2 个)

E.g.,
funs = functions(demand);
funs.workspace{1} <-- gets the embedded data in the demand function handle
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.

类别

帮助中心File Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by