Possibly a bug in the ASSUME function (Symbolic Math Toolbox)

I am afraid I have bumped across a bug, or at least a confusing feature. It deals with imposing an assumption on a symbolic function. Consider the following code
clear all
syms theta1(t)
assume(theta1(t),'real')
theta1' % checking if conjugate transpose correctly reduces to transpose for a real function
assumptions % gives a list of all assumptions
The outputs are
ans(t) =
theta1(t)
ans =
in(theta1(t), 'real')
Perfect. That is what we expect. But now consider a minor extension – we add one more symbolic function
clear all
syms theta1(t)
syms theta2(t)
assume(theta1(t),'real')
assume(theta2(t),'real')
theta1'
theta2'
assumptions
The outputs are incorrect:
ans(t) =
conj(theta1(t))
ans(t) =
theta2(t)
ans =
in(theta2(t), 'real')
It appears that upon imposing the second assumption, the first assumption is reset/removed.
Note that here the assumptions are imposed on two different/distinct objects; this is not the same case as the one with multiple assumptions mentioned in the manual.
But let's now check that the outcomes are correct if instead of symbolic functions we impose the assumptions on symbolic variables/expressions
clear all
syms theta1
syms theta2
assume(theta1,'real')
assume(theta2,'real')
theta1'
theta2'
assumptions
The outputs are
ans =
theta1
ans =
theta2
ans =
[in(theta1, 'real'), in(theta2, 'real')]
Correct. That is why I am tempted to conclude that the behaviour for functions exhibits a bug.

 Accepted Answer

Ameer Hamza
Ameer Hamza on 26 Nov 2020
Edited: Ameer Hamza on 26 Nov 2020
This is documented in the Tips section of assume() function: https://www.mathworks.com/help/releases/R2020b/symbolic/assume.html#bt07ws4-4
The toolbox does not support assumptions on symbolic functions. Make assumptions on symbolic variables and expressions instead.
Therefore, the results you are getting for second code can be considered undocumented behaviour.

8 Comments

Ameer, many thanks for the prompt response.
I confess I only skimmed through the tips and overlooked this little notice down in the Tips section.
Best regards from Prague,
Zdeněk
Exploiting these "undocumented features" a bit more, I finally get what I want:
clear all
syms theta1(t)
syms theta2(t)
assume(theta1(t),'real')
assumeAlso(theta2(t),'real')
theta1'
theta2'
assumptions
This gives the outputs:
ans(t) =
theta1(t)
ans(t) =
theta2(t)
ans =
[in(theta1(t), 'real'), in(theta2(t), 'real')]
This is probably not the intended usage for the assumeAlso function, EDIT: or is it? I first understood that is intended for adding assumptions imposed on the same object, while here I am imposing assumptions on two distinct objects, but having read the help for the assume function once again, in particular the section on multiple assumptions, it appears to me now that every use of assume resets the previous assumption. Using assumeAlso is then perfectly appropriate in my case.
Ok. I found that my answer was also not accurate. What the line from documentation meant is that something like the following is not allowed
assume(theta1,'real')
whereas
assume(theta1(t),'real')
is valid.
The problem actually happens because this line add assumptions on both theta1 and t, so when you run
assume(theta2(t),'real')
it again tried to add assumption on 't' which overwrites the previous assumption. assumeAlso() retain the previous assumptions on a variable, therefore, it works.
Exactly, using assumeAlso is the solution.
I even feel ashamed a bit, because only now after so many years of using assume (occassionally) I have learnt that I must use assumeAlso for adding other assumptions. Many thanks for the interaction.
Anyway, why do you think that you were not accurate when pointing me to that line in the Tips section, which reads "The toolbox does not support assumptions on symbolic functions. Make assumptions on symbolic variables and expressions instead."? It seems to me that what this says is that
assume(theta1(t),'real')
is not supported as you originally pointed out.
If you run the line,
assume(theta1,'real')
you will get the error written in the tips section. Whereas, no error occurs when you run
assume(theta1(t),'real')
Also note that the type of theta1 is symfun whereas theta1(t) is a symbolic expression
>> class(theta1)
ans =
'symfun'
>> class(theta1(t))
ans =
'sym'
I seem to be getting it. Finally. So, after defining theta1 as a symbolic function through
syms theta1(t)
whenever I refer just to
theta1
I am refering to a symbolic function whereas if I refer to
theta1(t)
I am refering to a symbolic expression.
I see, this is the understanding that I was missing... for quite some time. Thanks a million.
The symbolic function vs symbolic expression issue is now clear, perhaps, but back to the assume issue.
Why does the repeated use of assume in the final example from my original post – the one that only uses expressions and not functions – work fine? For convenience, I give it here again
clear all
syms theta1
syms theta2
assume(theta1,'real')
assume(theta2,'real')
theta1'
theta2'
assumptions
Why isn't the first assumption removed after the second assumption is encountered? Referring again to the multiple assumptions section in the help for assume function, the first assumption should have been deleted/ignored. And yet
ans =
theta1
ans =
theta2
ans =
[in(theta1, 'real'), in(theta2, 'real')]
This works fine because theta1 and theta2 are two independent variables. In the case of theta1(t) and theta2(t). They are not independent. Rather they are both dependent on 't'. assume(theta1(t),'real') applies assumption on both theta1, and t. Since applying assume() again on a variable remove its previous assumptions, therefore, assume(theta2(t),'real') applies assumption about 't' which force MATLAB to remove the previous assumption on 't'. Since theta1 is dependent on 't', so its assumption is also gone. For example, consider the following
>> syms x
>> assume(x > 0)
>> assumptions
ans =
0 < x
>> assume(x < 2)
>> assumptions
ans =
x < 2
As you can see, it deleted the assumption that x > 0. The workaround is to use assumeAlso().

Sign in to comment.

Products

Release

R2020b

Community Treasure Hunt

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

Start Hunting!