{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE UndecidableInstances #-}
module Numeric.Matrix.Internal
( MatrixTranspose (..)
, SquareMatrix (..)
, MatrixDeterminant (..)
, MatrixInverse (..)
, Matrix
, HomTransform4 (..)
, Mat22f, Mat23f, Mat24f
, Mat32f, Mat33f, Mat34f
, Mat42f, Mat43f, Mat44f
, Mat22d, Mat23d, Mat24d
, Mat32d, Mat33d, Mat34d
, Mat42d, Mat43d, Mat44d
, mat22, mat33, mat44
, (%*)
) where
import GHC.Base
import Numeric.DataFrame.Contraction ((%*))
import Numeric.DataFrame.Internal.PrimArray
import Numeric.DataFrame.Type
import Numeric.Dimensions
import Numeric.Scalar.Internal
import Numeric.Vector.Internal
type Matrix (t :: l) (n :: k) (m :: k) = DataFrame t '[n,m]
class MatrixTranspose t (n :: k) (m :: k) where
transpose :: Matrix t n m -> Matrix t m n
class SquareMatrix t (n :: Nat) where
eye :: Matrix t n n
diag :: Scalar t -> Matrix t n n
trace :: Matrix t n n -> Scalar t
class MatrixDeterminant t (n :: Nat) where
det :: Matrix t n n -> Scalar t
class MatrixInverse t (n :: Nat) where
inverse :: Matrix t n n -> Matrix t n n
class HomTransform4 t where
translate4 :: Vector t 4 -> Matrix t 4 4
translate3 :: Vector t 3 -> Matrix t 4 4
rotateX :: t -> Matrix t 4 4
rotateY :: t -> Matrix t 4 4
rotateZ :: t -> Matrix t 4 4
rotate :: Vector t 3 -> t -> Matrix t 4 4
rotateEuler :: t
-> t
-> t
-> Matrix t 4 4
lookAt :: Vector t 3
-> Vector t 3
-> Vector t 3
-> Matrix t 4 4
perspective :: t
-> t
-> t
-> t
-> Matrix t 4 4
orthogonal :: t
-> t
-> t
-> t
-> Matrix t 4 4
toHomPoint :: Vector t 3 -> Vector t 4
toHomVector :: Vector t 3 -> Vector t 4
fromHom :: Vector t 4 -> Vector t 3
type Mat22f = Matrix Float 2 2
type Mat32f = Matrix Float 3 2
type Mat42f = Matrix Float 4 2
type Mat23f = Matrix Float 2 3
type Mat33f = Matrix Float 3 3
type Mat43f = Matrix Float 4 3
type Mat24f = Matrix Float 2 4
type Mat34f = Matrix Float 3 4
type Mat44f = Matrix Float 4 4
type Mat22d = Matrix Double 2 2
type Mat32d = Matrix Double 3 2
type Mat42d = Matrix Double 4 2
type Mat23d = Matrix Double 2 3
type Mat33d = Matrix Double 3 3
type Mat43d = Matrix Double 4 3
type Mat24d = Matrix Double 2 4
type Mat34d = Matrix Double 3 4
type Mat44d = Matrix Double 4 4
mat22 :: PrimBytes (t :: Type)
=> Vector t 2 -> Vector t 2 -> Matrix t 2 2
mat22 :: Vector t 2 -> Vector t 2 -> Matrix t 2 2
mat22 = (PrimBytes t, Dimensions '[2, 2]) => PackDF t '[2] 2 (Matrix t 2 2)
forall t (d :: Nat) (ds :: [Nat]).
(PrimBytes t, Dimensions (d : ds)) =>
PackDF t ds d (DataFrame t (d : ds))
packDF @_ @2 @'[2]
{-# INLINE mat22 #-}
mat33 :: PrimBytes (t :: Type)
=> Vector t 3 -> Vector t 3 -> Vector t 3 -> Matrix t 3 3
mat33 :: Vector t 3 -> Vector t 3 -> Vector t 3 -> Matrix t 3 3
mat33 = (PrimBytes t, Dimensions '[3, 3]) => PackDF t '[3] 3 (Matrix t 3 3)
forall t (d :: Nat) (ds :: [Nat]).
(PrimBytes t, Dimensions (d : ds)) =>
PackDF t ds d (DataFrame t (d : ds))
packDF @_ @3 @'[3]
{-# INLINE mat33 #-}
mat44 :: PrimBytes (t :: Type)
=> Vector t 4
-> Vector t 4
-> Vector t 4
-> Vector t 4
-> Matrix t 4 4
mat44 :: Vector t 4
-> Vector t 4 -> Vector t 4 -> Vector t 4 -> Matrix t 4 4
mat44 = (PrimBytes t, Dimensions '[4, 4]) => PackDF t '[4] 4 (Matrix t 4 4)
forall t (d :: Nat) (ds :: [Nat]).
(PrimBytes t, Dimensions (d : ds)) =>
PackDF t ds d (DataFrame t (d : ds))
packDF @_ @4 @'[4]
{-# INLINE mat44 #-}
instance ( KnownDim n, KnownDim m
, PrimArray t (Matrix t n m)
, PrimArray t (Matrix t m n)
) => MatrixTranspose t (n :: Nat) (m :: Nat) where
transpose :: Matrix t n m -> Matrix t m n
transpose Matrix t n m
df = case Matrix t n m -> Either t CumulDims
forall t a. PrimArray t a => a -> Either t CumulDims
uniqueOrCumulDims Matrix t n m
df of
Left t
a -> t -> Matrix t m n
forall t a. PrimArray t a => t -> a
broadcast t
a
Right CumulDims
_
| Word
wm <- KnownDim m => Word
forall k (n :: k). KnownDim n => Word
dimVal' @m
, Word
wn <- KnownDim n => Word
forall k (n :: k). KnownDim n => Word
dimVal' @n
, Int#
m <- case Word
wm of W# Word#
w -> Word# -> Int#
word2Int# Word#
w
, Int#
n <- case Word
wn of W# Word#
w -> Word# -> Int#
word2Int# Word#
w
-> let f :: (Int, Int) -> (# (Int, Int), t #)
f ( I# Int#
i, I# Int#
j )
| Int# -> Bool
isTrue# (Int#
i Int# -> Int# -> Int#
==# Int#
n) = (Int, Int) -> (# (Int, Int), t #)
f ( Int
0, Int# -> Int
I# (Int#
j Int# -> Int# -> Int#
+# Int#
1#) )
| Bool
otherwise = (# ( Int# -> Int
I# (Int#
i Int# -> Int# -> Int#
+# Int#
1#), Int# -> Int
I# Int#
j )
, Int# -> Matrix t n m -> t
forall t a. PrimArray t a => Int# -> a -> t
ix# (Int#
i Int# -> Int# -> Int#
*# Int#
m Int# -> Int# -> Int#
+# Int#
j) Matrix t n m
df
#)
in case CumulDims
-> ((Int, Int) -> (# (Int, Int), t #))
-> (Int, Int)
-> (# (Int, Int), Matrix t m n #)
forall t a s.
PrimArray t a =>
CumulDims -> (s -> (# s, t #)) -> s -> (# s, a #)
gen# ([Word] -> CumulDims
CumulDims [Word
wmWord -> Word -> Word
forall a. Num a => a -> a -> a
*Word
wn, Word
wn, Word
1]) (Int, Int) -> (# (Int, Int), t #)
f (Int
0,Int
0) of (# (Int, Int)
_, Matrix t m n
r #) -> Matrix t m n
r
instance MatrixTranspose (t :: Type) (xn :: XNat) (xm :: XNat) where
transpose :: Matrix t xn xm -> Matrix t xm xn
transpose (XFrame (df :: DataFrame t ns))
| ((Dim y
D :: Dim n) :* (Dim y
D :: Dim m) :* TypedList Dim ys
U) <- Dimensions ns => TypedList Dim ns
forall k (ds :: [k]). Dimensions ds => Dims ds
dims @ns
, Dict (PrimBytes t)
Dict <- DataFrame t '[Head ns, Head (Tail ns)] -> Dict (PrimBytes t)
forall t (d :: Nat) (ds :: [Nat]).
KnownBackend t (d : ds) =>
DataFrame t (d : ds) -> Dict (PrimBytes t)
inferPrimElem DataFrame t ns
DataFrame t '[Head ns, Head (Tail ns)]
df
= DataFrame t '[y, y] -> Matrix t xm xn
forall l (ts :: l) (xns :: [XNat]) (ns :: [Nat]).
(All KnownDimType xns, FixedDims xns ns, Dimensions ns,
KnownBackends ts ns) =>
DataFrame ts ns -> DataFrame ts xns
XFrame (Matrix t y y -> DataFrame t '[y, y]
forall k k (t :: k) (n :: k) (m :: k).
MatrixTranspose t n m =>
Matrix t n m -> Matrix t m n
transpose DataFrame t ns
Matrix t y y
df :: Matrix t m n)
#if !MIN_VERSION_GLASGOW_HASKELL(8,10,0,0)
transpose _ = error "MatrixTranspose/transpose: impossible argument"
#endif
instance (KnownDim n, PrimArray t (Matrix t n n), Num t)
=> SquareMatrix t n where
eye :: Matrix t n n
eye
| Word
n <- KnownDim n => Word
forall k (n :: k). KnownDim n => Word
dimVal' @n
= let f :: Word -> (# Word, t #)
f Word
0 = (# Word
n , t
1 #)
f Word
k = (# Word
k Word -> Word -> Word
forall a. Num a => a -> a -> a
- Word
1, t
0 #)
in case CumulDims
-> (Word -> (# Word, t #)) -> Word -> (# Word, Matrix t n n #)
forall t a s.
PrimArray t a =>
CumulDims -> (s -> (# s, t #)) -> s -> (# s, a #)
gen# ([Word] -> CumulDims
CumulDims [Word
nWord -> Word -> Word
forall a. Num a => a -> a -> a
*Word
n, Word
n, Word
1]) Word -> (# Word, t #)
f Word
0 of
(# Word
_, Matrix t n n
r #) -> Matrix t n n
r
diag :: Scalar t -> Matrix t n n
diag Scalar t
se
| Word
n <- KnownDim n => Word
forall k (n :: k). KnownDim n => Word
dimVal' @n
, t
e <- Scalar t -> t
forall t. DataFrame t '[] -> t
unScalar Scalar t
se
= let f :: Word -> (# Word, t #)
f Word
0 = (# Word
n , t
e #)
f Word
k = (# Word
k Word -> Word -> Word
forall a. Num a => a -> a -> a
- Word
1, t
0 #)
in case CumulDims
-> (Word -> (# Word, t #)) -> Word -> (# Word, Matrix t n n #)
forall t a s.
PrimArray t a =>
CumulDims -> (s -> (# s, t #)) -> s -> (# s, a #)
gen# ([Word] -> CumulDims
CumulDims [Word
nWord -> Word -> Word
forall a. Num a => a -> a -> a
*Word
n, Word
n, Word
1]) Word -> (# Word, t #)
f Word
0 of
(# Word
_, Matrix t n n
r #) -> Matrix t n n
r
trace :: Matrix t n n -> Scalar t
trace Matrix t n n
df
| I# Int#
n <- Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word -> Int) -> Word -> Int
forall a b. (a -> b) -> a -> b
$ KnownDim n => Word
forall k (n :: k). KnownDim n => Word
dimVal' @n
, Int#
n1 <- Int#
n Int# -> Int# -> Int#
+# Int#
1#
= let f :: Int# -> t
f Int#
0# = Int# -> Matrix t n n -> t
forall t a. PrimArray t a => Int# -> a -> t
ix# Int#
0# Matrix t n n
df
f Int#
k = Int# -> Matrix t n n -> t
forall t a. PrimArray t a => Int# -> a -> t
ix# Int#
k Matrix t n n
df t -> t -> t
forall a. Num a => a -> a -> a
+ Int# -> t
f (Int#
k Int# -> Int# -> Int#
-# Int#
n1)
in t -> Scalar t
forall t. t -> DataFrame t '[]
scalar (t -> Scalar t) -> t -> Scalar t
forall a b. (a -> b) -> a -> b
$ Int# -> t
f (Int#
n Int# -> Int# -> Int#
*# Int#
n Int# -> Int# -> Int#
-# Int#
1#)