compare anonymous function handles

10 views (last 30 days)
Markus Leuthold
Markus Leuthold on 18 Apr 2016
Commented: Walter Roberson on 22 Apr 2016
The following comparison of two function handles should return true in my opinion
f1=@(x,y)(x*y)
f2=@(x,y)(x*y)
isequal(f1,f2)
However, R2016a returns false on this comparison, which is a bug in my opinion. According to the documentation:
Unlike handles to named functions, function handles that represent the same anonymous function are not equal. They are considered unequal because MATLAB cannot guarantee that the frozen values of nonargument variables are the same
In my example, both x and y ARE argument variables. The result is always the same for given arguments. What do you think?

Answers (2)

Matt J
Matt J on 18 Apr 2016
Edited: Matt J on 18 Apr 2016
The documentation that you quoted is referring to situations like this:
>> a=1; f1=@(x,y)(x*y+a);
>> a=2; f2=@(x,y)(x*y+a);
These display the same,
>> f1,f2
f1 =
@(x,y)(x*y+a)
f2 =
@(x,y)(x*y+a)
but give different output for the same input arguments:
>> f1(0,0), f2(0,0)
ans =
1
ans =
2
If you consider 2 anonymous functions to be equal if they display the same, you could compare them this way,
>> strcmp(func2str(f1),func2str(f2))
ans =
1
  1 Comment
Markus Leuthold
Markus Leuthold on 22 Apr 2016
Yes, you're right, the documentation covers the case where you have nonargument values like variable a in your example. Matlab cannot guarantee that a is frozen, as they call it.
However, in my example, Matlab can guarantee the same result for both f1 and f2 with the same input, since I don't have something like variable a from your example. Therefore they should be considererd "equal" in my opinion.
As for func2str(), I cannot use that in my particular use case. I have two huge hierarchical structs to compare. One of the fields contains f1 in the first struct, and f2 in the second struct. I'd like to use
A.x.r.fcn=@(x,y)(x+y);
A.x.s=1;
B.x.r.fcn=@(x,y)(x+y);
B.x.s=1;
isequal(A,B)
If I'd use instead
isequal_based_on_func2str(A,B)
where I iterate over all the fields and hierarchy of A and B, then yes, I could use func2str if the datatype of the current field is a function handle and the built-in isequal for all the other datatypes. That's what I am using right now. Unfortunately, this is slower by a factor of 100 compared to the Matlab built-in isequal in my particular use case.

Sign in to comment.


Walter Roberson
Walter Roberson on 18 Apr 2016
funinfo(1) = functions(f1);
funinfo(2) = functions(f2);
strcmp(funinfo(1).function, funinfo(2).function) && isequal(funinfo(1).workspace{1}, funinfo(2).workspace{1})
  3 Comments
Markus Leuthold
Markus Leuthold on 22 Apr 2016
Interesting solution, I wasn't aware of
funinfo=functions(fcn)
However, since I don't have nonargument variables, my local workspace
funinfo(1).workspace{1}
is always empty. But interesting anyways!
And it still doesn't solve my problem where I have a fcnhandles in deeply nested structs to compare
Walter Roberson
Walter Roberson on 22 Apr 2016
I doubt Mathworks will change this behaviour any time soon. The alternative would require that Mathworks search all existing function handles, in all scopes, to see if it could find a match.
I suggest you grab a File Exchange Contribution such as http://www.mathworks.com/matlabcentral/fileexchange/34565-micro-cache--memoize-cache-function-results and create a routine that takes in strings and return function handles -- we know that you can convert to string since you do not use any variables from your workspace. The routine would keep a copy of each function handle it generates and upon being presented with the same string would return the same function handle, which would then permit isequal() to operate.

Sign in to comment.

Categories

Find more on Variables in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!