using hasSymType(expression, 'constants') returns true when no constants
이전 댓글 표시
When trying to find if my expression has constants, hasSymType() always returns true. For example
syms s;
hasSymType(s*2,'constant')
returns true.
children() seems to separate out the terms into it's components as well. I would expect the following code to return [s*2] but it returns [s 2].
syms s;
children(s*2)
What am I missing?
채택된 답변
추가 답변 (1개)
Walter Roberson
2023년 9월 29일
Internally, inside the symbolic engine, s*2 is coded as a data structure
_mult(DOM_IDENT('s'), DOM_INT(2))
and taking children() of that strips off the
_mult
layer, resulting in the multiple outputs DOM_IDENT('s') and DOM_INT(2) . The interface layer knows to wrap the multiple outputs into a cell array. So the output is {s sym(2)}
2*s is not an atomic entity: it is an expression that can be decomposed into its parts. One of those parts is a constant, which is why hasType() succeeds.
댓글 수: 4
Andrew
2023년 9월 29일
syms a b c d s
f(s) = a*s^6 + b*s^4 + c*s + d
g(s) = a*s^6 + b*s^4 + c*s + 0
[val1, powers1] = coeffs(f(s),s)
constant_term1 = val1(powers1 == 1)
[val2, powers2] = coeffs(g(s),s)
constant_term2 = val2(powers2 == 1)
%alternative
val3 = coeffs(f(s), s, 'all')
val3(end)
val4 = coeffs(g(s), s, 'all')
val4(end)
Walter Roberson
2023년 9월 29일
이동: Walter Roberson
2023년 9월 29일
In the case where all of the coefficients are numeric (or convertable to double) you can use sym2poly and then look at the last entry.
Andrew
2023년 9월 29일
카테고리
도움말 센터 및 File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

