Useful packages

Julia provides a large package library. To add a package, we enter the package REPL by pressing ] and install a package by the keyword add.

(@v1.6) pkg> add Plots

Another option is to use the Pkg package to add it directly from the standard REPL.

julia> using Pkg

julia> Pkg.add("Plots")

We return from the package REPL (@v1.6) pkg> to the standard REPL julia> by pressing escape.

Multiple standard packages are shipped together with Julia. These packages do not need to be installed. They include Pkg and all packages introduced on this page. However, we still need to load them to use them.

julia> using Statistics

Statistics

The first package we mention is the Statistics package, which provides statistical analysis functions such as computation of mean, variance, or standard deviation.

julia> using Statistics
julia> x = rand(10);
julia> mean(x)0.5467543574897533
julia> var(x)0.11540275799992229
julia> std(x)0.339709814400353

See the official documentation for more information. More statistics-related functions can be found in the StatsBase package. This package provides functions for computing scalar statistics, high-order moment computation, counting, ranking, covariances, sampling, and empirical density estimation. This course dedicates one lecture to statisctics.

LinearAlgebra

Another package worth mentioning is the LinearAlgebra package, which provides a native implementation of many linear algebra operations. The package provides functions for computing matrix determinant, inversion, norm, eigenvalues, or eigenvectors.

julia> using LinearAlgebra
julia> A = [-4.0 -17.0; 2.0 2.0]2×2 Matrix{Float64}: -4.0 -17.0 2.0 2.0
julia> det(A)26.0
julia> inv(A)2×2 Matrix{Float64}: 0.0769231 0.653846 -0.0769231 -0.153846
julia> norm(A)17.69180601295413
julia> eigvals(A)2-element Vector{ComplexF64}: -1.0 - 5.0im -1.0 + 5.0im
julia> eigvecs(A)2×2 Matrix{ComplexF64}: 0.945905-0.0im 0.945905+0.0im -0.166924+0.278207im -0.166924-0.278207im

The package also provides implementation of multiple matrix types that represent matrices with special symmetries and structures. As examples, we mention Symmetric, Hermitian or Diagonal matrices. These particular matrix types allow for fast computation due to using specialized algorithms. Matrices of these types can be constructed via their constructors.

julia> D = Diagonal([1,2,3])3×3 LinearAlgebra.Diagonal{Int64, Vector{Int64}}:
 1  ⋅  ⋅
 ⋅  2  ⋅
 ⋅  ⋅  3

Another useful function provided by the package is the identity operator I representing the identity matrix. The identity operator I is defined as a constant and is an instance of UniformScaling. The size of this operator is generic and match the other matrix in the binary operations +, -, * and \.

julia> D + I3×3 LinearAlgebra.Diagonal{Int64, Vector{Int64}}:
 2  ⋅  ⋅
 ⋅  3  ⋅
 ⋅  ⋅  4
julia> D - I3×3 LinearAlgebra.Diagonal{Int64, Vector{Int64}}: 0 ⋅ ⋅ ⋅ 1 ⋅ ⋅ ⋅ 2

Note that for D+I and D-I, the matrix D must be square.

Random

The last package that we will describe in more detail is the Random package. This package provides advanced functionality for generating random numbers in Julia. The package allows setting the seed for the random generator using the seed! function. The seed! function is used to create a reproducible code that contains randomly generated values.

julia> using Random
julia> using Random: seed!
julia> seed!(1234);
julia> rand(2)2-element Vector{Float64}: 0.32597672886359486 0.5490511363155669
julia> seed!(1234);
julia> rand(2)2-element Vector{Float64}: 0.32597672886359486 0.5490511363155669

The randperm function constructs a random permutation of a given length.

julia> randperm(4)4-element Vector{Int64}:
 3
 2
 4
 1

The shuffle function returns a randomly permuted copy of a given array.

julia> v = [1,2,3,4]4-element Vector{Int64}:
 1
 2
 3
 4
julia> shuffle(v)4-element Vector{Int64}: 2 1 3 4
Other useful standard packages:

There are other useful standard packages in Julia, but there is not enough space to present them all.

  • Test provides simple unit testing functionality. Unit testing is a process to determine if your code is correct by checking that the results are what you expect. It helps to ensure the code works after changes. Unit tests can also be used during the development phase to specify the expected behaviour when implemented. We will provide more details later.
  • SparseArrays provides special types to store and work with sparse arrays.
  • Distributed includes support for distributed computing.

The section Standard Library in the official documentation provides more information.