%matplotlib inline
# Waking up Julia takes up to 40 seconds the first time
import julia
j = julia.Julia()
# evaluation using Julia interpreter
print(j.eval("sqrt(1 +31)"))
print(j.sind(90))
5.656854249492381 1.0
# 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...
%julia @pyimport matplotlib.pyplot as plt
%julia @pyimport numpy as np
%%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="--")
[<matplotlib.lines.Line2D at 0xd08d470>]
# 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)
6765
# original examples from Fernando Perez
# http://nbviewer.ipython.org/github/fperez/talk-1312-nips/blob/master/IPython-Interactive-Computing.ipynb