-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | n-dimensional arrays -- -- n-dimensional arrays founded on numhask. @package numhask-array @version 0.4.0.0 -- | Functions for manipulating shape. The module tends to supply -- equivalent functionality at type-level and value-level with functions -- of the same name (except for capitalization). module NumHask.Array.Shape -- | The Shape type holds a [Nat] at type level and the equivalent [Int] at -- value level. newtype Shape (s :: [Nat]) Shape :: [Int] -> Shape [shapeVal] :: Shape -> [Int] class HasShape s toShape :: HasShape s => Shape s -- | Number of dimensions rank :: [a] -> Int type family Rank (s :: [a]) :: Nat -- | The shape of a list of element indexes ranks :: [[a]] -> [Int] type family Ranks (s :: [[a]]) :: [Nat] -- | Number of elements size :: [Int] -> Int type family Size (s :: [Nat]) :: Nat -- | convert from n-dim shape index to a flat index -- --
-- >>> flatten [2,3,4] [1,1,1] -- 17 ---- --
-- >>> flatten [] [1,1,1] -- 0 --flatten :: [Int] -> [Int] -> Int -- | convert from a flat index to a shape index -- --
-- >>> shapen [2,3,4] 17 -- [1,1,1] --shapen :: [Int] -> Int -> [Int] -- | checkIndex i n checks if i is a valid index of a list of -- length n checkIndex :: Int -> Int -> Bool type family CheckIndex (i :: Nat) (n :: Nat) :: Bool -- | checkIndexes is n check if is are valid indexes of a -- list of length n checkIndexes :: [Int] -> Int -> Bool type family CheckIndexes (i :: [Nat]) (n :: Nat) :: Bool -- | dimension i is the i'th dimension of a Shape dimension :: [Int] -> Int -> Int type family Dimension (s :: [Nat]) (i :: Nat) :: Nat -- | minimum value in a list minimum :: [Int] -> Int type family Minimum (s :: [Nat]) :: Nat type family Take (n :: Nat) (a :: [k]) :: [k] type family Drop (n :: Nat) (a :: [k]) :: [k] type family Tail (a :: [k]) :: [k] type family Init (a :: [k]) :: [k] type family Head (a :: [k]) :: k type family Last (a :: [k]) :: k type family (a :: [k]) ++ (b :: [k]) :: [k] -- | drop the i'th dimension from a shape -- --
-- >>> dropIndex [2, 3, 4] 1 -- [2,4] --dropIndex :: [Int] -> Int -> [Int] type DropIndex s i = Take i s ++ Drop (i + 1) s -- | addIndex s i d adds a new dimension to shape s at -- position i -- --
-- >>> addIndex [2,4] 1 3 -- [2,3,4] --addIndex :: [Int] -> Int -> Int -> [Int] type AddIndex s i d = Take i s ++ (d : Drop i s) type Reverse (a :: [k]) = ReverseGo a '[] type family ReverseGo (a :: [k]) (b :: [k]) :: [k] -- | convert a list of position that references a final shape to one that -- references positions relative to an accumulator. Deletions are from -- the left and additions are from the right. -- -- deletions -- --
-- >>> posRelative [0,1] -- [0,0] ---- -- additions -- --
-- >>> reverse (posRelative (reverse [1,0])) -- [0,0] --posRelative :: [Int] -> [Int] type family PosRelative (s :: [Nat]) type family PosRelativeGo (r :: [Nat]) (s :: [Nat]) type family DecMap (x :: Nat) (ys :: [Nat]) :: [Nat] -- | drop dimensions of a shape according to a list of positions (where -- position refers to the initial shape) -- --
-- >>> dropIndexes [2, 3, 4] [1, 0] -- [4] --dropIndexes :: [Int] -> [Int] -> [Int] type family DropIndexes (s :: [Nat]) (i :: [Nat]) type family DropIndexesGo (s :: [Nat]) (i :: [Nat]) -- | insert a list of dimensions according to position and dimension lists. -- Note that the list of positions references the final shape and not the -- initial shape. -- --
-- >>> addIndexes [4] [1,0] [3,2] -- [2,3,4] --addIndexes :: () => [Int] -> [Int] -> [Int] -> [Int] type family AddIndexes (as :: [Nat]) (xs :: [Nat]) (ys :: [Nat]) type family AddIndexesGo (as :: [Nat]) (xs :: [Nat]) (ys :: [Nat]) -- | take list of dimensions according to position lists. -- --
-- >>> takeIndexes [2,3,4] [2,0] -- [4,2] --takeIndexes :: [Int] -> [Int] -> [Int] type family TakeIndexes (s :: [Nat]) (i :: [Nat]) type family (a :: [k]) !! (b :: Nat) :: k type family Enumerate (n :: Nat) type family EnumerateGo (n :: Nat) -- | turn a list of included positions for a given rank into a list of -- excluded positions -- --
-- >>> exclude 3 [1,2] -- [0] --exclude :: Int -> [Int] -> [Int] type family Exclude (r :: Nat) (i :: [Nat]) concatenate' :: Int -> [Int] -> [Int] -> [Int] type Concatenate i s0 s1 = Take i s0 ++ (Dimension s0 i + Dimension s1 i : Drop (i + 1) s0) type CheckConcatenate i s0 s1 s = (CheckIndex i (Rank s0) && DropIndex s0 i == DropIndex s1 i && Rank s0 == Rank s1) ~ 'True type CheckInsert d i s = (CheckIndex d (Rank s) && CheckIndex i (Dimension s d)) ~ 'True type Insert d s = Take d s ++ (Dimension s d + 1 : Drop (d + 1) s) -- | incAt d s increments the index at d of shape s by -- one. incAt :: Int -> [Int] -> [Int] -- | decAt d s decrements the index at d of shape s by -- one. decAt :: Int -> [Int] -> [Int] reorder' :: [Int] -> [Int] -> [Int] type family Reorder (s :: [Nat]) (ds :: [Nat]) :: [Nat] type family CheckReorder (ds :: [Nat]) (s :: [Nat]) squeeze' :: (Eq a, Num a) => [a] -> [a] type family Squeeze (a :: [Nat]) type family Filter (r :: [Nat]) (xs :: [Nat]) (i :: Nat) type family Sort (xs :: [k]) :: [k] data Flag FMin :: Flag FMax :: Flag type family Cmp (a :: k) (b :: k) :: Ordering type family SFilter (f :: Flag) (p :: k) (xs :: [k]) :: [k] type family Zip lst lst' type family ZipWith f lst lst' type family Fst a type family Snd a type family FMap f lst -- | Reflect a list of Nats class KnownNats (ns :: [Nat]) natVals :: KnownNats ns => Proxy ns -> [Int] -- | Reflect a list of list of Nats class KnownNatss (ns :: [[Nat]]) natValss :: KnownNatss ns => Proxy ns -> [[Int]] instance GHC.Show.Show (NumHask.Array.Shape.Shape s) instance NumHask.Array.Shape.KnownNatss '[] instance (NumHask.Array.Shape.KnownNats n, NumHask.Array.Shape.KnownNatss ns) => NumHask.Array.Shape.KnownNatss (n : ns) instance NumHask.Array.Shape.KnownNats '[] instance (GHC.TypeNats.KnownNat n, NumHask.Array.Shape.KnownNats ns) => NumHask.Array.Shape.KnownNats (n : ns) instance NumHask.Array.Shape.HasShape '[] instance (GHC.TypeNats.KnownNat n, NumHask.Array.Shape.HasShape s) => NumHask.Array.Shape.HasShape (n : s) module NumHask.Array.HMatrix newtype Array s a Array :: Matrix a -> Array s a [unArray] :: Array s a -> Matrix a -- | explicit rather than via Representable index :: forall s a. (HasShape s, Element a, Container Vector a) => Array s a -> [Int] -> a -- | fast mmult :: forall m n k a. (KnownNat k, KnownNat m, KnownNat n, HasShape [m, n], Ring a, Numeric a) => Array [m, k] a -> Array [k, n] a -> Array [m, n] a instance forall k (s :: k) a. GHC.Generics.Generic (NumHask.Array.HMatrix.Array s a) instance forall k (s :: k) a. (Foreign.Storable.Storable a, Control.DeepSeq.NFData a) => Control.DeepSeq.NFData (NumHask.Array.HMatrix.Array s a) instance forall k (s :: k) a. (GHC.Show.Show a, Internal.Matrix.Element a) => GHC.Show.Show (NumHask.Array.HMatrix.Array s a) instance (NumHask.Algebra.Abstract.Additive.Additive a, NumHask.Array.Shape.HasShape s, Internal.Numeric.Container Data.Vector.Storable.Vector a, GHC.Num.Num a) => NumHask.Algebra.Abstract.Additive.Additive (NumHask.Array.HMatrix.Array s a) instance (NumHask.Algebra.Abstract.Multiplicative.Multiplicative a, NumHask.Array.Shape.HasShape s, Internal.Numeric.Container Data.Vector.Storable.Vector a, GHC.Num.Num (Data.Vector.Storable.Vector a), GHC.Num.Num a) => NumHask.Algebra.Abstract.Multiplicative.Multiplicative (NumHask.Array.HMatrix.Array s a) instance (NumHask.Array.Shape.HasShape s, NumHask.Algebra.Abstract.Ring.Distributive a, NumHask.Algebra.Abstract.Ring.CommutativeRing a, NumHask.Algebra.Abstract.Ring.Semiring a, Internal.Numeric.Container Data.Vector.Storable.Vector a, GHC.Num.Num (Data.Vector.Storable.Vector a), GHC.Num.Num a) => NumHask.Analysis.Banach.Hilbert (NumHask.Array.HMatrix.Array s a) instance (NumHask.Array.Shape.HasShape s, NumHask.Algebra.Abstract.Multiplicative.Multiplicative a, Internal.Numeric.Container Data.Vector.Storable.Vector a, GHC.Num.Num a) => NumHask.Algebra.Abstract.Action.MultiplicativeAction (NumHask.Array.HMatrix.Array s a) instance (NumHask.Array.Shape.HasShape s, NumHask.Algebra.Abstract.Additive.Additive a, Internal.Matrix.Element a) => GHC.Exts.IsList (NumHask.Array.HMatrix.Array s a) -- | Arrays with a dynamic shape module NumHask.Array.Dynamic -- | a multidimensional array with a value-level shape -- --
-- >>> a -- [[[1, 2, 3, 4], -- [5, 6, 7, 8], -- [9, 10, 11, 12]], -- [[13, 14, 15, 16], -- [17, 18, 19, 20], -- [21, 22, 23, 24]]] --data Array a Array :: [Int] -> Vector a -> Array a [shape] :: Array a -> [Int] [unArray] :: Array a -> Vector a -- | convert from a list -- --
-- >>> fromFlatList [2,3,4] [1..24] == a -- True --fromFlatList :: [Int] -> [a] -> Array a -- | reshape an array (with the same number of elements) -- --
-- >>> reshape [4,3,2] a -- [[[1, 2], -- [3, 4], -- [5, 6]], -- [[7, 8], -- [9, 10], -- [11, 12]], -- [[13, 14], -- [15, 16], -- [17, 18]], -- [[19, 20], -- [21, 22], -- [23, 24]]] --reshape :: [Int] -> Array a -> Array a -- | reverse indices eg transposes the element Aijk to Akji -- --
-- >>> index (transpose a) [1,0,0] == index a [0,0,1] -- True --transpose :: Array a -> Array a -- | extract the diagonal -- --
-- >>> diag (ident [3,2]) -- [1, 1] --diag :: Array a -> Array a -- | selects ds ps a select from a, elements along ds -- dimensions at positions ps -- --
-- >>> let s = selects [0,1] [1,1] a -- -- >>> s -- [17, 18, 19, 20] --selects :: [Int] -> [Int] -> Array a -> Array a -- | select an index except along dimensions -- --
-- >>> let s = selectsExcept [2] [1,1] a -- -- >>> s -- [17, 18, 19, 20] --selectsExcept :: [Int] -> [Int] -> Array a -> Array a -- | fold along specified dimensions -- --
-- >>> folds sum [1] a -- [68, 100, 132] --folds :: (Array a -> b) -> [Int] -> Array a -> Array b -- | extracts dimensions to an outer layer -- --
-- >>> let e = extracts [1,2] a -- -- >>> shape <$> extracts [0] a -- [[3,4], [3,4]] --extracts :: [Int] -> Array a -> Array (Array a) -- | join inner and outer dimension layers -- --
-- >>> let e = extracts [1,0] a -- -- >>> let j = joins [1,0] e -- -- >>> a == j -- True --joins :: [Int] -> Array (Array a) -> Array a -- | maps along specified dimensions -- --
-- >>> shape $ maps (transpose) [1] a -- [4,3,2] --maps :: (Array a -> Array b) -> [Int] -> Array a -> Array b -- | concatenate along a dimension -- --
-- >>> shape $ concatenate 1 a a -- [2,6,4] --concatenate :: Int -> Array a -> Array a -> Array a -- | insert d i insert along the dimension d at position -- i -- --
-- >>> insert 2 0 a (fromFlatList [2,3] [100..105]) -- [[[100, 1, 2, 3, 4], -- [101, 5, 6, 7, 8], -- [102, 9, 10, 11, 12]], -- [[103, 13, 14, 15, 16], -- [104, 17, 18, 19, 20], -- [105, 21, 22, 23, 24]]] --insert :: Int -> Int -> Array a -> Array a -> Array a -- | insert along a dimension at the end -- --
-- >>> append 2 a (fromFlatList [2,3] [100..105]) -- [[[1, 2, 3, 4, 100], -- [5, 6, 7, 8, 101], -- [9, 10, 11, 12, 102]], -- [[13, 14, 15, 16, 103], -- [17, 18, 19, 20, 104], -- [21, 22, 23, 24, 105]]] --append :: Int -> Array a -> Array a -> Array a -- | change the order of dimensions -- --
-- >>> let r = reorder [2,0,1] a -- -- >>> r -- [[[1, 5, 9], -- [13, 17, 21]], -- [[2, 6, 10], -- [14, 18, 22]], -- [[3, 7, 11], -- [15, 19, 23]], -- [[4, 8, 12], -- [16, 20, 24]]] --reorder :: [Int] -> Array a -> Array a -- | product two arrays using the supplied binary function If the function -- is multiply, and the arrays are tensors, then this can be interpreted -- as a tensor product. -- -- https://en.wikipedia.org/wiki/Tensor_product -- -- The concept of a tensor product is a dense crossroad, and a complete -- treatment is elsewhere. To quote: ... the tensor product can be -- extended to other categories of mathematical objects in addition to -- vector spaces, such as to matrices, tensors, algebras, topological -- vector spaces, and modules. In each such case the tensor product is -- characterized by a similar universal property: it is the freest -- bilinear operation. The general concept of a "tensor product" is -- captured by monoidal categories; that is, the class of all things that -- have a tensor product is a monoidal category. -- --
-- >>> expand (*) v v -- [[1, 2, 3], -- [2, 4, 6], -- [3, 6, 9]] --expand :: (a -> b -> c) -> Array a -> Array b -> Array c -- | contract an array by applying the supplied (folding) function on -- diagonal elements of the dimensions. -- -- This generalises a tensor contraction by allowing the number of -- contracting diagonals to be other than 2, and allowing another binary -- other than addition -- --
-- >>> let b = fromFlatList [2,3] [1..6] :: Array Int -- -- >>> contract sum [1,2] (expand (*) b (transpose b)) -- [[14, 32], -- [32, 77]] --contract :: (Array a -> b) -> [Int] -> Array a -> Array b -- | a generalisation of a dot operation, which is a multiplicative -- expansion of two arrays and sum contraction along the middle two -- dimensions. -- -- dot sum (*) on two matrices is known as matrix multiplication -- --
-- >>> let b = fromFlatList [2,3] [1..6] :: Array Int -- -- >>> dot sum (*) b (transpose b) -- [[14, 32], -- [32, 77]] ---- -- dot sum (*) on two vectors is known as the inner product -- --
-- >>> let v = fromFlatList [3] [1..3] :: Array Int -- -- >>> dot sum (*) v v -- 14 ---- -- dot sum (*) m v on a matrix and a vector is matrix-vector -- multiplication Note that an `Array Int` with shape [3] is neither a -- row vector nor column vector. dot is not turning the vector -- into a matrix and then using matrix multiplication. -- --
-- >>> dot sum (*) v b -- [9, 12, 15] ---- --
-- >>> dot sum (*) b v -- [14, 32] --dot :: (Array c -> d) -> (a -> b -> c) -> Array a -> Array b -> Array d -- | select elements along every dimension -- --
-- >>> let s = slice [[0,1],[0,2],[1,2]] a -- -- >>> s -- [[[2, 3], -- [10, 11]], -- [[14, 15], -- [22, 23]]] --slice :: [[Int]] -> Array a -> Array a -- | remove singleton dimensions -- --
-- >>> let a' = fromFlatList [2,1,3,4,1] [1..24] :: Array Int -- -- >>> shape $ squeeze a' -- [2,3,4] --squeeze :: Array a -> Array a -- |
-- >>> singleton [3,2] one -- [[1, 1], -- [1, 1], -- [1, 1]] --singleton :: [Int] -> a -> Array a -- |
-- >>> ident [3,2] -- [[1, 0], -- [0, 1], -- [0, 0]] --ident :: Num a => [Int] -> Array a -- | Wiki Scalar -- -- An Array with shape [] despite being a Scalar is nevertheless a -- one-element vector under the hood. -- -- unwrapping scalars is probably a performance bottleneck -- --
-- >>> let s = fromFlatList [] [3] :: Array Int -- -- >>> fromScalar s -- 3 --fromScalar :: Array a -> a -- | convert a number to a scalar -- --
-- >>> :t toScalar 2 -- toScalar 2 :: Num a => Array a --toScalar :: a -> Array a -- | extract specialised to a matrix -- --
-- >>> col 1 m -- [1, 5, 9] --col :: Int -> Array a -> Array a -- | extract specialised to a matrix -- --
-- >>> row 1 m -- [4, 5, 6, 7] --row :: Int -> Array a -> Array a -- | matrix multiplication -- -- This is dot sum (*) specialised to matrices -- --
-- >>> let a = fromFlatList [2,2] [1, 2, 3, 4] :: Array Int -- -- >>> let b = fromFlatList [2,2] [5, 6, 7, 8] :: Array Int -- -- >>> a -- [[1, 2], -- [3, 4]] ---- --
-- >>> b -- [[5, 6], -- [7, 8]] ---- --
-- >>> mmult a b -- [[19, 22], -- [43, 50]] --mmult :: Ring a => Array a -> Array a -> Array a instance GHC.Generics.Generic (NumHask.Array.Dynamic.Array a) instance Control.DeepSeq.NFData a => Control.DeepSeq.NFData (NumHask.Array.Dynamic.Array a) instance GHC.Classes.Ord a => GHC.Classes.Ord (NumHask.Array.Dynamic.Array a) instance GHC.Classes.Eq a => GHC.Classes.Eq (NumHask.Array.Dynamic.Array a) instance GHC.Base.Functor NumHask.Array.Dynamic.Array instance Data.Foldable.Foldable NumHask.Array.Dynamic.Array instance Data.Traversable.Traversable NumHask.Array.Dynamic.Array instance GHC.Show.Show a => GHC.Show.Show (NumHask.Array.Dynamic.Array a) -- | Arrays with a fixed shape. module NumHask.Array.Fixed -- | a multidimensional array with a type-level shape -- --
-- >>> a
-- [[[1, 2, 3, 4],
-- [5, 6, 7, 8],
-- [9, 10, 11, 12]],
-- [[13, 14, 15, 16],
-- [17, 18, 19, 20],
-- [21, 22, 23, 24]]]
--
-- >>> [1,2,3] :: Array '[2,2] Int
-- [[*** Exception: NumHaskException {errorMessage = "shape mismatch"}
--
newtype Array s a
Array :: Vector a -> Array s a
[unArray] :: Array s a -> Vector a
-- | use a dynamic array in a fixed context
--
-- -- >>> with (D.fromFlatList [2,3,4] [1..24]) (selects (Proxy :: Proxy '[0,1]) [1,1] :: Array '[2,3,4] Int -> Array '[4] Int) -- [17, 18, 19, 20] --with :: forall a r s. HasShape s => Array a -> (Array s a -> r) -> r -- | get shape of an Array as a value -- --
-- >>> shape a -- [2,3,4] --shape :: forall a s. HasShape s => Array s a -> [Int] -- | convert to a dynamic array with shape at the value level. toDynamic :: HasShape s => Array s a -> Array a -- | reshape an array (with the same number of elements) -- --
-- >>> reshape a :: Array '[4,3,2] Int -- [[[1, 2], -- [3, 4], -- [5, 6]], -- [[7, 8], -- [9, 10], -- [11, 12]], -- [[13, 14], -- [15, 16], -- [17, 18]], -- [[19, 20], -- [21, 22], -- [23, 24]]] --reshape :: forall a s s'. (Size s ~ Size s', HasShape s, HasShape s') => Array s a -> Array s' a -- | reverse indices eg transposes the element Aijk to Akji -- --
-- >>> index (transpose a) [1,0,0] == index a [0,0,1] -- True --transpose :: forall a s. (HasShape s, HasShape (Reverse s)) => Array s a -> Array (Reverse s) a -- |
-- >>> diag (ident :: Array '[3,2] Int) -- [1, 1] --diag :: forall a s. (HasShape s, HasShape '[Minimum s]) => Array s a -> Array '[Minimum s] a -- | selects ds ps a select from a elements ds -- dimensions at positions ps -- --
-- >>> let s = selects (Proxy :: Proxy '[0,1]) [1,1] a -- -- >>> :t s -- s :: Array '[4] Int ---- --
-- >>> s -- [17, 18, 19, 20] --selects :: forall ds s s' a. (HasShape s, HasShape ds, HasShape s', s' ~ DropIndexes s ds) => Proxy ds -> [Int] -> Array s a -> Array s' a -- | select an index except along dimensions -- --
-- >>> let s = selectsExcept (Proxy :: Proxy '[2]) [1,1] a -- -- >>> :t s -- s :: Array '[4] Int ---- --
-- >>> s -- [17, 18, 19, 20] --selectsExcept :: forall ds s s' a. (HasShape s, HasShape ds, HasShape s', s' ~ TakeIndexes s ds) => Proxy ds -> [Int] -> Array s a -> Array s' a -- | fold along specified dimensions -- --
-- >>> folds sum (Proxy :: Proxy '[1]) a -- [68, 100, 132] --folds :: forall ds st si so a b. (HasShape st, HasShape ds, HasShape si, HasShape so, si ~ DropIndexes st ds, so ~ TakeIndexes st ds) => (Array si a -> b) -> Proxy ds -> Array st a -> Array so b -- | extracts dimensions to an outer layer -- --
-- >>> let e = extracts (Proxy :: Proxy '[1,2]) a -- -- >>> :t e -- e :: Array '[3, 4] (Array '[2] Int) --extracts :: forall ds st si so a. (HasShape st, HasShape ds, HasShape si, HasShape so, si ~ DropIndexes st ds, so ~ TakeIndexes st ds) => Proxy ds -> Array st a -> Array so (Array si a) -- | join inner and outer dimension layers -- --
-- >>> let e = extracts (Proxy :: Proxy '[1,0]) a ---- --
-- >>> :t e -- e :: Array '[3, 2] (Array '[4] Int) ---- --
-- >>> let j = joins (Proxy :: Proxy '[1,0]) e ---- --
-- >>> :t j -- j :: Array '[2, 3, 4] Int ---- --
-- >>> a == j -- True --joins :: forall ds si st so a. (HasShape st, HasShape ds, st ~ AddIndexes si ds so, HasShape si, HasShape so) => Proxy ds -> Array so (Array si a) -> Array st a -- | maps along specified dimensions -- --
-- >>> :t maps (transpose) (Proxy :: Proxy '[1]) a -- maps (transpose) (Proxy :: Proxy '[1]) a :: Array '[4, 3, 2] Int --maps :: forall ds st st' si si' so a b. (HasShape st, HasShape st', HasShape ds, HasShape si, HasShape si', HasShape so, si ~ DropIndexes st ds, so ~ TakeIndexes st ds, st' ~ AddIndexes si' ds so, st ~ AddIndexes si ds so) => (Array si a -> Array si' b) -> Proxy ds -> Array st a -> Array st' b -- | concatenate along a dimension -- --
-- >>> :t concatenate (Proxy :: Proxy 1) a a -- concatenate (Proxy :: Proxy 1) a a :: Array '[2, 6, 4] Int --concatenate :: forall a s0 s1 d s. (CheckConcatenate d s0 s1 s, Concatenate d s0 s1 ~ s, HasShape s0, HasShape s1, HasShape s, KnownNat d) => Proxy d -> Array s0 a -> Array s1 a -> Array s a -- | insert (Proxy :: Proxy d) (Proxy :: Proxy i) insert along the -- dimension d at position i -- --
-- >>> insert (Proxy :: Proxy 2) (Proxy :: Proxy 0) a ([100..105]) -- [[[100, 1, 2, 3, 4], -- [101, 5, 6, 7, 8], -- [102, 9, 10, 11, 12]], -- [[103, 13, 14, 15, 16], -- [104, 17, 18, 19, 20], -- [105, 21, 22, 23, 24]]] --insert :: forall a s s' d i. (DropIndex s d ~ s', CheckInsert d i s, KnownNat i, KnownNat d, HasShape s, HasShape s', HasShape (Insert d s)) => Proxy d -> Proxy i -> Array s a -> Array s' a -> Array (Insert d s) a -- | insert along a dimension at the end -- --
-- >>> :t append (Proxy :: Proxy 0) a -- append (Proxy :: Proxy 0) a -- :: Array '[3, 4] Int -> Array '[3, 3, 4] Int --append :: forall a d s s'. (DropIndex s d ~ s', CheckInsert d (Dimension s d - 1) s, KnownNat (Dimension s d - 1), KnownNat d, HasShape s, HasShape s', HasShape (Insert d s)) => Proxy d -> Array s a -> Array s' a -> Array (Insert d s) a -- | change the order of dimensions -- --
-- >>> let r = reorder (Proxy :: Proxy '[2,0,1]) a -- -- >>> :t r -- r :: Array '[4, 2, 3] Int --reorder :: forall a ds s. (HasShape ds, HasShape s, HasShape (Reorder s ds), CheckReorder ds s) => Proxy ds -> Array s a -> Array (Reorder s ds) a -- | product two arrays using the supplied binary function If the function -- is multiply, and the arrays are tensors, then this can be interpreted -- as a tensor product. -- -- https://en.wikipedia.org/wiki/Tensor_product -- -- The concept of a tensor product is a dense crossroad, and a complete -- treatment is elsewhere. To quote: ... the tensor product can be -- extended to other categories of mathematical objects in addition to -- vector spaces, such as to matrices, tensors, algebras, topological -- vector spaces, and modules. In each such case the tensor product is -- characterized by a similar universal property: it is the freest -- bilinear operation. The general concept of a "tensor product" is -- captured by monoidal categories; that is, the class of all things that -- have a tensor product is a monoidal category. -- --
-- >>> expand (*) v v -- [[1, 2, 3], -- [2, 4, 6], -- [3, 6, 9]] --expand :: forall s s' a b c. (HasShape s, HasShape s', HasShape ((++) s s')) => (a -> b -> c) -> Array s a -> Array s' b -> Array ((++) s s') c -- | contract an array by applying the supplied (folding) function on -- diagonal elements of the dimensions. -- -- This generalises a tensor contraction by allowing the number of -- contracting diagonals to be other than 2, and allowing another binary -- other than addition -- --
-- >>> let b = [1..6] :: Array '[2,3] Int -- -- >>> contract sum (Proxy :: Proxy '[1,2]) (expand (*) b (transpose b)) -- [[14, 32], -- [32, 77]] --contract :: forall a b s ss s' ds. (KnownNat (Minimum (TakeIndexes s ds)), HasShape (TakeIndexes s ds), HasShape s, HasShape ds, HasShape ss, HasShape s', s' ~ DropIndexes s ds, ss ~ '[Minimum (TakeIndexes s ds)]) => (Array ss a -> b) -> Proxy ds -> Array s a -> Array s' b -- | a generalisation of a dot operation, which is a multiplicative -- expansion of two arrays and sum contraction along the middle two -- dimensions. -- -- dot sum (*) on two matrices is known as matrix multiplication -- --
-- >>> let b = [1..6] :: Array '[2,3] Int -- -- >>> dot sum (*) b (transpose b) -- [[14, 32], -- [32, 77]] ---- -- dot sum (*) on two vectors is known as the inner product -- --
-- >>> let v = [1..3] :: Array '[3] Int -- -- >>> :t dot sum (*) v v -- dot sum (*) v v :: Array '[] Int ---- --
-- >>> dot sum (*) v v -- 14 ---- -- dot sum (*) m v on a matrix and a vector is matrix-vector -- multiplication Note that an `Array '[3] Int` is neither a row vector -- nor column vector. dot is not turning the vector into a matrix -- and then using matrix multiplication. -- --
-- >>> dot sum (*) v b -- [9, 12, 15] ---- --
-- >>> dot sum (*) b v -- [14, 32] --dot :: forall a b c d sa sb s' ss se. (HasShape sa, HasShape sb, HasShape (sa ++ sb), se ~ TakeIndexes (sa ++ sb) '[Rank sa - 1, Rank sa], HasShape se, KnownNat (Minimum se), KnownNat (Rank sa - 1), KnownNat (Rank sa), ss ~ '[Minimum se], HasShape ss, s' ~ DropIndexes (sa ++ sb) '[Rank sa - 1, Rank sa], HasShape s') => (Array ss c -> d) -> (a -> b -> c) -> Array sa a -> Array sb b -> Array s' d -- | select elements along every dimension -- --
-- >>> let s = slice (Proxy :: Proxy '[[0,1],[0,2],[1,2]]) a -- -- >>> :t s -- s :: Array '[2, 2, 2] Int ---- --
-- >>> s -- [[[2, 3], -- [10, 11]], -- [[14, 15], -- [22, 23]]] ---- --
-- >>> let s = squeeze $ slice (Proxy :: Proxy '[ '[0], '[0], '[0]]) a -- -- >>> :t s -- s :: Array '[] Int ---- --
-- >>> s -- 1 --slice :: forall (pss :: [[Nat]]) s s' a. (HasShape s, HasShape s', KnownNatss pss, KnownNat (Rank pss), s' ~ Ranks pss) => Proxy pss -> Array s a -> Array s' a -- | remove singleton dimensions -- --
-- >>> let a = [1..24] :: Array '[2,1,3,4,1] Int -- -- >>> a -- [[[[[1], -- [2], -- [3], -- [4]], -- [[5], -- [6], -- [7], -- [8]], -- [[9], -- [10], -- [11], -- [12]]]], -- [[[[13], -- [14], -- [15], -- [16]], -- [[17], -- [18], -- [19], -- [20]], -- [[21], -- [22], -- [23], -- [24]]]]] -- -- >>> squeeze a -- [[[1, 2, 3, 4], -- [5, 6, 7, 8], -- [9, 10, 11, 12]], -- [[13, 14, 15, 16], -- [17, 18, 19, 20], -- [21, 22, 23, 24]]] ---- --
-- >>> squeeze ([1] :: Array '[1,1] Double) -- 1.0 --squeeze :: forall s t a. t ~ Squeeze s => Array s a -> Array t a -- |
-- >>> ident :: Array '[3,2] Int -- [[1, 0], -- [0, 1], -- [0, 0]] --ident :: forall a s. (HasShape s, Additive a, Multiplicative a) => Array s a -- |
-- >>> singleton one :: Array '[3,2] Int -- [[1, 1], -- [1, 1], -- [1, 1]] --singleton :: HasShape s => a -> Array s a -- | Wiki Scalar -- -- An Array '[] a despite being a Scalar is never-the-less a -- one-element vector under the hood. Unification of representation is -- unexplored. type Scalar a = Array ('[] :: [Nat]) a -- | unwrapping scalars is probably a performance bottleneck -- --
-- >>> let s = [3] :: Array ('[] :: [Nat]) Int
--
-- >>> fromScalar s
-- 3
--
fromScalar :: HasShape ('[] :: [Nat]) => Array ('[] :: [Nat]) a -> a
-- | convert a number to a scalar
--
-- -- >>> :t toScalar 2 -- toScalar 2 :: Num a => Array '[] a --toScalar :: HasShape ('[] :: [Nat]) => a -> Array ('[] :: [Nat]) a -- | Wiki Vector type Vector s a = Array '[s] a -- | Wiki Matrix type Matrix m n a = Array '[m, n] a -- | extract specialised to a matrix -- --
-- >>> col 1 m -- [1, 5, 9] --col :: forall m n a. (KnownNat m, KnownNat n, HasShape '[m, n]) => Int -> Matrix m n a -> Vector n a -- | extract specialised to a matrix -- --
-- >>> row 1 m -- [4, 5, 6, 7] --row :: forall m n a. (KnownNat m, KnownNat n, HasShape '[m, n]) => Int -> Matrix m n a -> Vector n a -- | column extraction checked at type level -- --
-- >>> safeCol (Proxy :: Proxy 1) m -- [1, 5, 9] ---- --
-- >>> safeCol (Proxy :: Proxy 4) m -- ... -- ... index outside range -- ... --safeCol :: forall m n a j. ( 'True ~ CheckIndex j n, KnownNat j, KnownNat m, KnownNat n, HasShape '[m, n]) => Proxy j -> Matrix m n a -> Vector n a -- | row extraction checked at type level -- --
-- >>> safeRow (Proxy :: Proxy 1) m -- [4, 5, 6, 7] ---- --
-- >>> safeRow (Proxy :: Proxy 3) m -- ... -- ... index outside range -- ... --safeRow :: forall m n a j. ( 'True ~ CheckIndex j m, KnownNat j, KnownNat m, KnownNat n, HasShape '[m, n]) => Proxy j -> Matrix m n a -> Vector n a -- | matrix multiplication -- -- This is dot sum (*) specialised to matrices -- --
-- >>> let a = [1, 2, 3, 4] :: Array '[2, 2] Int -- -- >>> let b = [5, 6, 7, 8] :: Array '[2, 2] Int -- -- >>> a -- [[1, 2], -- [3, 4]] ---- --
-- >>> b -- [[5, 6], -- [7, 8]] ---- --
-- >>> mmult a b -- [[19, 22], -- [43, 50]] --mmult :: forall m n k a. (KnownNat k, KnownNat m, KnownNat n, HasShape [m, n], Ring a) => Array [m, k] a -> Array [k, n] a -> Array [m, n] a instance forall k (s :: k). Data.Traversable.Traversable (NumHask.Array.Fixed.Array s) instance forall k (s :: k) a. GHC.Generics.Generic (NumHask.Array.Fixed.Array s a) instance forall k (s :: k). Data.Foldable.Foldable (NumHask.Array.Fixed.Array s) instance forall k (s :: k). GHC.Base.Functor (NumHask.Array.Fixed.Array s) instance forall k (s :: k) a. Control.DeepSeq.NFData a => Control.DeepSeq.NFData (NumHask.Array.Fixed.Array s a) instance forall k (s :: k) a. GHC.Classes.Ord a => GHC.Classes.Ord (NumHask.Array.Fixed.Array s a) instance forall k (s :: k) a. GHC.Classes.Eq a => GHC.Classes.Eq (NumHask.Array.Fixed.Array s a) instance (NumHask.Algebra.Abstract.Multiplicative.Multiplicative a, NumHask.Algebra.Abstract.Ring.Distributive a, NumHask.Algebra.Abstract.Additive.Subtractive a, GHC.TypeNats.KnownNat m, NumHask.Array.Shape.HasShape '[m, m]) => NumHask.Algebra.Abstract.Multiplicative.Multiplicative (NumHask.Array.Fixed.Matrix m m a) instance (NumHask.Array.Shape.HasShape s, GHC.Show.Show a) => GHC.Show.Show (NumHask.Array.Fixed.Array s a) instance NumHask.Array.Shape.HasShape s => Data.Distributive.Distributive (NumHask.Array.Fixed.Array s) instance NumHask.Array.Shape.HasShape s => Data.Functor.Rep.Representable (NumHask.Array.Fixed.Array s) instance (NumHask.Algebra.Abstract.Additive.Additive a, NumHask.Array.Shape.HasShape s) => NumHask.Algebra.Abstract.Additive.Additive (NumHask.Array.Fixed.Array s a) instance (NumHask.Algebra.Abstract.Additive.Subtractive a, NumHask.Array.Shape.HasShape s) => NumHask.Algebra.Abstract.Additive.Subtractive (NumHask.Array.Fixed.Array s a) instance (NumHask.Algebra.Abstract.Multiplicative.Multiplicative a, NumHask.Array.Shape.HasShape s) => NumHask.Algebra.Linear.Hadamard.HadamardMultiplication (NumHask.Array.Fixed.Array s) a instance (NumHask.Algebra.Abstract.Multiplicative.Divisive a, NumHask.Array.Shape.HasShape s) => NumHask.Algebra.Linear.Hadamard.HadamardDivision (NumHask.Array.Fixed.Array s) a instance (NumHask.Array.Shape.HasShape s, NumHask.Algebra.Abstract.Multiplicative.Multiplicative a) => NumHask.Algebra.Abstract.Action.MultiplicativeAction (NumHask.Array.Fixed.Array s a) instance (NumHask.Array.Shape.HasShape s, NumHask.Algebra.Abstract.Lattice.JoinSemiLattice a) => NumHask.Algebra.Abstract.Lattice.JoinSemiLattice (NumHask.Array.Fixed.Array s a) instance (NumHask.Array.Shape.HasShape s, NumHask.Algebra.Abstract.Lattice.MeetSemiLattice a) => NumHask.Algebra.Abstract.Lattice.MeetSemiLattice (NumHask.Array.Fixed.Array s a) instance (NumHask.Array.Shape.HasShape s, NumHask.Algebra.Abstract.Additive.Subtractive a, NumHask.Analysis.Metric.Epsilon a) => NumHask.Analysis.Metric.Epsilon (NumHask.Array.Fixed.Array s a) instance NumHask.Array.Shape.HasShape s => GHC.Exts.IsList (NumHask.Array.Fixed.Array s a) -- | Higher-kinded numbers that can be represented with an [Int] index into -- elements. module NumHask.Array