# Introduction to Julia¶

ENGR 108, Stanford University

Julia is a new programming language for scientific computing.

• Julia is fully open source, i.e. free.
• Julia has a convenient syntax for mathematics.
• Julia is super fast.
• Julia was developed mostly by a group at MIT.

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.

In [0]:
# This is a comment
# Use them to write short notes and descriptions of your code


### Importing Libraries¶

Sometimes, you'll need additional functionality that is not in Julia's main workspace. You can import that functionality using the using command.

In [1]:
# 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


### Input and output¶

In a Julia notebook, the last item in a cell is printed.

In [1]:
str1 = "Hi" # This won't be printed
str2 = "Hello" # This will be printed

Out[1]:
"Hello"

To print multiple things, use @show or println().

In [9]:
@show 7
@show 8
println(9)
println(10)

7 = 7
8 = 8
9
10


To suppress the output of the last line in the cell, use a semicolon.

In [14]:
"Good morning";


### Data in Julia¶

In [2]:
2 # integer

Out[2]:
2
In [15]:
2.0 # floating point number (real numbers)

Out[15]:
2.0
In [16]:
1 // 2 # rational numbers!!

Out[16]:
1//2
In [17]:
-394.04 # negative numbers

Out[17]:
-394.04
In [18]:
5.5e9 # scientific notation is fine

Out[18]:
5.5e9
In [19]:
"Hello ENGR 108" # strings (text)

Out[19]:
"Hello ENGR 108"
In [20]:
true # booleans (true or false)

Out[20]:
true

### Variable assignment¶

We use = to assign a value to a variable.

In [21]:
x = 3
name = "lovelace"

# Note the difference between @show and println
@show x
println(name)

x = 3
lovelace


We can test whether one variable equals another variable or value using ==.

Warning: don't confuse = with ==.

In [22]:
a = 4
b = 4
c = 3

println(a == b)
println(a == c)
println(b == 5)

true
false
false


### Basic arithmetic¶

In [23]:
3 + 4

Out[23]:
7
In [24]:
3 - 4

Out[24]:
-1
In [25]:
3 * 4

Out[25]:
12
In [26]:
3 / 4

Out[26]:
0.75
In [27]:
3 ^ 4

Out[27]:
81
In [28]:
println(exp(10)) # natural exponent
println(sin(2*pi))
println(log(1)) # natural log

22026.465794806718
-2.4492935982947064e-16
0.0


### Boolean Operations¶

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 b
In [29]:
3 == 4

Out[29]:
false
In [30]:
(1 + 1) >= 2

Out[30]:
true

### Control flow (if-else statements)¶

Warning: don't forget to end if and if-else statements with an end

In [32]:
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

Less than 20
20


### Ranges¶

We can define a list of discrete values on a range.

In [33]:
1:5  # stored compactly

Out[33]:
1:5
In [34]:
collect(1:10)  # shows what's inside the range

Out[34]:
10-element Array{Int64,1}:
1
2
3
4
5
6
7
8
9
10
In [35]:
collect(1:0.5:4)  # we can (optionally) define the step size

Out[35]:
7-element Array{Float64,1}:
1.0
1.5
2.0
2.5
3.0
3.5
4.0

### Vectors¶

Vectors (a.k.a. lists, arrays) are declared using [] and ; in Julia.

In [37]:
vector = [3; 4; 5]

Out[37]:
3-element Array{Int64,1}:
3
4
5
In [38]:
vector[2]  # accesses the second element

Out[38]:
4

Warning: vectors are 1-indexed in Julia.

In [40]:
vector[0] # produces an error

BoundsError: attempt to access 3-element Array{Int64,1} at index [0]

Stacktrace:
[1] getindex(::Array{Int64,1}, ::Int64) at ./array.jl:809
[2] top-level scope at In[40]:1
In [41]:
vector[2:3] # slicing a vector

Out[41]:
2-element Array{Int64,1}:
4
5
In [42]:
length(vector)

Out[42]:
3
In [45]:
# Stacked vectors
vector1 = [2; 5; 7]
vector2 = [12; 19]

block_vector = [vector1; vector2]

Out[45]:
5-element Array{Int64,1}:
2
5
7
12
19

Question: What happens if I run the cell below twice?

In [47]:
block_vector = [block_vector; 31]  # an easy way to append to a vector

Out[47]:
7-element Array{Int64,1}:
2
5
7
12
19
31
31
In [51]:
# 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;

sum(vector) = 12
mean(vector) = 4.0
ones(3) = [1.0, 1.0, 1.0]
zeros(3) = [0.0, 0.0, 0.0]
e2 = [0.0, 1.0, 0.0]
dot(a, b) = 11
a' * b = 11
a' * b = 11
2a + b = [1, 11, 2]
a .+ 4 = [5, 8, 4]


### Loops¶

Loops let us run code multiple times.

Warning: don't forget to end for and while loops with an end

In [52]:
for i in 1:5  # iterate over a range
println(i)
end  # <--- don't forget the end

1
2
3
4
5

In [53]:
vector = [3; 4; 10; -2; 9]
for entry in vector # we can iterate over a vector just like a range!
@show entry
end

entry = 3
entry = 4
entry = 10
entry = -2
entry = 9

In [54]:
counter = 1
while counter <= length(vector) # while loops
println(vector[counter] + 2)
counter += 1
end

5
6
12
0
11


### Functions¶

Functions let us reuse code.

In [55]:
# 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

11.0