Day_01 - Julia in the Jupyter notebook

Getting Conda and Julia to meet

I reinstalled Julia via

pacman -Syu julia,

then created a conda environment to build a clean Jupyter notebook.

  1. conda create -n julia

  2. conda activate julia

  3. conda install jupyterlab

Then, I had to rebuild the IJulia package:

]build("IJulia")

Now, I’m up and running in the Jupyter notebook. Pretty cool so far.

Playing with some vector math and notation

x = [1, 2, 3];

It looks like Julia suppresses output with the ; like Matlab.

print(transpose(x));
[1 2 3]

This was an interesting choice here. The default shape for a vector is 1D, e.g. (N, ), but you need to do a tranpose to multiply vectors either dot product or outer product

print(x*transpose(x))
print('\n',transpose(x)*x)
[1 2 3; 2 4 6; 3 6 9]
14

The result is that the 1D vector has an implicit second dimension. This is like Matlab. A (3, ) vectors is really treated as a (3, 1) column vector.

There are some cool Linear Algebra things I might be able to build with this kind of framework.

print("size(x) = ", size(x))
print("\nsize(transpose(x)) = ", size(transpose(x)))
size(x) = (3,)
size(transpose(x)) = (1, 3)

Plotting first time to plot

Man, I really thought I messed something up with my first plot. It was taking forever. Then, I tried it in the REPL and same thing. Then, I found the first time to plot (issue?). It seems that getting the Julia environment ready to display data takes some computational work.

using Plots
x = range(-5, 5, length = 50)
y = x.^2
plot(x, y)
../_images/day_01_11_0.svg

In Matplotlib, the more plots you add, the more lines you have. Here it looks like the lines get overwritten more like Matlab. Ah, but adding a plot! adds the lines to the current plot.

plot(x, x.^3)
plot!(x, x.^2)
plot!(x, 1/2*x)
../_images/day_01_13_0.svg

Animated plots + help with functions

I found this awesome animated gif of a sine+cosine moving camera+tracking line. There were a bunch of plot calls, but one function stood out, @gif. I hadn’t seen a MATLAB/Python equivalent so I ran the code to get the gif. Success.

default(legend = false)
x = y = range(-5, 5, length = 40)
zs = zeros(0, 40)
n = 100

@gif for i in range(0, stop = 2π, length = n)
    f(x, y) = sin(x + 10sin(i)) + cos(y)

    # create a plot with 3 subplots and a custom layout
    l = @layout [a{0.7w} b; c{0.2h}]
    p = plot(x, y, f, st = [:surface, :contourf], layout = l)

    # induce a slight oscillating camera angle sweep, in degrees (azimuth, altitude)
    plot!(p[1], camera = (10 * (1 + cos(i)), 40))

    # add a tracking line
    fixed_x = zeros(40)
    z = map(f, fixed_x, y)
    plot!(p[1], fixed_x, y, z, line = (:black, 5, 0.2))
    vline!(p[2], [0], line = (:black, 5))

    # add to and show the tracked values over time
    global zs = vcat(zs, z')
    plot!(p[3], zs, alpha = 0.2, palette = cgrad(:blues).colors)
end
┌ Info: Saved animation to 
│   fn = /home/ryan/Documents/Career_docs/cooperrc-gh-pages/Julia-learning/tmp.gif
└ @ Plots /home/ryan/.julia/packages/Plots/D9pfj/src/animation.jl:114