def pointfind(b,d,e,xval,sign):
if xval^3+b*xval^2+d*xval+e >= 0:
z = n(sqrt(xval^3+b*xval^2+d*xval+e))
if sign>=0:
return (xval,z)
else:
return (xval,-z)
########################################
def compute_intersection(b,d,e,P1,P2):
if P1==P2:
m=(3*P1[0]^2+2*b*P1[0]+d)/float(2*P1[1])
v=P1[1]-m*P1[0]
x=(P1[0]^4-2*d*P1[0]^2-8*e*P1[0]+d^2-4*b*e)/float(4*P1[0]^3+4*b*P1[0]^2+4*d*P1[0]+4*e)
return(x,m*x+v)
else:
m=(P2[1]-P1[1])/float(P2[0]-P1[0])
v=P1[1]-m*P1[0]
x=m^2-b-P1[0]-P2[0]
return (x,m*x+v)
#########################################
def point_text(P,name,rgbcolor):
return text(name,(P[0]-.3,P[1]+1),rgbcolor=rgbcolor)
#########################################
def plot_grouplaw(b,d,e,xmin,xmax,ymin,ymax,P1,P2):
PA=linedraw(P1,P2,xmin,xmax,color='green')
PA+=point(P1,rgbcolor=(0,0,0),pointsize=40)
PA+=point(P2,rgbcolor=(0,0,0),pointsize=40)
C=compute_intersection(b,d,e,P1,P2)
PA+=point(C,rgbcolor=(0,1,0),pointsize=40)
#PA+=linedraw(P1,C,xmin,xmax,color='green')
PA+=line([C,(C[0],-C[1])],color='green',linestyle='--')
PA+=line([(C[0],-C[1]),(C[0],ymin)],color='green',linestyle='--')
PA+=line([(C[0],C[1]),(C[0],ymax)],color='green',linestyle='--')
PA+=point((C[0],-C[1]),rgbcolor=(0,1,0),pointsize=40)
PA+=point_text(C,"$P*Q$",rgbcolor=(1,0,0))
PA+=point_text((C[0],-C[1]),"$P+Q$",rgbcolor=(1,0,0))
return PA
##########################################
def linedraw(P1,P2,xmin,xmax,**keywords):
var('x')
if P1==P2:
m=(3*P1[0]^2+2*b*P1[0]+d)/float(2*P1[1])
v=P1[1]-m*P1[0]
else:
m=(P2[1]-P1[1])/float(P2[0]-P1[0])
v=P1[1]-m*P1[0]
f(x)=m*x+v
return plot(f(x),(xmin,xmax),**keywords)
##########################################
def is_on_curve(b,d,e,x):
return x^3+b*x^2+d*x+e >= 0
##########################################
@interact
def ellplot(xmin=slider(-20,-1,1,-3,label='$x$-min on plot: '),
xmax=slider(0,30,1,5,label='$x$-max on plot: '),
b=slider(-10,10,1,0,label='coefficient of $x^2$: '),
d=slider(-10,10,1,0,label='coefficient of $x$: '), e=slider(-10,10,1,1,label='constant coefficient: '),
xP=input_box(default=4,type=float,label='$x$-coord of P: '), xPsign=checkbox(default=True,label='desired sign of $y$-coord of P is positive? '), xQ=input_box(default=0,type=float,label='$x$-coord of Q: '), xQsign=checkbox(default=True,label='desired sign of $y$-coord of Q is positive? ')):
if (4*d^3+27*e^2)==0:
print 'please enter valid coefficients for a non-singular curve'
else:
if (not is_on_curve(b,d,e,xP)) or (not is_on_curve(b,d,e,xQ)):
print 'at least one of the x-coordinates entered does not define a point on the curve over RR'
else:
if xPsign == True:
xpsign=1
else:
xpsign=-1
if xQsign == True:
xqsign=1
else:
xqsign=-1
E=EllipticCurve([0,b,0,d,e])
P=pointfind(b,d,e,xP,xpsign)
Q=pointfind(b,d,e,xQ,xqsign)
p=plot(E,xmin,xmax)
p+=point_text(P,"$P$",rgbcolor=(1,0,0))
p+=point_text(Q,"$Q$",rgbcolor=(1,0,0))
ymin=p.ymin()
ymax=p.ymax()
p+=plot_grouplaw(b,d,e,xmin,xmax,ymin,ymax,P,Q)
print E
show(p)
print P,' + ',Q,' = ',(compute_intersection(b,d,e,P,Q)[0],-compute_intersection(b,d,e,P,Q)[1])
|
|
Click to the left again to hide and once more to show the dynamic interactive window
|