Example: Julia Language!
<img src="images/julia.jpeg", width="30%">
(thanks to Fernando Perez for providing the examples below)
%matplotlib inline
%run talktools
%load_ext julia.magic
%julia @pyimport matplotlib.pyplot as plt
%julia @pyimport numpy as np
Initializing Julia interpreter. This may take some time... Running src: @pyimport matplotlib.pyplot as plt Ans: 4363833856 Pyans (s, r): None ||| None Running src: @pyimport numpy as np Ans: 4363833856 Pyans (s, r): None ||| None
%%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="--")
Running src: # 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="--") Ans: 4453709952 Pyans (s, r): [<matplotlib.lines.Line2D object at 0x114e79ed0>] ||| [<matplotlib.lines.Line2D object at 0x114e79ed0>]
[<matplotlib.lines.Line2D at 0x114e79ed0>]
fig = %julia fig
fig
Running src: fig Ans: 4450582896 Pyans (s, r): Figure(480x320) ||| <matplotlib.figure.Figure object at 0x1061addd0>
# define a recursive julia fib function which calls Python
fib = %julia fib(n, fib2) = n < 2 ? n : fib2(n-1, fib) + fib2(n-2, fib)
# define a recursive python fib function which calls Julia
def fib2(n, fib):
print 'Into Python, n=',n,
if n < 2:
return n
print 'Out to Julia'
return fib(n-1, fib2) + fib(n-2, fib2)
fib2(6, fib)
Running src: fib(n, fib2) = n < 2 ? n : fib2(n-1, fib) + fib2(n-2, fib) Ans: 4564745280 Pyans (s, r): <PyCall.jlwrap fib> ||| <PyCall.jlwrap fib> Into Python, n= 6 Out to Julia Into Python, n= 4 Out to Julia Into Python, n= 2 Out to Julia Into Python, n= 1 Into Python, n= 1 Into Python, n= 0 Into Python, n= 3 Out to Julia Into Python, n= 1 Into Python, n= 0 Into Python, n= 3 Out to Julia Into Python, n= 1 Into Python, n= 0 Into Python, n= 2 Out to Julia
8