Copyright | (c) 2018 Daniel YU |
---|---|
License | BSD3 |
Maintainer | Daniel YU <leptonyu@gmail.com> |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
Tensor In Haskell
In ghci
λ> :set -XDataKinds λ> :set -XOverloadedLists λ> import Data.Tensor λ> a = identity :: Tensor '[3,3] Int λ> a [[1,0,0], [0,1,0], [0,0,1]] λ> b = [1..9] :: Tensor '[3,3] Int λ> b [[1,2,3], [4,5,6], [7,8,9]] λ> a + b [[2,2,3], [4,6,6], [7,8,10]] λ> a - b [[0,-2,-3], [-4,-4,-6], [-7,-8,-8]] λ> a * b [[1,0,0], [0,5,0], [0,0,9]] λ> a `dot` b [[1,2,3], [4,5,6], [7,8,9]] λ> :t a `dyad` b a `dyad` b :: Tensor '[3, 3, 3, 3] Int λ> contraction a (i0,i1) 3 λ> :t contraction a (i0,i1) contraction a (i0,i1) :: Tensor '[] Int λ> select a (i0,i0) [1,0,0] λ> select a (i0,i1) [0,1,0] λ> select a (i0,i2) [0,0,1] λ> c = 1 :: Tensor '[3,3] Int λ> c [[1,1,1], [1,1,1], [1,1,1]] λ> d = [1..4] :: Tensor '[2,2] Int λ> d [[1,2], [3,4]] λ> transpose d [[1,3], [2,4]]
Synopsis
- data Tensor (s :: [Nat]) n
- identity :: forall s n. (HasShape s, Num n) => Tensor s n
- type Scalar n = Tensor '[] n
- type Vector s n = Tensor '[s] n
- type Matrix a b n = Tensor '[a, b] n
- type SimpleTensor (r :: Nat) (dim :: Nat) n = Tensor (Replicate r dim) n
- data TensorIndex (shape :: [Nat])
- type Index = [Int]
- type family TensorRank (s :: [Nat]) :: Nat where ...
- type Shape = [Int]
- shape :: forall s n. HasShape s => Tensor s n -> [Int]
- rank :: forall s n. HasShape s => Tensor s n -> Int
- class HasShape s where
- reshape :: (TensorSize s ~ TensorSize s', HasShape s) => Tensor s n -> Tensor s' n
- clone :: HasShape s => Tensor s n -> Tensor s n
- type Transpose (a :: [Nat]) = Reverse a '[]
- transpose :: HasShape a => Tensor a n -> Tensor (Transpose a) n
- type Swapaxes i j s = (Take i s ++ (Dimension s j ': Drop i (Take j s))) ++ (Dimension s j ': Tail (Drop j s))
- swapaxes :: (CheckIndices i j s, HasShape s, KnownNat i, KnownNat j) => Proxy i -> Proxy j -> Tensor s n -> Tensor (Swapaxes i j s) n
- dyad' :: (r ~ (s ++ t), HasShape s, HasShape t, HasShape r) => (n -> m -> o) -> Tensor s n -> Tensor t m -> Tensor r o
- dyad :: (r ~ (s ++ t), HasShape s, HasShape t, HasShape r, Num n, Eq n) => Tensor s n -> Tensor t n -> Tensor r n
- type DotTensor s1 s2 = Init s1 ++ Init s2
- dot :: (Last s ~ Head s', r ~ DotTensor s s', HasShape s, HasShape s', HasShape r, Num n, Eq n) => Tensor s n -> Tensor s' n -> Tensor r n
- type Contraction s x y = DropIndex (DropIndex s y) x
- type family Dimension (s :: [Nat]) (i :: Nat) :: Nat where ...
- type DropIndex s i = Take i s ++ Drop (i + 1) s
- contraction :: forall x y s s' n. (CheckIndices x y s, s' ~ Contraction s x y, Dimension s x ~ Dimension s y, KnownNat x, KnownNat y, HasShape s, HasShape s', KnownNat (Dimension s x), Num n) => (Proxy x, Proxy y) -> Tensor s n -> Tensor s' n
- (!) :: HasShape s => Tensor s n -> TensorIndex s -> n
- type CheckDimension dim s = IsIndex dim (TensorRank s)
- type CheckSelect dim i s = (CheckDimension dim s && IsIndex i (Dimension s dim)) ~ True
- select :: (CheckSelect dim i s, HasShape s, KnownNat dim, KnownNat i) => (Proxy dim, Proxy i) -> Tensor s n -> Tensor (DropIndex s dim) n
- type CheckSlice dim from to s = (CheckDimension dim s && IsIndices from to (Dimension s dim)) ~ True
- type Slice dim from to s = Take dim s ++ ((to - from) ': Tail (Drop dim s))
- slice :: (CheckSlice dim from to s, s' ~ Slice dim from to s, KnownNat dim, KnownNat from, KnownNat (to - from), HasShape s) => (Proxy dim, (Proxy from, Proxy to)) -> Tensor s n -> Tensor s' n
- expand :: (TensorRank s ~ TensorRank s', HasShape s) => Tensor s n -> Tensor s' n
- type CheckConcatenate i a b = IsIndex i (TensorRank a) ~ True
- type Concatenate i a b = Take i a ++ ((Dimension a i + Dimension b i) ': Drop (i + 1) a)
- concatenate :: (TensorRank a ~ TensorRank b, DropIndex a i ~ DropIndex b i, CheckConcatenate i a b, Concatenate i a b ~ c, HasShape a, HasShape b, KnownNat i) => Proxy i -> Tensor a n -> Tensor b n -> Tensor c n
- type CheckInsert dim i b = (CheckDimension dim b && IsIndex i (Dimension b dim)) ~ True
- type Insert dim b = Take dim b ++ ((Dimension b dim + 1) ': Drop (dim + 1) b)
- insert :: (DropIndex b dim ~ a, CheckInsert dim i b, KnownNat i, KnownNat dim, HasShape a, HasShape b) => Proxy dim -> Proxy i -> Tensor a n -> Tensor b n -> Tensor (Insert dim b) n
- append :: forall dim a b n. (DropIndex b dim ~ a, CheckInsert dim (Dimension b dim) b, KnownNat (Dimension b dim), KnownNat dim, HasShape a, HasShape b) => Proxy dim -> Tensor a n -> Tensor b n -> Tensor (Insert dim b) n
- linspace :: forall s n. (KnownNat s, Fractional n) => n -> n -> Vector s n
- geospace :: forall s n. (KnownNat s, Floating n, Eq n) => n -> n -> Vector s n
- logspace :: forall s n. (KnownNat s, Floating n, Eq n) => n -> n -> n -> Vector s n
- grid :: (CheckDimension i s ~ True, KnownNat i, KnownNat (Dimension s i)) => Proxy i -> Vector (Dimension s i) n -> Tensor s n
- meshgrid2 :: (s ~ '[a, b], KnownNat a, KnownNat b) => Vector a n -> Vector b n -> [Tensor s n]
- meshgrid3 :: (s ~ '[a, b, c], KnownNat a, KnownNat b, KnownNat c) => Vector a n -> Vector b n -> Vector c n -> [Tensor s n]
- meshgrid4 :: (s ~ '[a, b, c, d], KnownNat a, KnownNat b, KnownNat c, KnownNat d) => Vector a n -> Vector b n -> Vector c n -> Vector d n -> [Tensor s n]
- meshgrid5 :: (s ~ '[a, b, c, d, e], KnownNat a, KnownNat b, KnownNat c, KnownNat d, KnownNat e) => Vector a n -> Vector b n -> Vector c n -> Vector d n -> Vector e n -> [Tensor s n]
- meshgrid6 :: (s ~ '[a, b, c, d, e, f], KnownNat a, KnownNat b, KnownNat c, KnownNat d, KnownNat e, KnownNat f) => Vector a n -> Vector b n -> Vector c n -> Vector d n -> Vector e n -> Vector f n -> [Tensor s n]
- meshgrid7 :: (s ~ '[a, b, c, d, e, f, g], KnownNat a, KnownNat b, KnownNat c, KnownNat d, KnownNat e, KnownNat f, KnownNat g) => Vector a n -> Vector b n -> Vector c n -> Vector d n -> Vector e n -> Vector f n -> Vector g n -> [Tensor s n]
- meshgrid8 :: (s ~ '[a, b, c, d, e, f, g, h], KnownNat a, KnownNat b, KnownNat c, KnownNat d, KnownNat e, KnownNat f, KnownNat g, KnownNat h) => Vector a n -> Vector b n -> Vector c n -> Vector d n -> Vector e n -> Vector f n -> Vector g n -> Vector h n -> [Tensor s n]
- meshgrid9 :: (s ~ '[a, b, c, d, e, f, g, h, i], KnownNat a, KnownNat b, KnownNat c, KnownNat d, KnownNat e, KnownNat f, KnownNat g, KnownNat h, KnownNat i) => Vector a n -> Vector b n -> Vector c n -> Vector d n -> Vector e n -> Vector f n -> Vector g n -> Vector h n -> Vector i n -> [Tensor s n]
- type SimpleMatrix a n = Matrix a a n
- det :: forall a n. (KnownNat a, Num n, Eq n) => SimpleMatrix a n -> n
- lu :: forall a n. (KnownNat a, Integral n) => SimpleMatrix a n -> (SimpleMatrix a (Ratio n), SimpleMatrix a (Ratio n), SimpleMatrix a n)
- det' :: forall a n. (KnownNat a, Integral n) => SimpleMatrix a n -> n
- dotM :: (KnownNat a, Num n, Eq n) => SimpleMatrix a n -> SimpleMatrix a n -> SimpleMatrix a n
- trace :: (KnownNat a, Num n) => SimpleMatrix a n -> n
- diag :: SimpleMatrix a n -> Vector a n
- average :: forall s n. (HasShape s, Fractional n) => Tensor s n -> n
- var :: forall s n. (HasShape s, Fractional n) => Tensor s n -> n
- std :: forall s n. (HasShape s, Floating n) => Tensor s n -> n
- runTensor :: HasShape s => Tensor s n -> Index -> n
- i0 :: Proxy 0
- i1 :: Proxy 1
- i2 :: Proxy 2
- i3 :: Proxy 3
- i4 :: Proxy 4
- i5 :: Proxy 5
- i6 :: Proxy 6
- i7 :: Proxy 7
- i8 :: Proxy 8
- i9 :: Proxy 9
Tensor Definition
data Tensor (s :: [Nat]) n Source #
Definition of Tensor.
s
means shape of tensor.
identity :: Tensor '[3,3] Int
Instances
Functor (Tensor s) Source # | |
Applicative (Tensor s) Source # | |
HasShape s => Foldable (Tensor s) Source # | |
Defined in Data.Tensor.Tensor fold :: Monoid m => Tensor s m -> m # foldMap :: Monoid m => (a -> m) -> Tensor s a -> m # foldr :: (a -> b -> b) -> b -> Tensor s a -> b # foldr' :: (a -> b -> b) -> b -> Tensor s a -> b # foldl :: (b -> a -> b) -> b -> Tensor s a -> b # foldl' :: (b -> a -> b) -> b -> Tensor s a -> b # foldr1 :: (a -> a -> a) -> Tensor s a -> a # foldl1 :: (a -> a -> a) -> Tensor s a -> a # elem :: Eq a => a -> Tensor s a -> Bool # maximum :: Ord a => Tensor s a -> a # minimum :: Ord a => Tensor s a -> a # | |
HasShape s => IsList (Tensor s n) Source # | |
(HasShape s, Eq n) => Eq (Tensor s n) Source # | |
(HasShape s, Floating n) => Floating (Tensor s n) Source # | |
Defined in Data.Tensor.Tensor exp :: Tensor s n -> Tensor s n # log :: Tensor s n -> Tensor s n # sqrt :: Tensor s n -> Tensor s n # (**) :: Tensor s n -> Tensor s n -> Tensor s n # logBase :: Tensor s n -> Tensor s n -> Tensor s n # sin :: Tensor s n -> Tensor s n # cos :: Tensor s n -> Tensor s n # tan :: Tensor s n -> Tensor s n # asin :: Tensor s n -> Tensor s n # acos :: Tensor s n -> Tensor s n # atan :: Tensor s n -> Tensor s n # sinh :: Tensor s n -> Tensor s n # cosh :: Tensor s n -> Tensor s n # tanh :: Tensor s n -> Tensor s n # asinh :: Tensor s n -> Tensor s n # acosh :: Tensor s n -> Tensor s n # atanh :: Tensor s n -> Tensor s n # log1p :: Tensor s n -> Tensor s n # expm1 :: Tensor s n -> Tensor s n # | |
(HasShape s, Fractional n) => Fractional (Tensor s n) Source # | |
(HasShape s, Num n) => Num (Tensor s n) Source # | |
Defined in Data.Tensor.Tensor | |
(HasShape s, Show n) => Show (Tensor s n) Source # | |
(HasShape s, NFData a) => NFData (Tensor s a) Source # | |
Defined in Data.Tensor.Tensor | |
type Item (Tensor s n) Source # | |
Defined in Data.Tensor.Tensor |
identity :: forall s n. (HasShape s, Num n) => Tensor s n Source #
Unit tensor of shape s, if all the indices are equal then return 1, otherwise return 0.
type SimpleTensor (r :: Nat) (dim :: Nat) n = Tensor (Replicate r dim) n Source #
Simple Tensor is rank r
tensor, has `n^r` dimension in total.
SimpleTensor 2 3 Int == Matrix 3 3 Int == Tensor '[3,3] Int SimpleTensor r 0 Int == Scalar Int
Tensor Index
data TensorIndex (shape :: [Nat]) Source #
Tensor Index, used to locate each point of tensor
Instances
Tensor Dimension
type family TensorRank (s :: [Nat]) :: Nat where ... Source #
Tensor rank.
TensorRank '[] = 0 | |
TensorRank (_ ': s) = TensorRank s + 1 |
shape :: forall s n. HasShape s => Tensor s n -> [Int] Source #
Shape of Tensor, is a list of integers, uniquely determine the shape of tensor.
Tensor Operation
Reshape Tensor
reshape :: (TensorSize s ~ TensorSize s', HasShape s) => Tensor s n -> Tensor s' n Source #
Reshape a tensor to another tensor, with total dimensions are equal.
Clone Tensor
Transpose Tensor
transpose :: HasShape a => Tensor a n -> Tensor (Transpose a) n Source #
Transpose tensor completely
λ> a = [1..9] :: Tensor '[3,3] Int λ> a [[1,2,3], [4,5,6], [7,8,9]] λ> transpose a [[1,4,7], [2,5,8], [3,6,9]]
type Swapaxes i j s = (Take i s ++ (Dimension s j ': Drop i (Take j s))) ++ (Dimension s j ': Tail (Drop j s)) Source #
swapaxes :: (CheckIndices i j s, HasShape s, KnownNat i, KnownNat j) => Proxy i -> Proxy j -> Tensor s n -> Tensor (Swapaxes i j s) n Source #
Swapaxes any rank
λ> a = [1..24] :: Tensor '[2,3,4] 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]]] λ> swapaxes i0 i1 a [[[1,2,3,4], [13,14,15,16]], [[5,6,7,8], [17,18,19,20]], [[9,10,11,12], [21,22,23,24]]] λ> :t swapaxes i0 i1 a swapaxes i0 i1 a :: Tensor '[3, 2, 4] Int λ> :t swapaxes i1 i2 a swapaxes i1 i2 a :: Tensor '[2, 4, 3] Int
In rank 2 tensor, swapaxes
is just transpose
transpose == swapaxes i0 i1
Dyadic Tensor
dyad' :: (r ~ (s ++ t), HasShape s, HasShape t, HasShape r) => (n -> m -> o) -> Tensor s n -> Tensor t m -> Tensor r o Source #
dyad :: (r ~ (s ++ t), HasShape s, HasShape t, HasShape r, Num n, Eq n) => Tensor s n -> Tensor t n -> Tensor r n Source #
λ> a = [1..4] :: Tensor '[2,2] Int λ> a [[1,2], [3,4]] λ> :t a `dyad` a a `dyad` a :: Tensor '[2, 2, 2, 2] Int λ> a `dyad` a [[[[1,2], [3,4]], [[2,4], [6,8]]], [[[3,6], [9,12]], [[4,8], [12,16]]]]
Tensor Product
dot :: (Last s ~ Head s', r ~ DotTensor s s', HasShape s, HasShape s', HasShape r, Num n, Eq n) => Tensor s n -> Tensor s' n -> Tensor r n Source #
Tensor Product
λ> a = [1..4] :: Tensor '[2,2] Int λ> a [[1,2], [3,4]] λ> a `dot` a [[7,10], [15,22]]
dot a b == contraction (dyad a b) (rank a - 1, rank a)
For rank 2 tensor, it is just matrix product.
Contraction Tensor
type Contraction s x y = DropIndex (DropIndex s y) x Source #
contraction :: forall x y s s' n. (CheckIndices x y s, s' ~ Contraction s x y, Dimension s x ~ Dimension s y, KnownNat x, KnownNat y, HasShape s, HasShape s', KnownNat (Dimension s x), Num n) => (Proxy x, Proxy y) -> Tensor s n -> Tensor s' n Source #
Contraction Tensor
λ> a = [1..16] :: Tensor '[4,4] Int λ> a [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]] λ> contraction (i0,i1) a 34
In rank 2 tensor, contraction of tensor is just the trace.
Tensor Selection
type CheckDimension dim s = IsIndex dim (TensorRank s) Source #
type CheckSelect dim i s = (CheckDimension dim s && IsIndex i (Dimension s dim)) ~ True Source #
select :: (CheckSelect dim i s, HasShape s, KnownNat dim, KnownNat i) => (Proxy dim, Proxy i) -> Tensor s n -> Tensor (DropIndex s dim) n Source #
Select i
indexing of tensor
λ> a = identity :: Tensor '[4,4] Int λ> select (i0,i0) a [1,0,0,0] λ> select (i0,i1) a [0,1,0,0]
type CheckSlice dim from to s = (CheckDimension dim s && IsIndices from to (Dimension s dim)) ~ True Source #
slice :: (CheckSlice dim from to s, s' ~ Slice dim from to s, KnownNat dim, KnownNat from, KnownNat (to - from), HasShape s) => (Proxy dim, (Proxy from, Proxy to)) -> Tensor s n -> Tensor s' n Source #
Slice tensor
λ> a = identity :: Tensor '[4,4] Int λ> a [[1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1]] λ> slice (i0,(i1,i3)) a [[0,1,0,0], [0,0,1,0]] λ> slice (i1,(i1,i3)) a [[0,0], [1,0], [0,1], [0,0]]
Tensor Manipulation
expand :: (TensorRank s ~ TensorRank s', HasShape s) => Tensor s n -> Tensor s' n Source #
Expand tensor
λ> a = identity :: Tensor '[2,2] Int λ> a [[1,0], [0,1]] λ> expand a :: Tensor '[4,4] Int [[1,0,1,0], [0,1,0,1], [1,0,1,0], [0,1,0,1]]
type CheckConcatenate i a b = IsIndex i (TensorRank a) ~ True Source #
concatenate :: (TensorRank a ~ TensorRank b, DropIndex a i ~ DropIndex b i, CheckConcatenate i a b, Concatenate i a b ~ c, HasShape a, HasShape b, KnownNat i) => Proxy i -> Tensor a n -> Tensor b n -> Tensor c n Source #
Join a sequence of arrays along an existing axis.
λ> a = [1..4] :: Tensor '[2,2] Int λ> a [[1,2], [3,4]] λ> b = [1,1,1,1] :: Tensor '[2,2] Int λ> b [[1,1], [1,1]] λ> concentrate i0 a b [[1,2], [3,4], [1,1], [1,1]] λ> concentrate i1 a b [[1,2,1,1], [3,4,1,1]]
type CheckInsert dim i b = (CheckDimension dim b && IsIndex i (Dimension b dim)) ~ True Source #
insert :: (DropIndex b dim ~ a, CheckInsert dim i b, KnownNat i, KnownNat dim, HasShape a, HasShape b) => Proxy dim -> Proxy i -> Tensor a n -> Tensor b n -> Tensor (Insert dim b) n Source #
Insert tensor to higher level tensor
λ> a = [1,2] :: Vector 2 Float λ> b = a `dyad` a λ> b [[1.0,2.0], [2.0,4.0]] λ> :t b b :: Tensor '[2, 2] Float λ> c = [1..4] :: Tensor '[1,2,2] Float λ> c [[[1.0,2.0], [3.0,4.0]]] λ> d = insert i0 i0 b c λ> :t d d :: Tensor '[2, 2, 2] Float λ> d [[[1.0,2.0], [2.0,4.0]], [[1.0,2.0], [3.0,4.0]]] λ> insert i0 i1 b c [[[1.0,2.0], [3.0,4.0]], [[1.0,2.0], [2.0,4.0]]]
append :: forall dim a b n. (DropIndex b dim ~ a, CheckInsert dim (Dimension b dim) b, KnownNat (Dimension b dim), KnownNat dim, HasShape a, HasShape b) => Proxy dim -> Tensor a n -> Tensor b n -> Tensor (Insert dim b) n Source #
Append tensor to the end of some dimension of other tensor
λ> a = [1,2] :: Vector 2 Float λ> a [1.0,2.0] λ> b = 3 :: Tensor '[] Float λ> b 3.0 λ> append i0 b a [1.0,2.0,3.0]
Tensor Space
linspace :: forall s n. (KnownNat s, Fractional n) => n -> n -> Vector s n Source #
Return evenly spaced numbers over a specified interval.
λ> linspace 1 10 :: Tensor '[10] Float [1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,..,10.0]
geospace :: forall s n. (KnownNat s, Floating n, Eq n) => n -> n -> Vector s n Source #
Return numbers spaced evenly on a log scale (a geometric progression).
λ> geospace 1 10 :: Vector 10 Float [1.0,1.2915497,1.6681006,2.1544347,2.7825596,3.593814,4.641589,5.994843,..,10.000001]
logspace :: forall s n. (KnownNat s, Floating n, Eq n) => n -> n -> n -> Vector s n Source #
Return numbers spaced evenly on a log scale.
In linear space, the sequence starts at base ** start (base to the power of start) and ends with base ** stop
λ> logspace 10 2 3 :: Vector 4 Float [100.0,215.44347,464.15887,1000.0]
grid :: (CheckDimension i s ~ True, KnownNat i, KnownNat (Dimension s i)) => Proxy i -> Vector (Dimension s i) n -> Tensor s n Source #
Grid
λ> a = [1..2] :: Vector 2 Int λ> a [1,2] λ> grid i0 a :: Tensor '[2,2,2] Int [[[1,1], [1,1]], [[2,2], [2,2]]] λ> grid i1 a :: Tensor '[2,2,2] Int [[[1,1], [2,2]], [[1,1], [2,2]]] λ> grid i2 a :: Tensor '[2,2,2] Int [[[1,2], [1,2]], [[1,2], [1,2]]]
meshgrid2 :: (s ~ '[a, b], KnownNat a, KnownNat b) => Vector a n -> Vector b n -> [Tensor s n] Source #
Generate 2 dimension grid coordinates by two vectors.
meshgrid3 :: (s ~ '[a, b, c], KnownNat a, KnownNat b, KnownNat c) => Vector a n -> Vector b n -> Vector c n -> [Tensor s n] Source #
Generate 3 dimension grid coordinates by three vectors.
meshgrid4 :: (s ~ '[a, b, c, d], KnownNat a, KnownNat b, KnownNat c, KnownNat d) => Vector a n -> Vector b n -> Vector c n -> Vector d n -> [Tensor s n] Source #
Generate 4 dimension grid coordinates by four vectors.
meshgrid5 :: (s ~ '[a, b, c, d, e], KnownNat a, KnownNat b, KnownNat c, KnownNat d, KnownNat e) => Vector a n -> Vector b n -> Vector c n -> Vector d n -> Vector e n -> [Tensor s n] Source #
Generate 5 dimension grid coordinates by five vectors.
meshgrid6 :: (s ~ '[a, b, c, d, e, f], KnownNat a, KnownNat b, KnownNat c, KnownNat d, KnownNat e, KnownNat f) => Vector a n -> Vector b n -> Vector c n -> Vector d n -> Vector e n -> Vector f n -> [Tensor s n] Source #
Generate 6 dimension grid coordinates by six vectors.
meshgrid7 :: (s ~ '[a, b, c, d, e, f, g], KnownNat a, KnownNat b, KnownNat c, KnownNat d, KnownNat e, KnownNat f, KnownNat g) => Vector a n -> Vector b n -> Vector c n -> Vector d n -> Vector e n -> Vector f n -> Vector g n -> [Tensor s n] Source #
Generate 7 dimension grid coordinates by seven vectors.
meshgrid8 :: (s ~ '[a, b, c, d, e, f, g, h], KnownNat a, KnownNat b, KnownNat c, KnownNat d, KnownNat e, KnownNat f, KnownNat g, KnownNat h) => Vector a n -> Vector b n -> Vector c n -> Vector d n -> Vector e n -> Vector f n -> Vector g n -> Vector h n -> [Tensor s n] Source #
Generate 8 dimension grid coordinates by eight vectors.
meshgrid9 :: (s ~ '[a, b, c, d, e, f, g, h, i], KnownNat a, KnownNat b, KnownNat c, KnownNat d, KnownNat e, KnownNat f, KnownNat g, KnownNat h, KnownNat i) => Vector a n -> Vector b n -> Vector c n -> Vector d n -> Vector e n -> Vector f n -> Vector g n -> Vector h n -> Vector i n -> [Tensor s n] Source #
Generate 9 dimension grid coordinates by nine vectors.
Matrix Operation
type SimpleMatrix a n = Matrix a a n Source #
det :: forall a n. (KnownNat a, Num n, Eq n) => SimpleMatrix a n -> n Source #
Determinant of n x n matrix
λ> a = [2,0,1,3,0,0,2,2,1,2,1,34,3,2,34,4] :: Tensor '[4,4] Int λ> a [[2,0,1,3], [0,0,2,2], [1,2,1,34], [3,2,34,4]] λ> det a 520
This implementation is not so fast, it can calculate 8 x 8 in 1 second with all the num none zero on my computer. It should be faster if more zero in the matrix.
lu :: forall a n. (KnownNat a, Integral n) => SimpleMatrix a n -> (SimpleMatrix a (Ratio n), SimpleMatrix a (Ratio n), SimpleMatrix a n) Source #
LU decomposition of n x n matrix
λ> a = [1,2,3,2,5,7,3,5,3]:: Tensor '[3,3] Int λ> (l,u,p) = lu a λ> l [[1 % 1,0 % 1,0 % 1], [2 % 1,1 % 1,0 % 1], [3 % 1,(-1) % 1,1 % 1]] λ> u [[1 % 1,2 % 1,3 % 1], [0 % 1,1 % 1,1 % 1], [0 % 1,0 % 1,(-5) % 1]] λ> p [[1,0,0], [0,1,0], [0,0,1]]
det' :: forall a n. (KnownNat a, Integral n) => SimpleMatrix a n -> n Source #
determiant using lu
decomposition
dotM :: (KnownNat a, Num n, Eq n) => SimpleMatrix a n -> SimpleMatrix a n -> SimpleMatrix a n Source #
diag :: SimpleMatrix a n -> Vector a n Source #
Statistical Functions
average :: forall s n. (HasShape s, Fractional n) => Tensor s n -> n Source #
Average of tensor
λ> average (identity :: Tensor '[3,3] Float) 0.33333334
var :: forall s n. (HasShape s, Fractional n) => Tensor s n -> n Source #
Variance of tensor
λ> var ([1,2,3,4] :: Vector 4 Double ) 1.25
std :: forall s n. (HasShape s, Floating n) => Tensor s n -> n Source #
Standard Deviation of tensor
λ> std ([1,2,3,4] :: Vector 4 Double ) 1.118033988749895