Why does checking for equality sometimes fail when using the Symbolic Toolbox?
조회 수: 7 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2021년 8월 6일
편집: MathWorks Support Team
2024년 9월 13일 17:40
Why does checking for equality with 'isequal' sometimes fail when using the Symbolic Toolbox? For example:
>> syms x y
>> isequal(x*y+x, x*(y+1))
ans =
logical
0
채택된 답변
MathWorks Support Team
2024년 9월 13일 0:00
편집: MathWorks Support Team
2024년 9월 13일 17:40
In general, symbolic expressions can have two notions of equality.
1) Mathematical Equality: Two expressions are mathematically equivalent if they produce the same result for every possible value of the symbolic variables that they are built from. Mathematical equality takes into account assumptions on variables and mathematical transformations. For instance, the expressions 'x' and 'sqrt(x^2)' are not mathematically equivalent in general. However, if x is assumed to be nonnegative with
>> assume(x >= 0)
Then the above expressions are indeed mathematically equivalent.
To test for mathematical equivalence between expressions, you should use the 'isAlways' function described in the following link:
The 'isAlways' function operates on conditions and tests that the condition is always true for every possible value of the input variables. Thus, to test if two expressions, 'expr1' and 'expr2' are mathematically equal, you can use the following command
>> isAlways(expr1 == expr2)
The 'logical' function is similar to the 'isAlways' function except that it does not always take variable assumptions or mathematical transformations into account. For more information about the 'logical' function, see the following link:
2) Structural Equality: Two expressions are structurally equivalent if they are recursively identical. When testing for structural equality we treat the symbolic expressions as simple MATLAB objects without any mathematical meaning. We say that two MATLAB symbolic objects are structurally equal if they have the same attributes and identical child expressions.
The 'isequal' and 'isequaln' functions test for structural equality instead of mathematical equality. Thus, the following commands return false even though the expressions are mathematically equivalent:
>> syms x y>> isequal(x*y+x, x*(y+1))
For more information about the 'isequal' function, see the following link:
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Assumptions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!