IPython is not just for Python...

Example: Julia Language!

<img src="images/julia.jpeg", width="30%">

(thanks to Fernando Perez for providing the examples below)

In [1]:
%matplotlib inline
%run talktools

Using the Julia Magic

In [2]:
%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
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="--")
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>]
Out[3]:
[<matplotlib.lines.Line2D at 0x114e79ed0>]

Julia Variables can be accessed in Python!

In [4]:
fig = %julia fig
fig
Running src: fig
Ans: 4450582896
Pyans (s, r): Figure(480x320) ||| <matplotlib.figure.Figure object at 0x1061addd0>
Out[4]:

Getting Crazy: two-stage recursion in Python and Julia

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)
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
Out[5]:
8

The Possibilities are Endless

Other language magics and kernels available as well: Julia, R, Ruby, Haskell, etc.

Look for the I*[your language here]* notebook!

  1. IPython "Official" version.
  2. IJulia
  3. IHaskell
  4. IFSharp
  5. IRuby
  6. IGo
  7. IScala
  8. IMathics
  9. IAldor
  10. IRKernel: Leaflet routing in R
  11. IErlang