Why does SymPy ignore my initial condition? - sympy

import sympy as sp
t = sp.Symbol("t")
y = sp.Function("y")
v = sp.Function("v")
sol = sp.dsolve([
y(t).diff(t) - v(t),
v(t).diff(t) + 2*y(t)
],
ics={y(0):2, v(0):0.45})
sol
gives:
[y(t) == C1*sin(sqrt(2)*t) + C2*cos(sqrt(2)*t),
v(t) == sqrt(2)*C1*cos(sqrt(2)*t) - sqrt(2)*C2*sin(sqrt(2)*t)]
Why are C1 and C2 not calculated?

Related

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 there a way in Sympy to simplify Matrix algebra? Say with woodbury identity

I want to specifya N x M matrix where N >> M and use the woodbury,
and for Sympy to simplify the matrix algebra.
Is this possible or something similar?
Is there a way in Sympy to simplify Matrix algebra? Say with woodbury identity
Yes, it's possible to apply Woodbury's identity. In the code below, the identity is applied on (I + Phi^T * Phi)**(-1).
The particular case used was (I+UV)^-1 = I - U*(I+VU)^-1*V
from sympy.simplify.simplify import nc_simplify
from sympy import *
N,M,sigma = symbols("N M \sigma")
Phi = MatrixSymbol("Phi", N, M)
In,Im = Identity(N), Identity(M)
f = (Im + Phi.transpose()#Phi).inverse()
f = f # Phi.transpose()
f = Phi # f
f = In - sigma**(-2)*f
f = sigma**(-2)*Phi.transpose()#f
f = f# Phi
def apply_woodburry_1(e,U,V):
# The Woodbury identity
#
# (I+UV)^-1 = I - U * (I+VU)^-1 * V
#
# Below, P will be LHS and Q will be RHS
#
UV_dim = (U * V).shape
I1 = Identity(UV_dim[0])
VU_dim = (V * U).shape
I2 = Identity(VU_dim[0])
P = (I1+U*V)**(-1)
Q = I1 - U * ( (I2+V*U)**(-1) ) * V
return e.replace(P,Q)
display(f)
f = apply_woodburry_1(f,Phi.transpose(),Phi)
display(f)
f = nc_simplify(f.expand())
display(f)
Output:

SymPy simplify cannot 2*3**n/3 ---> 2*3**(n-1)

Thank you in advance and sorry for the bad english!
FullScript.py
from sympy import *
var('n')
f= 3**n/3
print(simplify(f))
#---------------------
f= 2*3**n/3
print(simplify(f))
# 3**(n - 1) # OK
# 2*3**n/3 # I want 2*3**(n-1)
2018-11-27------------------------------
Please tell me how to use the if statement
How to extract numerator and denominator from polynomial without evaluating?
FullScript.py
from sympy import *
var('n')
def MySimplify(h):
Mypoly = poly(h)
aa = Mypoly.all_coeffs()[1]
bb = h - aa
n, d = fraction(bb)
nn0 = n.as_base_exp()[0]
nn1 = poly(nn0)
import re
rese1 = re.search('^Poly\((.+?),(.+?),', str(nn1)).group(1)
rese2 = re.search('^Poly\((.+?),(.+?),', str(nn1)).group(2)
two=sympify(rese1)/sympify(rese2)
ans = powsimp(bb/two)*two+aa
return ans
f= 3**n/3
print("f1=",f)
print("f2=",simplify(f))
g= 4+2*3**n/3
print("g1=",g)
print("g2=",simplify(g))
print("g3=",MySimplify(g))
# f1= 3**n/3
# f2= 3**(n - 1)
# g1= 2*3**n/3 + 4
# g2= 2*3**n/3 + 4
# g3= 2*3**(n - 1) + 4
2018-11-28------------------------------
FullScript.py
from sympy import *
var('m n p q r s t u v x')
def ps(e, *args):
x = Dummy(integer=True)
t=list(Add.make_args(e))
for i, ti in enumerate(t):
c, r = ti.as_coeff_Mul()
if c.is_Rational and not c.is_Integer:
t[i] = Mul(c.p, r, Pow(c.q, x), evaluate=False)
# t[i] = powersimp(t[i], *args).xreplace({x: -1})
t[i] = powsimp(t[i], *args).xreplace({x: -1})
else:
t[i] = powsimp(t[i], *args)
return Add(*t)
f= 4+2*3**n/3
print("f1=",f)
print("f1=",ps(f))
f= 4+2*3**n/3+5*2.4**(m-1)/2.4+6*5.6*(p-7)/8.9
print("f2=",f)
print("f2=",ps(f))
g= x+p**n/p
print("g1=",g)
print("g1=",ps(g))
g= x+p**n/p+q*s**(m-1)/s+r*t**(u-2)/v
print("g2=",g)
print("g2=",ps(g))
# f1= 2*3**n/3 + 4
# f1= 2*3**(n - 1) + 4
# f2= 2.08333333333333*2.4**(m - 1) + 2*3**n/3 + 3.7752808988764*p - 22.4269662921348
# f2= 2.08333333333333*2.4**(m - 1) + 2*3**(n - 1) + 3.7752808988764*p - 22.4269662921348
# g1= x + p**n/p
# g1= p**(n - 1) + x
# g2= q*s**(m - 1)/s + r*t**(u - 2)/v + x + p**n/p
# g2= p**(n - 1) + q*s**(m - 2) + r*t**(u - 2)/v + x
powsimp(f/2)*2 will do what you want. The following is a more general workaround that incorporates this idea:
def ps(e, *args):
x = Dummy(integer=True)
t=list(Add.make_args(e))
for i, ti in enumerate(t):
c, r = ti.as_coeff_Mul()
if c.is_Rational and not c.is_Integer:
t[i] = Mul(c.p, r, Pow(c.q, x), evaluate=False)
t[i] = powersimp(t[i], *args).xreplace({x: -1})
else:
t[i] = powsimp(t[i], *args)
return Add(*t)

Gradient Descent in Python 2

Part of my assignment is to implement the Gradient Descent to find the best approximation of values c_1, c_2 and r_1 for the function
.
Given is only a list of 30 y-values corresponding to x from 0 to 30. I am implementing this in Enthought Canopy like this:
First I start with random values:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as pyplt
c1 = -0.1
c2 = 0.1
r1 = 0.1
x = np.linspace(0,29,30) #start,stop,numitems
y = c1*np.exp(r1*x) + (c1*x)**3.0 - (c2*x)**2.0
pyplt.plot(x,y)
values_x = np.linspace(0,29,30)
values_y = np.array([0.2, -0.142682939241718, -0.886680607211679, -2.0095087143494, -3.47583798747496, -5.24396052331554, -7.2690008846359, -9.50451068338581, -11.9032604272567, -14.4176327390446, -16.9998176236069, -19.6019094345634, -22.1759550265352, -24.6739776668383, -27.0479889096801, -29.2499944927101, -31.2319972651608, -32.945998641919, -34.3439993255969, -35.3779996651013, -35.9999998336943, -36.161999917415, -35.8159999589895, -34.9139999796348, -33.4079999898869, -31.249999994978, -28.3919999975061, -24.7859999987616, -20.383999999385, -15.1379999996945])
pyplt.plot(values_x,values_y)
The squared error is quite high:
def Error(y,y0):
return ( (1.0)*sum((y-y0)**2.0) )
print Error(y,values_y)
Now, to implement the gradient descent, I derived the partial derivative functions for c_1, c_2 and r_1 and implemented the Gradient Descent:
step_size = 0.0000005
accepted_Error = 50
dc1 = c1
dc2 = c2
dr1 = r1
y0 = values_y
previous_Error = 100000
left = True
for _ in range(1000):
gc1 = (2.0) * sum( ( y - dc1*np.exp(dr1*x) - (dc1*x)**3 + (dc2*x)**2 ) * ( -1*np.exp(dr1*x) - (3*(dc1**2)*(x**3)) ) )
gc2 = (2.0) * sum( ( y - dc1*np.exp(dr1*x) - (dc1*x)**3 + (dc2*x)**2 ) * ( 2*dc2*(x**2) ) )
gr1 = (2.0) * sum( ( y - dc1*np.exp(dr1*x) - (dc1*x)**3 + (dc2*x)**2 ) * ( -1*dc1*x*np.exp(dr1*x) ) )
dc1 = dc1 - step_size*gc1
dc2 = dc2 - step_size*gc2
dr1 = dr1 - step_size*gr1
y1 = dc1*np.exp(dr1*x) + (dc1*x)**3.0 - (dc2*x)**2.0
current_Error = Error(y0,y1)
if (current_Error > accepted_Error):
print currentError
else:
break
if (current_Error > previous_Error):
print currentError
print "DIVERGING"
break
if (current_Error==previous_Error):
print "CAN'T IMPROVE"
break
previous_Error = current_Error
However, the error is not improving at all, and I tried to vary the step size. Is there a mistake in my code?

Python -- Matplotlib for elliptic curve with sympy solve()

I have an elliptic curve plotted. I want to draw a line along a P,Q,R (where P and Q will be determined independent of this question). The main problem with the P is that sympy solve() returns another equation and it needs to instead return a value so it can be used to plot the x-value for P. As I understood it, solve() should return a value, so I'm clearly doing something wrong here that I'm just totally not seeing. For reference, here's how P+Q=R should look:
I've been going over the docs and other material and this is as far as I've been able to get myself into trouble:
from mpl_toolkits.axes_grid.axislines import SubplotZero
from pylab import *
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches
from matplotlib import rc
import random
from sympy.solvers import solve
from sympy import *
def plotGraph():
fig = plt.figure(1)
#ax = SubplotZero(fig, 111)
#fig.add_subplot(ax)
#for direction in ["xzero", "yzero"]:
#ax.axis[direction].set_axisline_style("-|>")
#ax.axis[direction].set_visible(True)
#ax.axis([-10,10,-10,10])
a = -2; b = 1
y, x = np.ogrid[-10:10:100j, -10:10:100j]
xlist = x.ravel(); ylist = y.ravel()
elliptic_curve = pow(y, 2) - pow(x, 3) - x * a - b
plt.contour(xlist, ylist, elliptic_curve, [0])
#rand = random.uniform(-5,5)
randmid = random.randint(30,70)
#y = ylist[randmid]; x = xlist[randmid]
xsym, ysym = symbols('x ylist[randmid]')
x_result = solve(pow(ysym, 2) - pow(xsym, 3) - xsym * a - b, xsym) # 11/5/13 needs to return a value
plt.plot([-1.5,5], [-1,8], color = "c", linewidth=1) # plot([x1,x2,x3,...],[y1,y2,y3,...])
plt.plot([xlist[randmid],5], [ylist[randmid],8], color = "m", linewidth=1)
#rc('text', usetex=True)
text(-9,6,' size of xlist: %s \n size of ylist: %s \n x_coord: %s \n random_y: %s'
%(len(xlist),len(ylist),x_result,ylist[randmid]),
fontsize=10, color = 'blue',bbox=dict(facecolor='tan', alpha=0.5))
plt.annotate('$P+Q=R$', xy=(2, 1), xytext=(3, 1.5),arrowprops=dict(facecolor='black', shrink=0.05))
## verts = [(-5, -10),(5, 10)] # [(x,y)startpoint,(x,y)endpoint] #,(0, 0)]
## codes = [Path.MOVETO,Path.LINETO] # related to verts[] #,Path.STOP]
## path = Path(verts, codes)
## patch = patches.PathPatch(path, facecolor='none', lw=2)
## ax.add_patch(patch)
plt.grid(True)
plt.show()
def main():
plotGraph()
if __name__ == '__main__':
main()
Ultimately, I'd like to draw a line to show P+Q=R, so if someone also has something to add on how to code to get the Q that would be greatly appreciated. I'm teaching myself about Python and elliptic curves so I'm sure that any entry-level programmer can figure out in 2 minutes what I've been on for some time already.
I don't know what are you calculating, but here is the code that can plot the graph:
import numpy as np
import pylab as pl
Y, X = np.mgrid[-10:10:100j, -10:10:100j]
def f(x):
return x**3 -3*x + 5
px = -2.0
py = -np.sqrt(f(px))
qx = 0.5
qy = np.sqrt(f(qx))
k = (qy - py)/(qx - px)
b = -px*k + py
poly = np.poly1d([-1, k**2, 2*k*b+3, b**2-5])
x = np.roots(poly)
y = np.sqrt(f(x))
pl.contour(X, Y, Y**2 - f(X), levels=[0])
pl.plot(x, y, "o")
pl.plot(x, -y, "o")
x = np.linspace(-5, 5)
pl.plot(x, k*x+b)
graph:
based on HYRY's answer, I just update some details to make it better:
import numpy as np
import pylab as pl
Y, X = np.mgrid[-10:10:100j, -10:10:100j]
def f(x, a, b):
return x**3 + a*x + b
a = -2
b = 4
# the 1st point: 0, -2
x1 = 0
y1 = -np.sqrt(f(x1, a, b))
print(x1, y1)
# the second point
x2 = 3
y2 = np.sqrt(f(x2, a, b))
print(x2, y2)
# line: y=kl*x+bl
kl = (y2 - y1)/(x2 - x1)
bl = -x1*kl + y1 # bl = -x2*kl + y2
# y^2=x^3+ax+b , y=kl*x+bl => [-1, kl^2, 2*kl*bl, bl^2-b]
poly = np.poly1d([-1, kl**2, 2*kl*bl-a, bl**2-b])
# the roots of the poly
x = np.roots(poly)
y = np.sqrt(f(x, a, b))
print(x, y)
pl.contour(X, Y, Y**2 - f(X, a, b), levels=[0])
pl.plot(x, y, "o")
pl.plot(x, -y, "o")
x = np.linspace(-5, 5)
pl.plot(x, kl*x+bl)
And we got the roots of this poly:
[3. 2.44444444 0. ] [5. 3.7037037 2. ]