Adding an angle to a sum of trig functions - sympy

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⎠

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)

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

Is this a bug of SymPy?

Why do we have a strange result with integration of expression by SymPy.
>>> from sympy import *
>>> from sympy import __version__
>>> __version__
'1.8'
>>> x = Symbol('x')
>>> f = (x**2 - Rational(1, 4))**2 * sqrt(1 - x**2); f
sqrt(1 - x**2)*(x**2 - 1/4)**2
>>> integrate(f, (x, -1, 1))
0
The integrand is strictly positive, this result is wrong.
However, dividing the interval (x, -1, 1) into (x, -1, 0) and (x, 0, 1), we have the correct.
>>> integrate(f, (x, -1, 0))
pi/64
>>> integrate(f, (x, 0, 1))
pi/64
Expanding the integrand, the result is also correct.
>>> g = f.expand(); g
x**4*sqrt(1 - x**2) - x**2*sqrt(1 - x**2)/2 + sqrt(1 - x**2)/16
>>> integrate(g, (x, -1, 1))
pi/32
This strange phenomenon has occurred since version 1.5 of SymPy.
>>> from sympy import *
>>> from sympy import __version__
>>> __version__
'1.4'
>>> x = Symbol('x')
>>> f = (x**2 - Rational(1, 4))**2 * sqrt(1 - x**2); f
sqrt(1 - x**2)*(x**2 - 1/4)**2
>>> integrate(f, (x, -1, 1))
pi/32
Is this a bug?
Discussion is continued to the following.
https://github.com/sympy/sympy/issues/22033?fbclid=IwAR3oPgk-sLipSDWe7lsRmqG_hpw0fEEgED5XU5K96IKDi-UnVyOzqQqjSYY
I'm not sure the code of integrate function, but I understand as follows.
The primitive function of f = (x**2 - Rational(1,4))**2 * sqrt(1 - x**2) is this.
>>> from sympy import *
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> x = Symbol('x')
>>> f = (x**2 - Rational(1,4))**2 * sqrt(1 - x**2)
>>> F = integrate(f).simplify(); F
Piecewise((-x**3*(1 - x**2)**(3/2)/6 + x*sqrt(1 - x**2)/32 + asin(x)/32, (x > -1) & (x < 1)))
>>> X = np.linspace(-1, 1, 1000)
>>> Y = np.vectorize(Lambda(x, F))(X)
>>> plt.plot(X, Y)
>>> plt.show()
This is correct.
However, in the definite integral with respect to a real number, another expression fr of xr is used as folows.
>>> xr = Symbol('xr', real=True)
>>> fr = (xr**2 - Rational(1,4))**2 * sqrt(1 - xr**2)
>>> Fr = integrate(fr).simplify()
>>> Fr
Piecewise((xr**4*sqrt(1 - xr**2)*Abs(xr)/6 - xr**2*sqrt(1 - xr**2)*Abs(xr)/6 + sqrt(1 - xr**2)*Abs(xr)/32 - asin(sqrt(1 - xr**2))/32, (xr > -1) & (xr < 1) & Ne(xr, 0)))
>>> Yr = np.vectorize(Lambda(xr, Fr))(X)
>>> plt.plot(X, Yr)
>>> plt.show()
This is incorrect.
I think that integration by substitution with y = x**2 is used inadequetly like this..
>>> y = Symbol('y')
>>> g = (y - Rational(1,4))**2 * sqrt(1 - y)/2/sqrt(y)
>>> G = integrate(g).simplify(); G
Piecewise((y**(5/2)*sqrt(1 - y)/6 - y**(3/2)*sqrt(1 - y)/6 + sqrt(y)*sqrt(1 - y)/32 - asin(sqrt(1 - y))/32, (y <= 1) & (y > 0)))
>>> Gr = G.subs(y, x**2).simplify(); Gr
Piecewise((sqrt(1 - x**2)*(x**2)**(5/2)/6 - sqrt(1 - x**2)*(x**2)**(3/2)/6 + sqrt(1 - x**2)*sqrt(x**2)/32 - asin(sqrt(1 - x**2))/32, (x > -1) & (x < 1) & Ne(x, 0)))
>>> Z = np.vectorize(Lambda(x, Gr))(X)
>>> plt.plot(X, Z)
>>> plt.show()
This function is same as Fr above.
However, I don't know why the well known integration by substitution with x = sin(t) isn't used.
>>> t = Symbol('t')
>>> h = (sin(t)**2 - Rational(1,4))**2 * cos(t)**2
>>> H = integrate(h).simplify()
>>> H = integrate(h)
>>> H
t*sin(t)**6/16 + 3*t*sin(t)**4*cos(t)**2/16 - t*sin(t)**4/16 + 3*t*sin(t)**2*cos(t)**4/16 - t*sin(t)**2*cos(t)**2/8 + t*sin(t)**2/32 + t*cos(t)**6/16 - t*cos(t)**4/16 + t*cos(t)**2/32 + sin(t)**5*cos(t)/16 - sin(t)**3*cos(t)**3/6 - sin(t)**3*cos(t)/16 - sin(t)*cos(t)**5/16 + sin(t)*cos(t)**3/16 + sin(t)*cos(t)/32
>>> H1 = H.subs(t, asin(x)).simplify(); H1
x**5*sqrt(1 - x**2)/6 - x**3*sqrt(1 - x**2)/6 + x*sqrt(1 - x**2)/32 + asin(x)/32
This is same as primitive function F of f above and has another expression.
>>> H2 = H.simplify().subs(t, asin(x)); H2
sin(6*asin(x))/192 + asin(x)/32
>>> plt.show()
Using this we are able to have the following correct definite integral of f over [-1, 1].
>>> H2.subs(x, 1) - H2.subs(x, -1)
pi/32

Polynomial Coefficients from Sympy to Array

In the below code, L1 simplifies to the transfer function that I want:
import sympy as sy
z = sy.symbols('z')
L1 = sy.simplify(((z**2 - 0.5*z + 0.16) / (z-1)**2 ) - 1)
L1
After this, I manually enter the coefficients for the numerator and denominator as follow:
num = [1.5, -0.84]
den = [1., -2., 1.]
Is there a way to do this from code? I'm not sure how to convert the sympy result to something that I can work with again without manually creating the arrays num and den.
You can use as_numer_denom to get the numerator and denominator and then as_poly and coeffs to get the coefficients:
In [16]: import sympy as sy
...: z = sy.symbols('z')
...: L1 = sy.simplify(((z**2 - 0.5*z + 0.16) / (z-1)**2 ) - 1)
...: L1
Out[16]:
1.0⋅(1.5⋅z - 0.84)
────────────────────
2
1.0⋅z - 2.0⋅z + 1.0
In [17]: num, den = L1.as_numer_denom()
In [18]: num.as_poly(z).coeffs()
Out[18]: [1.5, -0.84]
In [19]: den.as_poly(z).coeffs()
Out[19]: [1.0, -2.0, 1.0]
Or to get the whole expression, you could do :
from sympy import *
z = symbols('z')
L1 = simplify(((z**2 - 0.5*z + 0.16) / (z-1)**2 ) - 1)
srepr(L1)
output:
"Mul(Float('1.0', precision=53), Add(Mul(Float('1.5', precision=53), Symbol('z')),
Float('-0.83999999999999997', precision=53)), Pow(Add(Mul(Float('1.0', precision=53),
Pow(Symbol('z'), Integer(2))), Mul(Integer(-1), Float('2.0', precision=53),
Symbol('z')), Float('1.0', precision=53)), Integer(-1)))"