Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 40 ยท Collections

Matrices and Linear Algebra

Learn Julia matrices and the LinearAlgebra standard library: matrix literals, A*B, transpose, the backslash solver, determinants and eigenvalues, with NumPy.

Linear algebra is built into Julia's syntax. Matrices have their own literal notation, the multiplication operator * means real matrix multiplication, the apostrophe transposes, and the backslash solves systems of equations. The heavy lifting is done by the LinearAlgebra standard library, which wraps the same optimized LAPACK and BLAS routines that MATLAB, NumPy and R use, so speed is on par with them.

This lesson shows how to write matrices, multiply them, solve Ax = b, and ask for determinants, inverses and eigenvalues. The Julia code is read-only on this site. The runnable examples use NumPy, whose @ operator and numpy.linalg module do the same jobs. Where a number appears in a comment, it is the exact mathematical answer or the value a real run prints, rounded.

Writing matrices

Spaces separate columns and semicolons or new lines separate rows. This matches how you would write a matrix on paper.

JULIA
A = [1 2 3
     4 5 6]            # 2x3 Matrix{Int64}
B = [1 2; 3 4]         # 2x2, the same literal on one line
v = [1, 2, 3]          # a Vector: a single column, but NOT a 1x3 or 3x1 matrix
r = [1 2 3]            # a 1x3 Matrix: a row

size(A)                # (2, 3)
A[2, 1]                # 4
A[:, 2]                # [2, 5], second column
A[1, :]                # [1, 2, 3], first row

A common surprise: [1, 2, 3] (commas) is a vector with one dimension, while [1 2 3] (spaces) is a two-dimensional row matrix. Mixing them up changes how products work. Special matrices come from helpers:

JULIA
using LinearAlgebra

zeros(2, 3)            # 2x3 of zeros
ones(2, 2)             # 2x2 of ones
rand(3, 3)             # 3x3 random values
I(3)                   # 3x3 identity matrix (from LinearAlgebra)
Diagonal([1, 2, 3])    # a diagonal matrix that stores only 3 numbers

Matrix tools in one place

A * B True matrix multiplication.
A' Transpose (conjugate for complex).
A \ b Solve a system of equations.
eigen(A) Eigenvalues and eigenvectors.
1 / 2

Multiplying, transposing and elementwise operations

Plain * follows the textbook rule: the number of columns of the left matrix must equal the number of rows of the right one.

JULIA
A = [1 2; 3 4]
B = [0 1; 1 0]

A * B          # [2 1; 4 3]   (matrix product)
A .* B         # [0 2; 3 0]   (elementwise product, note the dot)
A + B          # [1 3; 4 4]
A .+ 10        # add 10 to every entry (dot needed for scalar + matrix)
A^2            # A * A  =  [7 10; 15 22]
A .^ 2         # square each entry = [1 4; 9 16]

The apostrophe transposes. For real numbers it is a plain transpose, and for complex numbers it also conjugates, which is why it is called the adjoint. It is lazy: A' does not copy any data, it is a wrapper that reads the same memory the other way. Use transpose(A) if you need the non-conjugating version for complex values, and copy(A') or collect(A') if you want a real, independent matrix.

JULIA
M = [1 2 3; 4 5 6]
M'                 # 3x2 matrix [1 4; 2 5; 3 6]
M * M'             # 2x2: [14 32; 32 77]
[1, 2, 3]' * [4, 5, 6]   # 32 (dot product of two vectors)

The last line works because v' of a vector is a row, and a row times a column is a single number. You can write the same thing with dot(v, w) or the symbol โ‹… (type \cdot).

Solving equations with backslash

To solve the system A x = b, write A \ b. This is the way, not inv(A) * b. Backslash uses a factorization chosen for the matrix, such as LU, Cholesky or QR, which is faster and more accurate than forming the inverse.

The system

Text
2x +  y = 3
 x + 3y = 5

has the solution x = 0.8, y = 1.4.

JULIA
A = [2 1
     1 3]
b = [3, 5]

x = A \ b          # approximately [0.8, 1.4]
A * x โ‰ˆ b          # true  (โ‰ˆ is isapprox, type \approx)

Always compare floating-point results with โ‰ˆ (or isapprox) instead of ==, because tiny rounding differences are normal. For overdetermined systems (more equations than unknowns), A \ b returns the least-squares solution, which makes it a one-line linear regression.

Determinant, inverse and eigenvalues

JULIA
using LinearAlgebra

A = [2 1; 1 3]

det(A)          # 5.0 (up to rounding in the last digit)
tr(A)           # 5   (trace: sum of the diagonal)
inv(A)          # [0.6 -0.2; -0.2 0.4]
rank(A)         # 2

F = eigen(A)
F.values        # [1.381966..., 3.618034...]   ((5 - sqrt(5))/2 and (5 + sqrt(5))/2)
F.vectors       # columns are the matching eigenvectors

eigen returns a factorization object with two fields, values and vectors. For a symmetric matrix like this one the eigenvalues come back in increasing order. Each column F.vectors[:, i] satisfies A * v โ‰ˆ F.values[i] * v. Other factorizations follow the same pattern: lu(A), qr(A), cholesky(A) and svd(A). Unlike the functions above, they keep the pieces in a structure you can reuse, which pays off when you solve with many right-hand sides.

Julia also picks fast paths from the matrix type. If you wrap a matrix in Symmetric(A), Diagonal(d) or UpperTriangular(A), then \, inv and eigen use methods designed for that structure. That is multiple dispatch doing practical work.

Using det(A) == 0 to test whether a matrix is singular is unreliable with floating point numbers. Prefer rank(A), cond(A) (the condition number) or just try the solve and look at the error.

Stand-ins you can run

NumPy has the same operations under different spellings. The table below maps them.

Julia NumPy
A * B A @ B
A .* B A * B
A' A.T (or A.conj().T)
A \ b np.linalg.solve(A, b)
det(A), inv(A) np.linalg.det(A), np.linalg.inv(A)
eigen(A) np.linalg.eigh(A) for symmetric, eig in general

The first example mirrors multiplication and solving. NumPy prints values rounded to a number of digits, so the code rounds explicitly to keep the output predictable.

Python Multiply and solve with NumPy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The second example covers the properties of the same symmetric matrix as the Julia listing: determinant, trace, inverse and eigenpairs. It also checks the defining equation of an eigenvector.

Python Determinant, inverse and eigenvalues

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does [1 2 3] create in Julia?

    Spaces separate columns, so [1 2 3] is a one-row, three-column matrix. [1, 2, 3] with commas is a Vector.
  2. 2. What is the best way to solve A x = b in Julia?

    Backslash picks a suitable factorization for A, which is faster and more accurate than computing the inverse. .* multiplies entry by entry.
  3. 3. Which expression multiplies two matrices entry by entry?

    The dot turns the operator into an elementwise one. Without the dot, * is the matrix product.
  4. 4. Why should you compare computed results with โ‰ˆ instead of ==?

    Floating-point arithmetic rounds, so results that are mathematically equal can differ slightly. isapprox allows a small tolerance.

Summary

  • Matrix literals use spaces for columns and semicolons or new lines for rows; commas make a vector.
  • * is matrix multiplication, .* is elementwise, and A' is a lazy adjoint (transpose).
  • A \ b solves linear systems (and least squares) without forming the inverse.
  • using LinearAlgebra provides det, tr, inv, rank, eigen, lu, qr, svd, I and special matrix types.
  • Wrapping a matrix in Symmetric, Diagonal or UpperTriangular lets dispatch choose a faster algorithm.
  • Use โ‰ˆ to compare floating-point results.

Next: apply any function to whole arrays with Broadcasting and the Dot Syntax.