R6 — Matrices

Goal: build matrices, do algebra with them, pull a decomposition apart, and read the display when it gets wide.

A matrix is rows of rows

> A := [[1, 2], [3, 4]]
A := [[1.0, 2.0], [3.0, 4.0]]
> B := [[0, 1], [1, 0]]
B := [[0.0, 1.0], [1.0, 0.0]]

run this session

Why the double brackets?

Because the outer pair is the matrix and each inner pair is a row. One pair alone is not a matrix at all:

> [1, 2]
[1.0, 2.0]

run this session

It is the same [[...]] you met in R5’s solveSystem, and it is what makes a vector need no separate type:

> [[1, 2, 3]]
[[1.0, 2.0, 3.0]]
> [[1], [2], [3]]
[[1.0], [2.0], [3.0]]

run this session

A row vector is a one-row matrix; a column vector is a one-column matrix. There is no third thing to learn, and every matrix operation applies to both.

Arithmetic uses the ordinary operators

> A + B
[[1.0, 3.0], [4.0, 4.0]]
> A * B
[[2.0, 1.0], [4.0, 3.0]]
> 2 * A
[[2.0, 4.0], [6.0, 8.0]]

run this session

A * B is the matrix product, so order matters — worth seeing once rather than taking on trust:

> B * A
[[3.0, 4.0], [1.0, 2.0]]

run this session

Determinant, inverse, transpose

> det(A)
-2.0
> inv(A)
[[-2.0, 1.0], [1.5, -0.5]]
> transpose(A)
[[1.0, 3.0], [2.0, 4.0]]

run this session

Division by a matrix is multiplication by its inverse, so 1/A means what you would hope:

> 1/A
[[-2.0, 1.0], [1.5, -0.5]]

run this session

A singular matrix has no inverse, and you already know what that answer looks like:

> inv([[1, 2], [2, 4]])
inv([[1.0, 2.0], [2.0, 4.0]])

run this session

Unchanged — declined, exactly as in R4. That matrix’s rows are proportional, so there is no inverse to give, and nothing was invented.

Reading a wide result

One line per row is easier once a matrix is more than a line wide:

> pretty on
pretty = on
> inv(A)
[[-2.0,  1.0]
 [ 1.5, -0.5]]
> pretty off
pretty = off

run this session

pretty is a display setting like precision, saved with your session (R9).

Decompositions

Each returns a row of matrices, which is where R3’s naming and this lesson’s literal meet:

> eigen(A)
[[5.37228, -0.37228]]

run this session

> L, U, P := lu(A)
L := [[1.0, 0.0], [0.33333, 1.0]]
U := [[3.0, 4.0], [0.0, 0.66667]]
P := [[0.0, 1.0], [1.0, 0.0]]
> L
[[1.0, 0.0], [0.33333, 1.0]]
> U
[[3.0, 4.0], [0.0, 0.66667]]

run this session

Tuple assignment is what makes a multi-part result usable — one name per part, and the count must match exactly. qr(A) and eig(A) have the same shape; eig gives eigenvectors beside eigenvalues.

Eigenvalues may be complex, and those come back as complex numbers rather than being dropped:

> eigen([[0, -1], [1, 0]])
[[i, -i]]

run this session

That is a rotation by a quarter turn, which has no real eigenvalue at all.

Symbolic entries

Nothing above required numbers:

> det([[a, b], [c, d]])
((a * d) + (-1.0 * (b * c)))

run this session

Try it yourself

A has trace 5 and determinant −2, so the Cayley–Hamilton theorem says A² − 5A − 2I should be the zero matrix. Check it.

Answers ```text > A^2 - 5*A - 2*eye(2) [[0.0, 0.0], [0.0, 0.0]] ``` *[run this session](https://lycogrypho.github.io/Leonardo/app/#s=precision%205%0Acolors%20dark%0Apretty%20off%0Alatex%20off%0Anames%20off%0Alogic%20minmax%0Alogic%20symmetric%20off%0Aexact%20precision%2030%0Aexact%20off%0AA%20%3A%3D%20%5B%5B1.0%2C%202.0%5D%2C%20%5B3.0%2C%204.0%5D%5D%0AB%20%3A%3D%20%5B%5B0.0%2C%201.0%5D%2C%20%5B1.0%2C%200.0%5D%5D%0AL%20%3A%3D%20%5B%5B1.0%2C%200.0%5D%2C%20%5B0.3333333333333333%2C%201.0%5D%5D%0AP%20%3A%3D%20%5B%5B0.0%2C%201.0%5D%2C%20%5B1.0%2C%200.0%5D%5D%0AU%20%3A%3D%20%5B%5B3.0%2C%204.0%5D%2C%20%5B0.0%2C%200.6666666666666667%5D%5D)* `eye(n)` is the identity matrix and `zeros(r, c)` its counterpart — both constructors rather than literals you have to type out.

What you learned

  • [[...], [...]] is a matrix: outer brackets the matrix, inner brackets each row. A vector is just a one-row or one-column one.
  • +, *, det, inv, transpose work as written, 1/A is the inverse, and order matters in a product.
  • A singular or non-conforming operation declines rather than erroring — R4’s convention again.
  • pretty on stacks rows; tuple assignment unpacks lu/qr/eig; complex eigenvalues survive.

Previous: R5 — Equations · Next: R7 — Drawing and sampling · All lessons


This site uses Just the Docs, a documentation theme for Jekyll.