module Feldspar.Matrix where
import qualified Prelude as P
import Feldspar.Prelude
import Feldspar.Core
import Feldspar.Vector.Internal
type Matrix a = Vector2 a
tMat :: Patch a a -> Patch (Matrix a) (Matrix a)
tMat = tVec2
freezeMatrix :: Type a => Matrix a -> Data [[a]]
freezeMatrix = freezeVector . map freezeVector
thawMatrix :: Type a => Data [[a]] -> Matrix a
thawMatrix = map thawVector . thawVector
thawMatrix' :: Type a => Length -> Length -> Data [[a]] -> Matrix a
thawMatrix' y x = map (thawVector' x) . thawVector' y
matrix :: Type a => [[a]] -> Matrix a
matrix = value
indexedMat
:: Data Length
-> Data Length
-> (Data Index -> Data Index -> a)
-> Vector (Vector a)
indexedMat m n idx = indexed m $ \k -> indexed n $ \l -> idx k l
transpose :: Syntax a => Vector (Vector a) -> Vector (Vector a)
transpose a = indexedMat (length $ head a) (length a) $ \y x -> a ! x ! y
flatten :: Type a => Matrix a -> Vector (Data a)
flatten matr = Indexed (m*n) ixf Empty
where
m = length matr
n = (m==0) ? 0 $ length (head matr)
ixf i = matr ! y ! x
where
y = i `div` n
x = i `mod` n
diagonal :: Type a => Matrix a -> Vector (Data a)
diagonal m = zipWith (!) m (0 ... (length m 1))
distributeL :: (a -> b -> c) -> a -> Vector b -> Vector c
distributeL f = map . f
distributeR :: (a -> b -> c) -> Vector a -> b -> Vector c
distributeR = flip . distributeL . flip
class Mul a b
where
type Prod a b
(***) :: a -> b -> Prod a b
instance Numeric a => Mul (Data a) (Data a)
where
type Prod (Data a) (Data a) = Data a
(***) = (*)
instance Numeric a => Mul (Data a) (Vector1 a)
where
type Prod (Data a) (Vector1 a) = Vector1 a
(***) = distributeL (***)
instance Numeric a => Mul (Vector1 a) (Data a)
where
type Prod (Vector1 a) (Data a) = Vector1 a
(***) = distributeR (***)
instance Numeric a => Mul (Data a) (Matrix a)
where
type Prod (Data a) (Matrix a) = Matrix a
(***) = distributeL (***)
instance Numeric a => Mul (Matrix a) (Data a)
where
type Prod (Matrix a) (Data a) = Matrix a
(***) = distributeR (***)
instance Numeric a => Mul (Vector1 a) (Vector1 a)
where
type Prod (Vector1 a) (Vector1 a) = Data a
(***) = scalarProd
instance Numeric a => Mul (Vector1 a) (Matrix a)
where
type Prod (Vector1 a) (Matrix a) = (Vector1 a)
vec *** mat = distributeL (***) vec (transpose mat)
instance Numeric a => Mul (Matrix a) (Vector1 a)
where
type Prod (Matrix a) (Vector1 a) = (Vector1 a)
(***) = distributeR (***)
instance Numeric a => Mul (Matrix a) (Matrix a)
where
type Prod (Matrix a) (Matrix a) = (Matrix a)
(***) = distributeR (***)
mulMat :: Numeric a => Matrix a -> Matrix a -> Matrix a
mulMat = (***)
class Syntax a => ElemWise a
where
type Scalar a
elemWise :: (Scalar a -> Scalar a -> Scalar a) -> a -> a -> a
instance Type a => ElemWise (Data a)
where
type Scalar (Data a) = Data a
elemWise = id
instance (ElemWise a, Syntax (Vector a)) => ElemWise (Vector a)
where
type Scalar (Vector a) = Scalar a
elemWise = zipWith . elemWise
(.+) :: (ElemWise a, Num (Scalar a)) => a -> a -> a
(.+) = elemWise (+)
(.-) :: (ElemWise a, Num (Scalar a)) => a -> a -> a
(.-) = elemWise ()
(.*) :: (ElemWise a, Num (Scalar a)) => a -> a -> a
(.*) = elemWise (*)