Me: Josh Bevan - jbevan@bu.edu
Helper: Katia Bulekova - ktrn@bu.edu
Get Help from RCS: help@scc.bu.edu

Our website: rcs.bu.edu
Tutorial eval: rcs.bu.edu/eval
This notebook: http://scv.bu.edu/examples/julia/Intro/

Julia seeks to solve the "two-language" problem. Fast: C, Fortran, etc. / Productive: Python, MATLAB, etc. You can get around this for specific tasks by using a domain specific language, or specialized libraries, but this is not generalizable to a general-purpose approach.

Not convinced? Here are a couple samples showing what I mean:

Julia is a high-level, high-performance, dynamic programming language. With a large and active community and several well-supported IDEs (e.g. Juno, VSCode, etc.)

A good way to introduce yourself to a new language is by trying to solve a "non-trivial" problem; learning the tools and syntax necessary to solve the problem along the way. This motivates the syntax/tools in a "why" versus "what" way!

Setup and Use

We'll work in a Jupyter notebook environment for this tutorial. You can also work in the REPL or an IDE (or text editor of your choice).

There are several ways to work in the REPL, let's look at them...

You may need to setup an IJulia kernel to start a notebook in an existing Jupyter installation: for more info see here.

You will eventually want to run scripts developed in notebooks or at the REPL in a batch mode. You can easily do this by simply calling Julia with the script:

[...]$ julia my_script.jl

If you are working from an existing notebook, you can convert it to a script with:

jupyter nbconvert --to script my_notebook.ipynb

Let's solve an example problem:

ProjectEuler is a good source of "starter" problems.

Problem 1:

"If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000."

Pseudo-code:

1 "total" starts at 0
2 Run over all the numbers 1 to 999
3     Is the number a multiple of 3 or 5?
4         If yes, add to our cumulative sum "total"
5 Try the next number

Line 0: How do we do some basic stuff?

This one does though $\downarrow$

You can also use shell commands in the same way:

1 "total" starts at 0
2 Run over all the numbers 1 to 999
3     Is the number a multiple of 3 or 5?
4         If yes, add to our cumulative sum "total"
5 Try the next number

Line 1: We need to be able to store "variables". It can also be useful to display variables/info:

Fun sidebar: You can do some neat or unexpected things with strings in Julia

1 "total" starts at 0
2 Run over all the numbers 1 to 999
3     Is the number a multiple of 3 or 5?
4         If yes, add to our cumulative sum "total"
5 Try the next number

Line 2: We'd like to do the same action over and over, but on varying inputs. This is called a loop; there are several kinds including a "for" loop. We do the actions inside the loop "for" each value that the loop variable takes on

We can see how we'd use this to solve the problem: we'll loop over all the numbers 1 to 999. Within the loop we'll test to see if they are a multiple of 3 or 5 and if so add it to our "total" variable.

1 "total" starts at 0
2 Run over all the numbers 1 to 999
3     Is the number a multiple of 3 or 5?
4         If yes, add to our cumulative sum "total"
5 Try the next number

Line 3: How do we test if a number is a multiple? How do we take an action depending on some question (in this case if it is a multiple). Let's answer the second question first:

Floating point numbers have limited precision: https://en.wikipedia.org/wiki/Floating-point_arithmetic#Internal_representation

There are many pitfalls to working with floating point numbers: https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

So that answers the second question, but how do we test if something is a multiple? This is more a logic/math question than exclusively programming, but any non-trivial program will need you to figure things like this out. A number is a multiple of 3 if it is evenly divided by 3:

So our problem is now to test for divisibility. Let's cook up a couple ways:

What other ways might we do this? Can we think of other functions to use?

Putting it all together to solve Problem 1

1 "total" starts at 0
2 Loop over all the numbers 1 to 999
3     Is the number a multiple of 3 or 5?
4         If yes, add to our cumulative sum "total"
5 Try the next number

This space left intentionally blank














Vectors, Matrices, and Vectorization

Functions