Skip to content

HMath.Matrix

Module index · Numerical conventions

Description

Dense matrices with finite, row-major storage and zero-based indices.

The constructor is hidden so the stored element count always agrees with the shape. Zero rows and zero columns are valid and retain their shape. Checked operations describe invalid inputs with MathError; the corresponding Maybe operations retain the original failure convention.

On this page

API Purpose
Matrix An abstract dense matrix with explicit dimensions and flat row-major storage.
fromList Construct a matrix from a finite list in row-major order.
fromListChecked Checked fromList.
toList Return all elements in row-major order.
shape Return (rows, columns), including either dimension of an empty matrix.
zeroMatrix Construct a matrix filled with zeros, subject to the same shape checks as fromList.
zeroMatrixChecked Checked zeroMatrix.
diagonal Construct a square matrix with the finite input list on its diagonal and zeros elsewhere.
diagonalChecked Checked diagonal.
at Look up an element using zero-based row and column indices.
atChecked Checked at.
sliceMatrix Extract a slice given its (startRow, startColumn) and (rowCount, columnCount).
sliceMatrixChecked Checked sliceMatrix.
reshape Change the shape while preserving row-major element order.
reshapeChecked Checked reshape.
addMatrix Add matrices with equal shapes.
addMatrixChecked Checked addMatrix.
subtractMatrix Subtract matrices with equal shapes.
subtractMatrixChecked Checked subtractMatrix.
hadamard Multiply corresponding elements of matrices with equal shapes.
hadamardChecked Checked hadamard.
scaleMatrix Multiply every element by the scalar, preserving the shape.
multiply Matrix product when the left column count equals the right row count and the output element count fits in Int.
multiplyChecked Checked multiply.
transposeMatrix Swap rows and columns.
identity Construct a square identity matrix of nonnegative order.
identityChecked Checked identity.

Documentation

Matrix

data Matrix a Source #

An abstract dense matrix with explicit dimensions and flat row-major storage. Empty shapes require no list of empty rows. Show displays the internal representation for inspection; it is not a serialization format.

Instances

Instances details
MatrixLike Matrix Source # 
Instance details

Defined in HMath.API.Algebra

VectorSpace Matrix Source # 
Instance details

Defined in HMath.API.Algebra

Methods

vectorPlus :: Num a => Matrix a -> Matrix a -> Either MathError (Matrix a) Source #

vectorScale :: Num a => a -> Matrix a -> Matrix a Source #

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

Defined in HMath.Matrix

Methods

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

show :: Matrix a -> String #

showList :: [Matrix a] -> ShowS #

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

Defined in HMath.Matrix

Methods

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

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

fromList

fromList :: Int -> Int -> [a] -> Maybe (Matrix a) Source #

Construct a matrix from a finite list in row-major order. Negative shapes, shapes whose element count exceeds maxBound :: Int, and incorrect list lengths fail.

fromListChecked

fromListChecked :: Int -> Int -> [a] -> Either MathError (Matrix a) Source #

Checked fromList. The shape is validated before inspecting the list.

toList

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

Return all elements in row-major order.

shape

shape :: Matrix a -> (Int, Int) Source #

Return (rows, columns), including either dimension of an empty matrix.

zeroMatrix

zeroMatrix :: Num a => Int -> Int -> Maybe (Matrix a) Source #

Construct a matrix filled with zeros, subject to the same shape checks as fromList.

zeroMatrixChecked

zeroMatrixChecked :: Num a => Int -> Int -> Either MathError (Matrix a) Source #

Checked zeroMatrix.

diagonal

diagonal :: Num a => [a] -> Maybe (Matrix a) Source #

Construct a square matrix with the finite input list on its diagonal and zeros elsewhere. An empty list produces a 0 x 0 matrix.

diagonalChecked

diagonalChecked :: Num a => [a] -> Either MathError (Matrix a) Source #

Checked diagonal. Rejects a square shape whose element count overflows.

at

at :: Matrix a -> Int -> Int -> Maybe a Source #

Look up an element using zero-based row and column indices.

atChecked

atChecked :: Matrix a -> Int -> Int -> Either MathError a Source #

Checked at. Empty matrices have no valid element indices.

sliceMatrix

sliceMatrix :: (Int, Int) -> (Int, Int) -> Matrix a -> Maybe (Matrix a) Source #

Extract a slice given its (startRow, startColumn) and (rowCount, columnCount). Extents must be nonnegative and contained within the matrix. A zero extent may start at the corresponding shape endpoint.

sliceMatrixChecked

sliceMatrixChecked :: (Int, Int) -> (Int, Int) -> Matrix a -> Either MathError (Matrix a) Source #

Checked sliceMatrix. Extent shapes are validated before bounds; bounds checks avoid adding the start and extent, which could overflow.

reshape

reshape :: Int -> Int -> Matrix a -> Maybe (Matrix a) Source #

Change the shape while preserving row-major element order. The new shape must contain exactly the same number of elements.

reshapeChecked

reshapeChecked :: Int -> Int -> Matrix a -> Either MathError (Matrix a) Source #

Checked reshape. Validate the target shape before comparing counts.

addMatrix

addMatrix :: Num a => Matrix a -> Matrix a -> Maybe (Matrix a) Source #

Add matrices with equal shapes.

addMatrixChecked

addMatrixChecked :: Num a => Matrix a -> Matrix a -> Either MathError (Matrix a) Source #

Checked addMatrix.

subtractMatrix

subtractMatrix :: Num a => Matrix a -> Matrix a -> Maybe (Matrix a) Source #

Subtract matrices with equal shapes.

subtractMatrixChecked

subtractMatrixChecked :: Num a => Matrix a -> Matrix a -> Either MathError (Matrix a) Source #

hadamard

hadamard :: Num a => Matrix a -> Matrix a -> Maybe (Matrix a) Source #

Multiply corresponding elements of matrices with equal shapes.

hadamardChecked

hadamardChecked :: Num a => Matrix a -> Matrix a -> Either MathError (Matrix a) Source #

Checked hadamard.

scaleMatrix

scaleMatrix :: Num a => a -> Matrix a -> Matrix a Source #

Multiply every element by the scalar, preserving the shape.

multiply

multiply :: Num a => Matrix a -> Matrix a -> Maybe (Matrix a) Source #

Matrix product when the left column count equals the right row count and the output element count fits in Int. A product with zero inner dimension contains zeros, with the full output shape preserved.

multiplyChecked

multiplyChecked :: Num a => Matrix a -> Matrix a -> Either MathError (Matrix a) Source #

Checked multiply. Inner dimensions are checked before the output shape.

transposeMatrix

transposeMatrix :: Matrix a -> Matrix a Source #

Swap rows and columns. This preserves zero dimensions, so transposing twice returns the original matrix even when one dimension is zero.

identity

identity :: Num a => Int -> Maybe (Matrix a) Source #

Construct a square identity matrix of nonnegative order. The element count must fit in Int.

identityChecked

identityChecked :: Num a => Int -> Either MathError (Matrix a) Source #

Checked identity.