public
Created

Statically Type-Checked Vector and Matrix Algebra

  • Download Gist
Algebra.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
{-# LANGUAGE TypeFamilies, MultiParamTypeClasses, FlexibleInstances, EmptyDataDecls, OverlappingInstances, FlexibleContexts #-}
 
module Data.Algebra where
 
import Prelude hiding (Num(..), (/))
import qualified Prelude as P
 
import Control.Applicative
import Control.Arrow (first, second)
import Data.Ratio (Ratio, (%))
import Data.Array.Unboxed
import Data.List (transpose)
 
class Add a b where
type AddResult a b
 
(+) :: a -> b -> AddResult a b
 
instance P.Num n => Add n n where
type AddResult n n = n
 
(+) = (P.+)
 
class Mul a b where
type MulResult a b
 
(*) :: a -> b -> MulResult a b
 
instance P.Num n => Mul n n where
type MulResult n n = n
 
(*) = (P.*)
 
class Div a b where
type DivResult a b
 
(/) :: a -> b -> DivResult a b
 
instance Div Float Float where
type DivResult Float Float = Float
 
(/) = (P./)
 
instance Div Int Int where
type DivResult Int Int = Ratio Int
 
(/) = (%)
 
infixl 6 +
infixl 7 *, /
 
newtype Vector d = Vector { vecArray :: UArray Int Float }
 
data D1
data Succ d
type D2 = Succ D1
type D3 = Succ D2
 
vector2d :: Float -> Float -> Vector D2
vector2d x y = Vector $ listArray (1,2) [x,y]
 
vector3d :: Float -> Float -> Float -> Vector D3
vector3d x y z = Vector $ listArray (1,3) [x,y,z]
 
instance Mul Float (Vector d) where
type MulResult Float (Vector d) = (Vector d)
 
x * (Vector v) = Vector . amap (P.* x) $ v
 
instance Show (Vector d) where
show = show . elems . vecArray
 
instance Show (Matrix m n) where
show = unlines . map show . rows
 
instance Add (Vector d) (Vector d) where
type AddResult (Vector d) (Vector d) = Vector d
 
(Vector a) + (Vector b) = Vector $ listArray bds els where
bds = bounds a
els = zipWith (P.+) (elems a) (elems b)
 
newtype Matrix m n = Matrix { matArray :: UArray (Int,Int) Float }
 
instance Mul (Matrix m p) (Matrix p n) where
type MulResult (Matrix m p) (Matrix p n) = Matrix m n
 
(Matrix a) * (Matrix b) = Matrix $ listArray bds els where
bds = ((1,1),(m,n))
els = el <$> [1..m] <*> [1..n]
 
el i j = sum [a!(i,k) * b!(k,j) | k <- [1..p]]
 
(_,(m,p)) = bounds a
(_,(_,n)) = bounds b
 
row :: Vector d -> Matrix D1 d
row (Vector v) = Matrix $ ixmap ((1,1),(1,n)) (\(1,j) -> j) v where
(_,n) = bounds v
 
col :: Vector d -> Matrix d D1
col (Vector v) = Matrix $ ixmap ((1,1),(m,1)) (\(i,1) -> 1) v where
(m,_) = bounds v
 
rows :: Matrix m n -> [Vector n]
rows (Matrix mat) = map Vector $ [ixmap (1,n) (\i -> (k,i)) mat | k <- [1..m]]
where (_,(m,n)) = bounds mat
 
class Cons e c where
type ConsResult e c
 
(+:) :: e -> c -> ConsResult e c
 
infixr 5 +:
 
instance Cons Float (Vector d) where
type ConsResult Float (Vector d) = Vector (Succ d)
 
x +: (Vector v) = Vector . listArray bds . (x:) . elems $ v where
bds = second succ $ bounds v
 
instance Cons (Vector n) (Matrix m n) where
type ConsResult (Vector n) (Matrix m n) = Matrix (Succ m) n
 
(Vector a) +: (Matrix b) = Matrix $ listArray bds els where
bds = second (first succ) $ bounds b
els = elems a ++ elems b
 
class Concat a b where
type ConcatResult a b
 
(++:) :: a -> b -> ConcatResult a b
 
type family AddT a b
type instance AddT D1 b = Succ b
type instance AddT (Succ a) b = Succ (AddT a b)
 
instance Concat (Vector a) (Vector b) where
type ConcatResult (Vector a) (Vector b) = Vector (AddT a b)
 
(Vector a) ++: (Vector b) = Vector $ listArray bds els where
bds = (\(_,i)(_,j) -> (1,i+j)) (bounds a) (bounds b)
els = elems a ++ elems b
 
data Get d = Safe
 
getPrev :: Get (Succ d) -> Get d
getPrev _ = Safe
 
class GetIndex g where
getIndex :: g -> Int
 
instance GetIndex (Get D1) where
getIndex = const 1
 
instance GetIndex (Get d) => GetIndex (Get (Succ d)) where
getIndex = (P.+ 1) . getIndex . getPrev
 
class SafeGet g c where
type GetElem c
 
(!!!) :: c -> g -> GetElem c
 
-- Instance for all types (Get i) and (Vector d) for which i <= d
instance (GetIndex (Get i), IsLessEq i d ~ TTrue) => SafeGet (Get i) (Vector d) where
type GetElem (Vector d) = Float
 
Vector v !!! g = v ! getIndex g
 
instance (GetIndex (Get i), GetIndex (Get j), IsLessEq i m ~ TTrue, IsLessEq j n ~ TTrue)
=> SafeGet (Get i, Get j) (Matrix m n) where
 
type GetElem (Matrix m n) = Float
 
Matrix m !!! (i,j) = m ! (getIndex i, getIndex j)
 
data TTrue -- type level truth value
 
-- type level less-or-equal
class TLessEq x y where
type IsLessEq x y
 
-- D1 is equal to D1
instance TLessEq D1 D1 where
type IsLessEq D1 D1 = TTrue
 
-- D1 is less than any successor type
instance TLessEq D1 (Succ d) where
type IsLessEq D1 (Succ d) = TTrue
 
-- The ordering of x and y is the same as (Succ x) and (Succ y)
instance TLessEq x y => TLessEq (Succ x) (Succ y) where
type IsLessEq (Succ x) (Succ y) = IsLessEq x y
 
data Arb
 
class ToArb a where
type ArbResult a
 
toArb :: a -> ArbResult a
 
instance ToArb (Vector d) where
type ArbResult (Vector d) = Vector Arb
 
toArb (Vector v) = Vector v
 
instance ToArb (Matrix m n) where
type ArbResult (Matrix m n) = Matrix Arb Arb
 
toArb (Matrix m) = Matrix m

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.