Kulraj Suri

Student / Mathematician / Developer

Julia Basics

1. What is Julia?

  • Developed by MIT
  • Fully open source
  • Convenient syntax for building math constructs like vectors, matrices, etc
  • Very fast

2. Getting Started

1. Run Julia in the cloud:

2. Run Julia locally (recommended):

  • Faster and more flexible
  • Requires some experience with command line

Install Julia:

Open the Julia application:

  • You will be presented with a Julia command line

image53

Create a Julia .jl file:

  • Create and save a .jl file in a text editor

Open a Julia .jl file:

  • cd to the directory where your .jl file is located – remember to use “ \\ ”
  • Use “include” to run the .jl file

image14

3. Basics

Types

  • Integers: Int64, e.g. -45
  • Real numbers: Float64, e.g. 1.256
  • Booleans: Bool, true or false
  • Strings: ASCIIString, e.g. “Hello World”
  • Arrays: Array(Int64, 3) = 3 element array with Int64 values, e.g. [45, 3453, -23]

+, -, *, / Operators

image75

Exponential’s

image01

Boolean Expressions

  • Evaluate to true or false
  • Use the operators: == , != , < , > , <= , >=

image40

Random Numbers

  • Generate a random number between 0 and 1

image74

4. Variables

  • Assignments use the  = operator
  • E.g. value = 5.0
  • E.g. name = “Bob”

image11

5. Control Flow (if statements, for loops)

If Else Statements

image48

  • We see the result when we run this file in Julia:

image82

For Loops

image36

  • We see the result when we run this file in Julia:

image56

While Loops

image21

  • We see the result when we run this file in Julia:

image78

6. Functions

image64

image17

7. Arrays and Matrices

Arrays

  • Arrays can be initialised directly:

image30

  • Or initialised empty:

image65

  • Arrays like this can be initialised with a type:

image16

  • Ranges can be used to create arrays:

image79

  • Arrays can also be generated from comprehensions:

image51

  • We can access elements of the array:

image03

  • Arrays can be any type, so arrays of arrays can be created:

image08

  • We can push to arrays like so:

image49

Matrices

  • Matrices in Julia are represented by 2D arrays
  • To create a 2×3 matrix:

image32

  • Semicolons delimit rows; spaces delimit entries in a row
  • size(A) returns the size of A as a pair (n rows x m columns):

image13

  • Row vectors are 1xn matrices:

image46

Indexing and Slicing

  • Aij is found with A[ i, j ]:

image06

  • Ranges can also be used:

image39

  • : selects all elements along that dimension
  • A[ : , 3 ] selects the third column:

image63

  • A[ 2, : ] selects the second row:

image71

  • A[ : ] returns the columns of A stacked as a vector:

image00

Common Matrices

  • zeros( n, m ):

image55

  • ones( n, m ):

image27

  • eye(n):

image28

  • diag(x) where x is a vector:

image12

  • Random nxm matrix:

image47

Transpose and Matrix Addition

  • A transposed is written “ A’ “:

image05

  • Adding and subtraction of matrices, e.g.

image38

is written:

image42

Matrix Scalar Operations

  • All matrix scalar operations ( +, -, * ) apply element wise
  • For e.g. matrix-scalar addition:

image80

Is equivalent to:

image83

  • Scalar multiplication:

image67

Is equivalent to:

image09

Matrix Vector Multiplication

  • The * operator is used for matrix vector multiplication
  • For e.g.

image77

Is written:

image54

Matrix Multiplication

  • The * operator is used for matrix multiplication

image15Is written:

image34

8. Packages

  • List all available packages:

image02

  • Install one package (e.g. Calculus) and all it’s dependencies:

image57

  • To list all installed packages:

image18

  • To update all packages to their newest version:

image25

  • To use a package:

image04

  • This will import all functions of that package into the current namespace, so that it is possible to call:

image59

  • … without specifying the package it is included in
  • You can also specify which package the function is called from:

image41

  • … using ‘import’ is especially useful if there are conflicts in function/type-names between packages
  • For example, the plotting packages ‘Winston’ and ‘Gadfly’ both use the ‘plot’ function
  • This can be prevented by using ‘import’, as follows:

image24

DROP A COMMENT

Your email address will not be published. Required fields are marked *