Ask Your Question

Possible speed improvement for interactive complex plot

asked 2012-11-18 00:06:48 +0200

marco gravatar image

updated 2012-11-18 00:11:52 +0200

The following is a simple function which plots the real part of a complex polynomial f, the zeros of its derivative, and implicitly defined curves passing through these zeros. The whole function only depends on a parameter Z in the original polynomial f.

I would like to know if the speed of this can be dramatically improved (even at the expense of accuracy) so that I can make it interactive, so that I can vary the parameter Z. It seems to me that it should be possible to make this almost instantly responsive.

I = CDF.0                                      # Define i as complex double
Z = 1 + (-0.1)*I                               # Here is the complex number parameter 
f = lambda z: (z^3)/3 - Z*z                    # A complex polynomial           
P=complex_plot(lambda z: f(z).real(), (-5,5), (-5,5), plot_points=200)   # Plot the real part  
Roots=derivative(f(z),z).roots(ring=CDF)       # Find the roots of derivative(f)
R = [(Roots[i][0].real(),Roots[i][0].imag()) for i in range(len(Roots))]
F(x,y) = f(x+I*y)
COL=["black", "blue", "purple", "green", "orange"]
C=[implicit_plot((F-F(R[n][0],R[n][1])).imag(), (-5, 5), (-5, 5), color=COL[n]) for n in range(len(R))]
P+point(R,zorder=40) + point((Z.real(),Z.imag()),zorder=40)+ sum(C)  # Plot the real part of f, and the roots, and the parameter, and all the implicit curves given by imaginary(f) = imaginary(root)

This function ran in about 13 seconds on my (recent) imac and outputs image:example

edit retag flag offensive close merge delete


You may want to use Cython to speed up more - see and the sage-devel discussion which led to its improvement for some ideas.

kcrisman gravatar imagekcrisman ( 2012-11-18 22:54:22 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2012-11-18 14:12:40 +0200

marco gravatar image

I managed to speed it up a bit by using fast_callable, but annoyingly the function contour_plot does not take CDF, only RDF. Note that if I change RDF in the definition of F to CDF, countour_plot complains.

ff(z) =(z^3)/3 - Z*z 
ffr(z) = ff(z).real()
f = fast_callable(ff, domain=CDF, vars='z')
fr = fast_callable(ffr, domain=CDF, vars='z')                 
P=complex_plot(fr, (-5,5), (-5,5), plot_points=200)  
R = [(Roots[i][0].real(),Roots[i][0].imag()) for i in range(len(Roots))]
F = fast_callable(ff(x+I*y).imag(), vars = ('x','y'),domain=RDF)
COL=["blue","purple", "orange","green", "black"]
cont=[F(R[i][0],R[i][1]) for i in range(len(R))]
C = contour_plot(F, (x,-5,5), (y,-5,5), contours=tuple(cont), fill=false, cmap = COL)
return P+point(R,zorder=2) + point((Z.real(),Z.imag()),zorder=1,size=50,color="yellow")+ C


edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2012-11-18 00:06:48 +0200

Seen: 674 times

Last updated: Nov 18 '12