-- 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 the homepage http://noaxiom.org/tensor -- for more details, or Data.Tensor.Examples for a short -- tutorial. @package tensor @version 0.3.0 -- | This library defines data types and classes for fixed dimension -- vectors and tensors. The main objects are: -- --
-- >>> import 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 to the import list): -- --
-- >>> import Data.Tensor.Vector -- -- >>> import Data.Tensor.LinearAlgebra hiding (Matrix) -- -- >>> 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 (!) :: Tensor t => t -> Index t -> Elem t generate :: Tensor t => (Index t -> Elem t) -> t generateM :: (Tensor t, Monad m) => (Index t -> m (Elem t)) -> m t -- | Generates a Tensor consisting of the same -- Element repeated. replicate :: Tensor t => Elem t -> t replicateM :: (Monad m, Tensor t) => m (Elem t) -> m t -- | elemMap f t applies f to every -- Element of t. elemMap :: (Tensor t1, Tensor t2, Index t1 ~ Index t2) => (Elem t1 -> Elem t2) -> t1 -> t2 -- | In indexMap f t, the Element -- corresponding to the Index i is the -- Element that t assignes to the -- Index f i. indexMap :: (Tensor t1, Tensor t2, Elem t1 ~ Elem t2) => (Index t1 -> Index t2) -> t2 -> t1 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 split :: DirectSum n t1 t2 => n -> SumSpace n t1 t2 -> (t1, t2) class Transpose t where type family TransposeSpace t transpose :: Transpose t => t -> TransposeSpace t -- | Slices the Tensor t by dropping i at -- the beginning of its Index and j at the end. -- The result has type Slice i j t. class Sliceable i j t where type family Slice i j t slice :: Sliceable i j t => i -> j -> t -> Slice i j t 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 type C5 = Succ C4 type C6 = Succ C5 type C7 = Succ C6 type C8 = Succ C7 type C9 = Succ C8 type C10 = Succ C9 -- | 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 class GCardinality a where type family GCard a instance Generic Zero instance Datatype D1Zero instance (GCardinality (f p), GCardinality (g p)) => GCardinality ((:*:) f g p) instance (GCardinality (f p), GCardinality (g p)) => GCardinality ((:+:) f g p) instance GCardinality (f p) => GCardinality (M1 i c f p) instance Cardinality a => GCardinality (K1 i a p) instance GCardinality (U1 p) instance GCardinality (V1 p) 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 type Six = Succ Five type Seven = Succ Six type Eight = Succ Seven type Nine = Succ Eight type Ten = Succ Nine -- | 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 Generic One instance Eq n => Eq (Succ n) instance Generic (Succ n) instance Datatype D1One instance Constructor C1_0One instance Datatype D1Succ instance Constructor C1_0Succ instance Constructor C1_1Succ 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 (Bounded n, Ordinal n) => Random (Succ n) instance Ordinal n => Ordinal (Succ n) instance Ord n => Ord (Succ n) instance Bounded n => Bounded (Succ n) instance Random One 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 class TypeList l => HeadTail l where type family Head l type family Tail l head :: HeadTail l => l -> Head l tail :: HeadTail l => l -> Tail l (.|.) :: HeadTail l => Head l -> Tail l -> l -- | Extracts the n-th component of the list l class Component l n where type family (:!!:) l n (!!) :: Component l n => l -> n -> l :!!: n partialMap :: Component l n => n -> (l :!!: n -> l :!!: n) -> l -> 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' -- | Extend the list l to l' by adding the necessary -- extension Ext l l'. class Extend l l' where type family Ext l l' extend :: Extend l l' => l -> Ext l 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' instance (HeadTail l, Component (Tail l) n) => Component l (Succ n) instance HeadTail l => Component l Zero -- | 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 (Dimensions i, TypeList i) => MultiIndex i fromMultiIndex :: (MultiIndex i, Num n) => i -> [n] toMultiIndex :: (MultiIndex i, Eq n, Num n) => [n] -> i multiIndex2Linear :: (MultiIndex i, Num n) => i -> n -- | Class for types having multiple dimensions, like -- MultiIndexes or Tensors. class Dimensions i dimensions :: (Dimensions 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 Generic Nil instance (Eq a, Eq b) => Eq (a :|: b) instance Generic (a :|: b) instance Datatype D1Nil instance Constructor C1_0Nil instance Datatype D1:|: instance Constructor C1_0:|: instance Extend l l' => Extend (e :|: l) (e :|: l') instance Extend Nil l' instance (Random e, Random l) => Random (e :|: l) instance (Bounded e, Bounded l) => Bounded (e :|: l) instance Random Nil instance Bounded 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, Dimensions is) => Dimensions (i :|: is) instance Dimensions Nil 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 => HeadTail (e :|: 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. The operators -- .*. and ⊗ 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 type MatrixProductSpace t1 t2 = ProdSpace (Succ Zero) 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. (.*.) :: Product (Succ Zero) t1 t2 => t1 -> t2 -> MatrixProductSpace t1 t2 type (:⊗:) t1 t2 = ProdSpace C0 t1 t2 -- | Tensor product of t1 and t2. (⊗) :: Product C0 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] minPoly :: (SquareMatrix t, Eq e, Fractional e) => t e -> [e] det :: (SquareMatrix t, Num e) => t e -> e polyEval :: (SquareMatrix t, Num e) => t e -> [e] -> t e module Data.Tensor.Pure type Vector i = Tensor (i :|: Nil) type Matrix i j = Tensor (i :|: (j :|: Nil)) instance Eq e => Eq (Tensor Nil e) instance (MultiIndex is, Ordinal n, Tensor (Tensor is e), Tensor (Tensor (n :|: is) e), e ~ Elem (Tensor is e), is ~ Index (Tensor is e), e ~ Elem (Tensor (n :|: is) e), Index (Tensor (n :|: is) e) ~ (n :|: is)) => Tensor (Tensor (Succ n :|: is) e) instance (MultiIndex is, Ordinal n, Show (Tensor is e), Show (Tensor (n :|: is) e)) => Show (Tensor (Succ n :|: is) e) instance (MultiIndex is, Ordinal n, Applicative (Tensor is), Applicative (Tensor (n :|: is))) => Applicative (Tensor (Succ n :|: is)) instance (MultiIndex is, Ordinal n, Functor (Tensor is), Functor (Tensor (n :|: is))) => Functor (Tensor (Succ n :|: is)) instance (FromList (Tensor is), FromList (Tensor (n :|: is)), Ordinal n, MultiIndex is) => FromList (Tensor (Succ n :|: is)) instance Dimensions (Tensor (n :|: is) e) => Dimensions (Tensor (Succ n :|: is) e) instance (MultiIndex is, Ordinal n, Eq (Tensor is e), Eq (Tensor (n :|: is) e)) => Eq (Tensor (Succ n :|: is) e) instance (MultiIndex is, Tensor (Tensor is e), e ~ Elem (Tensor is e), is ~ Index (Tensor is e)) => Tensor (Tensor (One :|: is) e) instance (MultiIndex is, Show (Tensor is e)) => Show (Tensor (One :|: is) e) instance (MultiIndex is, Applicative (Tensor is)) => Applicative (Tensor (One :|: is)) instance (MultiIndex is, Functor (Tensor is)) => Functor (Tensor (One :|: is)) instance (FromList (Tensor is), MultiIndex is) => FromList (Tensor (One :|: is)) instance Dimensions (Tensor is e) => Dimensions (Tensor (One :|: is) e) instance (MultiIndex is, Eq (Tensor is e)) => Eq (Tensor (One :|: is) e) instance Tensor (Tensor Nil e) instance Show e => Show (Tensor Nil e) instance Applicative (Tensor Nil) instance Functor (Tensor Nil) instance FromList (Tensor Nil) instance Dimensions (Tensor Nil 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 fromVector :: MultiIndex i => Vector e -> (Tensor i e)