# 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 :
# 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 :
# 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 :
str1 = "Hi" # This won't be printed
str2 = "Hello" # This will be printed

Out:
"Hello"

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

In :
@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 :
"Good morning";


### Data in Julia¶

In :
2 # integer

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

Out:
2.0
In :
1 // 2 # rational numbers!!

Out:
1//2
In :
-394.04 # negative numbers

Out:
-394.04
In :
5.5e9 # scientific notation is fine

Out:
5.5e9
In :
"Hello ENGR 108" # strings (text)

Out:
"Hello ENGR 108"
In :
true # booleans (true or false)

Out:
true

### Variable assignment¶

We use = to assign a value to a variable.

In :
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 :
a = 4
b = 4
c = 3

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

true
false
false


### Basic arithmetic¶

In :
3 + 4

Out:
7
In :
3 - 4

Out:
-1
In :
3 * 4

Out:
12
In :
3 / 4

Out:
0.75
In :
3 ^ 4

Out:
81
In :
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 :
3 == 4

Out:
false
In :
(1 + 1) >= 2

Out:
true

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

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

In :
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 :
1:5  # stored compactly

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

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

Out:
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 :
vector = [3; 4; 5]

Out:
3-element Array{Int64,1}:
3
4
5
In :
vector  # accesses the second element

Out:
4

Warning: vectors are 1-indexed in Julia.

In :
vector # produces an error

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

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

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

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

block_vector = [vector1; vector2]

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

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

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

Out:
7-element Array{Int64,1}:
2
5
7
12
19
31
31
In :
# Useful functions
@show sum(vector)
@show mean(vector)

# Ones vector
@show ones(3)

# Zeros vector
@show zeros(3)

# Unit vectors
e2 = zeros(3)
e2 = 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 :
for i in 1:5  # iterate over a range
println(i)
end  # <--- don't forget the end

1
2
3
4
5

In :
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 :
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 :
# 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