jsMath
Adrian Belshaw: Unimodular polynomials

You can also download the Sage Worksheet.

# parametric plots of unimodular polynomials in orange, L2-norm in green
# graphs of distance from L2-norm in red; if this graph has a low maximum, the polynomial is flat

t=var('t')
x=1 + cos(2*pi*t) + cos(2*pi*(2*t))
y=sin(2*pi*t) + sin(2*pi*(2*t))
P = parametric_plot( (x(t),y(t)), 0, 1, rgbcolor=hue(0.1) )
d = sqrt(3)
Q = parametric_plot( (d*cos(2*pi*t),d*sin(2*pi*t)), 0, 1, rgbcolor=hue(0.5) )
W=P+Q

r = abs(sqrt(x(t)^2 + y(t)^2) - d)
R = plot(r(t), 0, 1, rgbcolor=hue(0.0))
R.xmin(0)
R.ymin(0)
R.ymax(2)
x2=1 + cos(2*pi*t) + cos(2*pi*(2*t+1/2))
y2=sin(2*pi*t) + sin(2*pi*(2*t+1/2))
P2 = parametric_plot( (x2(t),y2(t)), 0, 1, rgbcolor=hue(0.1) )
W2=P2+Q
W2.set_aspect_ratio(1)
r2 = abs(sqrt(x2(t)^2 + y2(t)^2) - d)
R2 = plot(r2(t), 0, 1, rgbcolor=hue(0.0))
R2.xmin(0)
R2.ymin(0)
R2.ymax(2)
G = graphics_array(  [  ( W, W2 ),( R, R2 )  ]  );
show(W2)
show(G, aspect_ratio=1) 
       

# html display

W.save('1.png', figsize=5, aspect_ratio=1)
W2.xmin(W.xmin())
W2.ymin(W.ymin())
W2.xmax(W.xmax())
W2.ymax(W.ymax())
W2.save('2.png', figsize=5, aspect_ratio=1)
R.save('3.png', figsize=5)
R2.save('4.png', figsize=5)
html("""
<table>
 <tr>
   <td><img src="cell://1.png"></td>
   <td><img src="cell://2.png"></td>
 </tr>
 <tr>
   <td><img src="cell://3.png"></td>
   <td><img src="cell://4.png"></td>
 </tr>
 <tr>
   <td><center><font size=+3>$1 + z + z^2$</font></td>
   <td><center><font size=+3>$1 + z - z^2$</font></td>
 </tr>
</table>
""") 
       

1+z+z2
1+zz2 

1+z+z2
1+zz2 
%time
#double animation

def g(k):
   t=var('t')
   x=1 + cos(2*pi*(t+k)) + cos(2*2*pi*t)
   y=sin(2*pi*(t+k)) + sin(2*2*pi*t)
   P = parametric_plot( (x(t),y(t)), 0, 1, rgbcolor=hue(0.1) )
   d = sqrt(3)
   Q = parametric_plot( (d*cos(2*pi*t),d*sin(2*pi*t)), 0, 1, rgbcolor=hue(0.5) )
   r = abs(sqrt(x(t)^2 + y(t)^2) - d)
   R = plot(r(t), 4, 5, rgbcolor=hue(0.0) )
   W = P + Q + R
   W.set_aspect_ratio(1)
   return(W)
   
a = animate(  [g(float(k))  for k in srange (0,1,.02)] )
show(a) 
       
CPU time: 65.80 s,  Wall time: 123.35 s
CPU time: 65.80 s,  Wall time: 123.35 s
%time
# single animation degree 3

def g(k):
   t=var('t')
   x=1 + cos(2*pi*(t+k)) + cos(2*2*pi*t) + cos(2*pi*3*t)
   y=sin(2*pi*(t+k)) + sin(2*2*pi*t) + sin(2*pi*3*t)
   P = parametric_plot( (x(t),y(t)), 0, 1, rgbcolor=hue(0.1) )
   d = sqrt(3)
   Q = parametric_plot( (d*cos(2*pi*t),d*sin(2*pi*t)), 0, 1, rgbcolor=hue(0.5) )
   return(P+Q)
a = animate(  [g(float(k))  for k in srange(0,1,.02)], aspect_ratio=1 )
show(a) 
       
CPU time: 82.97 s,  Wall time: 165.57 s
CPU time: 82.97 s,  Wall time: 165.57 s
%time
# parametric plot with any unimodular coefficients
# v can be a list of real numbers of any length (w.l.g. between 0 and 1)

v = [random(),   random(), random(), random(), random(), random(), random()]
d=len(v)-1
r = sqrt(d+1)
nu = d + 1 - r

def g(v):
   t=var('t')
   x= sum( cos(2*pi*(k*t + v[k]) ) for k in [0..d]  )
   y= sum( sin( 2*pi*(k*t+v[k])  ) for k in [0..d] )
   P = parametric_plot( (x(t),y(t)), 0, 1, rgbcolor=hue(0.1) )
   Q = parametric_plot( (r*cos(2*pi*t),r*sin(2*pi*t)), 0, 1, rgbcolor=hue(0.5) )
   return(P+Q)

def s(v):
   t=var('t')
   x= sum( cos(2*pi*(k*t + v[k]) ) for k in [0..d]  )
   y= sum( sin( 2*pi*(k*t+v[k])  ) for k in [0..d] )
   u = abs(sqrt(x(t)^2 + y(t)^2) - r )
   U = plot(u(t), 0, 1, rgbcolor=hue(0.0) )  
   return(U)


 
W = g(v)
W.set_aspect_ratio(1)
show(W)
print( "degree " + str(d) )
show( s(v), xmin = 0, ymin = 0, ymax = nu ) 
       
degree 6
CPU time: 7.75 s,  Wall time: 16.39 s
degree 6
CPU time: 7.75 s,  Wall time: 16.39 s