MyNixOS website logo
Description

typesafe tensor algebra library.

This package is intended to be used as a general purpose tensor algebra library. It defines the usual tensor algebra functions such as addition, scalar multiplication, tensor product, and contractions, but also general symmetrizations and further utility functions.

The implemented tensor data type is capable of being used with an arbitrary number of general abstract indices and can incorporate values of any type that allow for a meaningful addition, scaling, and multiplication. The package is thus very flexible and can easily be customised at wish.

The sparse-tensor Package

Hackage-Deps Hackage Build Status

sparse-tensor is a Haskell tensor algebra library. It defines the usual tensor algebra functions such as

  • addition
result = t1 &+ t2
  • scalar multiplication
result = s &. t
  • tensor product
result = t1 &* t2
  • or symmetrizations
result = symTens (0,1) t -- symmetrization in first two indices

The Tensor type

Tensor types can be defined with any value type and index types. For example, a tensor type with n contravariant and m covariant 4-d spacetime indices ranging from 0 to 3 and Rational values can be defined as

type MyTensor n m  = AbsTensor2 n m Ind3 (SField Rational)

The operations on tensors are type-safe, for example it is not possible to add two tensors of different rank,

>>> :set -XDataKinds
>>> (undefined :: MyTensor 0 1) &+ (undefined :: MyTensor 0 2)
<interactive>:3:33: error:
    • Couldn't match type ‘2’ with ‘1’
      [...]

as this causes a type error at compile time.

Predefined tensors

The package comes with pre-defined tensor types. Basic tensors of these types for applications in mathematical physics are exported by Math.Tensor.Examples.Gravity:

>>> sequence_ $ fmap print $ toListT2' delta3  -- print assocs of spacetime delta
(([0],[0]),SField (1 % 1))
(([1],[1]),SField (1 % 1))
(([2],[2]),SField (1 % 1))
(([3],[3]),SField (1 % 1))

>>> sequence_ $ fmap print $ toListT2' eta     -- print assocs of Minkowski metric
(([],[0,0]),SField ((-1) % 1))
(([],[1,1]),SField (1 % 1))
(([],[2,2]),SField (1 % 1))
(([],[3,3]),SField (1 % 1))

>>> let t = invEta &* epsilon
>>> contrATens1 (0,0) $ contrATens1 (1,1) t   -- contraction of inverse eta with epsilon
ZeroTensor

It is of course possible to define further custom tensor types and tensors.

Math.Tensor.LorentzGenerator exports functionality to generate a basis for the space of Lorentz-invariant tensors of certain rank which obey certain symmetries.

Automatic differentiation

sparse-tensor also supports tensors with functions as values. For such tensors, the package also provides the partial function for automatic differentiation. Math.Tensor.Examples.Gravity.Schwarzschild exports the Einstein tensor for a Schwarzschild spacetime, calculated from the Schwarzschild metric:

>>> let e = einstein 2.0 -- Einstein tensor for Schwarzschild metric with r_s = 2.0
>>> e `evalSec` [1.2, 3.1, 1.3, 2.2] -- evaluate at spacetime point
ZeroTensor

Symbolic calculations

The package can also handle symbolic tensor values. All manipulations, including differentiation, are then performed on strings which may be passed to a computer algebra engine. sparse-tensor itself cannot yet simplify these symbolic values. Math.Tensor.Examples.Gravity.SchwarzschildSymbolic exports the Schwarzschild metric with symbolic entries and methods to calculate derived geometric entities:

>>> let g  = schwarzschildS
>>> let g' = schwarzschildS'
>>> let gamma = christoffelS g g'
>>> let comps = toListT2 gamma     -- get assocs
>>> print $ snd $ comps !! 1       -- component gamma^t_tr
SSymbolic "(1 % 2)*((1/(1 - rs/r))*(diff(1 - rs / r, r)))"
Metadata

Version

0.2.1.5

License

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows