circuits-mat
Safe HaskellNone
LanguageGHC2024

Circuit.Mat.Field

Description

Matrix field calculus for dense, statically-shaped matrices.

This module provides Cholesky decomposition, triangular-matrix inversion, and square-matrix inversion for Array matrices. All operations are expressed in NumHask algebraic classes rather than GHC's Num/Floating hierarchy, so they work for any scalar that satisfies the required field structure.

The matrix-level Multiplicative and Divisive instances are also provided here, in circuits-mat, rather than in harpie: harpie stays a generic array library with relaxed element-wise constraints, while matrix calculus lives in the categorical/linear-algebra layer.

Synopsis

Square matrix alias

type MatrixM (n :: Nat) a = Matrix n n a Source #

A square matrix of statically-known size.

Field calculus

cholM :: forall (n :: Nat) a. (KnownNat n, KnownNats '[n, n], ExpField a) => MatrixM n a -> MatrixM n a Source #

Cholesky decomposition using the Cholesky-Crout algorithm.

>>> e = F.array @[3,3] @Double [4,12,-16,12,37,-43,-16,-43,98]
>>> pretty (cholM e)
[[2.0,0.0,0.0],
 [6.0,1.0,0.0],
 [-8.0,5.0,3.0]]
>>> F.mult (cholM e) (F.transpose (cholM e)) == e
True

invtriM :: forall (n :: Nat) a. (KnownNat n, KnownNats '[n, n], Subtractive a, Divisive a) => MatrixM n a -> MatrixM n a Source #

Inversion of a triangular matrix.

>>> t = F.array @[3,3] @Double [1,0,1,0,1,2,0,0,1]
>>> pretty (invtriM t)
[[1.0,0.0,-1.0],
 [0.0,1.0,-2.0],
 [0.0,0.0,1.0]]
>>> F.ident @[3,3] == F.mult t (invtriM t)
True

inverseM :: forall (n :: Nat) a. (KnownNat n, KnownNats '[n, n], ExpField a) => MatrixM n a -> MatrixM n a Source #

Inverse of a square matrix via Cholesky decomposition.

>>> e = F.array @[3,3] @Double [4,12,-16,12,37,-43,-16,-43,98]
>>> pretty (inverseM e)
[[49.36111111111111,-13.555555555555554,2.1111111111111107],
 [-13.555555555555554,3.7777777777777772,-0.5555555555555555],
 [2.1111111111111107,-0.5555555555555555,0.1111111111111111]]

luM :: forall (n :: Nat) a. (KnownNat n, KnownNats '[n, n], Subtractive a, Divisive a, Absolute a, Ord a) => MatrixM n a -> (MatrixM n a, MatrixM n a, MatrixM n a) Source #

LU decomposition with partial pivoting.

Returns (p, l, u) such that a = p^T . l . u, where p is the accumulated row-permutation matrix, l is unit lower triangular, and u is upper triangular. The algorithm is the standard iterative Schur-complement update: at each pivot k, swap the pivot row into place, then eliminate the rows below by a rank-1 update.

The Schur step a' = a - c . r is an outer product followed by subtraction, which is the matrix-level reading of the trace / feedback pattern used in circuit constructions.

luRank1M :: forall (n :: Nat) a. (KnownNat n, KnownNats '[n, n], Subtractive a, Divisive a, Absolute a, Ord a) => MatrixM n a -> (MatrixM n a, MatrixM n a, MatrixM n a) Source #

LU decomposition via iterated rank-1 updates.

This is the same factorisation as luM, but the Schur-complement step is written explicitly as an outer-product contraction:

A' = A - c ⊗ r

where c is the column of multipliers below the pivot and r is the pivot row. The outer product uses expand and the subtraction is elementwise; together they are the matrix-level reading of the trace / feedback pattern.

The result agrees with luM on any invertible square matrix.

householderStep :: forall (n :: Nat) a. (KnownNat n, KnownNats '[n, n], ExpField a, Ord a) => Int -> MatrixM n a -> MatrixM n a Source #

Apply a Householder reflection to zero the subdiagonal of column k.

For column x = a[:,k], compute a reflector H = I - 2 v v^T / (v^T v) such that H x = α e_k with α = -sign(x[k]) · ||x||. The action on the whole matrix is the rank-1 update

a' = a - (2 / (v^T v)) · v ⊗ (v^T a)

where v^T a is a contraction over the row axis and v ⊗ (...) is an outer product. This is the circuit-native QR step: a scheduled reflection implemented as expand contract subtract.

Matrix-as-ring wrapper

newtype MatField (n :: Nat) a Source #

Newtype wrapper that gives square matrices a multiplicative (and, over a field, divisible) structure.

The unit is the identity matrix; multiplication is the usual matrix product. Division uses Cholesky-based inversion.

Constructors

MatField 

Fields

Instances

Instances details
Eq a => Eq (MatField n a) Source # 
Instance details

Defined in Circuit.Mat.Field

Methods

(==) :: MatField n a -> MatField n a -> Bool #

(/=) :: MatField n a -> MatField n a -> Bool #

Show a => Show (MatField n a) Source # 
Instance details

Defined in Circuit.Mat.Field

Methods

showsPrec :: Int -> MatField n a -> ShowS #

show :: MatField n a -> String #

showList :: [MatField n a] -> ShowS #

(KnownNat n, KnownNats '[n, n], Additive a, Multiplicative a, ExpField a) => Divisive (MatField n a) Source # 
Instance details

Defined in Circuit.Mat.Field

Methods

recip :: MatField n a -> MatField n a #

(/) :: MatField n a -> MatField n a -> MatField n a #

(KnownNat n, KnownNats '[n, n], Additive a, Multiplicative a) => Multiplicative (MatField n a) Source # 
Instance details

Defined in Circuit.Mat.Field

Methods

(*) :: MatField n a -> MatField n a -> MatField n a #

one :: MatField n a #