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
Related
In sympy how can I make a substitution of a compound expression for a single variable as in the following example that only works for one of the instances of the common factor?
from sympy import *
x, y, z = symbols('x y z')
eq = Eq(2*(x+y) + 3*(x+y)**2, 0)
print(eq)
eq1 = Eq(z, x+y)
print(eq1)
eq2 = eq.subs(eq1.rhs, eq1.lhs)
print(eq2)
Output
Eq(2*x + 2*y + 3*(x + y)**2, 0)
Eq(z, x + y)
Eq(2*x + 2*y + 3*z**2, 0)
Desired output for last line
Eq(2*z + 3*z**2, 0)
Thanks to Oscar Benjamin's comment. I've solved the case I was actually interested in:
from sympy import *
t, L, C0, R, a, w0, h = symbols('t L C_0 R alpha omega_0 h')
Q = Function('Q')
ex0 = L*Q(t).diff(t, t) + R*Q(t).diff(t) + Q(t)*(1/(C0/(1+h*cos(a*t))))
print(ex0)
ex1 = ex0/L
ex1 = ex1.collect(Q(t)).expand()
print(ex1)
# substitute the following compound expression
ex2 = Eq(w0*w0, 1/(L*C0))
print(ex2)
ex3 = ex1.subs(L*C0, 1/(w0*w0))
ex4 = ex3.collect(Q(t))
print(ex4)
Output:
L*Derivative(Q(t), (t, 2)) + R*Derivative(Q(t), t) + (h*cos(alpha*t) + 1)*Q(t)/C_0
Derivative(Q(t), (t, 2)) + R*Derivative(Q(t), t)/L + h*Q(t)*cos(alpha*t)/(C_0*L) + Q(t)/(C_0*L)
Eq(omega_0**2, 1/(C_0*L))
(h*omega_0**2*cos(alpha*t) + omega_0**2)*Q(t) + Derivative(Q(t), (t, 2)) + R*Derivative(Q(t), t)/L
The substitution fails because subs does not encounter any argument x + y in the (sub)expression 2*(x + y): that expression automatically expands to 2*x + 2*y. So one solution is to do as Oscar suggested: make an algebraic substitution. I often follow this up with a restoration step to handle anything that didn't change as I expected. The other thing you can do is to use a helper function that groups together terms that are in the multi-term old object that you desire to replace:
def mvsubs(eq, old, new):
from sympy.core.exprtools import factor_terms
if not old.is_Add:
return eq.subs(old, new)
Add = old.func
free = old.free_symbols
for i in eq.atoms(Add):
reps = {}
for i in i.args:
if not all(i.has(x) for x in free):
reps.setdefault(i, Dummy())
eq = eq.subs(reps).subs(Add(*reps.values()),
factor_terms(Add(*reps.keys()))).subs(
old, new).xreplace({v:k for k,v in reps.items()})
return eq
>>> mvsubs(eq, x+y, z)
Eq(3*z**2 + 2*z, 0)
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)
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)
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)
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⎠