-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | A completely type-safe library for linear algebra -- -- This library defines data types and classes for fixed dimension -- vectors and tensors. See Data.Tensor.Examples for a -- short tutorial. @package tensor @version 0.1.1 -- | This library defines data types and classes for fixed dimension -- vectors and tensors. The main objects are: -- --
-- >>> :m Data.Ordinal Data.TypeList.MultiIndex Data.Tensor.Vector -- -- >>> fromList [2,3,5,1,3,6,0,5,4,2,1,3] :: Tensor (Four :|: (Three :|: Nil)) Int -- [[2,3,5],[1,3,6],[0,5,4],[2,1,3]] ---- -- The above defines a tensor with 4 rows and 3 columns (a matrix) and -- Int coefficients. The entries of this matrix are taken -- from a list using fromList which is a method of the -- class FromList. Notice the output: the -- Show instance is defined in such a way to give a -- readable representation as list of lists. The is equivalent but -- slightly more readable code: -- --
-- >>> fromList [2,3,5,1,3,6,0,5,4,2,1,3] :: Matrix Four Three Int -- [[2,3,5],[1,3,6],[0,5,4],[2,1,3]] ---- -- Analogously -- --
-- >>> fromList [7,3,-6] :: Tensor (Three :|: Nil) Int -- [7,3,-6] ---- -- and -- --
-- >>> fromList [7,3,-6] :: Vector Three Int -- [7,3,-6] ---- -- are the same. In order to access an entry of a Tensor -- we use the ! operator, which takes the same -- MultiIndex of the Tensor as its second -- argument: -- --
-- >>> let a = fromList [2,3,5,1,3,6,0,5,4,2,1,3] :: Matrix Four Three Int -- -- >>> let b = fromList [7,3,-6] :: Vector Three Int -- -- >>> a ! (toMultiIndex [1,3] :: (Four :|: (Three :|: Nil))) -- 5 -- -- >>> b ! (toMultiIndex [2] :: (Three :|: Nil)) -- 3 ---- -- it returns the element at the coordinate (1,3) of the matrix -- a, and the element at the coordinate 2 of the vector b. In -- fact, thanks to type inference, we could simply write -- --
-- >>> a ! toMultiIndex [1,3] -- 5 -- -- >>> b ! toMultiIndex [2] -- 2 ---- -- And now a couple of examples of algebraic operations (requires adding -- Data.Tensor.LinearAlgebra.Vector to the import list): -- --
-- >>> :m Data.Ordinal Data.TypeList.MultiIndex Data.Tensor.Vector Data.Tensor.LinearAlgebra.Vector -- -- >>> let a = fromList [2,3,5,1,3,6,0,5,4,2,1,3] :: Matrix Four Three Int -- -- >>> let b = fromList [7,3,-6] :: Vector Three Int -- -- >>> a .*. b -- [-7,-20,-9,-1] ---- -- is the product of matrix a and vector b, while -- --
-- >>> let c = fromList [3,4,0,-1,4,5,6,2,1] :: Matrix Three Three Int -- -- >>> c -- [[3,4,0],[-1,4,5],[6,2,1]] -- -- >>> charPoly c -- [106,13,8] ---- -- gives the coefficients of the characteristic polynomial of the matrix -- c. module Data.Tensor.Examples module Data.Tensor -- | A Tensor is a map from an Index type -- (which should be a MultiIndex) to an -- Element type. class Tensor t where type family Index t type family Elem t replicate e = generate (\ _ -> e) dims :: Tensor t => t -> Index t (!) :: Tensor t => t -> Index t -> Elem t generate :: Tensor t => (Index t -> Elem t) -> t replicate :: Tensor t => Elem t -> t class FromList t fromList :: FromList t => [e] -> t e class DirectSum n t1 t2 where type family SumSpace n t1 t2 directSum :: DirectSum n t1 t2 => n -> t1 -> t2 -> SumSpace n t1 t2 class Transpose t where type family TransposeSpace t transpose :: Transpose t => t -> TransposeSpace t class Zip t zipWith :: Zip t => (a -> b -> c) -> t a -> t b -> t c module Data.TypeAlgebra -- | Sum of types. class Sum a b where type family :+: a b (<+>) :: Sum a b => a -> b -> a :+: b -- | Product of types. class Prod a b where type family :*: a b (<*>) :: Prod a b => a -> b -> a :*: b module Data.Cardinal data Zero -- | Cardinal number as a type. The associated data type Succ -- a provides the next cardinal type. The method -- fromCardinal provides a numeric representation of the -- cardinal number; it should be independent on the argument and work on -- undefined. class Cardinal a where data family Succ a fromCardinal :: (Cardinal a, Num i) => a -> i type C0 = Zero type C1 = Succ C0 type C2 = Succ C1 type C3 = Succ C2 type C4 = Succ C3 -- | The cardinality of a type is defined by its Cardinal -- type Card a. class Cardinal (Card a) => Cardinality a where type family Card a -- | The numeric cardinality of a type. card is independent -- on its argument. card :: (Cardinality a, Num i) => a -> i instance (Cardinal a, Prod a b) => Prod a (Succ b) instance Cardinal a => Prod a Zero instance (Cardinal a, Cardinal b, Sum a b) => Sum a (Succ b) instance Cardinal a => Sum a Zero instance Cardinal a => Show (Succ a) instance Show Zero instance Cardinal a => Cardinal (Succ a) instance Cardinal Zero -- | In this module we provide a way to canonically define a totally -- ordered set with a given number of elements. These types have a custom -- Show instances so that their elements are displayed -- with usual decimal number. -- -- One = {One} = {1} -- -- Succ One = {First, -- Succ One} = {1,2} -- -- Succ Succ One = {First, -- Succ First, Succ Succ -- One} = {1,2,3} -- -- ... module Data.Ordinal -- | A set with one element. data One One :: One -- | If n is a set with n elements, Succ n is a -- set with n+1 elements. data Succ n -- | The first element of the type. First :: Succ n -- | The last n elements. Succ :: n -> Succ n type Two = Succ One type Three = Succ Two type Four = Succ Three type Five = Succ Four -- | Class of ordered sets with n elements. The methods in this class -- provide a convenient way to convert to and from a numeric type. class (Cardinality n, Ord n) => Ordinal n fromOrdinal :: (Ordinal n, Num i) => n -> i toOrdinal :: (Ordinal n, Eq i, Num i) => i -> n instance Eq One instance Eq n => Eq (Succ n) instance (Ordinal m, Ordinal n, Prod m n, Sum m (m :*: n), Ordinal (m :+: (m :*: n))) => Prod m (Succ n) instance Ordinal m => Prod m One instance (Ordinal m, Ordinal n, Ordinal (m :+: n), Sum m n) => Sum m (Succ n) instance Ordinal m => Sum m One instance Cardinality n => Cardinality (Succ n) instance Cardinality One instance Monad Succ instance Functor Succ instance Ordinal n => Show (Succ n) instance (Bounded n, Enum n, Ordinal n) => Enum (Succ n) instance Ordinal n => Ordinal (Succ n) instance Ord n => Ord (Succ n) instance Bounded n => Bounded (Succ n) instance Show One instance Enum One instance Ordinal One instance Ord One instance Bounded One -- | The Module Data.TypeList is a collection of classes to -- manipulate lists of types, a.k.a. heterogeneous lists. Check the -- module Data.TypeList.MultiIndex for a concrete implementation -- of TypeList. module Data.TypeList -- | Every TypeList has a Length. The -- Length is actually a type, and should be a -- Cardinal (see Data.Cardinal). class TypeList l where type family Length l length _ = undefined length :: TypeList l => l -> Length l -- | A class for appending two TypeLists. The result of -- appending l and l' has type l :++: -- l'. class (TypeList l, TypeList l') => AppendList l l' where type family :++: l l' (<++>) :: AppendList l l' => l -> l' -> l :++: l' -- | This is does for TakeList what take -- does for ordinary lists. class (Cardinal n, TypeList l) => TakeList n l where type family Take n l take :: TakeList n l => n -> l -> Take n l -- | This is does for TakeList what drop -- does for ordinary lists. class (Cardinal n, TypeList l) => DropList n l where type family Drop n l drop :: DropList n l => n -> l -> Drop n l -- | Reverse l and append it in front of l'. class (TypeList l, TypeList l') => TailRevList l l' where type family TailRev l l' rev :: TailRevList l l' => l -> l' -> TailRev l l' -- | Reverse the TypeList l, and get -- Reverse l. class TypeList l => ReverseList l where type family Reverse l reverse :: ReverseList l => l -> Reverse l -- | Join together TypeLists l and l' -- where the last n types of l coincide with the first -- n types of l'. The result has a the common -- n types eliminated. class JoinList n l l' where type family Join n l l' join :: JoinList n l l' => n -> l -> l' -> Join n l l' instance (ReverseList (Drop n (Reverse l)), DropList n (Reverse l), ReverseList l, AppendList (Reverse (Drop n (Reverse l))) (Drop n l'), DropList n l', Reverse (Take n (Reverse l)) ~ Take n l') => JoinList n l l' -- | We define the a multidimensional array of indices called -- MultiIndex. The canonical implementation of a -- MultiIndex is an heterogeneous list of -- Ordinals. Below we illustrate some example of -- MultiIndex types and the elements they contain. -- -- Three :|: Nil = {(1),(2),(3)} -- -- Three :|: (Two :|: Nil) = -- {(1,1),(1,2),(2,1),(2,2),(3,1),(3,2)} -- -- Three :|: (Two :|: (Two -- :|: Nil)) = -- {(1,1,1),(1,1,2),(1,2,1),(1,2,2),(2,1,1),(2,1,2),(2,2,1),(2,2,2),(3,1,1),(3,1,2),(3,2,1),(3,2,2)} module Data.TypeList.MultiIndex data Nil Nil :: Nil -- | This is the constructor for heterogeneous lists, equivalent to -- : for standard lists. Nil is used to -- end the lists, just like '[]'. data (:|:) a b (:|:) :: a -> b -> :|: a b class TypeList i => MultiIndex i fromMultiIndex :: (MultiIndex i, Num n) => i -> [n] toMultiIndex :: (MultiIndex i, Eq n, Num n) => [n] -> i dimensions :: (MultiIndex i, Num n) => i -> [n] multiIndex2Linear :: (MultiIndex i, Num n) => i -> n class (Cardinal n, MultiIndex is, MultiIndex js) => MultiIndexConcat n is js where type family Concat n is js instance Eq Nil instance (Eq a, Eq b) => Eq (a :|: b) instance (Bounded e, Bounded l) => Bounded (e :|: l) instance Bounded Nil instance (Ord e, Ord l) => Ord (e :|: l) instance Ord Nil instance (Cardinal n, Ordinal i, MultiIndex js, MultiIndex ks, MultiIndexConcat n js ks) => MultiIndexConcat (Succ n) (i :|: js) (i :|: ks) instance (Ordinal i1, Ordinal i2, Sum i1 i2, MultiIndex is) => MultiIndexConcat Zero (i1 :|: is) (i2 :|: is) instance (Ordinal i, MultiIndex is) => Show (i :|: is) instance (Ordinal i, MultiIndex is) => MultiIndex (i :|: is) instance Show Nil instance MultiIndex Nil instance (TailRevList l Nil, TailRevList (e :|: l) Nil) => ReverseList (e :|: l) instance ReverseList Nil instance (TailRevList l (e :|: l'), TypeList l') => TailRevList (e :|: l) l' instance TypeList l => TailRevList Nil l instance DropList n l => DropList (Succ n) (e :|: l) instance DropList Zero l => DropList Zero (e :|: l) instance DropList Zero Nil instance TakeList n l => TakeList (Succ n) (e :|: l) instance TakeList Zero l => TakeList Zero (e :|: l) instance TakeList Zero Nil instance AppendList l l' => AppendList (e :|: l) l' instance TypeList l => AppendList Nil l instance TypeList l => TypeList (e :|: l) instance TypeList Nil instance (Cardinality e, Cardinality l, Cardinal (Card e :*: Card l)) => Cardinality (e :|: l) instance Cardinality Nil module Data.Tensor.LinearAlgebra class VectorSpace v zero :: (VectorSpace v, Num e) => v e (*.) :: (VectorSpace v, Num e) => e -> v e -> v e (.+.) :: (VectorSpace v, Num e) => v e -> v e -> v e -- | A general form of product between two tensors, in which the last -- n dimensions of t1 are contracted with the first -- n dimensions of t2. The resulting tensor belongs to -- the space ProdSpace n t1 t2. -- MatrixProduct and TensorProduct below -- are particular cases where n is equal to 1 and 0 -- respectively. class Cardinal n => Product n t1 t2 where type family ProdSpace n t1 t2 prod :: Product n t1 t2 => n -> t1 -> t2 -> ProdSpace n t1 t2 -- | It is the product of the last dimension of t1 with the first -- dimension of t2. In the case where t1 and -- t2 are matrices this coincide with the ordinary matrix -- product. class MatrixProduct t1 t2 where type family MatrixProductSpace t1 t2 (.*.) :: MatrixProduct t1 t2 => t1 -> t2 -> MatrixProductSpace t1 t2 -- | Tensor product of t1 and t2. class TensorProduct t1 t2 where type family :⊗: t1 t2 (⊗) :: TensorProduct t1 t2 => t1 -> t2 -> t1 :⊗: t2 class DotProduct t dot :: (DotProduct t, Num e) => t e -> t e -> e -- | A matrix with i rows and j columns. class (Tensor t, (Index t) ~ (i :|: (j :|: Nil))) => Matrix i j t rowSwitch :: Matrix i j t => i -> i -> t -> t rowMult :: (Matrix i j t, Num e, (Elem t) ~ e) => i -> (Elem t) -> t -> t rowAdd :: (Matrix i j t, Num e, (Elem t) ~ e) => i -> (Elem t) -> i -> t -> t colSwitch :: Matrix i j t => j -> j -> t -> t colMult :: (Matrix i j t, Num e, (Elem t) ~ e) => j -> (Elem t) -> t -> t colAdd :: (Matrix i j t, Num e, (Elem t) ~ e) => j -> (Elem t) -> j -> t -> t rowEchelonForm :: (Matrix i j t, Eq e, Fractional e, (Elem t) ~ e) => t -> t -- | Solves linear systems AX=B; t1 is the type of -- A, t2 is the type of B, and -- SolSpace t1 t2 is the type of the solution X. class LinearSystem t1 t2 where type family SolSpace t1 t2 triangularSolve :: LinearSystem t1 t2 => t1 -> t2 -> (t1, t2) parametricSolve :: LinearSystem t1 t2 => t1 -> t2 -> Maybe (SolSpace t1 t2, [SolSpace t1 t2]) class SquareMatrix t where tr = last . charPoly det = head . charPoly unit :: (SquareMatrix t, Num e) => t e inverse :: (SquareMatrix t, Eq e, Fractional e) => t e -> Maybe (t e) tr :: (SquareMatrix t, Num e) => t e -> e charPoly :: (SquareMatrix t, Num e) => t e -> [e] det :: (SquareMatrix t, Num e) => t e -> e -- | This module define a datatype Tensor which implements -- the classes and methods defined in Data.Tensor and -- Data.Tensor.LinearAlgebra. It is represented internally as a -- Vector. module Data.Tensor.Vector data Tensor i e type Vector n = Tensor (n :|: Nil) type Matrix m n = Tensor (m :|: (n :|: Nil)) type ColumnVector n = Matrix n One vector2ColumnVector :: Vector n e -> ColumnVector n e columnVector2Vector :: ColumnVector n e -> Vector n e type RowVector n = Matrix One n vector2RowVector :: Vector n e -> RowVector n e rowVector2Vector :: RowVector n e -> Vector n e class FromVector t fromVector :: FromVector t => Vector e -> t e module Data.Tensor.LinearAlgebra.Vector instance (Eq e, Fractional e, Ordinal i, Ordinal j) => LinearSystem (Tensor (i :|: (j :|: Nil)) e) (Tensor (i :|: Nil) e) instance (Eq e, Fractional e, Ordinal i, Ordinal j, Ordinal k, Sum j k) => LinearSystem (Tensor (i :|: (j :|: Nil)) e) (Tensor (i :|: (k :|: Nil)) e) instance (Bounded i, Ordinal i, Sum i i) => SquareMatrix (Tensor (i :|: (i :|: Nil))) instance (Bounded i, Ordinal i, Bounded j, Ordinal j) => Matrix i j (Tensor (i :|: (j :|: Nil)) e) instance DotProduct (Tensor i) instance Product C0 t1 t2 => TensorProduct t1 t2 instance Product (Succ Zero) t1 t2 => MatrixProduct t1 t2 instance (Num e, Cardinal n, MultiIndex i, MultiIndex j, JoinList n i j) => Product n (Tensor i e) (Tensor j e) instance (Bounded i, Cardinality i, MultiIndex i) => VectorSpace (Tensor i)