Find Child Subexpressions of Symbolic Expression

4 次查看(过去 30 天)
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?

采纳的回答

Walter Roberson
Walter Roberson 2019-4-12
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 个评论
Erivelton Gualter
Erivelton Gualter 2019-4-12
编辑:Erivelton Gualter 2019-4-12
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 2019-4-14
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Properties and Assumptions 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by