Skip to main content

Section 5.1 Introduction to the Julia Language

The Julia Language was developed starting about 2010 as a language for Scientific Computing, a field which basically covers how to solve problems in mathematics and the sciences which often require the use of software to find or approximate the solution.
It is useful to have the background in some computing languageβ€”especially the basics of variables, arrays, loops and branching, however you can solve relatively complex LOPs using Julia. We will cover the basics here, but see the author’s Julia text for a deep dive into Julia.
Julia is a scripting functional language that is often run inside a Jupyter Notebook
 1 
In fact it is the Ju part of Jupyter
and see APPENDIX ??? for details on how to install it.
One of the features of Julia is that of a package manager, which handles installing extra code. Relevant for Linear Optimization Problems, there are packages JuMP and HiGHS that are important, and yes, the capitalization is important.

Subsection 5.1.1 Basics of Julia

The syntax of Julia expressions should be familiar to those who have programmed in python, but not too dissimilar for those with knowledge of C, C++, Javascript, or Java. For example, to declare a variable, enter
x= 3
to assign the value 3 to the variable x.

Note 5.1.1.

It is assumed here that you are using a Jupyter notebook, which has cells in which to enter code. If you enter more than one line of code in a cell, then only the last expression is echoed back to the notebook. That is if you have
x = 3
y = 4
then you will only see 4 echoed back after the cell. If you wish to suppress this output, append a semicolon, ; to the end of the line.

Subsection 5.1.2 For Loops

A for loop is a piece of code that is executed a fixed number of times. The following is an example of just printing out the values of a loop.
Listing 5.1.2.
for i = 1:10
  @show i
end
where the syntax 1:10 means that i takes on the values 1, 2, 3, ..., 10. Julia does have print and println commands, however, the @show macro is nice in that it also prints out the name of variable that you wish view its value.

Note 5.1.3.

Although the code in ListingΒ 5.1.2 has code that is indented, it is not required, like in python. It is typically indented for readability. In fact, for i=1:5 @show i end would produce the same result.
The for loop in ListingΒ 5.1.2 went through every integer between 1 and 10, but if you wish to skip values (whether positive or negative skip), the top line of a for loop can use the syntax start:skip:stop. For example
for i=2:2:14
  @show i
end
will print out the values 2, 4, 6, ..., 14. And
for i=15:-3:0
  @show i
end
will print out the values 15, 12, 9, ..., 0.

Note 5.1.4.

There are commands and functions in Julia with an @-sign in the front. @show is the first one you see, but there are important ones later. These are called macros. A macro does things a bit different behind the scenes and if you become a expert Julia programmer, it is important to know the difference, for purposes in this course, you can use them interchangeably.
One difference that you may see is that a macro is often not written with parentheses after the name (although it can be).

Subsection 5.1.3 Number Types in Julia

There are various number types in Julia including integers, floating points and rationals.
 2 
There are other number types including complex types that won’t be covered here.
Creating numbers with a given type are given with an example below
a = 139
x = 3.14159
p = 2//3
which make an integer, floating point and a rational respectively.
 3 
There are different types of each of these, the main difference being the storage size (in bits) of each number. Nearly all computers these days will default to 64-bit numbers and not knowing differences is not an issue for the material in this text.
Basic operations within a number type often keep the same number type (except for division between integers).
The techniques in this text usually use problems with integer coefficients, however, we will also see that Rational numbers are quite helpful as well. Floating-point numbers, however are generally the backboard of all number systems and solving problems with packages will use this type for all results.
Notice that basic operations between numbers results in a type that is the more general between the numbers. For example,
3 + 4.5
3 + 3//4
3 + 7
results in the floating point number 7.5, the rational 15//4 and the integer 10.

Subsection 5.1.4 Unicode Characters in Julia

One nice feature of Julia is that instead of relying on the standard ASCII characters, that most of the set of Unicode characters can be used. For example, we may want to use Greek letters and we can assign one a value with
\begin{equation*} \alpha = 2 \end{equation*}
and use it in expressions. To enter \(\alpha\text{,}\) inside of a Jupyter notebook, type \alpha then hit the TAB key and you will get a list of characters starting with \alpha. Select the character.
Another option is to use subscripts to make expressions look more like how we express them in writing or typed out. The character \(x_1\) can be created with x\_1 then TAB. This variable is two characters long where the second character is the subscript.
Although there are a lot of options including using emoji for characters, the other relevant character is the \(\geq\) (using \geq) and ≀ (using \leq). Those familiar with LaTeX will notice that the backslash and the shortcut is the same in LaTeX. In fact, many characters from LaTeX will transfer over.

Subsection 5.1.5 Packages in Julia

Julia uses packages/modules for additional functionality. Some modules are built-in to Julia, like LinearAlgebra, which has a lot of additional matrix functions. Other modules need to be downloaded. Further details are shown in SectionΒ A.2.
Let’s say that we want to use the cross function that calculates the cross product between vectors. If we have
x = [1; 2; 3]
y = [-2; 4; 1]
cross(x,y)
then we get the following:
UndefVarError: `cross` not defined in `Main`
  Suggestion: check for spelling errors or missing imports.
  Hint: a global variable of this name also exists in LinearAlgebra.
Looking at the last line, we get the idea that this function is in a package. To use this, we will need to enter using LinearAlgebra
 4 
With Julia 1.12, if a package is not installed, it should autoinstall. Inside of Jupyter notebooks, like in VS Code, the UI is hard to see, so you may either need to wait a while if doing a using statement or install other ways.
. If this is run first, then rerun the above 3 lines, then we get:
3-element Vector{Int64}:
  -10
   -7
    8