Find Child Subexpressions of Symbolic Expression

3 views (last 30 days)
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?

Accepted Answer

Walter Roberson
Walter Roberson on 12 Apr 2019
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
  4 Comments
Erivelton Gualter
Erivelton Gualter on 12 Apr 2019
Edited: Erivelton Gualter on 12 Apr 2019
My only issue happens when I have some of the following expressions for instance:
  • x*y , which results in [x,y], But I would like only x*y
  • sin(x) , which results in x, but again, I would like ony the sin(x)
  • x^2 , which resuls in [x,2], but I wanted only x^2.
For the expression you asked, I am solving like the following code,which is fine. The output for this case totally works for me, which is: [ x*sin(x - y), y*sin(x - y)]
syms x y
F = (x+y)*sin(x-y);
F = expand(F, 'ArithmeticOnly', true)
CF = children(F)
Basically, this piece of code I just provided do what I want; however, when I have elements such as x*y, sin(x), x^2 it removes this undesired layer. I would like to keep it just return these expression as they are.
In conclusion, I want to expand the symbolic equation (only in simple arithmetic) [1] and then get the sum parts of the element.
So, responding your question:
(x+y)*sin(x-y) ---> [ x*sin(x - y), y*sin(x - y)]
(x+y) --> [x,y]
But, the piece of code already provide it.
By the way, I am using R2018b. I just tried the R2019a online, but as expected it had the same output.
Walter Roberson
Walter Roberson on 14 Apr 2019
In R2018b the only way to proceed is to write MuPAD code that you evaluate using evalin(symengine) or feval(symengine) . The MuPAD code would have to know how expressions are internally represented.
R2019a added additional ways of examining sub-expressions without having to use MuPAD code.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!