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
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.
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.
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 #
Checked subtractMatrix.
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.