In [1]:
%matplotlib inline
%run talktools
In [2]:
%load_ext julia.magic
%julia @pyimport matplotlib.pyplot as plt
%julia @pyimport numpy as np
In [3]:
%%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[3]:
In [4]:
fig = %julia fig
fig
Out[4]:
In [5]:
# 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)
Out[5]: