{-# LANGUAGE CPP #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- |
-- Module      : Graphics.Color.Algebra
-- Copyright   : (c) Alexey Kuleshevich 2019-2020
-- License     : BSD3
-- Maintainer  : Alexey Kuleshevich <lehins@yandex.ru>
-- Stability   : experimental
-- Portability : non-portable
--
module Graphics.Color.Algebra
  ( -- * 2D
    V2(..)
    -- * 3D
  , V3(..)
  , showV3
  , dotProduct
  , M3x3(..)
  , showM3x3
  , detM3x3
  , invertM3x3
  , multM3x3byV3
  , multM3x3byM3x3
  , multM3x3byV3d
  , transposeM3x3
  , module Graphics.Color.Algebra.Elevator
  -- * Helpers
  , showsType
  , asProxy
  ) where

import Data.Typeable
import Foreign.Ptr
import Control.Applicative
import Foreign.Storable
import Graphics.Color.Algebra.Elevator


--------
-- V2 --
--------

-- | A 2D vector with @x@ and @y@ coordinates.
data V2 a = V2 !a !a
  deriving (V2 a -> V2 a -> Bool
(V2 a -> V2 a -> Bool) -> (V2 a -> V2 a -> Bool) -> Eq (V2 a)
forall a. Eq a => V2 a -> V2 a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: V2 a -> V2 a -> Bool
$c/= :: forall a. Eq a => V2 a -> V2 a -> Bool
== :: V2 a -> V2 a -> Bool
$c== :: forall a. Eq a => V2 a -> V2 a -> Bool
Eq, Eq (V2 a)
Eq (V2 a)
-> (V2 a -> V2 a -> Ordering)
-> (V2 a -> V2 a -> Bool)
-> (V2 a -> V2 a -> Bool)
-> (V2 a -> V2 a -> Bool)
-> (V2 a -> V2 a -> Bool)
-> (V2 a -> V2 a -> V2 a)
-> (V2 a -> V2 a -> V2 a)
-> Ord (V2 a)
V2 a -> V2 a -> Bool
V2 a -> V2 a -> Ordering
V2 a -> V2 a -> V2 a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (V2 a)
forall a. Ord a => V2 a -> V2 a -> Bool
forall a. Ord a => V2 a -> V2 a -> Ordering
forall a. Ord a => V2 a -> V2 a -> V2 a
min :: V2 a -> V2 a -> V2 a
$cmin :: forall a. Ord a => V2 a -> V2 a -> V2 a
max :: V2 a -> V2 a -> V2 a
$cmax :: forall a. Ord a => V2 a -> V2 a -> V2 a
>= :: V2 a -> V2 a -> Bool
$c>= :: forall a. Ord a => V2 a -> V2 a -> Bool
> :: V2 a -> V2 a -> Bool
$c> :: forall a. Ord a => V2 a -> V2 a -> Bool
<= :: V2 a -> V2 a -> Bool
$c<= :: forall a. Ord a => V2 a -> V2 a -> Bool
< :: V2 a -> V2 a -> Bool
$c< :: forall a. Ord a => V2 a -> V2 a -> Bool
compare :: V2 a -> V2 a -> Ordering
$ccompare :: forall a. Ord a => V2 a -> V2 a -> Ordering
$cp1Ord :: forall a. Ord a => Eq (V2 a)
Ord)

instance Elevator a => Show (V2 a) where
  showsPrec :: Int -> V2 a -> ShowS
showsPrec Int
_ (V2 a
x a
y) =
    (Char
'[' Char -> ShowS
forall a. a -> [a] -> [a]
:) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ShowS
forall e. Elevator e => e -> ShowS
toShowS a
x ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char
',' Char -> ShowS
forall a. a -> [a] -> [a]
:) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ShowS
forall e. Elevator e => e -> ShowS
toShowS a
y ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
" ]" String -> ShowS
forall a. [a] -> [a] -> [a]
++)

instance Functor V2 where
  fmap :: (a -> b) -> V2 a -> V2 b
fmap a -> b
f (V2 a
x a
y) = b -> b -> V2 b
forall a. a -> a -> V2 a
V2 (a -> b
f a
x) (a -> b
f a
y)
  {-# INLINE fmap #-}


zipWithV2 :: (a -> b -> c) -> V2 a -> V2 b -> V2 c
zipWithV2 :: (a -> b -> c) -> V2 a -> V2 b -> V2 c
zipWithV2 a -> b -> c
f (V2 a
x1 a
y1) (V2 b
x2 b
y2) = c -> c -> V2 c
forall a. a -> a -> V2 a
V2 (a -> b -> c
f a
x1 b
x2) (a -> b -> c
f a
y1 b
y2)
{-# INLINE zipWithV2 #-}

instance Applicative V2 where
  pure :: a -> V2 a
pure a
x = a -> a -> V2 a
forall a. a -> a -> V2 a
V2 a
x a
x
  {-# INLINE pure #-}
  <*> :: V2 (a -> b) -> V2 a -> V2 b
(<*>) (V2 a -> b
fx1 a -> b
fy1) (V2 a
x2 a
y2) = b -> b -> V2 b
forall a. a -> a -> V2 a
V2 (a -> b
fx1 a
x2) (a -> b
fy1 a
y2)
  {-# INLINE (<*>) #-}
#if MIN_VERSION_base(4,10,0)
  liftA2 :: (a -> b -> c) -> V2 a -> V2 b -> V2 c
liftA2 = (a -> b -> c) -> V2 a -> V2 b -> V2 c
forall a b c. (a -> b -> c) -> V2 a -> V2 b -> V2 c
zipWithV2
  {-# INLINE liftA2 #-}
#endif

instance Foldable V2 where
  foldr :: (a -> b -> b) -> b -> V2 a -> b
foldr a -> b -> b
f b
acc (V2 a
x a
y) = a -> b -> b
f a
x (a -> b -> b
f a
y b
acc)
  {-# INLINE foldr #-}

instance Traversable V2 where
  traverse :: (a -> f b) -> V2 a -> f (V2 b)
traverse a -> f b
f (V2 a
x a
y) = b -> b -> V2 b
forall a. a -> a -> V2 a
V2 (b -> b -> V2 b) -> f b -> f (b -> V2 b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
x f (b -> V2 b) -> f b -> f (V2 b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> f b
f a
y
  {-# INLINE traverse #-}

instance Num a => Num (V2 a) where
  + :: V2 a -> V2 a -> V2 a
(+)         = (a -> a -> a) -> V2 a -> V2 a -> V2 a
forall a b c. (a -> b -> c) -> V2 a -> V2 b -> V2 c
zipWithV2 a -> a -> a
forall a. Num a => a -> a -> a
(+)
  {-# INLINE (+) #-}
  (-)         = (a -> a -> a) -> V2 a -> V2 a -> V2 a
forall a b c. (a -> b -> c) -> V2 a -> V2 b -> V2 c
zipWithV2 (-)
  {-# INLINE (-) #-}
  * :: V2 a -> V2 a -> V2 a
(*)         = (a -> a -> a) -> V2 a -> V2 a -> V2 a
forall a b c. (a -> b -> c) -> V2 a -> V2 b -> V2 c
zipWithV2 a -> a -> a
forall a. Num a => a -> a -> a
(*)
  {-# INLINE (*) #-}
  abs :: V2 a -> V2 a
abs         = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Num a => a -> a
abs
  {-# INLINE abs #-}
  signum :: V2 a -> V2 a
signum      = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Num a => a -> a
signum
  {-# INLINE signum #-}
  fromInteger :: Integer -> V2 a
fromInteger = a -> V2 a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> V2 a) -> (Integer -> a) -> Integer -> V2 a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> a
forall a. Num a => Integer -> a
fromInteger
  {-# INLINE fromInteger #-}


instance Fractional a => Fractional (V2 a) where
  / :: V2 a -> V2 a -> V2 a
(/)          = (a -> a -> a) -> V2 a -> V2 a -> V2 a
forall a b c. (a -> b -> c) -> V2 a -> V2 b -> V2 c
zipWithV2 a -> a -> a
forall a. Fractional a => a -> a -> a
(/)
  {-# INLINE (/) #-}
  recip :: V2 a -> V2 a
recip        = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Fractional a => a -> a
recip
  {-# INLINE recip #-}
  fromRational :: Rational -> V2 a
fromRational = a -> V2 a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> V2 a) -> (Rational -> a) -> Rational -> V2 a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> a
forall a. Fractional a => Rational -> a
fromRational
  {-# INLINE fromRational #-}


instance Floating a => Floating (V2 a) where
  pi :: V2 a
pi      = a -> V2 a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Floating a => a
pi
  {-# INLINE pi #-}
  exp :: V2 a -> V2 a
exp     = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
exp
  {-# INLINE exp #-}
  log :: V2 a -> V2 a
log     = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
log
  {-# INLINE log #-}
  sin :: V2 a -> V2 a
sin     = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
sin
  {-# INLINE sin #-}
  cos :: V2 a -> V2 a
cos     = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
cos
  {-# INLINE cos #-}
  asin :: V2 a -> V2 a
asin    = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
asin
  {-# INLINE asin #-}
  atan :: V2 a -> V2 a
atan    = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
atan
  {-# INLINE atan #-}
  acos :: V2 a -> V2 a
acos    = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
acos
  {-# INLINE acos #-}
  sinh :: V2 a -> V2 a
sinh    = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
sinh
  {-# INLINE sinh #-}
  cosh :: V2 a -> V2 a
cosh    = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
cosh
  {-# INLINE cosh #-}
  asinh :: V2 a -> V2 a
asinh   = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
asinh
  {-# INLINE asinh #-}
  atanh :: V2 a -> V2 a
atanh   = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
atanh
  {-# INLINE atanh #-}
  acosh :: V2 a -> V2 a
acosh   = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
acosh
  {-# INLINE acosh #-}


instance Storable e => Storable (V2 e) where
  sizeOf :: V2 e -> Int
sizeOf V2 e
_ = Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* e -> Int
forall a. Storable a => a -> Int
sizeOf (e
forall a. HasCallStack => a
undefined :: e)
  {-# INLINE sizeOf #-}
  alignment :: V2 e -> Int
alignment V2 e
_ = e -> Int
forall a. Storable a => a -> Int
alignment (e
forall a. HasCallStack => a
undefined :: e)
  {-# INLINE alignment #-}
  peek :: Ptr (V2 e) -> IO (V2 e)
peek Ptr (V2 e)
p =
    let q :: Ptr b
q = Ptr (V2 e) -> Ptr b
forall a b. Ptr a -> Ptr b
castPtr Ptr (V2 e)
p
     in e -> e -> V2 e
forall a. a -> a -> V2 a
V2 (e -> e -> V2 e) -> IO e -> IO (e -> V2 e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr e -> IO e
forall a. Storable a => Ptr a -> IO a
peek Ptr e
forall b. Ptr b
q IO (e -> V2 e) -> IO e -> IO (V2 e)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Ptr e -> Int -> IO e
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr e
forall b. Ptr b
q Int
1
  {-# INLINE peek #-}
  poke :: Ptr (V2 e) -> V2 e -> IO ()
poke Ptr (V2 e)
p (V2 e
v0 e
v1) =
    let q :: Ptr b
q = Ptr (V2 e) -> Ptr b
forall a b. Ptr a -> Ptr b
castPtr Ptr (V2 e)
p
     in Ptr e -> e -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr e
forall b. Ptr b
q e
v0 IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Ptr e -> Int -> e -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr e
forall b. Ptr b
q Int
1 e
v1
  {-# INLINE poke #-}


--------
-- V3 --
--------

-- | A 3D vector with @x@, @y@ and @z@ coordinates.
data V3 a = V3 !a !a !a
  deriving (V3 a -> V3 a -> Bool
(V3 a -> V3 a -> Bool) -> (V3 a -> V3 a -> Bool) -> Eq (V3 a)
forall a. Eq a => V3 a -> V3 a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: V3 a -> V3 a -> Bool
$c/= :: forall a. Eq a => V3 a -> V3 a -> Bool
== :: V3 a -> V3 a -> Bool
$c== :: forall a. Eq a => V3 a -> V3 a -> Bool
Eq, Eq (V3 a)
Eq (V3 a)
-> (V3 a -> V3 a -> Ordering)
-> (V3 a -> V3 a -> Bool)
-> (V3 a -> V3 a -> Bool)
-> (V3 a -> V3 a -> Bool)
-> (V3 a -> V3 a -> Bool)
-> (V3 a -> V3 a -> V3 a)
-> (V3 a -> V3 a -> V3 a)
-> Ord (V3 a)
V3 a -> V3 a -> Bool
V3 a -> V3 a -> Ordering
V3 a -> V3 a -> V3 a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (V3 a)
forall a. Ord a => V3 a -> V3 a -> Bool
forall a. Ord a => V3 a -> V3 a -> Ordering
forall a. Ord a => V3 a -> V3 a -> V3 a
min :: V3 a -> V3 a -> V3 a
$cmin :: forall a. Ord a => V3 a -> V3 a -> V3 a
max :: V3 a -> V3 a -> V3 a
$cmax :: forall a. Ord a => V3 a -> V3 a -> V3 a
>= :: V3 a -> V3 a -> Bool
$c>= :: forall a. Ord a => V3 a -> V3 a -> Bool
> :: V3 a -> V3 a -> Bool
$c> :: forall a. Ord a => V3 a -> V3 a -> Bool
<= :: V3 a -> V3 a -> Bool
$c<= :: forall a. Ord a => V3 a -> V3 a -> Bool
< :: V3 a -> V3 a -> Bool
$c< :: forall a. Ord a => V3 a -> V3 a -> Bool
compare :: V3 a -> V3 a -> Ordering
$ccompare :: forall a. Ord a => V3 a -> V3 a -> Ordering
$cp1Ord :: forall a. Ord a => Eq (V3 a)
Ord)

instance Elevator a => Show (V3 a) where
  showsPrec :: Int -> V3 a -> ShowS
showsPrec Int
_ (V3 a
x a
y a
z) =
    (Char
'[' Char -> ShowS
forall a. a -> [a] -> [a]
:) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ShowS
forall e. Elevator e => e -> ShowS
toShowS a
x ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char
',' Char -> ShowS
forall a. a -> [a] -> [a]
:) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ShowS
forall e. Elevator e => e -> ShowS
toShowS a
y ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char
',' Char -> ShowS
forall a. a -> [a] -> [a]
:) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ShowS
forall e. Elevator e => e -> ShowS
toShowS a
z ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
" ]" String -> ShowS
forall a. [a] -> [a] -> [a]
++)

showV3 :: Show a => V3 a -> String
showV3 :: V3 a -> String
showV3 (V3 a
x a
y a
z) = [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [String
"[ ", a -> String
forall a. Show a => a -> String
show a
x, String
", ", a -> String
forall a. Show a => a -> String
show a
y, String
", ", a -> String
forall a. Show a => a -> String
show a
z, String
" ]"]

-- | Mulitply a 1x3 vector by a 3x1 vector, i.e. dot product.
--
-- @since 0.1.0
dotProduct :: Num a => V3 a -> V3 a -> a
dotProduct :: V3 a -> V3 a -> a
dotProduct (V3 a
u0 a
u1 a
u2) (V3 a
v0 a
v1 a
v2) = a
u0 a -> a -> a
forall a. Num a => a -> a -> a
* a
v0 a -> a -> a
forall a. Num a => a -> a -> a
+ a
u1 a -> a -> a
forall a. Num a => a -> a -> a
* a
v1 a -> a -> a
forall a. Num a => a -> a -> a
+ a
u2 a -> a -> a
forall a. Num a => a -> a -> a
* a
v2
{-# INLINE dotProduct #-}


zipWithV3 :: (a -> b -> c) -> V3 a -> V3 b -> V3 c
zipWithV3 :: (a -> b -> c) -> V3 a -> V3 b -> V3 c
zipWithV3 a -> b -> c
f (V3 a
x1 a
y1 a
z1) (V3 b
x2 b
y2 b
z2) = c -> c -> c -> V3 c
forall a. a -> a -> a -> V3 a
V3 (a -> b -> c
f a
x1 b
x2) (a -> b -> c
f a
y1 b
y2) (a -> b -> c
f a
z1 b
z2)
{-# INLINE zipWithV3 #-}

instance Functor V3 where
  fmap :: (a -> b) -> V3 a -> V3 b
fmap a -> b
f (V3 a
x a
y a
z) = b -> b -> b -> V3 b
forall a. a -> a -> a -> V3 a
V3 (a -> b
f a
x) (a -> b
f a
y) (a -> b
f a
z)
  {-# INLINE fmap #-}

instance Applicative V3 where
  pure :: a -> V3 a
pure a
x = a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
x a
x a
x
  {-# INLINE pure #-}
  <*> :: V3 (a -> b) -> V3 a -> V3 b
(<*>) (V3 a -> b
fx1 a -> b
fy1 a -> b
fz1) (V3 a
x2 a
y2 a
z2) = b -> b -> b -> V3 b
forall a. a -> a -> a -> V3 a
V3 (a -> b
fx1 a
x2) (a -> b
fy1 a
y2) (a -> b
fz1 a
z2)
  {-# INLINE (<*>) #-}
#if MIN_VERSION_base(4,10,0)
  liftA2 :: (a -> b -> c) -> V3 a -> V3 b -> V3 c
liftA2 = (a -> b -> c) -> V3 a -> V3 b -> V3 c
forall a b c. (a -> b -> c) -> V3 a -> V3 b -> V3 c
zipWithV3
  {-# INLINE liftA2 #-}
#endif

instance Foldable V3 where
  foldr :: (a -> b -> b) -> b -> V3 a -> b
foldr a -> b -> b
f b
acc (V3 a
x a
y a
z) = a -> b -> b
f a
x (a -> b -> b
f a
y (a -> b -> b
f a
z b
acc))
  {-# INLINE foldr #-}

instance Traversable V3 where
  traverse :: (a -> f b) -> V3 a -> f (V3 b)
traverse a -> f b
f (V3 a
x a
y a
z) = b -> b -> b -> V3 b
forall a. a -> a -> a -> V3 a
V3 (b -> b -> b -> V3 b) -> f b -> f (b -> b -> V3 b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
x f (b -> b -> V3 b) -> f b -> f (b -> V3 b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> f b
f a
y f (b -> V3 b) -> f b -> f (V3 b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> f b
f a
z
  {-# INLINE traverse #-}

instance Num a => Num (V3 a) where
  + :: V3 a -> V3 a -> V3 a
(+)         = (a -> a -> a) -> V3 a -> V3 a -> V3 a
forall a b c. (a -> b -> c) -> V3 a -> V3 b -> V3 c
zipWithV3 a -> a -> a
forall a. Num a => a -> a -> a
(+)
  {-# INLINE (+) #-}
  (-)         = (a -> a -> a) -> V3 a -> V3 a -> V3 a
forall a b c. (a -> b -> c) -> V3 a -> V3 b -> V3 c
zipWithV3 (-)
  {-# INLINE (-) #-}
  * :: V3 a -> V3 a -> V3 a
(*)         = (a -> a -> a) -> V3 a -> V3 a -> V3 a
forall a b c. (a -> b -> c) -> V3 a -> V3 b -> V3 c
zipWithV3 a -> a -> a
forall a. Num a => a -> a -> a
(*)
  {-# INLINE (*) #-}
  abs :: V3 a -> V3 a
abs         = (a -> a) -> V3 a -> V3 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Num a => a -> a
abs
  {-# INLINE abs #-}
  signum :: V3 a -> V3 a
signum      = (a -> a) -> V3 a -> V3 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Num a => a -> a
signum
  {-# INLINE signum #-}
  fromInteger :: Integer -> V3 a
fromInteger = a -> V3 a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> V3 a) -> (Integer -> a) -> Integer -> V3 a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> a
forall a. Num a => Integer -> a
fromInteger
  {-# INLINE fromInteger #-}


instance Fractional a => Fractional (V3 a) where
  / :: V3 a -> V3 a -> V3 a
(/)          = (a -> a -> a) -> V3 a -> V3 a -> V3 a
forall a b c. (a -> b -> c) -> V3 a -> V3 b -> V3 c
zipWithV3 a -> a -> a
forall a. Fractional a => a -> a -> a
(/)
  {-# INLINE (/) #-}
  recip :: V3 a -> V3 a
recip        = (a -> a) -> V3 a -> V3 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Fractional a => a -> a
recip
  {-# INLINE recip #-}
  fromRational :: Rational -> V3 a
fromRational = a -> V3 a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> V3 a) -> (Rational -> a) -> Rational -> V3 a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> a
forall a. Fractional a => Rational -> a
fromRational
  {-# INLINE fromRational #-}


instance Floating a => Floating (V3 a) where
  pi :: V3 a
pi      = a -> V3 a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Floating a => a
pi
  {-# INLINE pi #-}
  exp :: V3 a -> V3 a
exp     = (a -> a) -> V3 a -> V3 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
exp
  {-# INLINE exp #-}
  log :: V3 a -> V3 a
log     = (a -> a) -> V3 a -> V3 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
log
  {-# INLINE log #-}
  sin :: V3 a -> V3 a
sin     = (a -> a) -> V3 a -> V3 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
sin
  {-# INLINE sin #-}
  cos :: V3 a -> V3 a
cos     = (a -> a) -> V3 a -> V3 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
cos
  {-# INLINE cos #-}
  asin :: V3 a -> V3 a
asin    = (a -> a) -> V3 a -> V3 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
asin
  {-# INLINE asin #-}
  atan :: V3 a -> V3 a
atan    = (a -> a) -> V3 a -> V3 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
atan
  {-# INLINE atan #-}
  acos :: V3 a -> V3 a
acos    = (a -> a) -> V3 a -> V3 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
acos
  {-# INLINE acos #-}
  sinh :: V3 a -> V3 a
sinh    = (a -> a) -> V3 a -> V3 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
sinh
  {-# INLINE sinh #-}
  cosh :: V3 a -> V3 a
cosh    = (a -> a) -> V3 a -> V3 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
cosh
  {-# INLINE cosh #-}
  asinh :: V3 a -> V3 a
asinh   = (a -> a) -> V3 a -> V3 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
asinh
  {-# INLINE asinh #-}
  atanh :: V3 a -> V3 a
atanh   = (a -> a) -> V3 a -> V3 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
atanh
  {-# INLINE atanh #-}
  acosh :: V3 a -> V3 a
acosh   = (a -> a) -> V3 a -> V3 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
acosh
  {-# INLINE acosh #-}


instance Storable e => Storable (V3 e) where
  sizeOf :: V3 e -> Int
sizeOf V3 e
_ = Int
3 Int -> Int -> Int
forall a. Num a => a -> a -> a
* e -> Int
forall a. Storable a => a -> Int
sizeOf (e
forall a. HasCallStack => a
undefined :: e)
  {-# INLINE sizeOf #-}
  alignment :: V3 e -> Int
alignment V3 e
_ = e -> Int
forall a. Storable a => a -> Int
alignment (e
forall a. HasCallStack => a
undefined :: e)
  {-# INLINE alignment #-}
  peek :: Ptr (V3 e) -> IO (V3 e)
peek Ptr (V3 e)
p = do
    let q :: Ptr b
q = Ptr (V3 e) -> Ptr b
forall a b. Ptr a -> Ptr b
castPtr Ptr (V3 e)
p
    e
v0 <- Ptr e -> IO e
forall a. Storable a => Ptr a -> IO a
peek Ptr e
forall b. Ptr b
q
    e
v1 <- Ptr e -> Int -> IO e
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr e
forall b. Ptr b
q Int
1
    e
v2 <- Ptr e -> Int -> IO e
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr e
forall b. Ptr b
q Int
2
    V3 e -> IO (V3 e)
forall (m :: * -> *) a. Monad m => a -> m a
return (V3 e -> IO (V3 e)) -> V3 e -> IO (V3 e)
forall a b. (a -> b) -> a -> b
$! e -> e -> e -> V3 e
forall a. a -> a -> a -> V3 a
V3 e
v0 e
v1 e
v2

  {-# INLINE peek #-}
  poke :: Ptr (V3 e) -> V3 e -> IO ()
poke Ptr (V3 e)
p (V3 e
v0 e
v1 e
v2) = do
    let q :: Ptr b
q = Ptr (V3 e) -> Ptr b
forall a b. Ptr a -> Ptr b
castPtr Ptr (V3 e)
p
    Ptr e -> e -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr e
forall b. Ptr b
q e
v0
    Ptr e -> Int -> e -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr e
forall b. Ptr b
q Int
1 e
v1
    Ptr e -> Int -> e -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr e
forall b. Ptr b
q Int
2 e
v2
  {-# INLINE poke #-}

----------
-- M3x3 --
----------



-- | A 3x3 Matrix
data M3x3 a = M3x3
  { M3x3 a -> V3 a
m3x3row0 :: {-# UNPACK #-}!(V3 a)
  , M3x3 a -> V3 a
m3x3row1 :: {-# UNPACK #-}!(V3 a)
  , M3x3 a -> V3 a
m3x3row2 :: {-# UNPACK #-}!(V3 a)
  } deriving (M3x3 a -> M3x3 a -> Bool
(M3x3 a -> M3x3 a -> Bool)
-> (M3x3 a -> M3x3 a -> Bool) -> Eq (M3x3 a)
forall a. Eq a => M3x3 a -> M3x3 a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: M3x3 a -> M3x3 a -> Bool
$c/= :: forall a. Eq a => M3x3 a -> M3x3 a -> Bool
== :: M3x3 a -> M3x3 a -> Bool
$c== :: forall a. Eq a => M3x3 a -> M3x3 a -> Bool
Eq)

instance Elevator a => Show (M3x3 a) where
  showsPrec :: Int -> M3x3 a -> ShowS
showsPrec Int
_ (M3x3 V3 a
v0 V3 a
v1 V3 a
v2) =
    (String
"[ " String -> ShowS
forall a. [a] -> [a] -> [a]
++) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. V3 a -> ShowS
forall a. Show a => a -> ShowS
shows V3 a
v0 ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"\n, " String -> ShowS
forall a. [a] -> [a] -> [a]
++) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. V3 a -> ShowS
forall a. Show a => a -> ShowS
shows V3 a
v1 ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"\n, " String -> ShowS
forall a. [a] -> [a] -> [a]
++) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. V3 a -> ShowS
forall a. Show a => a -> ShowS
shows V3 a
v2 ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
" ]" String -> ShowS
forall a. [a] -> [a] -> [a]
++)


showM3x3 :: Show a => M3x3 a -> String
showM3x3 :: M3x3 a -> String
showM3x3 (M3x3 V3 a
v0 V3 a
v1 V3 a
v2) =
  [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [String
"[ ", V3 a -> String
forall a. Show a => V3 a -> String
showV3 V3 a
v0, String
"\n, ", V3 a -> String
forall a. Show a => V3 a -> String
showV3 V3 a
v1, String
"\n, ", V3 a -> String
forall a. Show a => V3 a -> String
showV3 V3 a
v2, String
" ]"]


-- | Mulitply a 3x3 matrix by a 3x1 vector, while getting a vector back.
--
-- @since 0.1.0
multM3x3byV3 :: Num a => M3x3 a -> V3 a -> V3 a
multM3x3byV3 :: M3x3 a -> V3 a -> V3 a
multM3x3byV3 (M3x3 (V3 a
a a
b a
c)
                   (V3 a
d a
e a
f)
                   (V3 a
g a
h a
i)) (V3 a
v0 a
v1 a
v2) = a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 (a
a a -> a -> a
forall a. Num a => a -> a -> a
* a
v0 a -> a -> a
forall a. Num a => a -> a -> a
+ a
b a -> a -> a
forall a. Num a => a -> a -> a
* a
v1 a -> a -> a
forall a. Num a => a -> a -> a
+ a
c a -> a -> a
forall a. Num a => a -> a -> a
* a
v2)
                                                  (a
d a -> a -> a
forall a. Num a => a -> a -> a
* a
v0 a -> a -> a
forall a. Num a => a -> a -> a
+ a
e a -> a -> a
forall a. Num a => a -> a -> a
* a
v1 a -> a -> a
forall a. Num a => a -> a -> a
+ a
f a -> a -> a
forall a. Num a => a -> a -> a
* a
v2)
                                                  (a
g a -> a -> a
forall a. Num a => a -> a -> a
* a
v0 a -> a -> a
forall a. Num a => a -> a -> a
+ a
h a -> a -> a
forall a. Num a => a -> a -> a
* a
v1 a -> a -> a
forall a. Num a => a -> a -> a
+ a
i a -> a -> a
forall a. Num a => a -> a -> a
* a
v2)
{-# INLINE multM3x3byV3 #-}


multM3x3byM3x3 :: Num a => M3x3 a -> M3x3 a -> M3x3 a
multM3x3byM3x3 :: M3x3 a -> M3x3 a -> M3x3 a
multM3x3byM3x3 M3x3 a
m1 M3x3 a
m2 =
  V3 a -> V3 a -> V3 a -> M3x3 a
forall a. V3 a -> V3 a -> V3 a -> M3x3 a
M3x3
  (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 (a
a1 a -> a -> a
forall a. Num a => a -> a -> a
* a
a2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
b1 a -> a -> a
forall a. Num a => a -> a -> a
* a
d2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
c1 a -> a -> a
forall a. Num a => a -> a -> a
* a
g2) (a
a1 a -> a -> a
forall a. Num a => a -> a -> a
* a
b2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
b1 a -> a -> a
forall a. Num a => a -> a -> a
* a
e2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
c1 a -> a -> a
forall a. Num a => a -> a -> a
* a
h2) (a
a1 a -> a -> a
forall a. Num a => a -> a -> a
* a
c2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
b1 a -> a -> a
forall a. Num a => a -> a -> a
* a
f2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
c1 a -> a -> a
forall a. Num a => a -> a -> a
* a
i2))
  (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 (a
d1 a -> a -> a
forall a. Num a => a -> a -> a
* a
a2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
e1 a -> a -> a
forall a. Num a => a -> a -> a
* a
d2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
f1 a -> a -> a
forall a. Num a => a -> a -> a
* a
g2) (a
d1 a -> a -> a
forall a. Num a => a -> a -> a
* a
b2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
e1 a -> a -> a
forall a. Num a => a -> a -> a
* a
e2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
f1 a -> a -> a
forall a. Num a => a -> a -> a
* a
h2) (a
d1 a -> a -> a
forall a. Num a => a -> a -> a
* a
c2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
e1 a -> a -> a
forall a. Num a => a -> a -> a
* a
f2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
f1 a -> a -> a
forall a. Num a => a -> a -> a
* a
i2))
  (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 (a
g1 a -> a -> a
forall a. Num a => a -> a -> a
* a
a2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
h1 a -> a -> a
forall a. Num a => a -> a -> a
* a
d2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
i1 a -> a -> a
forall a. Num a => a -> a -> a
* a
g2) (a
g1 a -> a -> a
forall a. Num a => a -> a -> a
* a
b2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
h1 a -> a -> a
forall a. Num a => a -> a -> a
* a
e2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
i1 a -> a -> a
forall a. Num a => a -> a -> a
* a
h2) (a
g1 a -> a -> a
forall a. Num a => a -> a -> a
* a
c2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
h1 a -> a -> a
forall a. Num a => a -> a -> a
* a
f2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
i1 a -> a -> a
forall a. Num a => a -> a -> a
* a
i2))
  where
    M3x3 (V3 a
a1 a
b1 a
c1)
         (V3 a
d1 a
e1 a
f1)
         (V3 a
g1 a
h1 a
i1) = M3x3 a
m1
    M3x3 (V3 a
a2 a
b2 a
c2)
         (V3 a
d2 a
e2 a
f2)
         (V3 a
g2 a
h2 a
i2) = M3x3 a
m2
{-# INLINE multM3x3byM3x3 #-}

-- | Multiply a 3x3 matrix by another 3x3 diagonal matrix represented by a 1x3 vector
multM3x3byV3d :: Num a => M3x3 a -> V3 a -> M3x3 a
multM3x3byV3d :: M3x3 a -> V3 a -> M3x3 a
multM3x3byV3d M3x3 a
m1 V3 a
m2 =
  V3 a -> V3 a -> V3 a -> M3x3 a
forall a. V3 a -> V3 a -> V3 a -> M3x3 a
M3x3
  (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 (a
a1 a -> a -> a
forall a. Num a => a -> a -> a
* a
a2) (a
b1 a -> a -> a
forall a. Num a => a -> a -> a
* a
e2) (a
c1 a -> a -> a
forall a. Num a => a -> a -> a
* a
i2))
  (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 (a
d1 a -> a -> a
forall a. Num a => a -> a -> a
* a
a2) (a
e1 a -> a -> a
forall a. Num a => a -> a -> a
* a
e2) (a
f1 a -> a -> a
forall a. Num a => a -> a -> a
* a
i2))
  (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 (a
g1 a -> a -> a
forall a. Num a => a -> a -> a
* a
a2) (a
h1 a -> a -> a
forall a. Num a => a -> a -> a
* a
e2) (a
i1 a -> a -> a
forall a. Num a => a -> a -> a
* a
i2))
  where
    M3x3 (V3 a
a1 a
b1 a
c1)
         (V3 a
d1 a
e1 a
f1)
         (V3 a
g1 a
h1 a
i1) = M3x3 a
m1
    V3 a
a2 a
e2 a
i2 = V3 a
m2
{-# INLINE multM3x3byV3d #-}


-- | Invert a 3x3 matrix.
--
-- @since 0.1.0
invertM3x3 :: Fractional a => M3x3 a -> M3x3 a
invertM3x3 :: M3x3 a -> M3x3 a
invertM3x3 (M3x3 (V3 a
a a
b a
c)
                 (V3 a
d a
e a
f)
                 (V3 a
g a
h a
i)) =
  V3 a -> V3 a -> V3 a -> M3x3 a
forall a. V3 a -> V3 a -> V3 a -> M3x3 a
M3x3 (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 (a
a' a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
det) (a
d' a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
det) (a
g' a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
det))
       (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 (a
b' a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
det) (a
e' a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
det) (a
h' a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
det))
       (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 (a
c' a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
det) (a
f' a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
det) (a
i' a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
det))
  where
    !a' :: a
a' =   a
ea -> a -> a
forall a. Num a => a -> a -> a
*a
i a -> a -> a
forall a. Num a => a -> a -> a
- a
fa -> a -> a
forall a. Num a => a -> a -> a
*a
h
    !b' :: a
b' = -(a
da -> a -> a
forall a. Num a => a -> a -> a
*a
i a -> a -> a
forall a. Num a => a -> a -> a
- a
fa -> a -> a
forall a. Num a => a -> a -> a
*a
g)
    !c' :: a
c' =   a
da -> a -> a
forall a. Num a => a -> a -> a
*a
h a -> a -> a
forall a. Num a => a -> a -> a
- a
ea -> a -> a
forall a. Num a => a -> a -> a
*a
g
    !d' :: a
d' = -(a
ba -> a -> a
forall a. Num a => a -> a -> a
*a
i a -> a -> a
forall a. Num a => a -> a -> a
- a
ca -> a -> a
forall a. Num a => a -> a -> a
*a
h)
    !e' :: a
e' =   a
aa -> a -> a
forall a. Num a => a -> a -> a
*a
i a -> a -> a
forall a. Num a => a -> a -> a
- a
ca -> a -> a
forall a. Num a => a -> a -> a
*a
g
    !f' :: a
f' = -(a
aa -> a -> a
forall a. Num a => a -> a -> a
*a
h a -> a -> a
forall a. Num a => a -> a -> a
- a
ba -> a -> a
forall a. Num a => a -> a -> a
*a
g)
    !g' :: a
g' =   a
ba -> a -> a
forall a. Num a => a -> a -> a
*a
f a -> a -> a
forall a. Num a => a -> a -> a
- a
ca -> a -> a
forall a. Num a => a -> a -> a
*a
e
    !h' :: a
h' = -(a
aa -> a -> a
forall a. Num a => a -> a -> a
*a
f a -> a -> a
forall a. Num a => a -> a -> a
- a
ca -> a -> a
forall a. Num a => a -> a -> a
*a
d)
    !i' :: a
i' =   a
aa -> a -> a
forall a. Num a => a -> a -> a
*a
e a -> a -> a
forall a. Num a => a -> a -> a
- a
ba -> a -> a
forall a. Num a => a -> a -> a
*a
d
    !det :: a
det = a
aa -> a -> a
forall a. Num a => a -> a -> a
*a
a' a -> a -> a
forall a. Num a => a -> a -> a
+ a
ba -> a -> a
forall a. Num a => a -> a -> a
*a
b' a -> a -> a
forall a. Num a => a -> a -> a
+ a
ca -> a -> a
forall a. Num a => a -> a -> a
*a
c'
{-# INLINE invertM3x3 #-}


-- | Compute a determinant of a 3x3 matrix.
--
-- @since 0.1.0
detM3x3 :: Num a => M3x3 a -> a
detM3x3 :: M3x3 a -> a
detM3x3 (M3x3 (V3 a
i00 a
i01 a
i02)
              (V3 a
i10 a
i11 a
i12)
              (V3 a
i20 a
i21 a
i22)) = 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
i22 a -> a -> a
forall a. Num a => a -> a -> a
- a
i12 a -> a -> a
forall a. Num a => a -> a -> a
* a
i21) a -> a -> a
forall a. Num a => a -> a -> a
+
                                  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
i20 a -> a -> a
forall a. Num a => a -> a -> a
- a
i10 a -> a -> a
forall a. Num a => a -> a -> a
* a
i22) a -> a -> a
forall a. Num a => a -> a -> a
+
                                  a
i02 a -> a -> a
forall a. Num a => a -> a -> a
* (a
i10 a -> a -> a
forall a. Num a => a -> a -> a
* a
i21 a -> a -> a
forall a. Num a => a -> a -> a
- a
i11 a -> a -> a
forall a. Num a => a -> a -> a
* a
i20)
{-# INLINE detM3x3 #-}


transposeM3x3 :: M3x3 a -> M3x3 a
transposeM3x3 :: M3x3 a -> M3x3 a
transposeM3x3 (M3x3 (V3 a
i00 a
i01 a
i02)
                    (V3 a
i10 a
i11 a
i12)
                    (V3 a
i20 a
i21 a
i22)) = V3 a -> V3 a -> V3 a -> M3x3 a
forall a. V3 a -> V3 a -> V3 a -> M3x3 a
M3x3 (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
i00 a
i10 a
i20)
                                             (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
i01 a
i11 a
i21)
                                             (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
i02 a
i12 a
i22)
{-# INLINE transposeM3x3 #-}



pureM3x3 :: a -> M3x3 a
pureM3x3 :: a -> M3x3 a
pureM3x3 a
x = V3 a -> V3 a -> V3 a -> M3x3 a
forall a. V3 a -> V3 a -> V3 a -> M3x3 a
M3x3 (a -> V3 a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x) (a -> V3 a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x) (a -> V3 a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x)
{-# INLINE pureM3x3 #-}

mapM3x3 :: (a -> a) -> M3x3 a -> M3x3 a
mapM3x3 :: (a -> a) -> M3x3 a -> M3x3 a
mapM3x3 a -> a
f (M3x3 V3 a
v0 V3 a
v1 V3 a
v2) = V3 a -> V3 a -> V3 a -> M3x3 a
forall a. V3 a -> V3 a -> V3 a -> M3x3 a
M3x3 ((a -> a) -> V3 a -> V3 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
f V3 a
v0) ((a -> a) -> V3 a -> V3 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
f V3 a
v1) ((a -> a) -> V3 a -> V3 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
f V3 a
v2)
{-# INLINE mapM3x3 #-}

zipWithM3x3 :: (a -> b -> c) -> M3x3 a -> M3x3 b -> M3x3 c
zipWithM3x3 :: (a -> b -> c) -> M3x3 a -> M3x3 b -> M3x3 c
zipWithM3x3 a -> b -> c
f (M3x3 V3 a
v10 V3 a
v11 V3 a
v12) (M3x3 V3 b
v20 V3 b
v21 V3 b
v22) =
  V3 c -> V3 c -> V3 c -> M3x3 c
forall a. V3 a -> V3 a -> V3 a -> M3x3 a
M3x3 ((a -> b -> c) -> V3 a -> V3 b -> V3 c
forall a b c. (a -> b -> c) -> V3 a -> V3 b -> V3 c
zipWithV3 a -> b -> c
f V3 a
v10 V3 b
v20) ((a -> b -> c) -> V3 a -> V3 b -> V3 c
forall a b c. (a -> b -> c) -> V3 a -> V3 b -> V3 c
zipWithV3 a -> b -> c
f V3 a
v11 V3 b
v21) ((a -> b -> c) -> V3 a -> V3 b -> V3 c
forall a b c. (a -> b -> c) -> V3 a -> V3 b -> V3 c
zipWithV3 a -> b -> c
f V3 a
v12 V3 b
v22)
{-# INLINE zipWithM3x3 #-}


instance Functor M3x3 where
  fmap :: (a -> b) -> M3x3 a -> M3x3 b
fmap a -> b
f (M3x3 V3 a
v0 V3 a
v1 V3 a
v2) = V3 b -> V3 b -> V3 b -> M3x3 b
forall a. V3 a -> V3 a -> V3 a -> M3x3 a
M3x3 ((a -> b) -> V3 a -> V3 b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f V3 a
v0) ((a -> b) -> V3 a -> V3 b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f V3 a
v1) ((a -> b) -> V3 a -> V3 b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f V3 a
v2)
  {-# INLINE fmap #-}

instance Applicative M3x3 where
  pure :: a -> M3x3 a
pure a
x = V3 a -> V3 a -> V3 a -> M3x3 a
forall a. V3 a -> V3 a -> V3 a -> M3x3 a
M3x3 (a -> V3 a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x) (a -> V3 a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x) (a -> V3 a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x)
  {-# INLINE pure #-}
  <*> :: M3x3 (a -> b) -> M3x3 a -> M3x3 b
(<*>) (M3x3 V3 (a -> b)
fx1 V3 (a -> b)
fy1 V3 (a -> b)
fz1) (M3x3 V3 a
x2 V3 a
y2 V3 a
z2) = V3 b -> V3 b -> V3 b -> M3x3 b
forall a. V3 a -> V3 a -> V3 a -> M3x3 a
M3x3 (V3 (a -> b)
fx1 V3 (a -> b) -> V3 a -> V3 b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> V3 a
x2) (V3 (a -> b)
fy1 V3 (a -> b) -> V3 a -> V3 b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> V3 a
y2) (V3 (a -> b)
fz1 V3 (a -> b) -> V3 a -> V3 b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> V3 a
z2)
  {-# INLINE (<*>) #-}
#if MIN_VERSION_base(4,10,0)
  liftA2 :: (a -> b -> c) -> M3x3 a -> M3x3 b -> M3x3 c
liftA2 = (a -> b -> c) -> M3x3 a -> M3x3 b -> M3x3 c
forall a b c. (a -> b -> c) -> M3x3 a -> M3x3 b -> M3x3 c
zipWithM3x3
  {-# INLINE liftA2 #-}
#endif

instance Foldable M3x3 where
  foldr :: (a -> b -> b) -> b -> M3x3 a -> b
foldr a -> b -> b
f b
acc (M3x3 V3 a
x V3 a
y V3 a
z) = (a -> b -> b) -> b -> V3 a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> b -> b
f ((a -> b -> b) -> b -> V3 a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> b -> b
f ((a -> b -> b) -> b -> V3 a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> b -> b
f b
acc V3 a
z) V3 a
y) V3 a
x
  {-# INLINE foldr #-}

instance Traversable M3x3 where
  traverse :: (a -> f b) -> M3x3 a -> f (M3x3 b)
traverse a -> f b
f (M3x3 V3 a
x V3 a
y V3 a
z) = V3 b -> V3 b -> V3 b -> M3x3 b
forall a. V3 a -> V3 a -> V3 a -> M3x3 a
M3x3 (V3 b -> V3 b -> V3 b -> M3x3 b)
-> f (V3 b) -> f (V3 b -> V3 b -> M3x3 b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f b) -> V3 a -> f (V3 b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f V3 a
x f (V3 b -> V3 b -> M3x3 b) -> f (V3 b) -> f (V3 b -> M3x3 b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (a -> f b) -> V3 a -> f (V3 b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f V3 a
y f (V3 b -> M3x3 b) -> f (V3 b) -> f (M3x3 b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (a -> f b) -> V3 a -> f (V3 b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f V3 a
z
  {-# INLINE traverse #-}


instance Num a => Num (M3x3 a) where
  + :: M3x3 a -> M3x3 a -> M3x3 a
(+)         = (a -> a -> a) -> M3x3 a -> M3x3 a -> M3x3 a
forall a b c. (a -> b -> c) -> M3x3 a -> M3x3 b -> M3x3 c
zipWithM3x3 a -> a -> a
forall a. Num a => a -> a -> a
(+)
  {-# INLINE (+) #-}
  (-)         = (a -> a -> a) -> M3x3 a -> M3x3 a -> M3x3 a
forall a b c. (a -> b -> c) -> M3x3 a -> M3x3 b -> M3x3 c
zipWithM3x3 (-)
  {-# INLINE (-) #-}
  * :: M3x3 a -> M3x3 a -> M3x3 a
(*)         = (a -> a -> a) -> M3x3 a -> M3x3 a -> M3x3 a
forall a b c. (a -> b -> c) -> M3x3 a -> M3x3 b -> M3x3 c
zipWithM3x3 a -> a -> a
forall a. Num a => a -> a -> a
(*)
  {-# INLINE (*) #-}
  abs :: M3x3 a -> M3x3 a
abs         = (a -> a) -> M3x3 a -> M3x3 a
forall a. (a -> a) -> M3x3 a -> M3x3 a
mapM3x3 a -> a
forall a. Num a => a -> a
abs
  {-# INLINE abs #-}
  signum :: M3x3 a -> M3x3 a
signum      = (a -> a) -> M3x3 a -> M3x3 a
forall a. (a -> a) -> M3x3 a -> M3x3 a
mapM3x3 a -> a
forall a. Num a => a -> a
signum
  {-# INLINE signum #-}
  fromInteger :: Integer -> M3x3 a
fromInteger = a -> M3x3 a
forall a. a -> M3x3 a
pureM3x3 (a -> M3x3 a) -> (Integer -> a) -> Integer -> M3x3 a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> a
forall a. Num a => Integer -> a
fromInteger
  {-# INLINE fromInteger #-}


instance Fractional a => Fractional (M3x3 a) where
  / :: M3x3 a -> M3x3 a -> M3x3 a
(/)          = (a -> a -> a) -> M3x3 a -> M3x3 a -> M3x3 a
forall a b c. (a -> b -> c) -> M3x3 a -> M3x3 b -> M3x3 c
zipWithM3x3 a -> a -> a
forall a. Fractional a => a -> a -> a
(/)
  {-# INLINE (/) #-}
  recip :: M3x3 a -> M3x3 a
recip        = (a -> a) -> M3x3 a -> M3x3 a
forall a. (a -> a) -> M3x3 a -> M3x3 a
mapM3x3 a -> a
forall a. Fractional a => a -> a
recip
  {-# INLINE recip #-}
  fromRational :: Rational -> M3x3 a
fromRational = a -> M3x3 a
forall a. a -> M3x3 a
pureM3x3 (a -> M3x3 a) -> (Rational -> a) -> Rational -> M3x3 a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> a
forall a. Fractional a => Rational -> a
fromRational
  {-# INLINE fromRational #-}


instance Floating a => Floating (M3x3 a) where
  pi :: M3x3 a
pi      = a -> M3x3 a
forall a. a -> M3x3 a
pureM3x3 a
forall a. Floating a => a
pi
  {-# INLINE pi #-}
  exp :: M3x3 a -> M3x3 a
exp     = (a -> a) -> M3x3 a -> M3x3 a
forall a. (a -> a) -> M3x3 a -> M3x3 a
mapM3x3 a -> a
forall a. Floating a => a -> a
exp
  {-# INLINE exp #-}
  log :: M3x3 a -> M3x3 a
log     = (a -> a) -> M3x3 a -> M3x3 a
forall a. (a -> a) -> M3x3 a -> M3x3 a
mapM3x3 a -> a
forall a. Floating a => a -> a
log
  {-# INLINE log #-}
  sin :: M3x3 a -> M3x3 a
sin     = (a -> a) -> M3x3 a -> M3x3 a
forall a. (a -> a) -> M3x3 a -> M3x3 a
mapM3x3 a -> a
forall a. Floating a => a -> a
sin
  {-# INLINE sin #-}
  cos :: M3x3 a -> M3x3 a
cos     = (a -> a) -> M3x3 a -> M3x3 a
forall a. (a -> a) -> M3x3 a -> M3x3 a
mapM3x3 a -> a
forall a. Floating a => a -> a
cos
  {-# INLINE cos #-}
  asin :: M3x3 a -> M3x3 a
asin    = (a -> a) -> M3x3 a -> M3x3 a
forall a. (a -> a) -> M3x3 a -> M3x3 a
mapM3x3 a -> a
forall a. Floating a => a -> a
asin
  {-# INLINE asin #-}
  atan :: M3x3 a -> M3x3 a
atan    = (a -> a) -> M3x3 a -> M3x3 a
forall a. (a -> a) -> M3x3 a -> M3x3 a
mapM3x3 a -> a
forall a. Floating a => a -> a
atan
  {-# INLINE atan #-}
  acos :: M3x3 a -> M3x3 a
acos    = (a -> a) -> M3x3 a -> M3x3 a
forall a. (a -> a) -> M3x3 a -> M3x3 a
mapM3x3 a -> a
forall a. Floating a => a -> a
acos
  {-# INLINE acos #-}
  sinh :: M3x3 a -> M3x3 a
sinh    = (a -> a) -> M3x3 a -> M3x3 a
forall a. (a -> a) -> M3x3 a -> M3x3 a
mapM3x3 a -> a
forall a. Floating a => a -> a
sinh
  {-# INLINE sinh #-}
  cosh :: M3x3 a -> M3x3 a
cosh    = (a -> a) -> M3x3 a -> M3x3 a
forall a. (a -> a) -> M3x3 a -> M3x3 a
mapM3x3 a -> a
forall a. Floating a => a -> a
cosh
  {-# INLINE cosh #-}
  asinh :: M3x3 a -> M3x3 a
asinh   = (a -> a) -> M3x3 a -> M3x3 a
forall a. (a -> a) -> M3x3 a -> M3x3 a
mapM3x3 a -> a
forall a. Floating a => a -> a
asinh
  {-# INLINE asinh #-}
  atanh :: M3x3 a -> M3x3 a
atanh   = (a -> a) -> M3x3 a -> M3x3 a
forall a. (a -> a) -> M3x3 a -> M3x3 a
mapM3x3 a -> a
forall a. Floating a => a -> a
atanh
  {-# INLINE atanh #-}
  acosh :: M3x3 a -> M3x3 a
acosh   = (a -> a) -> M3x3 a -> M3x3 a
forall a. (a -> a) -> M3x3 a -> M3x3 a
mapM3x3 a -> a
forall a. Floating a => a -> a
acosh
  {-# INLINE acosh #-}



showsType :: Typeable t => proxy t -> ShowS
showsType :: proxy t -> ShowS
showsType = TypeRep -> ShowS
showsTypeRep (TypeRep -> ShowS) -> (proxy t -> TypeRep) -> proxy t -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. proxy t -> TypeRep
forall k (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep

asProxy :: p -> (Proxy p -> t) -> t
asProxy :: p -> (Proxy p -> t) -> t
asProxy p
_ Proxy p -> t
f = Proxy p -> t
f (forall k (t :: k). Proxy t
Proxy :: Proxy a)