In [1]:
%matplotlib inline
In [2]:
# Waking up Julia takes up to 40 seconds the first time
import julia
j = julia.Julia()
In [3]:
# evaluation using Julia interpreter
print(j.eval("sqrt(1 +31)"))
print(j.sind(90))
5.656854249492381 1.0
In [4]:
# Warning: loading '%load_ext julia.magic' before 'import julia' will change behaviour of julia object
# So here, we do it after
%load_ext julia.magic
Initializing Julia interpreter. This may take some time...
In [5]:
%julia @pyimport matplotlib.pyplot as plt
%julia @pyimport numpy as np
In [6]:
%%julia
# Note how we mix numpy and julia:
t = linspace(0, 2*pi, 1000); # use the julia linspace
s = sin(3 * t + 4 * np.cos(2 * t)); # use the numpy cosine and julia sine
fig = plt.gcf() # **** WATCH THIS VARIABLE ****
plt.plot(t, s, color="red", linewidth=2.0, linestyle="--")
Out[6]:
[<matplotlib.lines.Line2D at 0xd08d470>]
In [7]:
# fibonacci with each layer being Python than Julia
jfib = %julia jfib(n, fib) = n < 2 ? n : fib(n-1, jfib) + fib(n-2, jfib)
def pyfib(n, fib):
if n < 2:
return n
return fib(n-1, pyfib) + fib(n-2, pyfib)
pyfib(20, jfib)
Out[7]:
6765
In [8]:
# original examples from Fernando Perez
# http://nbviewer.ipython.org/github/fperez/talk-1312-nips/blob/master/IPython-Interactive-Computing.ipynb
In [ ]: