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.
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.
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
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
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.
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
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.
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.
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
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.
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.
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: