This notebook is a preview demo of **IJulia**: a Julia-language backend combined with the IPython interactive environment. This combination allows you to interact with the Julia language using IPython's powerful graphical notebook, which combines code, formatted text, math, and multimedia in a single document.

- Note: this is a
*preview*, because it relies on pre-release bleeding-edge versions of Julia, IPython, and several Julia packages, as explained on the IJulia github page, and functionality is evolving rapidly. We hope to have a more polished release soon.

Basic mathematical expressions work like you expect:

In [6]:

```
function f(x)
y = x.^2;
return y;
end
g = x -> x.^2
```

Out[6]:

In [7]:

```
a = randn(3,3);
f(a) - g(a)
```

Out[7]:

In [29]:

```
a[:]
```

Out[29]:

You can define variables, write loops, and execute arbitrary multiline code blocks. Here is an example of an alternating harmonic series $\sum_{n=1}^\infty \frac{(-1)^n}{n}$ from a Julia tutorial by Homer Reid:

In [2]:

```
s = 0.0
for n = 1:2:10000
s += 1/n - 1/(n+1)
end
s # an expression on the last line (if it doesn't end with ";") is printed as "Out"
```

Out[2]:

Previous outputs can be referred to via `Out[`

*n*`]`

, following the IPython, for example `Out[2]`

for the result above. You can also use the shorthand `_2`

, or `_`

for the previous result, as in IPython. Like in Matlab, `ans`

can also be used to refer to the previous result, *even if it was not printed* (when the command ended with `;`

).

For example, the harmonic series above should be converging (slowly) to $\ln 2$, and we can check this:

In [3]:

```
Out[2] - log(2)
```

Out[3]:

Like Matlab or Scipy + Numpy, Julia has lots of mathematical functions and linear algebra built in. For example, we can define a $500\times500$ random matrix $R$ and form the positive-definite matrix $R^* R$:

In [5]:

```
R = rand(500,500)
R' * R + 1
```

Out[5]:

(Notice that, by default, only a portion of a large matrix is shown. You didn't really want to read $500^2 = 250,000$ numbers, did you?)

Standard output from Julia is also captured and sent to the IJulia notebook as you'd expect:

In [6]:

```
println("Hello world!\n")
println(STDERR, "Börk börk börk, some unicode output on stderr...\n")
```

IJulia even captures output from external C libraries (and notice how easy it is using Julia's `ccall`

intrinsic):

In [7]:

```
ccall(:printf, Cint, (Ptr{Uint8},), "Hello from C!!\n");
```

We can define functions, of course, and use them in later input cells:

In [8]:

```
f(x) = x + 1
```

Out[8]:

In [9]:

```
println(f(3))
f([1,1,2,3,5,8])
```

Out[9]:

Notice that the input above both printed an scalar to `STDOUT`

and also returned a vector, the latter using Julia's ability to write polymorphic functions and built-in array operations.

On the other hand adding a string to a number is not defined (there is no `+`

method defined for those types, although we could easily add one), and attempting to do so will throw an exception:

In [10]:

```
f("Hello?")
```

Julia can easily and transparently call external Python code using a package called PyCall, and to illustrate that capability we will show some examples calling SciPy and Matplotlib from Julia.

For example, we can use the Newton solver in scipy.optimize to solve a transcendental equation $\cos(x) - x = 0$ given a *Julia* function:

In [13]:

```
using PyCall
@pyimport scipy.optimize as so
so.newton(x -> cos(x) - x, 1)
```

Out[13]:

We can use the same `@pyimport`

syntax to import Matplotlib (specifically, the `matplotlib.pyplot`

module), but to integrate Matplotlib's graphics with the IJulia display requires a little more work. To simplify this, we've created a PyPlot module for Julia:

In [8]:

```
using PyPlot
x = linspace(0,2*pi,1000)
y = sin(3*x + 4*cos(2*x))
plot(x, y, color="red", linewidth=2.0, linestyle="--")
ylabel("the y axis")
xlabel("the x axis")
title("a sinusoidally-modulated sinusoid")
```

Out[8]:

In [13]:

```
g = imread("nt_toolbox/data/hibiscus.bmp");
```

In [18]:

```
imshow(g, interpolation="nearest")
set_cmap("gray")
axis("off")
```

Out[18]:

In [30]:

```
include("nt_toolbox/nt_signal.jl")
using nt_signal
imageplot(g)
```