using hasSymType(expression, 'constants') returns true when no constants
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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?
0 comentarios
Respuesta aceptada
Paul
el 29 de Sept. de 2023
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')
syms s
children(s*2)
What is the reason expect different results?
2 comentarios
Paul
el 29 de Sept. de 2023
Editada: Paul
el 29 de Sept. de 2023
For polynomials we can use coeffs
syms a b c d s
f(s) = a*s^3 + b*s^2 + c*s + d
[cfs,term] = coeffs(f(s),s,'all') % make sure to use 'all'
cfs(end)
f(s) = a*s^3 + b*s^2 + c*s
[cfs,terms] = coeffs(f(s),s,'all') % make sure to use 'all'
cfs(end)
Más respuestas (1)
Walter Roberson
el 29 de Sept. de 2023
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 comentarios
Walter Roberson
el 29 de Sept. de 2023
Movida: Walter Roberson
el 29 de Sept. de 2023
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.
Ver también
Categorías
Más información sobre Number Theory en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!