MATLAB: Find Child Subexpressions of Symbolic Expression

symbolic

I believe the function children might have a problem. Acording to the documentation it returns a cell array containing the child subexpressions of each expression in A.
For example:
syms x y
children(x^2 + x*y + y^2)
It will return:
[ x*y, x^2, y^2]
However, for the following:
children(x*y)
it return:
[ x, y]
I believe it should return:
x*y
Is it a bug ? Or am I missing something?

Best Answer

You are missing something.
x^2 + x*y + y^2
is internally
_plus(_power(x, 2), _mult(x, y), _power(y,2))
children() removes the outer layer call and returns its arguments, giving back what would be internally
matrix([_power(x, 2), _mult(x, y), _power(y,2)])
which the interface translates back to
[x^2, x*y, y^2]
And likewise,
x*y
is internally
_multi(x,y)
children() removes the outer layer call and returns its arguments, giving back what would be internally
matrix([x, y])
which the interface translates back to
[x, y]
The children() call does not just find operands of additions and declare anything non-addition to be indivisible: it removes the outermost operation, no matter what it is. For example, children(sin(x*pi/2)) would remove the sin() giving you back x*pi/2