Add documentation
This commit is contained in:
parent
11d771b926
commit
59af31cdd7
10 changed files with 188 additions and 37 deletions
|
|
@ -8,10 +8,12 @@ import Data.NumIdr.Vector
|
|||
%default total
|
||||
|
||||
|
||||
||| A matrix is a rank-2 array.
|
||||
public export
|
||||
Matrix : Nat -> Nat -> Type -> Type
|
||||
Matrix m n = Array [m,n]
|
||||
|
||||
||| A synonym for a square matrix with dimensions of length `n`.
|
||||
public export
|
||||
Matrix' : Nat -> Type -> Type
|
||||
Matrix' n = Matrix n n
|
||||
|
|
@ -22,26 +24,30 @@ Matrix' n = Matrix n n
|
|||
--------------------------------------------------------------------------------
|
||||
|
||||
|
||||
||| Construct a matrix with the given order and elements.
|
||||
export
|
||||
matrix' : {m, n : _} -> Order -> Vect m (Vect n a) -> Matrix m n a
|
||||
matrix' ord x = array' [m,n] ord x
|
||||
|
||||
||| Construct a matrix with the given elements.
|
||||
export
|
||||
matrix : {m, n : _} -> Vect m (Vect n a) -> Matrix m n a
|
||||
matrix = matrix' COrder
|
||||
|
||||
|
||||
||| Construct a matrix with a specific value along the diagonal.
|
||||
|||
|
||||
||| @ diag The value to repeat along the diagonal
|
||||
||| @ other The value to repeat elsewhere
|
||||
export
|
||||
repeatDiag : {m, n : _} -> (diag, other : a) -> Matrix m n a
|
||||
repeatDiag d o = fromFunction [m,n]
|
||||
(\[i,j] => if i `eq` j then d else o)
|
||||
where
|
||||
eq : {0 m,n : Nat} -> Fin m -> Fin n -> Bool
|
||||
eq FZ FZ = True
|
||||
eq (FS x) (FS y) = eq x y
|
||||
eq FZ (FS _) = False
|
||||
eq (FS _) FZ = False
|
||||
repeatDiag d o = fromFunctionNB [m,n]
|
||||
(\[i,j] => if i == j then d else o)
|
||||
|
||||
||| Construct a matrix given its diagonal elements.
|
||||
|||
|
||||
||| @ diag The elements of the matrix's diagonal
|
||||
||| @ other The value to repeat elsewhere
|
||||
export
|
||||
fromDiag : {m, n : _} -> (diag : Vect (minimum m n) a) -> (other : a) -> Matrix m n a
|
||||
fromDiag ds o = fromFunction [m,n] (\[i,j] => maybe o (`index` ds) $ i `eq` j)
|
||||
|
|
@ -53,15 +59,18 @@ fromDiag ds o = fromFunction [m,n] (\[i,j] => maybe o (`index` ds) $ i `eq` j)
|
|||
eq (FS _) FZ = Nothing
|
||||
|
||||
|
||||
||| The `n`-dimensional identity matrix.
|
||||
export
|
||||
identity : Num a => {n : _} -> Matrix' n a
|
||||
identity = repeatDiag 1 0
|
||||
|
||||
|
||||
||| Calculate the matrix that scales a vector by the given value.
|
||||
export
|
||||
scaling : Num a => {n : _} -> a -> Matrix' n a
|
||||
scaling x = repeatDiag x 0
|
||||
|
||||
||| Calculate the rotation matrix of an angle.
|
||||
export
|
||||
rotation2D : Double -> Matrix' 2 Double
|
||||
rotation2D a = matrix [[cos a, - sin a], [sin a, cos a]]
|
||||
|
|
@ -72,19 +81,24 @@ rotation2D a = matrix [[cos a, - sin a], [sin a, cos a]]
|
|||
--------------------------------------------------------------------------------
|
||||
|
||||
|
||||
||| Index the matrix at the given coordinates.
|
||||
export
|
||||
index : Fin m -> Fin n -> Matrix m n a -> a
|
||||
index m n = index [m,n]
|
||||
|
||||
||| Index the matrix at the given coordinates, returning `Nothing` if the
|
||||
||| coordinates are out of bounds.
|
||||
export
|
||||
indexNB : Nat -> Nat -> Matrix m n a -> Maybe a
|
||||
indexNB m n = indexNB [m,n]
|
||||
|
||||
|
||||
||| Return a row of the matrix as a vector.
|
||||
export
|
||||
getRow : Fin m -> Matrix m n a -> Vector n a
|
||||
getRow r mat = rewrite sym (minusZeroRight n) in indexRange [One r, All] mat
|
||||
|
||||
||| Return a column of the matrix as a vector.
|
||||
export
|
||||
getColumn : Fin n -> Matrix m n a -> Vector m a
|
||||
getColumn c mat = rewrite sym (minusZeroRight m) in indexRange [All, One c] mat
|
||||
|
|
@ -94,19 +108,22 @@ getColumn c mat = rewrite sym (minusZeroRight m) in indexRange [All, One c] mat
|
|||
-- Operations
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
||| Concatenate two matrices vertically.
|
||||
export
|
||||
vconcat : Matrix m n a -> Matrix m' n a -> Matrix (m + m') n a
|
||||
vconcat = concat 0
|
||||
|
||||
||| Concatenate two matrices horizontally.
|
||||
export
|
||||
hconcat : Matrix m n a -> Matrix m n' a -> Matrix m (n + n') a
|
||||
hconcat = concat 1
|
||||
|
||||
|
||||
||| Calculate the kronecker product of two vectors as a matrix.
|
||||
export
|
||||
kronecker : Num a => Vector m a -> Vector n a -> Matrix m n a
|
||||
kronecker a b with (viewShape a, viewShape b)
|
||||
_ | (Shape [m], Shape [n]) = fromFunction [m,n] (\[i,j] => index i a * index j b)
|
||||
_ | (Shape [m], Shape [n]) = fromFunction [m,n] (\[i,j] => a !! i * b !! j)
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue