{-# LANGUAGE CPP #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Trustworthy #-}
#endif

---------------------------------------------------------------------------
-- |
-- Copyright   :  (C) 2012-2015 Edward Kmett
-- License     :  BSD-style (see the file LICENSE)
--
-- Maintainer  :  Edward Kmett <ekmett@gmail.com>
-- Stability   :  experimental
-- Portability :  non-portable
--
-- Simple matrix operation for low-dimensional primitives.
---------------------------------------------------------------------------
module Linear.Matrix
  ( (!*!), (!+!), (!-!), (!*), (*!), (!!*), (*!!), (!!/)
  , column
  , adjoint
  , M22, M23, M24, M32, M33, M34, M42, M43, M44
  , m33_to_m44, m43_to_m44
  , det22, det33, det44, inv22, inv33, inv44
  , identity
  , Trace(..)
  , translation
  , transpose
  , fromQuaternion
  , mkTransformation
  , mkTransformationMat
  , _m22, _m23, _m24
  , _m32, _m33, _m34
  , _m42, _m43, _m44
  ) where

#if __GLASGOW_HASKELL__ < 710
import Control.Applicative
#endif
import Control.Lens hiding (index)
import Control.Lens.Internal.Context
import Data.Distributive
import Data.Foldable as Foldable
import Data.Functor.Rep
import Linear.Quaternion
import Linear.V2
import Linear.V3
import Linear.V4
import Linear.Vector
import Linear.Conjugate
import Linear.Trace

#ifdef HLINT
{-# ANN module "HLint: ignore Reduce duplication" #-}
#endif

-- | This is a generalization of 'Control.Lens.inside' to work over any corepresentable 'Functor'.
--
-- @
-- 'column' :: 'Representable' f => 'Lens' s t a b -> 'Lens' (f s) (f t) (f a) (f b)
-- @
--
-- In practice it is used to access a column of a matrix.
--
-- >>> V2 (V3 1 2 3) (V3 4 5 6) ^._x
-- V3 1 2 3
--
-- >>> V2 (V3 1 2 3) (V3 4 5 6) ^.column _x
-- V2 1 4
column :: Representable f => LensLike (Context a b) s t a b -> Lens (f s) (f t) (f a) (f b)
column :: LensLike (Context a b) s t a b -> Lens (f s) (f t) (f a) (f b)
column l :: LensLike (Context a b) s t a b
l f :: f a -> f (f b)
f es :: f s
es = f b -> f t
o (f b -> f t) -> f (f b) -> f (f t)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a -> f (f b)
f f a
i where
   go :: s -> Context a b t
go = LensLike (Context a b) s t a b
l ((b -> b) -> a -> Context a b b
forall a b t. (b -> t) -> a -> Context a b t
Context b -> b
forall a. a -> a
id)
   i :: f a
i = (Rep f -> a) -> f a
forall (f :: * -> *) a. Representable f => (Rep f -> a) -> f a
tabulate ((Rep f -> a) -> f a) -> (Rep f -> a) -> f a
forall a b. (a -> b) -> a -> b
$ \ e :: Rep f
e -> Context a b t -> a
forall (w :: * -> * -> * -> *) a c t.
IndexedComonadStore w =>
w a c t -> a
ipos (Context a b t -> a) -> Context a b t -> a
forall a b. (a -> b) -> a -> b
$ s -> Context a b t
go (f s -> Rep f -> s
forall (f :: * -> *) a. Representable f => f a -> Rep f -> a
index f s
es Rep f
e)
   o :: f b -> f t
o eb :: f b
eb = (Rep f -> t) -> f t
forall (f :: * -> *) a. Representable f => (Rep f -> a) -> f a
tabulate ((Rep f -> t) -> f t) -> (Rep f -> t) -> f t
forall a b. (a -> b) -> a -> b
$ \ e :: Rep f
e -> b -> Context a b t -> t
forall (w :: * -> * -> * -> *) c a t.
IndexedComonadStore w =>
c -> w a c t -> t
ipeek (f b -> Rep f -> b
forall (f :: * -> *) a. Representable f => f a -> Rep f -> a
index f b
eb Rep f
e) (s -> Context a b t
go (f s -> Rep f -> s
forall (f :: * -> *) a. Representable f => f a -> Rep f -> a
index f s
es Rep f
e))

-- $setup
-- >>> import Data.Complex
-- >>> import Data.IntMap
-- >>> import Debug.SimpleReflect.Vars
-- >>> import Linear.V

infixl 7 !*!
-- | Matrix product. This can compute any combination of sparse and dense multiplication.
--
-- >>> V2 (V3 1 2 3) (V3 4 5 6) !*! V3 (V2 1 2) (V2 3 4) (V2 4 5)
-- V2 (V2 19 25) (V2 43 58)
--
-- >>> V2 (fromList [(1,2)]) (fromList [(2,3)]) !*! fromList [(1,V3 0 0 1), (2, V3 0 0 5)]
-- V2 (V3 0 0 2) (V3 0 0 15)
(!*!) :: (Functor m, Foldable t, Additive t, Additive n, Num a) => m (t a) -> t (n a) -> m (n a)
f :: m (t a)
f !*! :: m (t a) -> t (n a) -> m (n a)
!*! g :: t (n a)
g = (t a -> n a) -> m (t a) -> m (n a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\ f' :: t a
f' -> (n a -> n a -> n a) -> n a -> t (n a) -> n a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl' n a -> n a -> n a
forall (f :: * -> *) a. (Additive f, Num a) => f a -> f a -> f a
(^+^) n a
forall (f :: * -> *) a. (Additive f, Num a) => f a
zero (t (n a) -> n a) -> t (n a) -> n a
forall a b. (a -> b) -> a -> b
$ (a -> n a -> n a) -> t a -> t (n a) -> t (n a)
forall (f :: * -> *) a b c.
Additive f =>
(a -> b -> c) -> f a -> f b -> f c
liftI2 a -> n a -> n a
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
(*^) t a
f' t (n a)
g) m (t a)
f

infixl 6 !+!
-- | Entry-wise matrix addition.
--
-- >>> V2 (V3 1 2 3) (V3 4 5 6) !+! V2 (V3 7 8 9) (V3 1 2 3)
-- V2 (V3 8 10 12) (V3 5 7 9)
(!+!) :: (Additive m, Additive n, Num a) => m (n a) -> m (n a) -> m (n a)
as :: m (n a)
as !+! :: m (n a) -> m (n a) -> m (n a)
!+! bs :: m (n a)
bs = (n a -> n a -> n a) -> m (n a) -> m (n a) -> m (n a)
forall (f :: * -> *) a.
Additive f =>
(a -> a -> a) -> f a -> f a -> f a
liftU2 n a -> n a -> n a
forall (f :: * -> *) a. (Additive f, Num a) => f a -> f a -> f a
(^+^) m (n a)
as m (n a)
bs

infixl 6 !-!
-- | Entry-wise matrix subtraction.
--
-- >>> V2 (V3 1 2 3) (V3 4 5 6) !-! V2 (V3 7 8 9) (V3 1 2 3)
-- V2 (V3 (-6) (-6) (-6)) (V3 3 3 3)
(!-!) :: (Additive m, Additive n, Num a) => m (n a) -> m (n a) -> m (n a)
as :: m (n a)
as !-! :: m (n a) -> m (n a) -> m (n a)
!-! bs :: m (n a)
bs = (n a -> n a -> n a) -> m (n a) -> m (n a) -> m (n a)
forall (f :: * -> *) a.
Additive f =>
(a -> a -> a) -> f a -> f a -> f a
liftU2 n a -> n a -> n a
forall (f :: * -> *) a. (Additive f, Num a) => f a -> f a -> f a
(^-^) m (n a)
as m (n a)
bs

infixl 7 !*
-- | Matrix * column vector
--
-- >>> V2 (V3 1 2 3) (V3 4 5 6) !* V3 7 8 9
-- V2 50 122
(!*) :: (Functor m, Foldable r, Additive r, Num a) => m (r a) -> r a -> m a
m :: m (r a)
m !* :: m (r a) -> r a -> m a
!* v :: r a
v = (r a -> a) -> m (r a) -> m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\r :: r a
r -> r a -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
Foldable.sum (r a -> a) -> r a -> a
forall a b. (a -> b) -> a -> b
$ (a -> a -> a) -> r a -> r a -> r a
forall (f :: * -> *) a b c.
Additive f =>
(a -> b -> c) -> f a -> f b -> f c
liftI2 a -> a -> a
forall a. Num a => a -> a -> a
(*) r a
r r a
v) m (r a)
m

infixl 7 *!
-- | Row vector * matrix
--
-- >>> V2 1 2 *! V2 (V3 3 4 5) (V3 6 7 8)
-- V3 15 18 21

-- (*!) :: (Metric r, Additive n, Num a) => r a -> r (n a) -> n a
-- f *! g = dot f <$> distribute g

(*!) :: (Num a, Foldable t, Additive f, Additive t) => t a -> t (f a) -> f a
f :: t a
f *! :: t a -> t (f a) -> f a
*! g :: t (f a)
g = t (f a) -> f a
forall (f :: * -> *) (v :: * -> *) a.
(Foldable f, Additive v, Num a) =>
f (v a) -> v a
sumV (t (f a) -> f a) -> t (f a) -> f a
forall a b. (a -> b) -> a -> b
$ (a -> f a -> f a) -> t a -> t (f a) -> t (f a)
forall (f :: * -> *) a b c.
Additive f =>
(a -> b -> c) -> f a -> f b -> f c
liftI2 a -> f a -> f a
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
(*^) t a
f t (f a)
g

infixl 7 *!!
-- | Scalar-matrix product
--
-- >>> 5 *!! V2 (V2 1 2) (V2 3 4)
-- V2 (V2 5 10) (V2 15 20)
(*!!) :: (Functor m, Functor r, Num a) => a -> m (r a) -> m (r a)
s :: a
s *!! :: a -> m (r a) -> m (r a)
*!! m :: m (r a)
m = (r a -> r a) -> m (r a) -> m (r a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a
s a -> r a -> r a
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^) m (r a)
m
{-# INLINE (*!!) #-}

infixl 7 !!*
-- | Matrix-scalar product
--
-- >>> V2 (V2 1 2) (V2 3 4) !!* 5
-- V2 (V2 5 10) (V2 15 20)
(!!*) :: (Functor m, Functor r, Num a) => m (r a) -> a -> m (r a)
!!* :: m (r a) -> a -> m (r a)
(!!*) = (a -> m (r a) -> m (r a)) -> m (r a) -> a -> m (r a)
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> m (r a) -> m (r a)
forall (m :: * -> *) (r :: * -> *) a.
(Functor m, Functor r, Num a) =>
a -> m (r a) -> m (r a)
(*!!)
{-# INLINE (!!*) #-}

infixl 7 !!/
-- | Matrix-scalar division
(!!/) :: (Functor m, Functor r, Fractional a) => m (r a) -> a -> m (r a)
m :: m (r a)
m !!/ :: m (r a) -> a -> m (r a)
!!/ s :: a
s = (r a -> r a) -> m (r a) -> m (r a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (r a -> a -> r a
forall (f :: * -> *) a.
(Functor f, Fractional a) =>
f a -> a -> f a
^/ a
s) m (r a)
m
{-# INLINE (!!/) #-}

-- | Hermitian conjugate or conjugate transpose
--
-- >>> adjoint (V2 (V2 (1 :+ 2) (3 :+ 4)) (V2 (5 :+ 6) (7 :+ 8)))
-- V2 (V2 (1.0 :+ (-2.0)) (5.0 :+ (-6.0))) (V2 (3.0 :+ (-4.0)) (7.0 :+ (-8.0)))
adjoint :: (Functor m, Distributive n, Conjugate a) => m (n a) -> n (m a)
adjoint :: m (n a) -> n (m a)
adjoint = (n a -> n a) -> m (n a) -> n (m a)
forall (g :: * -> *) (f :: * -> *) a b.
(Distributive g, Functor f) =>
(a -> g b) -> f a -> g (f b)
collect ((a -> a) -> n a -> n a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Conjugate a => a -> a
conjugate)
{-# INLINE adjoint #-}

-- * Matrices
--
-- Matrices use a row-major representation.

-- | A 2x2 matrix with row-major representation
type M22 a = V2 (V2 a)
-- | A 2x3 matrix with row-major representation
type M23 a = V2 (V3 a)
-- | A 2x4 matrix with row-major representation
type M24 a = V2 (V4 a)
-- | A 3x2 matrix with row-major representation
type M32 a = V3 (V2 a)
-- | A 3x3 matrix with row-major representation
type M33 a = V3 (V3 a)
-- | A 3x4 matrix with row-major representation
type M34 a = V3 (V4 a)
-- | A 4x2 matrix with row-major representation
type M42 a = V4 (V2 a)
-- | A 4x3 matrix with row-major representation
type M43 a = V4 (V3 a)
-- | A 4x4 matrix with row-major representation
type M44 a = V4 (V4 a)

-- | Build a rotation matrix from a unit 'Quaternion'.
fromQuaternion :: Num a => Quaternion a -> M33 a
fromQuaternion :: Quaternion a -> M33 a
fromQuaternion (Quaternion w :: a
w (V3 x :: a
x y :: a
y z :: a
z)) =
  V3 a -> V3 a -> V3 a -> M33 a
forall a. a -> a -> a -> V3 a
V3 (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 (1a -> a -> a
forall a. Num a => a -> a -> a
-2a -> a -> a
forall a. Num a => a -> a -> a
*(a
y2a -> a -> a
forall a. Num a => a -> a -> a
+a
z2)) (2a -> a -> a
forall a. Num a => a -> a -> a
*(a
xya -> a -> a
forall a. Num a => a -> a -> a
-a
zw)) (2a -> a -> a
forall a. Num a => a -> a -> a
*(a
xza -> a -> a
forall a. Num a => a -> a -> a
+a
yw)))
     (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 (2a -> a -> a
forall a. Num a => a -> a -> a
*(a
xya -> a -> a
forall a. Num a => a -> a -> a
+a
zw)) (1a -> a -> a
forall a. Num a => a -> a -> a
-2a -> a -> a
forall a. Num a => a -> a -> a
*(a
x2a -> a -> a
forall a. Num a => a -> a -> a
+a
z2)) (2a -> a -> a
forall a. Num a => a -> a -> a
*(a
yza -> a -> a
forall a. Num a => a -> a -> a
-a
xw)))
     (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 (2a -> a -> a
forall a. Num a => a -> a -> a
*(a
xza -> a -> a
forall a. Num a => a -> a -> a
-a
yw)) (2a -> a -> a
forall a. Num a => a -> a -> a
*(a
yza -> a -> a
forall a. Num a => a -> a -> a
+a
xw)) (1a -> a -> a
forall a. Num a => a -> a -> a
-2a -> a -> a
forall a. Num a => a -> a -> a
*(a
x2a -> a -> a
forall a. Num a => a -> a -> a
+a
y2)))
  where x2 :: a
x2 = a
xa -> a -> a
forall a. Num a => a -> a -> a
*a
x
        y2 :: a
y2 = a
ya -> a -> a
forall a. Num a => a -> a -> a
*a
y
        z2 :: a
z2 = a
za -> a -> a
forall a. Num a => a -> a -> a
*a
z
        xy :: a
xy = a
xa -> a -> a
forall a. Num a => a -> a -> a
*a
y
        xz :: a
xz = a
xa -> a -> a
forall a. Num a => a -> a -> a
*a
z
        xw :: a
xw = a
xa -> a -> a
forall a. Num a => a -> a -> a
*a
w
        yz :: a
yz = a
ya -> a -> a
forall a. Num a => a -> a -> a
*a
z
        yw :: a
yw = a
ya -> a -> a
forall a. Num a => a -> a -> a
*a
w
        zw :: a
zw = a
za -> a -> a
forall a. Num a => a -> a -> a
*a
w
{-# INLINE fromQuaternion #-}

-- | Build a transformation matrix from a rotation matrix and a
-- translation vector.
mkTransformationMat :: Num a => M33 a -> V3 a -> M44 a
mkTransformationMat :: M33 a -> V3 a -> M44 a
mkTransformationMat (V3 r1 :: V3 a
r1 r2 :: V3 a
r2 r3 :: V3 a
r3) (V3 tx :: a
tx ty :: a
ty tz :: a
tz) =
  V4 a -> V4 a -> V4 a -> V4 a -> M44 a
forall a. a -> a -> a -> a -> V4 a
V4 (V3 a -> a -> V4 a
forall a. V3 a -> a -> V4 a
snoc3 V3 a
r1 a
tx) (V3 a -> a -> V4 a
forall a. V3 a -> a -> V4 a
snoc3 V3 a
r2 a
ty) (V3 a -> a -> V4 a
forall a. V3 a -> a -> V4 a
snoc3 V3 a
r3 a
tz) (a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 0 0 0 1)
  where snoc3 :: V3 a -> a -> V4 a
snoc3 (V3 x :: a
x y :: a
y z :: a
z) = a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
x a
y a
z
{-# INLINE mkTransformationMat #-}

-- |Build a transformation matrix from a rotation expressed as a
-- 'Quaternion' and a translation vector.
mkTransformation :: Num a => Quaternion a -> V3 a -> M44 a
mkTransformation :: Quaternion a -> V3 a -> M44 a
mkTransformation = M33 a -> V3 a -> M44 a
forall a. Num a => M33 a -> V3 a -> M44 a
mkTransformationMat (M33 a -> V3 a -> M44 a)
-> (Quaternion a -> M33 a) -> Quaternion a -> V3 a -> M44 a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Quaternion a -> M33 a
forall a. Num a => Quaternion a -> M33 a
fromQuaternion
{-# INLINE mkTransformation #-}

-- | Convert from a 4x3 matrix to a 4x4 matrix, extending it with the @[ 0 0 0 1 ]@ column vector
m43_to_m44 :: Num a => M43 a -> M44 a
m43_to_m44 :: M43 a -> M44 a
m43_to_m44
  (V4 (V3 a :: a
a b :: a
b c :: a
c)
      (V3 d :: a
d e :: a
e f :: a
f)
      (V3 g :: a
g h :: a
h i :: a
i)
      (V3 j :: a
j k :: a
k l :: a
l)) =
  V4 a -> V4 a -> V4 a -> V4 a -> M44 a
forall a. a -> a -> a -> a -> V4 a
V4 (a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
a a
b a
c 0)
     (a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
d a
e a
f 0)
     (a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
g a
h a
i 0)
     (a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
j a
k a
l 1)
{-# ANN m43_to_m44 "HLint: ignore Use camelCase" #-}

-- | Convert a 3x3 matrix to a 4x4 matrix extending it with 0's in the new row and column.
m33_to_m44 :: Num a => M33 a -> M44 a
m33_to_m44 :: M33 a -> M44 a
m33_to_m44 (V3 r1 :: V3 a
r1 r2 :: V3 a
r2 r3 :: V3 a
r3) = V4 a -> V4 a -> V4 a -> V4 a -> M44 a
forall a. a -> a -> a -> a -> V4 a
V4 (V3 a -> V4 a
forall a. Num a => V3 a -> V4 a
vector V3 a
r1) (V3 a -> V4 a
forall a. Num a => V3 a -> V4 a
vector V3 a
r2) (V3 a -> V4 a
forall a. Num a => V3 a -> V4 a
vector V3 a
r3) (V3 a -> V4 a
forall a. Num a => V3 a -> V4 a
point 0)
{-# ANN m33_to_m44 "HLint: ignore Use camelCase" #-}

-- |The identity matrix for any dimension vector.
--
-- >>> 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)
identity :: (Num a, Traversable t, Applicative t) => t (t a)
identity :: t (t a)
identity = t a -> t (t a)
forall (t :: * -> *) a. (Traversable t, Num a) => t a -> t (t a)
scaled (a -> t a
forall (f :: * -> *) a. Applicative f => a -> f a
pure 1)

-- |Extract the translation vector (first three entries of the last
-- column) from a 3x4 or 4x4 matrix.
translation :: (Representable t, R3 t, R4 v) => Lens' (t (v a)) (V3 a)
translation :: Lens' (t (v a)) (V3 a)
translation = LensLike (Context a a) (v a) (v a) a a
-> Lens (t (v a)) (t (v a)) (t a) (t a)
forall (f :: * -> *) a b s t.
Representable f =>
LensLike (Context a b) s t a b -> Lens (f s) (f t) (f a) (f b)
column LensLike (Context a a) (v a) (v a) a a
forall (t :: * -> *) a. R4 t => Lens' (t a) a
_w((t a -> f (t a)) -> t (v a) -> f (t (v a)))
-> ((V3 a -> f (V3 a)) -> t a -> f (t a))
-> (V3 a -> f (V3 a))
-> t (v a)
-> f (t (v a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(V3 a -> f (V3 a)) -> t a -> f (t a)
forall (t :: * -> *) a. R3 t => Lens' (t a) (V3 a)
_xyz
{-
translation f rs = aux <$> f (view _w <$> view _xyz rs)
 where aux (V3 x y z) = (_x._w .~ x) . (_y._w .~ y) . (_z._w .~ z) $ rs

-- translation :: (R3 t, R4 v, Functor f, Functor t) => (V3 a -> f (V3 a)) -> t (v a) -> f (t a)
-- translation = (. fmap (^._w)) . _xyz where
--   x ^. l = getConst (l Const x)
-}

-- |Extract a 2x2 matrix from a matrix of higher dimensions by dropping excess
-- rows and columns.
_m22 :: (Representable t, R2 t, R2 v) => Lens' (t (v a)) (M22 a)
_m22 :: Lens' (t (v a)) (M22 a)
_m22 = LensLike (Context (V2 a) (V2 a)) (v a) (v a) (V2 a) (V2 a)
-> Lens (t (v a)) (t (v a)) (t (V2 a)) (t (V2 a))
forall (f :: * -> *) a b s t.
Representable f =>
LensLike (Context a b) s t a b -> Lens (f s) (f t) (f a) (f b)
column LensLike (Context (V2 a) (V2 a)) (v a) (v a) (V2 a) (V2 a)
forall (t :: * -> *) a. R2 t => Lens' (t a) (V2 a)
_xy((t (V2 a) -> f (t (V2 a))) -> t (v a) -> f (t (v a)))
-> ((M22 a -> f (M22 a)) -> t (V2 a) -> f (t (V2 a)))
-> (M22 a -> f (M22 a))
-> t (v a)
-> f (t (v a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(M22 a -> f (M22 a)) -> t (V2 a) -> f (t (V2 a))
forall (t :: * -> *) a. R2 t => Lens' (t a) (V2 a)
_xy

-- |Extract a 2x3 matrix from a matrix of higher dimensions by dropping excess
-- rows and columns.
_m23 :: (Representable t, R2 t, R3 v) => Lens' (t (v a)) (M23 a)
_m23 :: Lens' (t (v a)) (M23 a)
_m23 = LensLike (Context (V3 a) (V3 a)) (v a) (v a) (V3 a) (V3 a)
-> Lens (t (v a)) (t (v a)) (t (V3 a)) (t (V3 a))
forall (f :: * -> *) a b s t.
Representable f =>
LensLike (Context a b) s t a b -> Lens (f s) (f t) (f a) (f b)
column LensLike (Context (V3 a) (V3 a)) (v a) (v a) (V3 a) (V3 a)
forall (t :: * -> *) a. R3 t => Lens' (t a) (V3 a)
_xyz((t (V3 a) -> f (t (V3 a))) -> t (v a) -> f (t (v a)))
-> ((M23 a -> f (M23 a)) -> t (V3 a) -> f (t (V3 a)))
-> (M23 a -> f (M23 a))
-> t (v a)
-> f (t (v a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(M23 a -> f (M23 a)) -> t (V3 a) -> f (t (V3 a))
forall (t :: * -> *) a. R2 t => Lens' (t a) (V2 a)
_xy

-- |Extract a 2x4 matrix from a matrix of higher dimensions by dropping excess
-- rows and columns.
_m24 :: (Representable t, R2 t, R4 v) => Lens' (t (v a)) (M24 a)
_m24 :: Lens' (t (v a)) (M24 a)
_m24 = LensLike (Context (V4 a) (V4 a)) (v a) (v a) (V4 a) (V4 a)
-> Lens (t (v a)) (t (v a)) (t (V4 a)) (t (V4 a))
forall (f :: * -> *) a b s t.
Representable f =>
LensLike (Context a b) s t a b -> Lens (f s) (f t) (f a) (f b)
column LensLike (Context (V4 a) (V4 a)) (v a) (v a) (V4 a) (V4 a)
forall (t :: * -> *) a. R4 t => Lens' (t a) (V4 a)
_xyzw((t (V4 a) -> f (t (V4 a))) -> t (v a) -> f (t (v a)))
-> ((M24 a -> f (M24 a)) -> t (V4 a) -> f (t (V4 a)))
-> (M24 a -> f (M24 a))
-> t (v a)
-> f (t (v a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(M24 a -> f (M24 a)) -> t (V4 a) -> f (t (V4 a))
forall (t :: * -> *) a. R2 t => Lens' (t a) (V2 a)
_xy

-- |Extract a 3x2 matrix from a matrix of higher dimensions by dropping excess
-- rows and columns.
_m32 :: (Representable t, R3 t, R2 v) => Lens' (t (v a)) (M32 a)
_m32 :: Lens' (t (v a)) (M32 a)
_m32 = LensLike (Context (V2 a) (V2 a)) (v a) (v a) (V2 a) (V2 a)
-> Lens (t (v a)) (t (v a)) (t (V2 a)) (t (V2 a))
forall (f :: * -> *) a b s t.
Representable f =>
LensLike (Context a b) s t a b -> Lens (f s) (f t) (f a) (f b)
column LensLike (Context (V2 a) (V2 a)) (v a) (v a) (V2 a) (V2 a)
forall (t :: * -> *) a. R2 t => Lens' (t a) (V2 a)
_xy((t (V2 a) -> f (t (V2 a))) -> t (v a) -> f (t (v a)))
-> ((M32 a -> f (M32 a)) -> t (V2 a) -> f (t (V2 a)))
-> (M32 a -> f (M32 a))
-> t (v a)
-> f (t (v a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(M32 a -> f (M32 a)) -> t (V2 a) -> f (t (V2 a))
forall (t :: * -> *) a. R3 t => Lens' (t a) (V3 a)
_xyz

-- |Extract a 3x3 matrix from a matrix of higher dimensions by dropping excess
-- rows and columns.
_m33 :: (Representable t, R3 t, R3 v) => Lens' (t (v a)) (M33 a)
_m33 :: Lens' (t (v a)) (M33 a)
_m33 = LensLike (Context (V3 a) (V3 a)) (v a) (v a) (V3 a) (V3 a)
-> Lens (t (v a)) (t (v a)) (t (V3 a)) (t (V3 a))
forall (f :: * -> *) a b s t.
Representable f =>
LensLike (Context a b) s t a b -> Lens (f s) (f t) (f a) (f b)
column LensLike (Context (V3 a) (V3 a)) (v a) (v a) (V3 a) (V3 a)
forall (t :: * -> *) a. R3 t => Lens' (t a) (V3 a)
_xyz((t (V3 a) -> f (t (V3 a))) -> t (v a) -> f (t (v a)))
-> ((M33 a -> f (M33 a)) -> t (V3 a) -> f (t (V3 a)))
-> (M33 a -> f (M33 a))
-> t (v a)
-> f (t (v a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(M33 a -> f (M33 a)) -> t (V3 a) -> f (t (V3 a))
forall (t :: * -> *) a. R3 t => Lens' (t a) (V3 a)
_xyz

-- |Extract a 3x4 matrix from a matrix of higher dimensions by dropping excess
-- rows and columns.
_m34 :: (Representable t, R3 t, R4 v) => Lens' (t (v a)) (M34 a)
_m34 :: Lens' (t (v a)) (M34 a)
_m34 = LensLike (Context (V4 a) (V4 a)) (v a) (v a) (V4 a) (V4 a)
-> Lens (t (v a)) (t (v a)) (t (V4 a)) (t (V4 a))
forall (f :: * -> *) a b s t.
Representable f =>
LensLike (Context a b) s t a b -> Lens (f s) (f t) (f a) (f b)
column LensLike (Context (V4 a) (V4 a)) (v a) (v a) (V4 a) (V4 a)
forall (t :: * -> *) a. R4 t => Lens' (t a) (V4 a)
_xyzw((t (V4 a) -> f (t (V4 a))) -> t (v a) -> f (t (v a)))
-> ((M34 a -> f (M34 a)) -> t (V4 a) -> f (t (V4 a)))
-> (M34 a -> f (M34 a))
-> t (v a)
-> f (t (v a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(M34 a -> f (M34 a)) -> t (V4 a) -> f (t (V4 a))
forall (t :: * -> *) a. R3 t => Lens' (t a) (V3 a)
_xyz

-- |Extract a 4x2 matrix from a matrix of higher dimensions by dropping excess
-- rows and columns.
_m42 :: (Representable t, R4 t, R2 v) => Lens' (t (v a)) (M42 a)
_m42 :: Lens' (t (v a)) (M42 a)
_m42 = LensLike (Context (V2 a) (V2 a)) (v a) (v a) (V2 a) (V2 a)
-> Lens (t (v a)) (t (v a)) (t (V2 a)) (t (V2 a))
forall (f :: * -> *) a b s t.
Representable f =>
LensLike (Context a b) s t a b -> Lens (f s) (f t) (f a) (f b)
column LensLike (Context (V2 a) (V2 a)) (v a) (v a) (V2 a) (V2 a)
forall (t :: * -> *) a. R2 t => Lens' (t a) (V2 a)
_xy((t (V2 a) -> f (t (V2 a))) -> t (v a) -> f (t (v a)))
-> ((M42 a -> f (M42 a)) -> t (V2 a) -> f (t (V2 a)))
-> (M42 a -> f (M42 a))
-> t (v a)
-> f (t (v a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(M42 a -> f (M42 a)) -> t (V2 a) -> f (t (V2 a))
forall (t :: * -> *) a. R4 t => Lens' (t a) (V4 a)
_xyzw

-- |Extract a 4x3 matrix from a matrix of higher dimensions by dropping excess
-- rows and columns.
_m43 :: (Representable t, R4 t, R3 v) => Lens' (t (v a)) (M43 a)
_m43 :: Lens' (t (v a)) (M43 a)
_m43 = LensLike (Context (V3 a) (V3 a)) (v a) (v a) (V3 a) (V3 a)
-> Lens (t (v a)) (t (v a)) (t (V3 a)) (t (V3 a))
forall (f :: * -> *) a b s t.
Representable f =>
LensLike (Context a b) s t a b -> Lens (f s) (f t) (f a) (f b)
column LensLike (Context (V3 a) (V3 a)) (v a) (v a) (V3 a) (V3 a)
forall (t :: * -> *) a. R3 t => Lens' (t a) (V3 a)
_xyz((t (V3 a) -> f (t (V3 a))) -> t (v a) -> f (t (v a)))
-> ((M43 a -> f (M43 a)) -> t (V3 a) -> f (t (V3 a)))
-> (M43 a -> f (M43 a))
-> t (v a)
-> f (t (v a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(M43 a -> f (M43 a)) -> t (V3 a) -> f (t (V3 a))
forall (t :: * -> *) a. R4 t => Lens' (t a) (V4 a)
_xyzw

-- |Extract a 4x4 matrix from a matrix of higher dimensions by dropping excess
-- rows and columns.
_m44 :: (Representable t, R4 t, R4 v) => Lens' (t (v a)) (M44 a)
_m44 :: Lens' (t (v a)) (M44 a)
_m44 = LensLike (Context (V4 a) (V4 a)) (v a) (v a) (V4 a) (V4 a)
-> Lens (t (v a)) (t (v a)) (t (V4 a)) (t (V4 a))
forall (f :: * -> *) a b s t.
Representable f =>
LensLike (Context a b) s t a b -> Lens (f s) (f t) (f a) (f b)
column LensLike (Context (V4 a) (V4 a)) (v a) (v a) (V4 a) (V4 a)
forall (t :: * -> *) a. R4 t => Lens' (t a) (V4 a)
_xyzw((t (V4 a) -> f (t (V4 a))) -> t (v a) -> f (t (v a)))
-> ((M44 a -> f (M44 a)) -> t (V4 a) -> f (t (V4 a)))
-> (M44 a -> f (M44 a))
-> t (v a)
-> f (t (v a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(M44 a -> f (M44 a)) -> t (V4 a) -> f (t (V4 a))
forall (t :: * -> *) a. R4 t => Lens' (t a) (V4 a)
_xyzw

-- |2x2 matrix determinant.
--
-- >>> det22 (V2 (V2 a b) (V2 c d))
-- a * d - b * c
det22 :: Num a => M22 a -> a
det22 :: M22 a -> a
det22 (V2 (V2 a :: a
a b :: a
b) (V2 c :: a
c d :: a
d)) = a
a a -> a -> a
forall a. Num a => a -> a -> a
* a
d a -> a -> a
forall a. Num a => a -> a -> a
- a
b a -> a -> a
forall a. Num a => a -> a -> a
* a
c
{-# INLINE det22 #-}

-- |3x3 matrix determinant.
--
-- >>> det33 (V3 (V3 a b c) (V3 d e f) (V3 g h i))
-- a * (e * i - f * h) - d * (b * i - c * h) + g * (b * f - c * e)
det33 :: Num a => M33 a -> a
det33 :: M33 a -> a
det33 (V3 (V3 a :: a
a b :: a
b c :: a
c)
          (V3 d :: a
d e :: a
e f :: a
f)
          (V3 g :: a
g h :: a
h i :: a
i)) = a
a a -> a -> a
forall a. Num a => a -> a -> a
* (a
ea -> a -> a
forall a. Num a => a -> a -> a
*a
ia -> a -> a
forall a. Num a => a -> a -> a
-a
fa -> a -> a
forall a. Num a => a -> a -> a
*a
h) a -> a -> a
forall a. Num a => a -> a -> a
- a
d a -> a -> a
forall a. Num a => a -> a -> a
* (a
ba -> a -> a
forall a. Num a => a -> a -> a
*a
ia -> a -> a
forall a. Num a => a -> a -> a
-a
ca -> a -> a
forall a. Num a => a -> a -> a
*a
h) a -> a -> a
forall a. Num a => a -> a -> a
+ a
g a -> a -> a
forall a. Num a => a -> a -> a
* (a
ba -> a -> a
forall a. Num a => a -> a -> a
*a
fa -> a -> a
forall a. Num a => a -> a -> a
-a
ca -> a -> a
forall a. Num a => a -> a -> a
*a
e)
{-# INLINE det33 #-}

-- |4x4 matrix determinant.
det44 :: Num a => M44 a -> a
det44 :: M44 a -> a
det44 (V4 (V4 i00 :: a
i00 i01 :: a
i01 i02 :: a
i02 i03 :: a
i03)
          (V4 i10 :: a
i10 i11 :: a
i11 i12 :: a
i12 i13 :: a
i13)
          (V4 i20 :: a
i20 i21 :: a
i21 i22 :: a
i22 i23 :: a
i23)
          (V4 i30 :: a
i30 i31 :: a
i31 i32 :: a
i32 i33 :: a
i33)) =
  let
    s0 :: a
s0 = a
i00 a -> a -> a
forall a. Num a => a -> a -> a
* a
i11 a -> a -> a
forall a. Num a => a -> a -> a
- a
i10 a -> a -> a
forall a. Num a => a -> a -> a
* a
i01
    s1 :: a
s1 = a
i00 a -> a -> a
forall a. Num a => a -> a -> a
* a
i12 a -> a -> a
forall a. Num a => a -> a -> a
- a
i10 a -> a -> a
forall a. Num a => a -> a -> a
* a
i02
    s2 :: a
s2 = a
i00 a -> a -> a
forall a. Num a => a -> a -> a
* a
i13 a -> a -> a
forall a. Num a => a -> a -> a
- a
i10 a -> a -> a
forall a. Num a => a -> a -> a
* a
i03
    s3 :: a
s3 = a
i01 a -> a -> a
forall a. Num a => a -> a -> a
* a
i12 a -> a -> a
forall a. Num a => a -> a -> a
- a
i11 a -> a -> a
forall a. Num a => a -> a -> a
* a
i02
    s4 :: a
s4 = a
i01 a -> a -> a
forall a. Num a => a -> a -> a
* a
i13 a -> a -> a
forall a. Num a => a -> a -> a
- a
i11 a -> a -> a
forall a. Num a => a -> a -> a
* a
i03
    s5 :: a
s5 = a
i02 a -> a -> a
forall a. Num a => a -> a -> a
* a
i13 a -> a -> a
forall a. Num a => a -> a -> a
- a
i12 a -> a -> a
forall a. Num a => a -> a -> a
* a
i03

    c5 :: a
c5 = a
i22 a -> a -> a
forall a. Num a => a -> a -> a
* a
i33 a -> a -> a
forall a. Num a => a -> a -> a
- a
i32 a -> a -> a
forall a. Num a => a -> a -> a
* a
i23
    c4 :: a
c4 = a
i21 a -> a -> a
forall a. Num a => a -> a -> a
* a
i33 a -> a -> a
forall a. Num a => a -> a -> a
- a
i31 a -> a -> a
forall a. Num a => a -> a -> a
* a
i23
    c3 :: a
c3 = a
i21 a -> a -> a
forall a. Num a => a -> a -> a
* a
i32 a -> a -> a
forall a. Num a => a -> a -> a
- a
i31 a -> a -> a
forall a. Num a => a -> a -> a
* a
i22
    c2 :: a
c2 = a
i20 a -> a -> a
forall a. Num a => a -> a -> a
* a
i33 a -> a -> a
forall a. Num a => a -> a -> a
- a
i30 a -> a -> a
forall a. Num a => a -> a -> a
* a
i23
    c1 :: a
c1 = a
i20 a -> a -> a
forall a. Num a => a -> a -> a
* a
i32 a -> a -> a
forall a. Num a => a -> a -> a
- a
i30 a -> a -> a
forall a. Num a => a -> a -> a
* a
i22
    c0 :: a
c0 = a
i20 a -> a -> a
forall a. Num a => a -> a -> a
* a
i31 a -> a -> a
forall a. Num a => a -> a -> a
- a
i30 a -> a -> a
forall a. Num a => a -> a -> a
* a
i21
  in a
s0 a -> a -> a
forall a. Num a => a -> a -> a
* a
c5 a -> a -> a
forall a. Num a => a -> a -> a
- a
s1 a -> a -> a
forall a. Num a => a -> a -> a
* a
c4 a -> a -> a
forall a. Num a => a -> a -> a
+ a
s2 a -> a -> a
forall a. Num a => a -> a -> a
* a
c3 a -> a -> a
forall a. Num a => a -> a -> a
+ a
s3 a -> a -> a
forall a. Num a => a -> a -> a
* a
c2 a -> a -> a
forall a. Num a => a -> a -> a
- a
s4 a -> a -> a
forall a. Num a => a -> a -> a
* a
c1 a -> a -> a
forall a. Num a => a -> a -> a
+ a
s5 a -> a -> a
forall a. Num a => a -> a -> a
* a
c0
{-# INLINE det44 #-}

-- |2x2 matrix inverse.
--
-- >>> inv22 $ V2 (V2 1 2) (V2 3 4)
-- V2 (V2 (-2.0) 1.0) (V2 1.5 (-0.5))
inv22 :: Fractional a => M22 a -> M22 a
inv22 :: M22 a -> M22 a
inv22 m :: M22 a
m@(V2 (V2 a :: a
a b :: a
b) (V2 c :: a
c d :: a
d)) = (1 a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
det) a -> M22 a -> M22 a
forall (m :: * -> *) (r :: * -> *) a.
(Functor m, Functor r, Num a) =>
a -> m (r a) -> m (r a)
*!! V2 a -> V2 a -> M22 a
forall a. a -> a -> V2 a
V2 (a -> a -> V2 a
forall a. a -> a -> V2 a
V2 a
d (-a
b)) (a -> a -> V2 a
forall a. a -> a -> V2 a
V2 (-a
c) a
a)
  where det :: a
det = M22 a -> a
forall a. Num a => M22 a -> a
det22 M22 a
m
{-# INLINE inv22 #-}

-- |3x3 matrix inverse.
--
-- >>> inv33 $ V3 (V3 1 2 4) (V3 4 2 2) (V3 1 1 1)
-- V3 (V3 0.0 0.5 (-1.0)) (V3 (-0.5) (-0.75) 3.5) (V3 0.5 0.25 (-1.5))
inv33 :: Fractional a => M33 a -> M33 a
inv33 :: M33 a -> M33 a
inv33 m :: M33 a
m@(V3 (V3 a :: a
a b :: a
b c :: a
c)
            (V3 d :: a
d e :: a
e f :: a
f)
            (V3 g :: a
g h :: a
h i :: a
i))
  = (1 a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
det) a -> M33 a -> M33 a
forall (m :: * -> *) (r :: * -> *) a.
(Functor m, Functor r, Num a) =>
a -> m (r a) -> m (r a)
*!! V3 a -> V3 a -> V3 a -> M33 a
forall a. a -> a -> a -> V3 a
V3 (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
a' a
b' a
c')
                     (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
d' a
e' a
f')
                     (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
g' a
h' a
i')
  where a' :: a
a' = (a, a, a, a) -> a
forall a. Num a => (a, a, a, a) -> a
cofactor (a
e,a
f,a
h,a
i)
        b' :: a
b' = (a, a, a, a) -> a
forall a. Num a => (a, a, a, a) -> a
cofactor (a
c,a
b,a
i,a
h)
        c' :: a
c' = (a, a, a, a) -> a
forall a. Num a => (a, a, a, a) -> a
cofactor (a
b,a
c,a
e,a
f)
        d' :: a
d' = (a, a, a, a) -> a
forall a. Num a => (a, a, a, a) -> a
cofactor (a
f,a
d,a
i,a
g)
        e' :: a
e' = (a, a, a, a) -> a
forall a. Num a => (a, a, a, a) -> a
cofactor (a
a,a
c,a
g,a
i)
        f' :: a
f' = (a, a, a, a) -> a
forall a. Num a => (a, a, a, a) -> a
cofactor (a
c,a
a,a
f,a
d)
        g' :: a
g' = (a, a, a, a) -> a
forall a. Num a => (a, a, a, a) -> a
cofactor (a
d,a
e,a
g,a
h)
        h' :: a
h' = (a, a, a, a) -> a
forall a. Num a => (a, a, a, a) -> a
cofactor (a
b,a
a,a
h,a
g)
        i' :: a
i' = (a, a, a, a) -> a
forall a. Num a => (a, a, a, a) -> a
cofactor (a
a,a
b,a
d,a
e)
        cofactor :: (a, a, a, a) -> a
cofactor (q :: a
q,r :: a
r,s :: a
s,t :: a
t) = M22 a -> a
forall a. Num a => M22 a -> a
det22 (V2 a -> V2 a -> M22 a
forall a. a -> a -> V2 a
V2 (a -> a -> V2 a
forall a. a -> a -> V2 a
V2 a
q a
r) (a -> a -> V2 a
forall a. a -> a -> V2 a
V2 a
s a
t))
        det :: a
det = M33 a -> a
forall a. Num a => M33 a -> a
det33 M33 a
m
{-# INLINE inv33 #-}


-- | 'transpose' is just an alias for 'distribute'
--
-- > transpose (V3 (V2 1 2) (V2 3 4) (V2 5 6))
-- V2 (V3 1 3 5) (V3 2 4 6)
transpose :: (Distributive g, Functor f) => f (g a) -> g (f a)
transpose :: f (g a) -> g (f a)
transpose = f (g a) -> g (f a)
forall (g :: * -> *) (f :: * -> *) a.
(Distributive g, Functor f) =>
f (g a) -> g (f a)
distribute
{-# INLINE transpose #-}

-- |4x4 matrix inverse.
inv44 :: Fractional a => M44 a -> M44 a
inv44 :: M44 a -> M44 a
inv44 (V4 (V4 i00 :: a
i00 i01 :: a
i01 i02 :: a
i02 i03 :: a
i03)
          (V4 i10 :: a
i10 i11 :: a
i11 i12 :: a
i12 i13 :: a
i13)
          (V4 i20 :: a
i20 i21 :: a
i21 i22 :: a
i22 i23 :: a
i23)
          (V4 i30 :: a
i30 i31 :: a
i31 i32 :: a
i32 i33 :: a
i33)) =
  let s0 :: a
s0 = a
i00 a -> a -> a
forall a. Num a => a -> a -> a
* a
i11 a -> a -> a
forall a. Num a => a -> a -> a
- a
i10 a -> a -> a
forall a. Num a => a -> a -> a
* a
i01
      s1 :: a
s1 = a
i00 a -> a -> a
forall a. Num a => a -> a -> a
* a
i12 a -> a -> a
forall a. Num a => a -> a -> a
- a
i10 a -> a -> a
forall a. Num a => a -> a -> a
* a
i02
      s2 :: a
s2 = a
i00 a -> a -> a
forall a. Num a => a -> a -> a
* a
i13 a -> a -> a
forall a. Num a => a -> a -> a
- a
i10 a -> a -> a
forall a. Num a => a -> a -> a
* a
i03
      s3 :: a
s3 = a
i01 a -> a -> a
forall a. Num a => a -> a -> a
* a
i12 a -> a -> a
forall a. Num a => a -> a -> a
- a
i11 a -> a -> a
forall a. Num a => a -> a -> a
* a
i02
      s4 :: a
s4 = a
i01 a -> a -> a
forall a. Num a => a -> a -> a
* a
i13 a -> a -> a
forall a. Num a => a -> a -> a
- a
i11 a -> a -> a
forall a. Num a => a -> a -> a
* a
i03
      s5 :: a
s5 = a
i02 a -> a -> a
forall a. Num a => a -> a -> a
* a
i13 a -> a -> a
forall a. Num a => a -> a -> a
- a
i12 a -> a -> a
forall a. Num a => a -> a -> a
* a
i03
      c5 :: a
c5 = a
i22 a -> a -> a
forall a. Num a => a -> a -> a
* a
i33 a -> a -> a
forall a. Num a => a -> a -> a
- a
i32 a -> a -> a
forall a. Num a => a -> a -> a
* a
i23
      c4 :: a
c4 = a
i21 a -> a -> a
forall a. Num a => a -> a -> a
* a
i33 a -> a -> a
forall a. Num a => a -> a -> a
- a
i31 a -> a -> a
forall a. Num a => a -> a -> a
* a
i23
      c3 :: a
c3 = a
i21 a -> a -> a
forall a. Num a => a -> a -> a
* a
i32 a -> a -> a
forall a. Num a => a -> a -> a
- a
i31 a -> a -> a
forall a. Num a => a -> a -> a
* a
i22
      c2 :: a
c2 = a
i20 a -> a -> a
forall a. Num a => a -> a -> a
* a
i33 a -> a -> a
forall a. Num a => a -> a -> a
- a
i30 a -> a -> a
forall a. Num a => a -> a -> a
* a
i23
      c1 :: a
c1 = a
i20 a -> a -> a
forall a. Num a => a -> a -> a
* a
i32 a -> a -> a
forall a. Num a => a -> a -> a
- a
i30 a -> a -> a
forall a. Num a => a -> a -> a
* a
i22
      c0 :: a
c0 = a
i20 a -> a -> a
forall a. Num a => a -> a -> a
* a
i31 a -> a -> a
forall a. Num a => a -> a -> a
- a
i30 a -> a -> a
forall a. Num a => a -> a -> a
* a
i21
      det :: a
det = a
s0 a -> a -> a
forall a. Num a => a -> a -> a
* a
c5 a -> a -> a
forall a. Num a => a -> a -> a
- a
s1 a -> a -> a
forall a. Num a => a -> a -> a
* a
c4 a -> a -> a
forall a. Num a => a -> a -> a
+ a
s2 a -> a -> a
forall a. Num a => a -> a -> a
* a
c3 a -> a -> a
forall a. Num a => a -> a -> a
+ a
s3 a -> a -> a
forall a. Num a => a -> a -> a
* a
c2 a -> a -> a
forall a. Num a => a -> a -> a
- a
s4 a -> a -> a
forall a. Num a => a -> a -> a
* a
c1 a -> a -> a
forall a. Num a => a -> a -> a
+ a
s5 a -> a -> a
forall a. Num a => a -> a -> a
* a
c0
      invDet :: a
invDet = a -> a
forall a. Fractional a => a -> a
recip a
det
  in a
invDet a -> M44 a -> M44 a
forall (m :: * -> *) (r :: * -> *) a.
(Functor m, Functor r, Num a) =>
a -> m (r a) -> m (r a)
*!! V4 a -> V4 a -> V4 a -> V4 a -> M44 a
forall a. a -> a -> a -> a -> V4 a
V4 (a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 (a
i11 a -> a -> a
forall a. Num a => a -> a -> a
* a
c5 a -> a -> a
forall a. Num a => a -> a -> a
- a
i12 a -> a -> a
forall a. Num a => a -> a -> a
* a
c4 a -> a -> a
forall a. Num a => a -> a -> a
+ a
i13 a -> a -> a
forall a. Num a => a -> a -> a
* a
c3)
                       (-a
i01 a -> a -> a
forall a. Num a => a -> a -> a
* a
c5 a -> a -> a
forall a. Num a => a -> a -> a
+ a
i02 a -> a -> a
forall a. Num a => a -> a -> a
* a
c4 a -> a -> a
forall a. Num a => a -> a -> a
- a
i03 a -> a -> a
forall a. Num a => a -> a -> a
* a
c3)
                       (a
i31 a -> a -> a
forall a. Num a => a -> a -> a
* a
s5 a -> a -> a
forall a. Num a => a -> a -> a
- a
i32 a -> a -> a
forall a. Num a => a -> a -> a
* a
s4 a -> a -> a
forall a. Num a => a -> a -> a
+ a
i33 a -> a -> a
forall a. Num a => a -> a -> a
* a
s3)
                       (-a
i21 a -> a -> a
forall a. Num a => a -> a -> a
* a
s5 a -> a -> a
forall a. Num a => a -> a -> a
+ a
i22 a -> a -> a
forall a. Num a => a -> a -> a
* a
s4 a -> a -> a
forall a. Num a => a -> a -> a
- a
i23 a -> a -> a
forall a. Num a => a -> a -> a
* a
s3))
                   (a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 (-a
i10 a -> a -> a
forall a. Num a => a -> a -> a
* a
c5 a -> a -> a
forall a. Num a => a -> a -> a
+ a
i12 a -> a -> a
forall a. Num a => a -> a -> a
* a
c2 a -> a -> a
forall a. Num a => a -> a -> a
- a
i13 a -> a -> a
forall a. Num a => a -> a -> a
* a
c1)
                       (a
i00 a -> a -> a
forall a. Num a => a -> a -> a
* a
c5 a -> a -> a
forall a. Num a => a -> a -> a
- a
i02 a -> a -> a
forall a. Num a => a -> a -> a
* a
c2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
i03 a -> a -> a
forall a. Num a => a -> a -> a
* a
c1)
                       (-a
i30 a -> a -> a
forall a. Num a => a -> a -> a
* a
s5 a -> a -> a
forall a. Num a => a -> a -> a
+ a
i32 a -> a -> a
forall a. Num a => a -> a -> a
* a
s2 a -> a -> a
forall a. Num a => a -> a -> a
- a
i33 a -> a -> a
forall a. Num a => a -> a -> a
* a
s1)
                       (a
i20 a -> a -> a
forall a. Num a => a -> a -> a
* a
s5 a -> a -> a
forall a. Num a => a -> a -> a
- a
i22 a -> a -> a
forall a. Num a => a -> a -> a
* a
s2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
i23 a -> a -> a
forall a. Num a => a -> a -> a
* a
s1))
                   (a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 (a
i10 a -> a -> a
forall a. Num a => a -> a -> a
* a
c4 a -> a -> a
forall a. Num a => a -> a -> a
- a
i11 a -> a -> a
forall a. Num a => a -> a -> a
* a
c2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
i13 a -> a -> a
forall a. Num a => a -> a -> a
* a
c0)
                       (-a
i00 a -> a -> a
forall a. Num a => a -> a -> a
* a
c4 a -> a -> a
forall a. Num a => a -> a -> a
+ a
i01 a -> a -> a
forall a. Num a => a -> a -> a
* a
c2 a -> a -> a
forall a. Num a => a -> a -> a
- a
i03 a -> a -> a
forall a. Num a => a -> a -> a
* a
c0)
                       (a
i30 a -> a -> a
forall a. Num a => a -> a -> a
* a
s4 a -> a -> a
forall a. Num a => a -> a -> a
- a
i31 a -> a -> a
forall a. Num a => a -> a -> a
* a
s2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
i33 a -> a -> a
forall a. Num a => a -> a -> a
* a
s0)
                       (-a
i20 a -> a -> a
forall a. Num a => a -> a -> a
* a
s4 a -> a -> a
forall a. Num a => a -> a -> a
+ a
i21 a -> a -> a
forall a. Num a => a -> a -> a
* a
s2 a -> a -> a
forall a. Num a => a -> a -> a
- a
i23 a -> a -> a
forall a. Num a => a -> a -> a
* a
s0))
                   (a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 (-a
i10 a -> a -> a
forall a. Num a => a -> a -> a
* a
c3 a -> a -> a
forall a. Num a => a -> a -> a
+ a
i11 a -> a -> a
forall a. Num a => a -> a -> a
* a
c1 a -> a -> a
forall a. Num a => a -> a -> a
- a
i12 a -> a -> a
forall a. Num a => a -> a -> a
* a
c0)
                       (a
i00 a -> a -> a
forall a. Num a => a -> a -> a
* a
c3 a -> a -> a
forall a. Num a => a -> a -> a
- a
i01 a -> a -> a
forall a. Num a => a -> a -> a
* a
c1 a -> a -> a
forall a. Num a => a -> a -> a
+ a
i02 a -> a -> a
forall a. Num a => a -> a -> a
* a
c0)
                       (-a
i30 a -> a -> a
forall a. Num a => a -> a -> a
* a
s3 a -> a -> a
forall a. Num a => a -> a -> a
+ a
i31 a -> a -> a
forall a. Num a => a -> a -> a
* a
s1 a -> a -> a
forall a. Num a => a -> a -> a
- a
i32 a -> a -> a
forall a. Num a => a -> a -> a
* a
s0)
                       (a
i20 a -> a -> a
forall a. Num a => a -> a -> a
* a
s3 a -> a -> a
forall a. Num a => a -> a -> a
- a
i21 a -> a -> a
forall a. Num a => a -> a -> a
* a
s1 a -> a -> a
forall a. Num a => a -> a -> a
+ a
i22 a -> a -> a
forall a. Num a => a -> a -> a
* a
s0))
{-# INLINE inv44 #-}