Last active
February 6, 2023 00:47
-
-
Save workflow/1e786ed04b25426a2f1f43ce8544f7d5 to your computer and use it in GitHub Desktop.
Matrix Multiplication, Idris Style
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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