How to print an expression ordered by a symbol in sympy? - sympy

I tried to use init_printing(order=..) but none of those orders match what i need.
For example:
exp=m*x**3 + 3*m**2*x + x**2 + 1
print(exp,x) will give:
m*x**3 + x**2 + 3*m**2*x + 1
print(exp,m) will give:
3*x*m**2 + 3*x**3*m + x**2 + 1

Related

sympy conversion function? :2*a*b + 2*a*c + 2*b*c --> 2*(a*b + b*c + c*a) : I want string ok

sympy conversion function?
2*a*b + 2*a*c + 2*b*c --> 2*(a*b + b*c + c*a) :
I want string ok
from sympy import *
var('a b c')
f=2*a*b+2*a*c+2*b*c
print("#f ",f)
print("#f/2",f/2)
#f 2*a*b + 2*a*c + 2*b*c
#f/2 a*b + a*c + b*c
(2022-02-02)
i try factor
i try function
thank you
from sympy import factor, factor_terms
from sympy.abc import a,b,c
def myFactor(h):
print("# factor :",h,"--->",factor(h))
print("# factor_terms :",h,"--->",factor_terms(h))
return
myFactor(2*a*b + 2*a*c + 2*b*c)
myFactor(a**2 - b**2)
print("# factor:thank you")
# factor : 2*a*b + 2*a*c + 2*b*c ---> 2*(a*b + a*c + b*c)
# factor_terms : 2*a*b + 2*a*c + 2*b*c ---> 2*(a*b + a*c + b*c)
# factor : a**2 - b**2 ---> (a - b)*(a + b)
# factor_terms : a**2 - b**2 ---> a**2 - b**2
# factor:thank you
>>> from sympy import factor, factor_terms
>>> from sympy.abc import a,b,c
>>> f = 2*a*b + 2*a*c + 2*b*c
>>> factor_terms(f)
2*(a*b + a*c + b*c)
>>> factor_terms(a**2 - b**2)
a**2 - b**2
>>> factor(_)
(a - b)*(a + b)

how to rewrite (a+b+c)**2 as a**2 + b**2 + c*2 + 2*(a*b + b*c + c*a) & ^2-->**2

sympy conversion:(a+b+c)^2 --> a^2 +b^2+c^2+2*(ab + bc + c*a) : I want
sorry add
sympy conversion: I want
(a+b+c)**2 --> a**2 +b**2+c**2+2*(a*b + b*c + c*a)
I try
from sympy import *
var('a b c')
f=(a+b+c)**2
print("#f",f)
print("#e",expand(f))
#f (a + b + c)**2
#e a**2 + 2*a*b + 2*a*c + b**2 + 2*b*c + c**2
(2022-02-02)
i try use factor atom
from sympy import *
var('a b c x')
f_str="a+b+c"
g_str="a**2+b**2+c**2"
f =factor(simplify(f_str))
g =factor(simplify(g_str))
mySubs={f:1,g:13}
#
h =factor(f**2-g)
ha=list(h.atoms(Number))[0]
hb=h/ha
Le=(f.subs(mySubs))**2
Ri1=g.subs(mySubs)
Ri2=h.subs({hb:x})
print("#","(",f_str,")**2=",g_str,"+",ha,"*(",hb,")")
print("#",f_str,"=",mySubs[f],",",g_str,"=",mySubs[g])
print("#",Le,"**2=",Ri1,"+",ha,"*(",solve(Eq(Le,Ri1+Ri2))[0],")")
# ( a+b+c )**2= a**2+b**2+c**2 + 2 *( a*b + a*c + b*c )
# a+b+c = 1 , a**2+b**2+c**2 = 13
# 1 **2= 13 + 2 *( -6 )
(2022-02-04) value subs
from sympy import *
var('a b c')
f_str="a+b+c "
g_str="a**2+b**2+c**2"
h_str="a*b+a*c+b*c "
mySubs={a:0,b:-2,c:3}
f=factor(simplify(f_str)).subs(mySubs)
g=factor(simplify(g_str)).subs(mySubs)
h=factor(simplify(h_str)).subs(mySubs)
print("#",mySubs)
print("#",f_str,"=",f)
print("#",g_str,"=",g)
print("#",h_str,"=",h)
print("#",f_str,"**2=",g_str,"+2*(",h_str,")")
print("#",f,"**2=",g,"+2*(",h,")")
# {a: 0, b: -2, c: 3}
# a+b+c = 1
# a**2+b**2+c**2 = 13
# a*b+a*c+b*c = -6
# a+b+c **2= a**2+b**2+c**2 +2*( a*b+a*c+b*c )
# 1 **2= 13 +2*( -6 )
(2022-02-05) value subs
from sympy import *
var('a b c')
f=a+b+c
g=a**2+b**2+c**2
h=a*b+a*c+b*c
mySubs={a:0,b:-2,c:3}
#
f_val=factor(f).subs(mySubs)
g_val=factor(g).subs(mySubs)
h_val=factor(h).subs(mySubs)
print("#",mySubs)
print("#",f,"=",f_val)
print("#",g,"=",g_val)
print("#",h,"=",h_val)
print("#",f,"**2=",g_val,"+2*(",h,")")
print("#",f_val,"**2=",g_val,"+2*(",h_val,")")
# {a: 0, b: -2, c: 3}
# a + b + c = 1
# a**2 + b**2 + c**2 = 13
# a*b + a*c + b*c = -6
# a + b + c **2= 13 +2*( a*b + a*c + b*c )
# 1 **2= 13 +2*( -6 )
(Reference)
2022 Mathematics IA Q1 < The Common Test for University Admissions is a common entrance examination for Japanese universities
japanese only
https://cdn.mainichi.jp/item/jp/etc/kyotsu-2022/pdf/MT1P.pdf#page=1
>>> from sympy.parsing.sympy_parser import *
>>> f = parse_expr('(a+b+c)^2', transformations=(auto_symbol, convert_xor))
>>> f
(a + b + c)**2
>>> from sympy import Pow
>>> nopow, pow = f.expand().as_independent(Pow)
>>> pow + nopow.factor()
a**2 + b**2 + c**2 + 2*(a*b + a*c + b*c)

Sympy : How is it possible to simplify power of sum?

Considering an expression of this form:
x,y,n=sp.symbols("x y n",positive=True,real=True)
sp.Pow(x+y+x**2,n+1)*sp.Pow(x+2*y+4*y**3,-n-1)
how is it possible to simplify it to have a common power ?
(i.e. sp.Pow((x+y+x**2)/(x+2*y+y**3),n+1) )
This is the same general problem as here
>>> var('z', positiv=True)
z
>>> expr = sp.Pow(x+y+x**2,n+1)*sp.Pow(x+2*y+4*y**3,-n-1)
>>> powsimp(expr.subs(n + 1, var('z',positive=1))).subs(z, n + 1)
((x**2 + x + y)/(x + 4*y**3 + 2*y))**(n + 1)

Collecting sub-expressions of a multivariate polynomial function in Sympy

I have a degree 6 multivariate equation in x and y written in Sympy, e.g.
eqn = a*x**6 + b*x**5*y + c*x**4*y + d*x**3*y + e*x**3*y**2 + ...
Is there a way to collect (x**2+y**2) and rearrange them into the following format?
eqn2 = A*(x**2+y**2)**3 + B*(x**2+y**2)**2 + C*(x**2+y**2) + D
A, B, C, D can be in x, y.
So far I have only tried collect(eqn, x**2 + y**2) and it returned the original equation.
Thank you!
Consider using a temporary symbol z = x**2 + y**2 and replace x**2 with z - y**2, then expand and restore:
>>> ex
A*x**6 + 3*A*x**4*y**2 + 3*A*x**2*y**4 + A*y**6 + B*x**4 + 2*B*x**2*y**2 +
B*y**4 + C*x**2 + C*y**2 + D
>>> ex.subs(x**2, z - y**2).expand().subs(z, x**2 + y**2)
A*(x**2 + y**2)**3 + B*(x**2 + y**2)**2 + C*(x**2 + y**2) + D
Although that works, perhaps a more direct thing to do is separate the expression by coefficients A-D and then factor those collections of terms:
def separatevars_additively(expr, symbols=[]):
free = set(symbols) or expr.free_symbols
d = {}
while free:
f = free.pop()
expr, dep = expr.as_independent(f, as_Add=True)
if dep.has(*free):
return None
d[f] = dep
if expr:
d[0] = expr
return d
>>> coeff = var("A:D")
>>> separatevars_additively(ex, coeff)
{B: B*x**4 + 2*B*x**2*y**2 + B*y**4, A: A*x**6 + 3*A*x**4*y**2 + 3*A*x**2*y**4 + A*y**6, D: D, C: C*x**2 + C*y**2}
>>> Add(*[factor(i) for i in _.values()])
A*(x**2 + y**2)**3 + B*(x**2 + y**2)**2 + C*(x**2 + y**2) + D

Adding an angle to a sum of trig functions

I have an expression which is the sum of some trig functions:
import sympy as sy
from sympy import cos,sin,pi
theta = sy.symbols('theta')
expr = 5*cos(theta) + sin(theta*2)+3*cos(3*theta)
I'd like to add a "phase shift" by pi/4 to each trig function:
sin(2*theta + pi/4) + 5*cos(theta + pi/4) + 3*cos(3*theta + pi/4)
How can this be achieved? Is there a way to walk the expression tree and do an insertion of pi/4?
There are many ways to do this. Here is one that uses pattern matching:
In [5]: expr
Out[5]: sin(2⋅θ) + 5⋅cos(θ) + 3⋅cos(3⋅θ)
In [6]: w = Wild('w')
In [7]: expr.replace(sin(w), sin(w+pi/4))
Out[7]:
⎛ π⎞
sin⎜2⋅θ + ─⎟ + 5⋅cos(θ) + 3⋅cos(3⋅θ)
⎝ 4⎠
In [8]: expr.replace(sin(w), sin(w+pi/4)).replace(cos(w), cos(w + pi/4))
Out[8]:
⎛ π⎞ ⎛ π⎞ ⎛ π⎞
sin⎜2⋅θ + ─⎟ + 5⋅cos⎜θ + ─⎟ + 3⋅cos⎜3⋅θ + ─⎟
⎝ 4⎠ ⎝ 4⎠ ⎝ 4⎠