Grouping Terms

17 views
Skip to first unread message

brombo

unread,
Jan 16, 2023, 11:05:47 AM1/16/23
to sympy
I have a sum of terms some of which are multiplied by cos(th).  I want to group these terms together (in parenthesis) and have the terms not multiplied by cos(th).  A complicating issue is that the sum is a numerator in a fraction.  Consider the following expression:

(A*cos(th)+B*cos(th)+C)/D

what I want is:

((A+B)*cos(th)+C)/D

or:

(A+B)*cos(th)/D +C/D

Any suggestions would be welcome.  Note that if I expand the first expression it prints out the same and not as:

A*cos(th)/D+B*cos(th)/D+C/D

Oscar Benjamin

unread,
Jan 16, 2023, 11:42:17 AM1/16/23
to sy...@googlegroups.com
I get something different from calling expand. I think what you are
generally looking for here is collect:

In [66]: print(e)
(A*cos(th) + B*cos(th) + C)/D

In [67]: print(expand(e))
A*cos(th)/D + B*cos(th)/D + C/D

In [68]: print(expand(e).collect(cos(th)))
C/D + (A/D + B/D)*cos(th)

In [69]: print(expand(e).collect(cos(th), factor))
C/D + (A + B)*cos(th)/D

In [70]: print(e.collect(cos(th)))
(C + (A + B)*cos(th))/D

--
Oscar
Reply all
Reply to author
Forward
0 new messages