PyconBrasil 2008

1340 days ago by pub

# sage eh Python -- podemos definir uma funcao, por exemplo: def hello(n): print 'Hello world!\n' * n hello(2) 
       
Hello world!
Hello world!
Hello world!
Hello world!
# (eh *quase* Python, na verdade...) print 2**3, 2^3, eval("2^3") 
       
8 8 1
8 8 1
# vem com varios modulos prontos para serem importados import numpy import scipy import pylab x = numpy.linspace(0, 10*3.14, 1000) y = numpy.sin(x) pylab.close() pylab.plot(x, y) pylab.show() pylab.savefig('sin.png') 
       
# e tem autocomplete/ajuda #pylab.<tab> 
       
# plots 2D usando comandos nativos do Sage f = lambda x,y: cos(x*y) C = contour_plot(f, (-4, 4), (-4, 4)) show(C) 
       
def slow_logistic(n,a,lamb = 3.82): """ A slow logistic map. """ q = a for i in range(n): q = lamb*q*(1-q) return q time a = slow_logistic(10^6,.5) 
       
Time: CPU 4.91 s, Wall: 4.93 s
Time: CPU 4.91 s, Wall: 4.93 s
%cython def cython_logistic(int n, float a, float lamb = 3.82): cdef float q q = a for i in range(n): q = lamb*q*(1-q) return q time a = cython_logistic(10^6,.5) 
       
Traceback (click to the left of this block for traceback)
...
/home/sage13/.sage/temp/sage/22788/spyx/_home_server2_sage_notebook_work\
sheets_roberto_0_code_sage39_spyx/_home_server2_sage_notebook_worksheets\
_roberto_0_code_sage39_spyx_0.pyx:13:5: Syntax error in simple statement
list
Traceback (most recent call last):        cdef float q
  File "/home/server2/sage/local/lib/python2.5/site-packages/sage/server/support.py", line 362, in cython_import_all
    create_local_c_file=create_local_c_file)
  File "/home/server2/sage/local/lib/python2.5/site-packages/sage/server/support.py", line 343, in cython_import
    create_local_c_file=create_local_c_file)
  File "/home/server2/sage/local/lib/python2.5/site-packages/sage/misc/cython.py", line 344, in cython
    raise RuntimeError, "Error converting %s to C:\n%s\n%s"%(filename, log, err)
RuntimeError: Error converting /home/server2/sage_notebook/worksheets/roberto/0/code/sage39.spyx to C:


Error converting Pyrex file to C:
------------------------------------------------------------
...
    q = a
    for i in range(n):
        q = lamb*q*(1-q)
    return q

time a = cython_logistic(10^6,.5)    ^
------------------------------------------------------------

/home/sage13/.sage/temp/sage/22788/spyx/_home_server2_sage_notebook_worksheets_roberto_0_code_sage39_spyx/_home_server2_sage_notebook_worksheets_roberto_0_code_sage39_spyx_0.pyx:13:5: Syntax error in simple statement list
# resolvendo equacoes diferenciais via Maxima t = var('t') # define a variable t x = function('x',t) # define x to be a function of that variable DE = lambda y: diff(y,t) + y - 1 resposta = desolve(DE(x(t)), [x,t]) print resposta 
       
%e^-t*(%e^t+%c)
%e^-t*(%e^t+%c)
# plots parametricos interativos u, v = var('u,v') fx = u*v fy = u fz = v^2 P = parametric_plot3d([fx, fy, fz], (u, -1, 1), (v, -1, 1), frame=False, color="yellow") show(P) 
       
@interact def rwalk3d(n=(50,1000), frame=True): pnt = [0,0,0] v = [copy(pnt)] for i in range(int(n)): pnt[0] += random()-0.5 pnt[1] += random()-0.5 pnt[2] += random()-0.5 v.append(copy(pnt)) show(line3d(v,color='black'),aspect_ratio=[1,1,1],frame=frame) 
       
frame 
CPU time: 0.01 s, Wall time: 0.01 s

Click to the left again to hide and once more to show the dynamic interactive window

var('x') x0 = 0 f = sin(x)*e^(-x) p = plot(f,-1,5, thickness=2) dot = point((x0,f(x0)),pointsize=80,rgbcolor=(1,0,0)) @interact def _(order=(1..12)): ft = f.taylor(x,x0,order) pt = plot(ft,-1, 5, color='green', thickness=2) html('$f(x)\;=\;%s$'%latex(f)) html('$\hat{f}(x;%s)\;=\;%s+\mathcal{O}(x^{%s})$'%(x0,latex(ft),order+1)) show(dot + p + pt, ymin = -.5, ymax = 1) 
       
order 
CPU time: 0.12 s, Wall time: 0.34 s

Click to the left again to hide and once more to show the dynamic interactive window