gpt4 book ai didi

python-2.7 - Python——用于椭圆曲线的 Matplotlib 与 sympy solve()

转载 作者:行者123 更新时间:2023-12-03 15:52:41 25 4
gpt4 key购买 nike

我绘制了一条椭圆曲线。我想沿着 P,Q,R 画一条线(其中 PQ 将独立于此问题而确定)。 P的主要问题是同情吗solve()返回另一个方程,它需要返回一个值,以便它可以用于绘制 P 的 x 值.据我了解,solve()应该返回一个值,所以我显然在这里做错了我完全没有看到的事情。作为引用,这里是如何P+Q=R应该看起来:

enter image description here

我一直在查看文档和 other material这就是我能够让自己陷入困境的程度:

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

最后,我想画一条线来显示 P+Q=R ,所以如果有人还想添加一些关于如何编码以获取 Q 的内容。将不胜感激。我正在自学 Python 和椭圆曲线,所以我相信任何入门级程序员都可以在 2 分钟内弄清楚我已经研究了一段时间。

最佳答案

我不知道你在计算什么,但这里是可以绘制图形的代码:

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)

图形:

enter image description here

关于python-2.7 - Python——用于椭圆曲线的 Matplotlib 与 sympy solve(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19800518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com