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?

 採用された回答

Paul
Paul 2023 年 9 月 29 日
Hi Andrew,
Both of those examples seem to be in accordance with doc hasSymType and children, except that children returns a cell array, not an array of sym.
syms s
hasSymType(s*2,'constant')
ans = logical
1
syms s
children(s*2)
ans = 1×2 cell array
{[s]} {[2]}
What is the reason expect different results?

2 件のコメント

Andrew
Andrew 2023 年 9 月 29 日
編集済み: Andrew 2023 年 9 月 29 日
Well clearly I misinterpreted the docs. My next question would be how might I figure out if there is a constant term in my expression?
For example, I have the polynomial expression f(s)=as^n+bs^(n-1)...cs+d (where n is the order of the polynomial, and a,b,c,d are constants) How would I find out if d is zero or not. Or in other words how would I find out if there is a non-zero s^0 term?
Paul
Paul 2023 年 9 月 29 日
編集済み: Paul 2023 年 9 月 29 日
For polynomials we can use coeffs
syms a b c d s
f(s) = a*s^3 + b*s^2 + c*s + d
f(s) = 
[cfs,term] = coeffs(f(s),s,'all') % make sure to use 'all'
cfs = 
term = 
cfs(end)
ans = 
d
f(s) = a*s^3 + b*s^2 + c*s
f(s) = 
[cfs,terms] = coeffs(f(s),s,'all') % make sure to use 'all'
cfs = 
terms = 
cfs(end)
ans = 
0

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
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
Andrew 2023 年 9 月 29 日
Well clearly I misinterpreted the docs. My next question would be how might I figure out if there is a constant term in my expression?
For example, I have the polynomial expression f(s)=as^n+bs^(n-1)...cs+d (where n is the order of the polynomial, and a,b,c,d are constants) How would I find out if d is zero or not. Or in other words how would I find out if there is a non-zero s^0 term?
Walter Roberson
Walter Roberson 2023 年 9 月 29 日
移動済み: Walter Roberson 2023 年 9 月 29 日
syms a b c d s
f(s) = a*s^6 + b*s^4 + c*s + d
f(s) = 
g(s) = a*s^6 + b*s^4 + c*s + 0
g(s) = 
[val1, powers1] = coeffs(f(s),s)
val1 = 
powers1 = 
constant_term1 = val1(powers1 == 1)
constant_term1 = 
d
[val2, powers2] = coeffs(g(s),s)
val2 = 
powers2 = 
constant_term2 = val2(powers2 == 1)
constant_term2 = Empty sym: 1-by-0
%alternative
val3 = coeffs(f(s), s, 'all')
val3 = 
val3(end)
ans = 
d
val4 = coeffs(g(s), s, 'all')
val4 = 
val4(end)
ans = 
0
Walter Roberson
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
Andrew 2023 年 9 月 29 日
Thanks! Very helpful

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeSymbolic Math Toolbox についてさらに検索

製品

リリース

R2023a

タグ

質問済み:

2023 年 9 月 29 日

編集済み:

2023 年 9 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by