stackuser
stackuser

Reputation: 869

Python -- sympy solve() returning another equation instead of value

This creates a list of points and the sympy solve() method should be returning the value of x. Instead it's returning yet another equation, and I'm not sure why. The ogrid() and ravel() are creating a list of points in the plot and this is on Matplotlib, if that makes a difference but I don't think it should. It should be finding root(s) of the equation. I'm not sure what I'm doing wrong here that causes it to not return a value, but instead returns another equation:

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 *

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])
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

I'm teaching myself Python so this is probably something a junior programmer can help me out with, but if a pro sees this and can spare a moment to help that'd be great.

EDIT:

Returns a value for y roughly 3.xx where there's not 3 possible x-values:

enter image description here

Upvotes: 2

Views: 1498

Answers (1)

HYRY
HYRY

Reputation: 97291

x_result is a list of expressions, which is the solutions of your equation. It's not a list of value because ysym is a symbol. If you want the numeric results, you need call subs() and evalf():

[e.subs({ysym:ylist[randmid]}).evalf() for e in x_result]

output:

[0.0871073310916539 - 8.0e-17*I,
 1.36864647418387 + 4.37e-17*I,
 -1.45575380527552 + 3.63e-17*I]

Upvotes: 2

Related Questions