Skip to content

Instantly share code, notes, and snippets.

@workflow
Last active February 6, 2023 00:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save workflow/1e786ed04b25426a2f1f43ce8544f7d5 to your computer and use it in GitHub Desktop.
Save workflow/1e786ed04b25426a2f1f43ce8544f7d5 to your computer and use it in GitHub Desktop.
Matrix Multiplication, Idris Style
module Main
import Data.Vect
Matrix: Nat -> Nat -> Type -> Type
Matrix n m a = Vect n (Vect m a)
total createEmpties : Matrix n Z a
createEmpties {n = Z} = []
createEmpties {n = (S k)} = [] :: createEmpties
total vectDotProduct : Num a => Vect n a -> Vect n a -> a
vectDotProduct [] [] = 0
vectDotProduct (x :: xs) (y :: ys) = x * y + vectDotProduct xs ys
total matrixVectMult : Num a => Matrix n m a -> Vect m a -> Vect n a
matrixVectMult [] [] = []
matrixVectMult [] (x :: xs) = []
matrixVectMult (row :: rows) as = (vectDotProduct row as) :: matrixVectMult rows as
total transposeMatrix : Matrix n m a -> Matrix m n a
transposeMatrix [] = createEmpties
transposeMatrix (row :: rows) = let xsTransposed = transposeMatrix rows in
zipWith (::) row xsTransposed
total matrixMult: Num a => Matrix n m a -> Matrix m p a -> Matrix n p a
matrixMult[] [] = []
matrixMult[] (row :: rows) = []
matrixMult(row :: rows) ys = let transposedYs = transposeMatrix ys in
matrixVectMult transposedYs row :: matrixMult rows ys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment