Intro 2: Basic Concepts in Julia

1. The Julia Environment

After launching Julia, execute using IJulia followed by jupyterlab() at the command prompt to launch Jupyter in your default browser. Julia has an extensive set of built-in functions as well as additional packages that consist of functions related to more specialized topics. It can be used in two different ways: as a traditional programming environment and as an interactive calculator. In calculator mode (running Julia either in Jupyter or at the command prompt), the built-in and package functions provide a convenient means of performing one-off calculations and graphical plotting; in programming mode, running Julia in an IDE like Visual Studio Code (VS Code) with Julia extensions, provides a programming environment (editor, debugger, and profiler) that enables the user to write their own functions and scripts.

The OR Software Stack

image.png

Julia scripting language:

2. Creating Arrays

In Julia, integer and real-valued scalars are distinguished, and vectors and matrices are special 1- and 2-dimensional cases of an n-dimensional array:

Scalar variables and arrays can be created as follows (# is used for comments in code, Markdown is used for this comment since it is in a separate non-code cell):

In Julia, the case of a variable matters; e.g., the arrays a and A are different variables. The last expression in a cell is displayed. To suppress the output, end the expression with a semicolon (;):

The macro @show can be used to display an expression. An empty array is considered of type Any:

The following operators and functions can be used to automatically create basic structured arrays:

image.png

Why all the dots?

The rand function generates random numbers between 0 and 1. (Each time it is run, it generates different numbers.)

A random permutation of the integers 1 to n can be generates using the randperm(n) function, which is in the Random package and using Random is used to load it the first time it is called:

List the variables currently in the workspace:

Variables cannot be removed from the worspace and, instead, can be set equal to nothing to that the memory they were using is freed:

3. Selecting Array Elements

In Julia, indices or index arrays inside of square brackets are used to select elements of an array.

1-D array:

2-D arrays

The colon operator : is used to select an entire row or column:

The vector $ \left[ {\begin{array}{c} 1,3 \end{array}} \right] $ is an index array, where each element corresponds to a column index number of the original matrix A. The keyword end can be used to indicate the last row or column:

The selected portion of the one array can be assigned to a new array:

4. Changing an Array

Change elements of array:

Elements of an array can be changed by selecting a portion of an array as the left-hand-side target of an assignment statement:

Assign a value to multiple locations:

Delete selected array elements:

Delete and return last element:

Insert selected array elements:

5. Manipulating Arrays

The following operators and functions can be used to manipulate arrays:

6. Multiplication and Addition

Scalars

A scalar can be added to or multiplied with each element of an array; e.g.,

Multiplication

Arrays are multiplied together in two different ways: matrix multiplication, where the inner dimensions of the arrays must be the same, and element-by-element multiplication, also called broadcasting, indicated by the use of a dot (.) along with the operator, where the arrays must have compatible sizes: two arrays have compatible sizes if each respective dimension either (a) has the same size, or (b) the size of one of the arrays is one, in which case the array with the size-one dimension is automatically duplicated so that it matches the size of the other array, resulting in an output array with a dimension is equal to the non-one size dimension; i.e.,

$ \begin{eqnarray}\textsf{Matrix multiplication:}\;\; {{\bf{A}}_{m \times n}}\;*\;{{\bf{B}}_{n \times p}} &=& {{\bf{C}}_{m \times p}}\\ \textsf{Element-by-element multiplication:}\;\; {{\bf{A}}_{m \times n}}\,.*\;{{\bf{B}}_{m \times n}} &=& {{\bf{C}}_{m \times n}}\\ {{\bf{A}}_{m \times n}}\,.*\;{{\bf{b}}_{1 \times n}} &=& {{\bf{C}}_{m \times n}}\\ {{\bf{a}}_{m \times 1}}\,.*\;{{\bf{B}}_{m \times n}} &=& {{\bf{C}}_{m \times n}}\\ {{\bf{a}}_{m \times 1}}\,.*\;{{\bf{b}}_{1 \times n}} &=& {{\bf{C}}_{m \times n}}\\ {{\bf{a}}_{1 \times n}}\,.*\;{{\bf{b}}_{m \times 1}} &=& {{\bf{C}}_{m \times n}}\end{eqnarray} $

Matrix Multiplication

image.png

Element-by-Element Multiplication

image.png

Addition

Arrays are added together element by element; thus, each array must be the same size or a compatible size; i.e.,

$ \begin{eqnarray}\textsf{Addition:}\;\; {{\bf{A}}_{m \times n}}+{{\bf{B}}_{m \times n}} &=& {{\bf{C}}_{m \times n}}\\ {{\bf{A}}_{m \times n}}+{{\bf{b}}_{1 \times n}} &=& {{\bf{C}}_{m \times n}}\\ {{\bf{a}}_{m \times 1}}+{{\bf{B}}_{m \times n}} &=& {{\bf{C}}_{m \times n}}\\ {{\bf{a}}_{m \times 1}}+{{\bf{b}}_{1 \times n}} &=& {{\bf{C}}_{m \times n}}\\ {{\bf{a}}_{1 \times n}}+{{\bf{b}}_{m \times 1}} &=& {{\bf{C}}_{m \times n}}\end{eqnarray} $

The 3-element array a is by default a 3 × 1 column vector that is not compatible with A:

Convert a to a row vector so that it is compatible with A:

Summation

The elements of a single array can be added together using the sum and cumsum functions. Since only the last expression in a cell is displayed, the macro @show can be used to display an expression that is not the last:

By default, Julia sums all the elements in a matrix. To sum each row or column of a matrix, the dimension must be specified: