5

我绘制了一条椭圆曲线。我想沿着 a 画一条线P,Q,R(在哪里PQ将独立于这个问题来确定)。的主要问题P是 sympysolve()返回另一个方程,它需要返回一个值,以便它可以用于绘制 x 值P。据我了解,solve()应该返回一个值,所以我显然在这里做错了,我完全没有看到。作为参考,以下是P+Q=R外观:

在此处输入图像描述

我一直在阅读文档和其他材料,这就是我能够让自己陷入困境的情况:

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 分钟内弄清楚我已经从事了一段时间的工作。

4

1 回答 1

9

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

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)

图形:

在此处输入图像描述

于 2013-11-06T12:49:02.317 回答