hex

11.5. Elementary operations🔗

The elementary operations work over any ring. Each row operation has a column mirror — Hex.Matrix.rowSwap/Hex.Matrix.colSwap, Hex.Matrix.rowScale/Hex.Matrix.colScale, Hex.Matrix.rowAdd/Hex.Matrix.colAdd — and each has a determinant law proved in HexDeterminant. HexRowReduce uses the row operations for Gauss-Jordan reduction over a field.

🔗def
Hex.Matrix.rowSwap.{u_1} {R : Type u_1} {n m : } (M : Hex.Matrix R n m) (i j : Fin n) : Hex.Matrix R n m
Hex.Matrix.rowSwap.{u_1} {R : Type u_1} {n m : } (M : Hex.Matrix R n m) (i j : Fin n) : Hex.Matrix R n m

Swap rows i and j in a dense matrix.

Implemented with Vector.swap, which updates the dense backing store in place when M is uniquely referenced, rather than reading both rows and writing them back through two sets (which forces a copy of the outer vector).

🔗def
Hex.Matrix.rowScale.{u_1} {R : Type u_1} {n m : } [Mul R] (M : Hex.Matrix R n m) (i : Fin n) (c : R) : Hex.Matrix R n m
Hex.Matrix.rowScale.{u_1} {R : Type u_1} {n m : } [Mul R] (M : Hex.Matrix R n m) (i : Fin n) (c : R) : Hex.Matrix R n m

Scale row i by c.

Per-entry in place via Hex.Matrix.modifyEntries: each of the row's m entries is a single Vector.modify of the flat backing buffer, with no row materialization, when M is uniquely referenced.

🔗def
Hex.Matrix.rowAdd.{u_1} {R : Type u_1} {n m : } [Mul R] [Add R] (M : Hex.Matrix R n m) (src dst : Fin n) (c : R) : Hex.Matrix R n m
Hex.Matrix.rowAdd.{u_1} {R : Type u_1} {n m : } [Mul R] [Add R] (M : Hex.Matrix R n m) (src dst : Fin n) (c : R) : Hex.Matrix R n m

Replace row dst by row dst + c * row src.

The source row is read once into rsrc (one contiguous copy, a borrowed read taken before the write); the subsequent Hex.Matrix.modifyEntries then holds the only live reference to the buffer and updates the dst row's entries in place when the runtime sees it uniquely referenced, with no destination-row materialization.

🔗def
Hex.Matrix.colSwap.{u_1} {R : Type u_1} {n m : } (M : Hex.Matrix R n m) (i j : Fin m) : Hex.Matrix R n m
Hex.Matrix.colSwap.{u_1} {R : Type u_1} {n m : } (M : Hex.Matrix R n m) (i j : Fin m) : Hex.Matrix R n m

Swap columns i and j in a dense matrix.

Both columns are read once (two borrowed O(n) reads taken before the writes), then written back with two Hex.Matrix.setCol passes that update one flat-buffer entry per row in place, reusing the backing store when M is uniquely referenced. This replaces the former Hex.Matrix.mapRows pass, which materialized and reflattened every row. The column mirror of Hex.Matrix.rowSwap.

🔗def
Hex.Matrix.colScale.{u_1} {R : Type u_1} {n m : } [Mul R] (M : Hex.Matrix R n m) (j : Fin m) (c : R) : Hex.Matrix R n m
Hex.Matrix.colScale.{u_1} {R : Type u_1} {n m : } [Mul R] (M : Hex.Matrix R n m) (j : Fin m) (c : R) : Hex.Matrix R n m

Scale column j by c.

In-place per-entry column update via Hex.Matrix.modifyCol: each row's single j entry is multiplied by c, reusing the freed row slot when M is uniquely referenced. The column mirror of Hex.Matrix.rowScale.