ENGR 108, Stanford University
Julia is a new programming language for scientific computing.
In this class, we prefer that you use Julia within the Saturn notebook environment in Ed. You can do this using the predefined lessons (upper right corner, third tab from the left) or using custom workspaces (upper right corner, second tab from the left). Choose a Saturn workspace, not a Julia workspace to obtain the notebook environment.
In this notebook, we'll introduce some basic concepts in Julia.
# This is a comment
# Use them to write short notes and descriptions of your code
Sometimes, you'll need additional functionality that is not in Julia's main workspace. You can import that functionality using the using
command.
# VMLS is the textbook's package
# It imports functions and datasets that will be useful during the course
using VMLS
# Some other packages worth knowing how to import are
using LinearAlgebra # additional linear algebra tools
using Statistics # additional statistics tools
# You usually won't have to import these
In a Julia notebook, the last item in a cell is printed.
str1 = "Hi" # This won't be printed
str2 = "Hello" # This will be printed
To print multiple things, use @show
or println()
.
@show 7
@show 8
println(9)
println(10)
To suppress the output of the last line in the cell, use a semicolon.
"Good morning";
2 # integer
2.0 # floating point number (real numbers)
1 // 2 # rational numbers!!
-394.04 # negative numbers
5.5e9 # scientific notation is fine
"Hello ENGR 108" # strings (text)
true # booleans (true or false)
We use =
to assign a value to a variable.
x = 3
name = "lovelace"
# Note the difference between @show and println
@show x
println(name)
We can test whether one variable equals another variable or value using ==
.
Warning: don't confuse =
with ==
.
a = 4
b = 4
c = 3
println(a == b)
println(a == c)
println(b == 5)
3 + 4
3 - 4
3 * 4
3 / 4
3 ^ 4
println(exp(10)) # natural exponent
println(sin(2*pi))
println(log(1)) # natural log
There are many operations which return true or false:
𝚊 == 𝚋
: tests whether a and b are equal𝚊 != 𝚋
: tests whether a and b are NOT equal𝚊 < 𝚋
: tests whether a is (strictly) less than b𝚊 > 𝚋
: tests whether a is (strictly) greater than b𝚊 <= 𝚋
: tests whether a is less than or equal to b𝚊 >= 𝚋
: tests whether a is greater than or equal to b3 == 4
(1 + 1) >= 2
if-else
statements)¶Warning: don't forget to end if
and if-else
statements with an end
value = 9
# Control what happens next w/ if-elseif-else
if value < 20
println("Less than 20")
end
if value < 5
println(10)
elseif value == 5
println(15)
else
println(20)
end # <---- don't forget this
We can define a list of discrete values on a range.
1:5 # stored compactly
collect(1:10) # shows what's inside the range
collect(1:0.5:4) # we can (optionally) define the step size
Vectors (a.k.a. lists, arrays) are declared using []
and ;
in Julia.
vector = [3; 4; 5]
vector[2] # accesses the second element
Warning: vectors are 1-indexed in Julia.
vector[0] # produces an error
vector[2:3] # slicing a vector
length(vector)
# Stacked vectors
vector1 = [2; 5; 7]
vector2 = [12; 19]
block_vector = [vector1; vector2]
Question: What happens if I run the cell below twice?
block_vector = [block_vector; 31] # an easy way to append to a vector
# Useful functions
@show sum(vector)
@show mean(vector)
# Ones vector
@show ones(3)
# Zeros vector
@show zeros(3)
# Unit vectors
e2 = zeros(3)
e2[2] = 1
@show e2
# Inner products
a = [1; 4; 0]
b = [-1; 3; 2]
@show dot(a, b)
@show a' * b
@show a'b
# Vector addition and scalar multiplication
@show 2*a + b
# Adding a scalar to a vector (note the .)
@show a .+ 4;
Loops let us run code multiple times.
Warning: don't forget to end for
and while
loops with an end
for i in 1:5 # iterate over a range
println(i)
end # <--- don't forget the end
vector = [3; 4; 10; -2; 9]
for entry in vector # we can iterate over a vector just like a range!
@show entry
end
counter = 1
while counter <= length(vector) # while loops
println(vector[counter] + 2)
counter += 1
end
Functions let us reuse code.
# The function is `my_inner_product`
# The arguments are `a` and `b`
# The output is whatever follows `return`
function my_inner_product(a, b)
total = 0.0
for i in 1:length(a)
total += a[i] * b[i]
end
return total
end
my_inner_product(a, b)