Julia knows about integers, floating point numbers, even complex numbers like 1+2im.
In [1]:
2+2
Out[1]:
In [2]:
2/3
Out[2]:
In [8]:
(1+2im)*(2+3im)
Out[8]:
Julia already knows about sines and cosines, arrays, ranges of numbers. So you can save a lot of time and do calculations quickly. For instance, we can sum the integers from 1 to 100 as follows:
In [15]:
sum(1:100)
Out[15]:
We can compute the Riemann sum of the integral $\int_0^\pi \sin(x) dx$ as $$\sum_{k=1}^N \sin(x_k)\Delta_k$$ as:
In [17]:
sum(sin(0:.01:pi))*.01
Out[17]:
In [27]:
# Here is a matrix. Notice there are no commas. The semicolon separates the rows.
m = [10 20; 30 40]
Out[27]:
In [28]:
det(m)
Out[28]:
In [38]:
eig(m)
Out[38]:
In [40]:
eigval,eigvec = eig(m)
Out[40]:
In [41]:
# Check that the product of the eignvalues is the determinant.
prod(eigval)
Out[41]:
In [42]:
m*m
Out[42]:
In [43]:
m^2
Out[43]:
In [45]:
det(m^2)
Out[45]:
In [46]:
exp(m)
Out[46]:
In [47]:
expm(m)
Out[47]:
To plot, we have to load in a package. The one we will use is called PyPlot, which is the same package as we used in Python plotting. (matplotlib.pyplot)
Notice the syntax for loading a package is very different in Julia.
Warning: Also note that this is a dangerous spot for the workshop, as this beta implementation of Julia tends to choke on loading packages. Especially if everyone in class tries to do it at once.
In [32]:
using PyPlot
In [33]:
plot([1,2,3,4])
Out[33]:
In [34]:
x = linspace(-3, 3)
y = x.^3 - 3*x
plot(x,y)
Out[34]:
In [35]:
n = 100
x = linspace(-3, 3, n)
y = linspace(-3,3,n)
xgrid = repmat(x',n,1)
ygrid = repmat(y,1,n)
z=exp(-xgrid.^2 - ygrid.^2)
plot_surface(x,y,z)
Out[35]:
In [36]:
plot_surface(x,y,z,cmap="jet")
Out[36]:
In [37]:
imshow(z)
Out[37]:
In [ ]: