Edit or run this notebook

Introduction to Julia for CATAM

29.2 μs

About this manual

This manual is intended as an introduction to the programming language Julia for use for CATAM. Some of the advantages of Julia over other programming languages can be found in the section Learning Julia. The headline advantage that it has over the likes of perennial favourites Matlab and Python is speed, which can be critical in some projects, and remains handy for all others. This is rare among languages with the intuitive and wide-ranging syntax that Julia provides.

This manual is perfectly viewable in web-page form, and you will be able to copy code from the manual into Julia to test/edit it. No code which is used in creating the outputs seen in this document is hidden.

However, the interactivity built into the Pluto notebook in which this is written is lost. To enable this:

  • Download the notebook as a .jl file by clicking Edit or run this notebook in the top right, and clicking the download button

  • Download and install Julia as detailed in the section Installing Julia (note that no editor, e.g. VSCode, is needed)

  • Download the Pluto package as detailed in the section Pluto notebooks

  • Open the notebook which you have downloaded in Pluto

A more detailed guide to downloading Julia and Pluto, with images, is here

491 μs

Installing Julia

To use Julia, two elements are required: Julia itself, and an editor to allow you to write and run your programs.

Julia is a standalone program downloaded from https://julialang.org/downloads/

  • For most Windows users, the version to download will be the 64-bit (installer). Once downloaded, run the installer, making sure to check the Add Julia to PATH box (which will allow the editor to find it)

  • For Mac users there is no such paralysis of choice, the single Mac download link is the one you want

  • For Linux users, you know what you're doing anyway

Additional platform specific information can be found at https://julialang.org/downloads/platform/

An editor is technically not essential, although it makes programming immeasuarably faster and easier. This manual will use the editor VSCode, which is developed by Microsoft but available on Mac and Linux as well as Windows. This is downloadable from https://code.visualstudio.com/Download

The third and final step is to open VSCode and install the Julia extension which allows it to recognise and run Julia code. This is found in the Extensions tab (accessible via View > Extensions, or Ctrl + Shift + X, or as the fifth symbol on the left sidebar). Search for Julia, and click Install. Once the extension has installed, restart VSCode, and it will be ready to use. Documentation for the extension is found at https://www.julia-vscode.org/docs/dev/, which provides (at the time of writing) limited information on using the extension.

Many alternatives to VSCode exist, as detailed in the Editors and IDEs section of https://julialang.org/. If you already have an editor of choice, it is likely that a Julia syntax-highlighting or even REPL extension exists for it.

5.2 ms

Using Julia with VSCode

VSCode gives two ways to interact with Julia: the REPL and the editor

The Julia REPL

The read-eval-print-loop (REPL) is the command line interface of Julia, akin to the MATLAB Command Window or the Python Shell. It allows single lines of code to be written and evaluated, and is also the place where any programs you write will run. It can be opened by the key sequence Alt + J followed by Alt + O, or by opening VSCode's command palette via the View menu or with Ctrl + Shift + P, and searching for the command Julia: Start REPL. It is identical to the command line which appears when running Julia as an application, so there is no need to interact with Julia outside of VSCode.

To test it, try copying the code in the boxes below, and see if the outputs match:

25.4 μs
300 ns
b
2
400 ns
5
200 ns

Note that without the semicolon, an output is displayed, which may or may not be desirable. Multiple lines can be written at once by seperation by semicolons, e.g.:

8.5 μs
14
1.1 μs

One particularly useful feature of the REPL is help mode. By typing ? into the REPL. the prompt changes from julia> to help?>, after which typing the name of any variable or function tells you about it. Help mode can be exited by Backspace on an empty line, or Ctrl + C at any time.

The editor

The editor pane is the large central pane, which will allow the writing of programs in the form of scripts. To create a new file, select File and choose New File from the menu, or use Ctrl + N. Then, you need to tell VSCode that you will be writing Julia, which can be done through the Select a language prompt, or by saving the file with the file type .jl.

VSCode will prompt you to open a folder, which you will want to do. This not only allows for saving of your files to a chosen location, but also will determine the place from which any file paths that you use (such as in the section Customising outputs), allowing your program to work independent of its file location.

Once a script has been written and saved, it can be run through the REPL by clicking the triangle in the top right and selecting Julia: Execute File in REPL. This does not have a keyboard shortcut automatically, but this (and any other keyboard shortcuts) can be changed through the command palette (Ctrl + Shift + P) by searching for the relevant command and clicking the cog that appears when you mouse over it.

42.2 μs

Learning Julia and its advantages

This manual doesn't focus on teaching Julia, purely because there are already plenty of freely available resources online to get you started

However, there are some particular features worth highlighting:

49.5 μs

Unicode support

Julia is unusual in supporting the use of characters usual Latin alphabet, Arabic numerals, and common English punctuation. For example:

  • Instead of pi and exp(1), we can use π and

  • Where Greek letters are used by mathematical convention, they can be used in programs, such as ρ for density, or λ for the parameter of a Poisson distribution, improving comprehension

  • Some symbols have syntactic meaning, such as using instead of <=, or instead of the keyword in

  • Non-Latin writing systems are supported, allowing variable/function names as well as text within the program to be written in your (human) language of choice

  • Emoji can be used similarly. Unfortunately, the author of this manual is too boring to appreciate this. 😒 = true

These can be copied and pasted into your code, or directly typed if you have the capabilities, but the easiest way tends to be to use LATEX-like shortcuts with tab-completion, such as \alpha<tab> for α or \leq<tab> for . The help mode of the REPL allows pasting of characters to see the relevant shortcuts available for typing them.

33.7 μs

Mathematical notation uncommon in other languages

The usual syntax for defining functions is similar to many other programming languages

12.7 μs
f (generic function with 1 method)
46.8 μs
5.0
300 ns

However, an alternative syntax is particularly useful for readability of short functions, by writing them in the compact form:

12.6 μs
g (generic function with 1 method)
52.0 μs
5.0
200 ns

This is convenient notation since it mirrors the mathematical equivalent. The expression after the equals must be a single line, but can also be extended to longer expressions by using a begin-end block, which allows multiple lines to be treated together as a single line:

17.7 μs
h (generic function with 1 method)
98.9 μs
2
400 ns
Loading...
ii
Loading...