rings-0.0.3: Ring-like objects.

Safe HaskellSafe
LanguageHaskell2010

Data.Semimodule.Matrix

Contents

Synopsis

Documentation

type M11 a = V1 (V1 a) Source #

A 1x1 matrix.

type M12 a = V1 (V2 a) Source #

A 1x2 matrix.

type M13 a = V1 (V3 a) Source #

A 1x3 matrix.

type M14 a = V1 (V4 a) Source #

A 1x4 matrix.

type M21 a = V2 (V1 a) Source #

A 2x1 matrix.

type M31 a = V3 (V1 a) Source #

A 3x1 matrix.

type M41 a = V4 (V1 a) Source #

A 4x1 matrix.

type M22 a = V2 (V2 a) Source #

A 2x2 matrix.

type M23 a = V2 (V3 a) Source #

A 2x3 matrix.

type M24 a = V2 (V4 a) Source #

A 2x4 matrix.

type M32 a = V3 (V2 a) Source #

A 3x2 matrix.

type M33 a = V3 (V3 a) Source #

A 3x3 matrix.

type M34 a = V3 (V4 a) Source #

A 3x4 matrix.

type M42 a = V4 (V2 a) Source #

A 4x2 matrix.

type M43 a = V4 (V3 a) Source #

A 4x3 matrix.

type M44 a = V4 (V4 a) Source #

A 4x4 matrix.

lensRep :: Eq (Rep f) => Representable f => Rep f -> forall g. Functor g => (a -> g a) -> f a -> g (f a) Source #

grateRep :: Representable f => forall g. Functor g => (Rep f -> g a -> b) -> g (f a) -> f b Source #

idx :: Semiring a => Free f => Rep f -> f a Source #

Create a unit vector at an index.

>>> idx I21 :: V2 Int
V2 1 0
>>> idx I42 :: V4 Int
V4 0 1 0 0

dot :: Semiring a => Free f => Foldable f => f a -> f a -> a infix 6 Source #

Dot product.

>>> V3 1 2 3 `dot` V3 1 2 3
14

quadrance :: Semiring a => Free f => Foldable f => f a -> a Source #

Squared l2 norm of a vector.

qd :: FreeModule a f => Foldable f => f a -> f a -> a Source #

Squared l2 norm of the difference between two vectors.

tran :: Semiring a => Basis b f => Basis c g => Foldable g => f (g a) -> Tran a b c Source #

row :: Representable f => Rep f -> f a -> a Source #

Retrieve a row of a row-major matrix or element of a row vector.

>>> row I21 (V2 1 2)
1

col :: Functor f => Representable g => Rep g -> f (g a) -> f a Source #

Retrieve a column of a row-major matrix.

>>> row I22 . col I31 $ V2 (V3 1 2 3) (V3 4 5 6)
4

(#.) :: (Semiring a, Free f, Foldable f, Free g) => f a -> f (g a) -> g a infixl 7 Source #

Multiply a matrix on the left by a row vector.

>>> V2 1 2 #. m23 3 4 5 6 7 8
V3 15 18 21
>>> V2 1 2 #. m23 3 4 5 6 7 8 #. m32 1 0 0 0 0 0
V2 15 0

(.#) :: (Semiring a, Free f, Free g, Foldable g) => f (g a) -> g a -> f a infixr 7 Source #

Multiply a matrix on the right by a column vector.

 (.#) = app . tran
>>> app (tran $ m23 1 2 3 4 5 6) (V3 7 8 9) :: V2 Int
V2 50 122
>>> m23 1 2 3 4 5 6 .# V3 7 8 9 :: V2 Int
V2 50 122
>>> m22 1 0 0 0 .# m23 1 2 3 4 5 6 .# V3 7 8 9
V2 50 0

(.#.) :: (Semiring a, Free f, Free g, Free h, Foldable g) => f (g a) -> g (h a) -> f (h a) infixr 7 Source #

Multiply two matrices.

>>> m22 1 2 3 4 .#. m22 1 2 3 4 :: M22 Int
V2 (V2 7 10) (V2 15 22)
>>> m23 1 2 3 4 5 6 .#. m32 1 2 3 4 4 5 :: M22 Int
V2 (V2 19 25) (V2 43 58)

outer :: Semiring a => Functor f => Functor g => f a -> g a -> f (g a) Source #

Outer product of two vectors.

>>> V2 1 1 `outer` V2 1 1
V2 (V2 1 1) (V2 1 1)

diag :: (Additive - Monoid) a => Free f => f a -> f (f a) Source #

Obtain a diagonal matrix from a vector.

>>> diag (V2 2 3)
V2 (V2 2 0) (V2 0 3)

identity :: Semiring a => Free f => f (f a) Source #

Identity matrix.

>>> identity :: M44 Int
V4 (V4 1 0 0 0) (V4 0 1 0 0) (V4 0 0 1 0) (V4 0 0 0 1)
>>> identity :: V3 (V3 Int)
V3 (V3 1 0 0) (V3 0 1 0) (V3 0 0 1)

trace :: Semiring a => Free f => Foldable f => f (f a) -> a Source #

Compute the trace of a matrix.

>>> trace $ V2 (V2 1.0 2.0) (V2 3.0 4.0)
5.0

diagonal :: Representable f => f (f a) -> f a Source #

Obtain the diagonal of a matrix as a vector.

>>> diagonal $ V2 (V2 1.0 2.0) (V2 3.0 4.0)
V2 1.0 4.0

ij :: Representable f => Representable g => Rep f -> Rep g -> f (g a) -> a Source #

inv1 :: Field a => M11 a -> M11 a Source #

1x1 matrix inverse over a field.

>>> inv1 $ m11 4.0 :: M11 Double
V1 (V1 0.25)

bdet2 :: Semiring a => Basis I2 f => Basis I2 g => f (g a) -> (a, a) Source #

2x2 matrix bdeterminant over a commutative semiring.

>>> bdet2 $ m22 1 2 3 4
(4,6)

det2 :: Ring a => Basis I2 f => Basis I2 g => f (g a) -> a Source #

2x2 matrix determinant over a commutative ring.

det2 = uncurry (-) . bdet2
>>> det2 $ m22 1 2 3 4 :: Double
-2.0

inv2 :: Field a => M22 a -> M22 a Source #

2x2 matrix inverse over a field.

>>> inv2 $ m22 1 2 3 4 :: M22 Double
V2 (V2 (-2.0) 1.0) (V2 1.5 (-0.5))

bdet3 :: Semiring a => Basis I3 f => Basis I3 g => f (g a) -> (a, a) Source #

3x3 matrix bdeterminant over a commutative semiring.

>>> bdet3 (V3 (V3 1 2 3) (V3 4 5 6) (V3 7 8 9))
(225, 225)

det3 :: Ring a => Basis I3 f => Basis I3 g => f (g a) -> a Source #

3x3 double-precision matrix determinant.

det3 = uncurry (-) . bdet3

Implementation uses a cofactor expansion to avoid loss of precision.

>>> det3 (V3 (V3 1 2 3) (V3 4 5 6) (V3 7 8 9))
0

inv3 :: Field a => M33 a -> M33 a Source #

3x3 matrix inverse.

>>> inv3 $ m33 1 2 4 4 2 2 1 1 1 :: M33 Double
V3 (V3 0.0 0.5 (-1.0)) (V3 (-0.5) (-0.75) 3.5) (V3 0.5 0.25 (-1.5))

bdet4 :: Semiring a => Basis I4 f => Basis I4 g => f (g a) -> (a, a) Source #

4x4 matrix bdeterminant over a commutative semiring.

>>> bdet4 (V4 (V4 1 2 3 4) (V4 5 6 7 8) (V4 9 10 11 12) (V4 13 14 15 16))
(27728,27728)

det4 :: Ring a => Basis I4 f => Basis I4 g => f (g a) -> a Source #

4x4 matrix determinant over a commutative ring.

det4 = uncurry (-) . bdet4

This implementation uses a cofactor expansion to avoid loss of precision.

>>> det4 (m44 1 0 3 2 2 0 2 1 0 0 0 1 0 3 4 0 :: M44 Rational)
(-12) % 1

inv4 :: Field a => M44 a -> M44 a Source #

4x4 matrix inverse.

>>> row I41 $ inv4 (m44 1 0 3 2 2 0 2 1 0 0 0 1 0 3 4 0 :: M44 Rational)
V4 (6 % (-12)) ((-9) % (-12)) ((-3) % (-12)) (0 % (-12))

m11 :: a -> M11 a Source #

Construct a 1x1 matrix.

>>> m11 1 :: M11 Int
V1 (V1 1)

m12 :: a -> a -> M12 a Source #

Construct a 1x2 matrix.

>>> m12 1 2 :: M12 Int
V1 (V2 1 2)

m13 :: a -> a -> a -> M13 a Source #

Construct a 1x3 matrix.

>>> m13 1 2 3 :: M13 Int
V1 (V3 1 2 3)

m14 :: a -> a -> a -> a -> M14 a Source #

Construct a 1x4 matrix.

>>> m14 1 2 3 4 :: M14 Int
V1 (V4 1 2 3 4)

m21 :: a -> a -> M21 a Source #

Construct a 2x1 matrix.

>>> m21 1 2 :: M21 Int
V2 (V1 1) (V1 2)

m31 :: a -> a -> a -> M31 a Source #

Construct a 3x1 matrix.

>>> m31 1 2 3 :: M31 Int
V3 (V1 1) (V1 2) (V1 3)

m41 :: a -> a -> a -> a -> M41 a Source #

Construct a 4x1 matrix.

>>> m41 1 2 3 4 :: M41 Int
V4 (V1 1) (V1 2) (V1 3) (V1 4)

m22 :: a -> a -> a -> a -> M22 a Source #

Construct a 2x2 matrix.

Arguments are in row-major order.

>>> m22 1 2 3 4 :: M22 Int
V2 (V2 1 2) (V2 3 4)

m23 :: a -> a -> a -> a -> a -> a -> M23 a Source #

Construct a 2x3 matrix.

Arguments are in row-major order.

m24 :: a -> a -> a -> a -> a -> a -> a -> a -> M24 a Source #

Construct a 2x4 matrix.

Arguments are in row-major order.

m32 :: a -> a -> a -> a -> a -> a -> M32 a Source #

Construct a 3x2 matrix.

Arguments are in row-major order.

m33 :: a -> a -> a -> a -> a -> a -> a -> a -> a -> M33 a Source #

Construct a 3x3 matrix.

Arguments are in row-major order.

m34 :: a -> a -> a -> a -> a -> a -> a -> a -> a -> a -> a -> a -> M34 a Source #

Construct a 3x4 matrix.

Arguments are in row-major order.

m42 :: a -> a -> a -> a -> a -> a -> a -> a -> M42 a Source #

Construct a 4x2 matrix.

Arguments are in row-major order.

m43 :: a -> a -> a -> a -> a -> a -> a -> a -> a -> a -> a -> a -> M43 a Source #

Construct a 4x3 matrix.

Arguments are in row-major order.

m44 :: a -> a -> a -> a -> a -> a -> a -> a -> a -> a -> a -> a -> a -> a -> a -> a -> M44 a Source #

Construct a 4x4 matrix.

Arguments are in row-major order.

Orphan instances

Semiring a => Semigroup (Multiplicative (M44 a)) Source # 
Instance details

Semiring a => Semigroup (Multiplicative (M33 a)) Source # 
Instance details

Semiring a => Semigroup (Multiplicative (M22 a)) Source # 
Instance details

Semiring a => Semigroup (Multiplicative (M11 a)) Source # 
Instance details

Semiring a => Monoid (Multiplicative (M44 a)) Source # 
Instance details

Semiring a => Monoid (Multiplicative (M33 a)) Source # 
Instance details

Semiring a => Monoid (Multiplicative (M22 a)) Source # 
Instance details

Semiring a => Monoid (Multiplicative (M11 a)) Source # 
Instance details

Field a => Group (Multiplicative (M11 a)) Source # 
Instance details

Field a => Loop (Multiplicative (M11 a)) Source # 
Instance details

Field a => Quasigroup (Multiplicative (M11 a)) Source # 
Instance details

Field a => Magma (Multiplicative (M11 a)) Source # 
Instance details

Ring a => Ring (M44 a) Source # 
Instance details

Ring a => Ring (M33 a) Source # 
Instance details

Ring a => Ring (M22 a) Source # 
Instance details

Ring a => Ring (M11 a) Source # 
Instance details

Semiring a => Semiring (M44 a) Source # 
Instance details

Semiring a => Semiring (M33 a) Source # 
Instance details

Semiring a => Semiring (M22 a) Source # 
Instance details

Semiring a => Semiring (M11 a) Source # 
Instance details

Semiring a => Presemiring (M44 a) Source # 
Instance details

Semiring a => Presemiring (M33 a) Source # 
Instance details

Semiring a => Presemiring (M22 a) Source # 
Instance details

Semiring a => Presemiring (M11 a) Source # 
Instance details

Semiring a => RightSemimodule (M44 a) (M24 a) Source # 
Instance details

Methods

rscale :: M44 a -> M24 a -> M24 a Source #

Semiring a => RightSemimodule (M44 a) (M14 a) Source # 
Instance details

Methods

rscale :: M44 a -> M14 a -> M14 a Source #

Semiring a => RightSemimodule (M33 a) (M23 a) Source # 
Instance details

Methods

rscale :: M33 a -> M23 a -> M23 a Source #

Semiring a => RightSemimodule (M33 a) (M13 a) Source # 
Instance details

Methods

rscale :: M33 a -> M13 a -> M13 a Source #

Semiring a => RightSemimodule (M22 a) (M42 a) Source # 
Instance details

Methods

rscale :: M22 a -> M42 a -> M42 a Source #

Semiring a => RightSemimodule (M22 a) (M32 a) Source # 
Instance details

Methods

rscale :: M22 a -> M32 a -> M32 a Source #

Semiring a => RightSemimodule (M22 a) (M12 a) Source # 
Instance details

Methods

rscale :: M22 a -> M12 a -> M12 a Source #

Semiring a => RightSemimodule (M11 a) (M41 a) Source # 
Instance details

Methods

rscale :: M11 a -> M41 a -> M41 a Source #

Semiring a => RightSemimodule (M11 a) (M31 a) Source # 
Instance details

Methods

rscale :: M11 a -> M31 a -> M31 a Source #

Semiring a => RightSemimodule (M11 a) (M21 a) Source # 
Instance details

Methods

rscale :: M11 a -> M21 a -> M21 a Source #

Semiring a => LeftSemimodule (M44 a) (M42 a) Source # 
Instance details

Methods

lscale :: M44 a -> M42 a -> M42 a Source #

Semiring a => LeftSemimodule (M44 a) (M41 a) Source # 
Instance details

Methods

lscale :: M44 a -> M41 a -> M41 a Source #

Semiring a => LeftSemimodule (M33 a) (M32 a) Source # 
Instance details

Methods

lscale :: M33 a -> M32 a -> M32 a Source #

Semiring a => LeftSemimodule (M33 a) (M31 a) Source # 
Instance details

Methods

lscale :: M33 a -> M31 a -> M31 a Source #

Semiring a => LeftSemimodule (M22 a) (M24 a) Source # 
Instance details

Methods

lscale :: M22 a -> M24 a -> M24 a Source #

Semiring a => LeftSemimodule (M22 a) (M23 a) Source # 
Instance details

Methods

lscale :: M22 a -> M23 a -> M23 a Source #

Semiring a => LeftSemimodule (M22 a) (M21 a) Source # 
Instance details

Methods

lscale :: M22 a -> M21 a -> M21 a Source #

Semiring a => LeftSemimodule (M11 a) (M14 a) Source # 
Instance details

Methods

lscale :: M11 a -> M14 a -> M14 a Source #

Semiring a => LeftSemimodule (M11 a) (M13 a) Source # 
Instance details

Methods

lscale :: M11 a -> M13 a -> M13 a Source #

Semiring a => LeftSemimodule (M11 a) (M12 a) Source # 
Instance details

Methods

lscale :: M11 a -> M12 a -> M12 a Source #

Semiring a => Bisemimodule (M44 a) (M22 a) (M42 a) Source # 
Instance details

Methods

discale :: M44 a -> M22 a -> M42 a -> M42 a Source #

Semiring a => Bisemimodule (M44 a) (M11 a) (M41 a) Source # 
Instance details

Methods

discale :: M44 a -> M11 a -> M41 a -> M41 a Source #

Semiring a => Bisemimodule (M33 a) (M22 a) (M32 a) Source # 
Instance details

Methods

discale :: M33 a -> M22 a -> M32 a -> M32 a Source #

Semiring a => Bisemimodule (M33 a) (M11 a) (M31 a) Source # 
Instance details

Methods

discale :: M33 a -> M11 a -> M31 a -> M31 a Source #

Semiring a => Bisemimodule (M22 a) (M44 a) (M24 a) Source # 
Instance details

Methods

discale :: M22 a -> M44 a -> M24 a -> M24 a Source #

Semiring a => Bisemimodule (M22 a) (M33 a) (M23 a) Source # 
Instance details

Methods

discale :: M22 a -> M33 a -> M23 a -> M23 a Source #

Semiring a => Bisemimodule (M22 a) (M11 a) (M21 a) Source # 
Instance details

Methods

discale :: M22 a -> M11 a -> M21 a -> M21 a Source #

Semiring a => Bisemimodule (M11 a) (M44 a) (M14 a) Source # 
Instance details

Methods

discale :: M11 a -> M44 a -> M14 a -> M14 a Source #

Semiring a => Bisemimodule (M11 a) (M33 a) (M13 a) Source # 
Instance details

Methods

discale :: M11 a -> M33 a -> M13 a -> M13 a Source #

Semiring a => Bisemimodule (M11 a) (M22 a) (M12 a) Source # 
Instance details

Methods

discale :: M11 a -> M22 a -> M12 a -> M12 a Source #