from pyx import *
import subprocess

from euler import forward_euler, exp_example, sincos_example

def exp_plot():
    x, y = exp_example()
    g = graph.graphxy(width=8)
    g.plot(graph.data.points(zip(x, y), x=1, y=2))
    g.writetofile("euler_exp.eps")

def sincos_plot():
    x, y = sincos_example()
    plotdata = zeros((n+1, 5))
    plotdata[:,0] = x
    plotdata[:,1:3] = y
    plotdata[:,3] = cos(x)
    plotdata[:,4] = sin(x)    
    label = ["x", "y[0]", "y[1]", "cos", "sin" ]

    g = graph.graphxy(width=8, 
                      x=graph.axis.lin(min=0, max=maxx, title="x"),
                      y=graph.axis.lin(title="y"), #min=-1, max=1))
                      key=graph.key.key(pos=None, hpos=1.5, vpos=1.0))
    g.plot([ graph.data.points(plotdata,x=1,y=i+1,title=label[i])
             for i in range(1, 5) ],
           [graph.style.line()])
    ux, uy = g.vpos(0.1, 1.1)
    g.text(ux, uy, "Forward Euler, n=%d, h=%.6g" % (n, h))           
    g.writetofile("euler_sincos.eps")
    
    g = graph.graphxy(width=8, height=8,
                      x=graph.axis.lin(min=-2, max=2),
                      y=graph.axis.lin(min=-2, max=2))
    g.plot(graph.data.points(y, x=1, y=2),
           [graph.style.line(lineattrs=[ color.rgb.red ])])
    g.plot(graph.data.points(plotdata[:, 3:5], x=1, y=2),
           [graph.style.line(lineattrs=[ color.rgb.blue ])])
    g.writetofile("euler_sincos_phase.eps")

def show(s):
    subprocess.Popen(["gv", "--watch", s])

def show_all():
    show("euler_exp.eps")
    show("euler_sincos.eps")
    show("euler_sincos_phase.eps")


if __name__ == "__main__":
    exp_plot()
    #sincos_plot()
