module Numeric.LinearAlgebra.Class where
class Functor f => Additive f where
zero :: Num a => f a
(^+^) :: Num a => f a -> f a -> f a
negated :: (Num a, Functor f) => f a -> f a
negated = fmap negate
(^-^) :: (Additive f, Num a) => f a -> f a -> f a
x ^-^ y = x ^+^ negated y
class Additive f => VectorSpace f where
(.*) :: Num a => a -> f a -> f a
lerp :: (VectorSpace f, Num a) => a -> f a -> f a -> f a
lerp a u v = a .* u ^+^ ((1a) .* v)
class VectorSpace f => Hilbert f where
dot :: Num a => f a -> f a -> a
hilbertDistSq :: (Hilbert f, Num a) => f a -> f a -> a
hilbertDistSq x y = dot t t where
t = x ^-^ y
class Hilbert f => Normed f where
norm :: (Floating a, Eq a) => a -> f a -> a
normSq :: (Hilbert f, Num a) => f a -> a
normSq v = v `dot` v
norm1 :: (Foldable t, Num a, Functor t) => t a -> a
norm1 v = sum (fmap abs v)
norm2 :: (Hilbert f, Floating a) => f a -> a
norm2 v = sqrt (normSq v)
normP :: (Foldable t, Functor t, Floating a) => a -> t a -> a
normP p v = sum u**(1/p) where
u = fmap (**p) v
normInfty :: (Foldable t, Ord a) => t a -> a
normInfty = maximum
normalize :: (Normed f, Floating a, Eq a) => a -> f a -> f a
normalize p v = (1 / norm p v) .* v
dotLp :: (Set t, Foldable t, Floating a) => a -> t a -> t a -> a
dotLp p v1 v2 = sum u**(1/p) where
f a b = (a*b)**p
u = liftI2 f v1 v2
reciprocal :: (Functor f, Fractional b) => f b -> f b
reciprocal = fmap recip
scale :: (Num b, Functor f) => b -> f b -> f b
scale n = fmap (* n)
class Additive f => FiniteDim f where
type FDSize f :: *
dim :: f a -> FDSize f
withDim :: (FiniteDim f, Show e) =>
f a
-> (FDSize f -> f a -> Bool)
-> (f a -> c)
-> String
-> (f a -> e)
-> c
withDim x p f e ef | p (dim x) x = f x
| otherwise = error e' where e' = e ++ show (ef x)
withDim2 :: (FiniteDim f, FiniteDim g, Show e) =>
f a
-> g b
-> (FDSize f -> FDSize g -> f a -> g b -> Bool)
-> (f a -> g b -> c)
-> String
-> (f a -> g b -> e)
-> c
withDim2 x y p f e ef | p (dim x) (dim y) x y = f x y
| otherwise = error e' where e' = e ++ show (ef x y)
class Additive f => HasData f a where
type HDData f a :: *
dat :: f a -> HDData f a
class (FiniteDim f, HasData f a) => Sparse f a where
spy :: Fractional b => f a -> b
class Functor f => Set f where
liftU2 :: (a -> a -> a) -> f a -> f a -> f a
liftI2 :: (a -> b -> c) -> f a -> f b -> f c
class IxContainer (c :: * -> *) a where
type Ix c :: *
ixcLookup :: Ix c -> c a -> Maybe a
ixcLookupDefault :: a -> Ix c -> c a -> a
ixcFilter :: (a -> Bool) -> c a -> c a
ixcIfilter :: (Ix c -> a -> Bool) -> c a -> c a
ixcInsert :: Ix c -> a -> c a -> c a
ixcFromList :: [(Ix c, a)] -> c a
ixcToList :: c a -> [(Ix c, a)]