New to Julia? Look at the cheatsheet

Veteran looking for challenges? check it out

previous edition of NumQM

python reminder

many ways to loop

for i1 in 1:N1, i2 in 1:N2
	# do something for mat[i1, i2]
end

# col. major, faster
for i2 in 1:N2, i1 in 1:N1
	# do something for mat[i1, i2]
end

sites = Base.product(1:N1, 1:N2)

for site in sites
	i1, i2 = site
	# do something
end

launch Julia from terminal in MacOS

e.g. for Julia-1.9, link it to /usr/local/bin/ by

ln -s /Applications/Julia-1.9.app/Contents/Resources/julia/bin/julia /usr/local/bin/julia

no more side effect

try the following code

xarr = [1, 2, 3]
yarr = [2, 4, 6]

a = xarr
a += yarr

In python, this would have modified xarr. In Julia this is not the case.

matrix side effect

try the following code


Amat = [1 2 3; 4 5 6]
Bmat = Amat
Bmat[:,1] = [7 11]
# what would Bmat be? And more importantly, what happens to Amat?

In Julia (and may be python also?), Amat would be modified also.

Trick to avoid that


Amat = [[1, -1, 0, 0] [1, 1, -2, 0] [1, 1, 1, -3]]

# avoid side effect:
Bmat = I * Amat
# also work
# Bmat = 1 * Amat
# Bmat = copy(Amat)

Bmat[:,1] = [2, 2, 2, 2]

modern programming feature

list comprehension array construction iterator, generator

style guide

input / output files

  • use this to handle numerical data file: delimited files

  • demonstration: (you may want to write your own wrapper)


# basic example
using DelimitedFiles
# to write
f(x, y) = x * y
data = []
for i1 = 1:10
    x = i1
    y = rand()
    push!(data, [x, y, f(x, y)]')  # entry as row
end
data = vcat(data...)  # turn data into a matrix

filename = "result.dat"
open(filename, "w") do f
    Base.write(f, "# some captions \n")
    writedlm(f, data)
end

# to read, comments like #... are ignored
file = filename
data2 = readdlm(file, comments = true)

data2 == data
# to fetch, e.g. xvec
xvec = data[:, 1]

optional arguments in function


# required, optional + sequential; optional keywords (arbitrary order)

function f(x, y = 2, z = 3; a = 4.0, b = 5.0)
    ret = x + y + z^a + b
    println([x, y, z, a, b])
    return ret
end

f(1)

f(1, 10, b = 173, a = 77)

# need to put in y in to specify z 
f(1, 2, 4, b = 173)

use ! to indicate function modifies its input.

function mvup!(xvec, d)
	xvec[d] += 1
    return nothing
end

reading user inputs from terminal


# string input
char_in = readline()

# integer / float input
i_in = parse(Int, readline())
f_in = parse(Float64, readline())

simple terminal menu


import REPL
using REPL.TerminalMenus

todo = ["task 1", "task 2", "task 3", "task 4"]

# form a menu
menu = RadioMenu(todo)

# ask for input from user
choice = request("Choose a task:", menu)

# further manipulation with choice
println("you choose: ", todo[choice])

interpolations

http://juliamath.github.io/Interpolations.jl/latest/convenience-construction/

run script from terminal

julia -e "using Pluto; Pluto.run()"

or

julia -e "using Pkg; Pkg.update()"

PyCall, PyPlot, SymPy packages

check the python3 path

which python3 | pbcopy
ENV["PYTHON"] = "/usr/local/bin/python3"
#then ]
build PyCall; add PyCall

do the same for IJulia, etc.

updating Julia in linux

wget https://julialang-s3.julialang.org/bin/linux/x64/1.9/julia-1.9.3-linux-x86_64.tar.gz
tar zvxf julia-1.9.3-linux-x86_64.tar.gz

then replace the folder and link.

JuliaFormatter

add JuliaFormatter open a julia session

format(".")

to format *.jl in the current directory.

format("./main.jl")

to format a particular file.

profiling

using Profile
using ProfileView
@profile somefunc(a, b)
Profile.print()

@profview somefunc(a, b)
# it will generate a plot
readline()