**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
```

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
```

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]:

To print multiple things, use `@show`

or `println()`

.

In [9]:

```
@show 7
@show 8
println(9)
println(10)
```

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

In [14]:

```
"Good morning";
```

In [2]:

```
2 # integer
```

Out[2]:

In [15]:

```
2.0 # floating point number (real numbers)
```

Out[15]:

In [16]:

```
1 // 2 # rational numbers!!
```

Out[16]:

In [17]:

```
-394.04 # negative numbers
```

Out[17]:

In [18]:

```
5.5e9 # scientific notation is fine
```

Out[18]:

In [19]:

```
"Hello ENGR 108" # strings (text)
```

Out[19]:

In [20]:

```
true # booleans (true or false)
```

Out[20]:

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)
```

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)
```

In [23]:

```
3 + 4
```

Out[23]:

In [24]:

```
3 - 4
```

Out[24]:

In [25]:

```
3 * 4
```

Out[25]:

In [26]:

```
3 / 4
```

Out[26]:

In [27]:

```
3 ^ 4
```

Out[27]:

In [28]:

```
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 b

In [29]:

```
3 == 4
```

Out[29]:

In [30]:

```
(1 + 1) >= 2
```

Out[30]:

`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
```

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

In [33]:

```
1:5 # stored compactly
```

Out[33]:

In [34]:

```
collect(1:10) # shows what's inside the range
```

Out[34]:

In [35]:

```
collect(1:0.5:4) # we can (optionally) define the step size
```

Out[35]:

Vectors (a.k.a. lists, arrays) are declared using `[]`

and `;`

in Julia.

In [37]:

```
vector = [3; 4; 5]
```

Out[37]:

In [38]:

```
vector[2] # accesses the second element
```

Out[38]:

**Warning:** vectors are **1-indexed** in Julia.

In [40]:

```
vector[0] # produces an error
```

In [41]:

```
vector[2:3] # slicing a vector
```

Out[41]:

In [42]:

```
length(vector)
```

Out[42]:

In [45]:

```
# Stacked vectors
vector1 = [2; 5; 7]
vector2 = [12; 19]
block_vector = [vector1; vector2]
```

Out[45]:

**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]:

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;
```

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
```

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
```

In [54]:

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

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
return total
end
my_inner_product(a, b)
```

Out[55]: