circuits-mat
Safe HaskellNone
LanguageGHC2024

Circuit.Mat.Dense

Description

Value-sized dense matrices backed by Array.

This module provides the canonical dense-matrix carrier for numhask-based computation: Kleene star (reflexive-transitive closure), matrix multiplication, and matrix-vector products. It is intentionally rank-2 and value-sized so it can serve categories whose object constraints live at runtime (e.g. Finite enumeration or plain list channels) as well as typed settings.

Moved from harpie-numhask as part of the matrix-calculus extraction into circuits-mat.

Synopsis

Documentation

newtype Matrix a Source #

Square matrix stored as a rank-2 Array in row-major order.

Constructors

Matrix 

Fields

Instances

Instances details
Eq a => Eq (Matrix a) Source # 
Instance details

Defined in Circuit.Mat.Dense

Methods

(==) :: Matrix a -> Matrix a -> Bool #

(/=) :: Matrix a -> Matrix a -> Bool #

Show a => Show (Matrix a) Source # 
Instance details

Defined in Circuit.Mat.Dense

Methods

showsPrec :: Int -> Matrix a -> ShowS #

show :: Matrix a -> String #

showList :: [Matrix a] -> ShowS #

fromLists :: [[a]] -> Matrix a Source #

Build a matrix from nested rows.

An empty list becomes a 0×0 matrix.

toLists :: Matrix a -> [[a]] Source #

Convert a matrix to nested rows.

matPlus :: Additive a => Matrix a -> Matrix a -> Matrix a Source #

Elementwise addition.

matTimes :: (Additive a, Multiplicative a) => Matrix a -> Matrix a -> Matrix a Source #

Matrix multiplication.

matVec :: (Additive a, Multiplicative a) => Matrix a -> [a] -> [a] Source #

Matrix–vector product.

starMatrix :: StarSemiring a => Matrix a -> Matrix a Source #

Kleene star of a square matrix by the standard state-elimination (Warshall / Floyd-Kleene) algorithm.

For a matrix A, computes A* = I + A + A² + ... as the least fixed point of X ↦ I + A·X. Requires a StarSemiring element type so that star x is available for the pivot updates.

When the element type is a Quantale, this is the join of the geometric series I + A + A² + …; the algorithm uses the StarSemiring fragment (iterative joins) to compute it finitely.

qrM :: (ExpField a, Ord a) => Matrix a -> (Matrix a, Matrix a) Source #

QR decomposition via Householder reflections.

Returns (q, r) with q orthogonal and r upper triangular such that a = q r. The algorithm is the standard column-by-column Householder reduction on value-sized Array matrices.

forwardSubstStream :: (Subtractive a, Multiplicative a) => Array a -> Array a -> Array a Source #

Forward substitution as a stream morphism.

Solves L y = b for unit lower-triangular L by streaming rows of L and components of b, accumulating y one component at a time. Each step is a dot product of the already-computed prefix of y with the active row prefix of L, followed by subtraction from the current b component.

solve2 :: [[Double]] -> [Double] -> [Double] Source #

Solve a 2×2 linear system.

For a system A x = b with A = [[a,b],[c,d]] and b = [r,s], returns x using Cramer's rule. If the determinant is near zero, returns a zero vector as a soft failure.

>>> solve2 [[1.0, 2.0], [3.0, 4.0]] [5.0, 6.0]
[-4.0,4.5]