필터 지우기
필터 지우기

Find Child Subexpressions of Symbolic Expression

조회 수: 4 (최근 30일)
Erivelton Gualter
Erivelton Gualter 2019년 4월 12일
댓글: Walter Roberson 2019년 4월 14일
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