sparse-linear-algebra-0.3.1: Numerical computing in native Haskell

Copyright (c) Marco Zocca 2017 GPL-3 (see the file LICENSE) zocca marco gmail experimental portable None Haskell2010

Numeric.LinearAlgebra.Class

Description

Typeclasses for linear algebra and related concepts

Synopsis

# Matrix and vector elements (optionally Complex)

class (Eq e, Fractional e, Floating e, Num (EltMag e), Ord (EltMag e)) => Elt e where Source #

Minimal complete definition

mag

Associated Types

type EltMag e :: * Source #

Methods

conj :: e -> e Source #

Complex conjugate, or identity function if its input is real-valued

mag :: e -> EltMag e Source #

Magnitude

Instances

 Source # Associated Typestype EltMag Double :: * Source # Methods Source # Associated Typestype EltMag Float :: * Source # Methods RealFloat e => Elt (Complex e) Source # Associated Typestype EltMag (Complex e) :: * Source # Methodsconj :: Complex e -> Complex e Source #mag :: Complex e -> EltMag (Complex e) Source #

# Additive group

class AdditiveGroup v where Source #

Minimal complete definition

Methods

zeroV :: v Source #

The zero element: identity for '(^+^)'

(^+^) :: v -> v -> v infixl 6 Source #

Add vectors

negateV :: v -> v Source #

Additive inverse

(^-^) :: v -> v -> v infixl 6 Source #

Group subtraction

Instances

 Source # Methods Source # Instances for builtin types Methods Source # Methods Source # Methods Source # Methods(^+^) :: SpVector a -> SpVector a -> SpVector a Source #(^-^) :: SpVector a -> SpVector a -> SpVector a Source # Source # SpMatrixes form an additive group, in that they can have an invertible associtative operation (matrix sum) Methods(^+^) :: SpMatrix a -> SpMatrix a -> SpMatrix a Source #(^-^) :: SpMatrix a -> SpMatrix a -> SpMatrix a Source #

# Vector space v.

class (AdditiveGroup v, Num (Scalar v)) => VectorSpace v where Source #

Minimal complete definition

(.*)

Associated Types

type Scalar v :: * Source #

Methods

(.*) :: Scalar v -> v -> v infixr 7 Source #

Scale a vector

Instances

 Source # Associated Typestype Scalar Double :: * Source # Methods Source # Associated Typestype Scalar Float :: * Source # Methods Source # Associated Typestype Scalar (Complex Double) :: * Source # Methods Source # Associated Typestype Scalar (Complex Float) :: * Source # Methods Source # Associated Typestype Scalar (SpVector a) :: * Source # Methods(.*) :: Scalar (SpVector a) -> SpVector a -> SpVector a Source # Source # Associated Typestype Scalar (SpMatrix a) :: * Source # Methods(.*) :: Scalar (SpMatrix a) -> SpMatrix a -> SpMatrix a Source #

class VectorSpace v => InnerSpace v where Source #

Adds inner (dot) products.

Minimal complete definition

(<.>)

Methods

(<.>) :: v -> v -> Scalar v Source #

Inner/dot product

Instances

 Source # Methods Source # Methods Source # Methods Source # Methods InnerSpace a => InnerSpace (SpVector a) Source # Methods(<.>) :: SpVector a -> SpVector a -> Scalar (SpVector a) Source #

dot :: InnerSpace v => v -> v -> Scalar v Source #

Inner product

(./) :: (VectorSpace v, s ~ Scalar v, Fractional s) => v -> s -> v infixr 7 Source #

Scale a vector by the reciprocal of a number (e.g. for normalization)

(*.) :: (VectorSpace v, s ~ Scalar v) => v -> s -> v infixl 7 Source #

Vector multiplied by scalar

cvx :: VectorSpace v => Scalar v -> v -> v -> v Source #

Convex combination of two vectors (NB: 0 <= a <= 1).

## Hilbert-space distance function

hilbertDistSq :: InnerSpace v => v -> v -> Scalar v Source #

hilbertDistSq x y = || x - y ||^2 computes the squared L2 distance between two vectors

# Normed vector spaces

class (InnerSpace v, Num (RealScalar v), Eq (RealScalar v), Epsilon (Magnitude v), Show (Magnitude v), Ord (Magnitude v)) => Normed v where Source #

Minimal complete definition

Associated Types

type Magnitude v :: * Source #

type RealScalar v :: * Source #

Methods

norm1 :: v -> Magnitude v Source #

L1 norm

norm2Sq :: v -> Magnitude v Source #

Euclidean (L2) norm squared

normP :: RealScalar v -> v -> Magnitude v Source #

Lp norm (p > 0)

normalize :: RealScalar v -> v -> v Source #

Normalize w.r.t. Lp norm

normalize2 :: v -> v Source #

Normalize w.r.t. L2 norm

normalize2' :: Floating (Scalar v) => v -> v Source #

Normalize w.r.t. norm2' instead of norm2

norm2 :: Floating (Magnitude v) => v -> Magnitude v Source #

Euclidean (L2) norm

norm2' :: Floating (Scalar v) => v -> Scalar v Source #

Euclidean (L2) norm; returns a Complex (norm :+ 0) for Complex-valued vectors

norm :: Floating (Magnitude v) => RealScalar v -> v -> Magnitude v Source #

Lp norm (p > 0)

Instances

 Source # Associated Typestype Magnitude Double :: * Source #type RealScalar Double :: * Source # Methods Source # Associated Typestype Magnitude Float :: * Source #type RealScalar Float :: * Source # Methods Source # Associated Typestype Magnitude (Complex Double) :: * Source #type RealScalar (Complex Double) :: * Source # Methods Source # Associated Typestype Magnitude (Complex Float) :: * Source #type RealScalar (Complex Float) :: * Source # Methods (Normed a, (~) * (Magnitude a) (RealScalar a), (~) * (RealScalar a) (Scalar a)) => Normed (SpVector a) Source # Associated Typestype Magnitude (SpVector a) :: * Source #type RealScalar (SpVector a) :: * Source # MethodsnormP :: RealScalar (SpVector a) -> SpVector a -> Magnitude (SpVector a) Source #normalize :: RealScalar (SpVector a) -> SpVector a -> SpVector a Source #norm2' :: SpVector a -> Scalar (SpVector a) Source #norm :: RealScalar (SpVector a) -> SpVector a -> Magnitude (SpVector a) Source #

normInftyR :: (Foldable t, Ord a) => t a -> a Source #

Infinity-norm (Real)

normInftyC :: (Foldable t, RealFloat a, Functor t) => t (Complex a) -> a Source #

Infinity-norm (Complex)

dotLp :: (Set t, Foldable t, Floating a) => a -> t a -> t a -> a Source #

Lp inner product (p > 0)

reciprocal :: (Functor f, Fractional b) => f b -> f b Source #

Reciprocal

scale :: (Num b, Functor f) => b -> f b -> f b Source #

Scale

# Matrix ring

class (AdditiveGroup m, Epsilon (MatrixNorm m)) => MatrixRing m where Source #

A matrix ring is any collection of matrices over some ring R that form a ring under matrix addition and matrix multiplication

Minimal complete definition

Associated Types

type MatrixNorm m :: * Source #

Methods

(##) :: m -> m -> m Source #

Matrix-matrix product

(##^) :: m -> m -> m Source #

Matrix times matrix transpose (A B^T)

(#^#) :: m -> m -> m Source #

Matrix transpose times matrix (A^T B)

transpose :: m -> m Source #

Matrix transpose (Hermitian conjugate in the Complex case)

normFrobenius :: m -> MatrixNorm m Source #

Frobenius norm

Instances

 Source # Associated Typestype MatrixNorm (SpMatrix Double) :: * Source # Methods Source # Associated Typestype MatrixNorm (SpMatrix (Complex Double)) :: * Source # Methods

# Linear vector space

class VectorSpace v => LinearVectorSpace v where Source #

Minimal complete definition

Associated Types

type MatrixType v :: * Source #

Methods

(#>) :: MatrixType v -> v -> v Source #

Matrix-vector action

(<#) :: v -> MatrixType v -> v Source #

Dual matrix-vector action

## Linear systems

class LinearVectorSpace v => LinearSystem v where Source #

Minimal complete definition

(<\>)

Methods

Arguments

 :: (MonadIO m, MonadThrow m) => MatrixType v System matrix -> v Right-hand side -> m v Result

Solve a linear system; uses GMRES internally as default method

# FiniteDim : finite-dimensional objects

class FiniteDim f where Source #

Minimal complete definition

dim

Associated Types

type FDSize f Source #

Methods

dim :: f -> FDSize f Source #

Dimension (i.e. Int for SpVector, (Int, Int) for SpMatrix)

Instances

 Source # SpVectors form a vector space because they can be multiplied by a scalarSpVectors are finite-dimensional vectors Associated Typestype FDSize (SpVector a) :: * Source # Methodsdim :: SpVector a -> FDSize (SpVector a) Source # Source # SpMatrixes are maps between finite-dimensional spaces Associated Typestype FDSize (SpMatrix a) :: * Source # Methodsdim :: SpMatrix a -> FDSize (SpMatrix a) Source #

# HasData : accessing inner data (do not export)

class HasData f where Source #

Minimal complete definition

Associated Types

type HDData f Source #

Methods

nnz :: f -> Int Source #

Number of nonzeros

dat :: f -> HDData f Source #

Instances

 Source # Associated Typestype HDData (SpVector a) :: * Source # Methodsnnz :: SpVector a -> Int Source #dat :: SpVector a -> HDData (SpVector a) Source # Source # Associated Typestype HDData (SpMatrix a) :: * Source # Methodsnnz :: SpMatrix a -> Int Source #dat :: SpMatrix a -> HDData (SpMatrix a) Source #

# Sparse : sparse datastructures

class (FiniteDim f, HasData f) => Sparse f where Source #

Minimal complete definition

spy

Methods

spy :: Fractional b => f -> b Source #

Sparsity (fraction of nonzero elements)

Instances

 Source # Methodsspy :: Fractional b => SpVector a -> b Source # Source # Methodsspy :: Fractional b => SpMatrix a -> b Source #

# Set : types that behave as sets

class Functor f => Set f where Source #

Minimal complete definition

Methods

liftU2 :: (a -> a -> a) -> f a -> f a -> f a Source #

Union binary lift : apply function on _union_ of two "sets"

liftI2 :: (a -> a -> b) -> f a -> f a -> f b Source #

Intersection binary lift : apply function on _intersection_ of two "sets"

Instances

 Source # MethodsliftU2 :: (a -> a -> a) -> SpVector a -> SpVector a -> SpVector a Source #liftI2 :: (a -> a -> b) -> SpVector a -> SpVector a -> SpVector b Source # Source # MethodsliftU2 :: (a -> a -> a) -> SpMatrix a -> SpMatrix a -> SpMatrix a Source #liftI2 :: (a -> a -> b) -> SpMatrix a -> SpMatrix a -> SpMatrix b Source #

# SpContainer : sparse container datastructures. Insertion, lookup, toList, lookup with 0 default

class Sparse c => SpContainer c where Source #

Minimal complete definition

Associated Types

type ScIx c :: * Source #

type ScElem c Source #

Methods

scInsert :: ScIx c -> ScElem c -> c -> c Source #

scLookup :: c -> ScIx c -> Maybe (ScElem c) Source #

scToList :: c -> [(ScIx c, ScElem c)] Source #

(@@) :: c -> ScIx c -> ScElem c Source #

Instances

 Elt a => SpContainer (SpVector a) Source # SpVectors are sparse containers too, i.e. any specific component may be missing (so it is assumed to be 0) Associated Typestype ScIx (SpVector a) :: * Source #type ScElem (SpVector a) :: * Source # MethodsscInsert :: ScIx (SpVector a) -> ScElem (SpVector a) -> SpVector a -> SpVector a Source #scLookup :: SpVector a -> ScIx (SpVector a) -> Maybe (ScElem (SpVector a)) Source #scToList :: SpVector a -> [(ScIx (SpVector a), ScElem (SpVector a))] Source #(@@) :: SpVector a -> ScIx (SpVector a) -> ScElem (SpVector a) Source # Num a => SpContainer (SpMatrix a) Source # SpMatrixes are sparse containers too, i.e. any specific component may be missing (so it is assumed to be 0) Associated Typestype ScIx (SpMatrix a) :: * Source #type ScElem (SpMatrix a) :: * Source # MethodsscInsert :: ScIx (SpMatrix a) -> ScElem (SpMatrix a) -> SpMatrix a -> SpMatrix a Source #scLookup :: SpMatrix a -> ScIx (SpMatrix a) -> Maybe (ScElem (SpMatrix a)) Source #scToList :: SpMatrix a -> [(ScIx (SpMatrix a), ScElem (SpMatrix a))] Source #(@@) :: SpMatrix a -> ScIx (SpMatrix a) -> ScElem (SpMatrix a) Source #

# SparseVector

class SpContainer v => SparseVector v where Source #

Minimal complete definition

Associated Types

type SpvIx v :: * Source #

Methods

svFromList :: Int -> [(SpvIx v, ScElem v)] -> v Source #

svFromListDense :: Int -> [ScElem v] -> v Source #

svConcat :: Foldable t => t v -> v Source #

# SparseMatrix

class SpContainer m => SparseMatrix m where Source #

Minimal complete definition

Methods

smFromVector :: LexOrd -> (Int, Int) -> Vector (IxRow, IxCol, ScElem m) -> m Source #

smTranspose :: m -> m Source #

encodeIx :: m -> LexOrd -> (IxRow, IxCol) -> LexIx Source #

decodeIx :: m -> LexOrd -> LexIx -> (IxRow, IxCol) Source #

# Utilities

toC :: Num a => a -> Complex a Source #

Lift a real number onto the complex plane