{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveLift #-}

#ifndef MIN_VERSION_hashable
#define MIN_VERSION_hashable(x,y,z) 1
#endif

#ifndef MIN_VERSION_vector
#define MIN_VERSION_vector(x,y,z) 1
#endif

#ifndef MIN_VERSION_base
#define MIN_VERSION_base(x,y,z) 1
#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
--
-- Quaternions
----------------------------------------------------------------------------
module Linear.Quaternion
  ( Quaternion(..)
  , Complicated(..)
  , Hamiltonian(..)
  , ee, ei, ej, ek
  , slerp
  , asinq
  , acosq
  , atanq
  , asinhq
  , acoshq
  , atanhq
  , absi
  , pow
  , rotate
  , axisAngle
  ) where

import Control.Applicative
import Control.DeepSeq (NFData(rnf))
import Control.Monad (liftM)
import Control.Monad.Fix
import Control.Monad.Zip
import Control.Lens as Lens hiding ((<.>))
import Data.Binary as Binary
import Data.Bytes.Serial
import Data.Complex (Complex((:+)))
import Data.Data
import Data.Distributive
import Data.Foldable
import qualified Data.Foldable.WithIndex as WithIndex
import Data.Functor.Bind
import Data.Functor.Classes
import Data.Functor.Rep
import qualified Data.Functor.WithIndex as WithIndex
import Data.Hashable
import Data.Hashable.Lifted
#if !(MIN_VERSION_base(4,11,0))
import Data.Semigroup (Semigroup(..))
#endif
import Data.Serialize as Cereal
import GHC.Arr (Ix(..))
import qualified Data.Foldable as F
import qualified Data.Traversable.WithIndex as WithIndex
import qualified Data.Vector as V
import qualified Data.Vector.Generic.Mutable as M
import qualified Data.Vector.Generic as G
import qualified Data.Vector.Unboxed.Base as U
import Foreign.Ptr (castPtr, plusPtr)
import Foreign.Storable (Storable(..))
import GHC.Generics (Generic, Generic1)
#if defined(MIN_VERSION_template_haskell)
import Language.Haskell.TH.Syntax (Lift)
#endif
import Linear.Epsilon
import Linear.Conjugate
import Linear.Metric
import Linear.V
import Linear.V2
import Linear.V3
import Linear.V4
import Linear.Vector
import Prelude hiding (any)
import System.Random (Random(..))

-- | Quaternions
data Quaternion a = Quaternion !a {-# UNPACK #-}!(V3 a)
                    deriving (Quaternion a -> Quaternion a -> Bool
(Quaternion a -> Quaternion a -> Bool)
-> (Quaternion a -> Quaternion a -> Bool) -> Eq (Quaternion a)
forall a. Eq a => Quaternion a -> Quaternion a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Quaternion a -> Quaternion a -> Bool
$c/= :: forall a. Eq a => Quaternion a -> Quaternion a -> Bool
== :: Quaternion a -> Quaternion a -> Bool
$c== :: forall a. Eq a => Quaternion a -> Quaternion a -> Bool
Eq,Eq (Quaternion a)
Eq (Quaternion a)
-> (Quaternion a -> Quaternion a -> Ordering)
-> (Quaternion a -> Quaternion a -> Bool)
-> (Quaternion a -> Quaternion a -> Bool)
-> (Quaternion a -> Quaternion a -> Bool)
-> (Quaternion a -> Quaternion a -> Bool)
-> (Quaternion a -> Quaternion a -> Quaternion a)
-> (Quaternion a -> Quaternion a -> Quaternion a)
-> Ord (Quaternion a)
Quaternion a -> Quaternion a -> Bool
Quaternion a -> Quaternion a -> Ordering
Quaternion a -> Quaternion a -> Quaternion 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 (Quaternion a)
forall a. Ord a => Quaternion a -> Quaternion a -> Bool
forall a. Ord a => Quaternion a -> Quaternion a -> Ordering
forall a. Ord a => Quaternion a -> Quaternion a -> Quaternion a
min :: Quaternion a -> Quaternion a -> Quaternion a
$cmin :: forall a. Ord a => Quaternion a -> Quaternion a -> Quaternion a
max :: Quaternion a -> Quaternion a -> Quaternion a
$cmax :: forall a. Ord a => Quaternion a -> Quaternion a -> Quaternion a
>= :: Quaternion a -> Quaternion a -> Bool
$c>= :: forall a. Ord a => Quaternion a -> Quaternion a -> Bool
> :: Quaternion a -> Quaternion a -> Bool
$c> :: forall a. Ord a => Quaternion a -> Quaternion a -> Bool
<= :: Quaternion a -> Quaternion a -> Bool
$c<= :: forall a. Ord a => Quaternion a -> Quaternion a -> Bool
< :: Quaternion a -> Quaternion a -> Bool
$c< :: forall a. Ord a => Quaternion a -> Quaternion a -> Bool
compare :: Quaternion a -> Quaternion a -> Ordering
$ccompare :: forall a. Ord a => Quaternion a -> Quaternion a -> Ordering
$cp1Ord :: forall a. Ord a => Eq (Quaternion a)
Ord,ReadPrec [Quaternion a]
ReadPrec (Quaternion a)
Int -> ReadS (Quaternion a)
ReadS [Quaternion a]
(Int -> ReadS (Quaternion a))
-> ReadS [Quaternion a]
-> ReadPrec (Quaternion a)
-> ReadPrec [Quaternion a]
-> Read (Quaternion a)
forall a. Read a => ReadPrec [Quaternion a]
forall a. Read a => ReadPrec (Quaternion a)
forall a. Read a => Int -> ReadS (Quaternion a)
forall a. Read a => ReadS [Quaternion a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Quaternion a]
$creadListPrec :: forall a. Read a => ReadPrec [Quaternion a]
readPrec :: ReadPrec (Quaternion a)
$creadPrec :: forall a. Read a => ReadPrec (Quaternion a)
readList :: ReadS [Quaternion a]
$creadList :: forall a. Read a => ReadS [Quaternion a]
readsPrec :: Int -> ReadS (Quaternion a)
$creadsPrec :: forall a. Read a => Int -> ReadS (Quaternion a)
Read,Int -> Quaternion a -> ShowS
[Quaternion a] -> ShowS
Quaternion a -> String
(Int -> Quaternion a -> ShowS)
-> (Quaternion a -> String)
-> ([Quaternion a] -> ShowS)
-> Show (Quaternion a)
forall a. Show a => Int -> Quaternion a -> ShowS
forall a. Show a => [Quaternion a] -> ShowS
forall a. Show a => Quaternion a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Quaternion a] -> ShowS
$cshowList :: forall a. Show a => [Quaternion a] -> ShowS
show :: Quaternion a -> String
$cshow :: forall a. Show a => Quaternion a -> String
showsPrec :: Int -> Quaternion a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Quaternion a -> ShowS
Show,Typeable (Quaternion a)
DataType
Constr
Typeable (Quaternion a)
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> Quaternion a -> c (Quaternion a))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (Quaternion a))
-> (Quaternion a -> Constr)
-> (Quaternion a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (Quaternion a)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (Quaternion a)))
-> ((forall b. Data b => b -> b) -> Quaternion a -> Quaternion a)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Quaternion a -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Quaternion a -> r)
-> (forall u. (forall d. Data d => d -> u) -> Quaternion a -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> Quaternion a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Quaternion a -> m (Quaternion a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Quaternion a -> m (Quaternion a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Quaternion a -> m (Quaternion a))
-> Data (Quaternion a)
Quaternion a -> DataType
Quaternion a -> Constr
(forall d. Data d => c (t d)) -> Maybe (c (Quaternion a))
(forall b. Data b => b -> b) -> Quaternion a -> Quaternion a
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Quaternion a -> c (Quaternion a)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Quaternion a)
forall a. Data a => Typeable (Quaternion a)
forall a. Data a => Quaternion a -> DataType
forall a. Data a => Quaternion a -> Constr
forall a.
Data a =>
(forall b. Data b => b -> b) -> Quaternion a -> Quaternion a
forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> Quaternion a -> u
forall a u.
Data a =>
(forall d. Data d => d -> u) -> Quaternion a -> [u]
forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Quaternion a -> r
forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Quaternion a -> r
forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Quaternion a -> m (Quaternion a)
forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Quaternion a -> m (Quaternion a)
forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Quaternion a)
forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Quaternion a -> c (Quaternion a)
forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Quaternion a))
forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Quaternion a))
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Quaternion a -> u
forall u. (forall d. Data d => d -> u) -> Quaternion a -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Quaternion a -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Quaternion a -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Quaternion a -> m (Quaternion a)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Quaternion a -> m (Quaternion a)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Quaternion a)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Quaternion a -> c (Quaternion a)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Quaternion a))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Quaternion a))
$cQuaternion :: Constr
$tQuaternion :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> Quaternion a -> m (Quaternion a)
$cgmapMo :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Quaternion a -> m (Quaternion a)
gmapMp :: (forall d. Data d => d -> m d) -> Quaternion a -> m (Quaternion a)
$cgmapMp :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Quaternion a -> m (Quaternion a)
gmapM :: (forall d. Data d => d -> m d) -> Quaternion a -> m (Quaternion a)
$cgmapM :: forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Quaternion a -> m (Quaternion a)
gmapQi :: Int -> (forall d. Data d => d -> u) -> Quaternion a -> u
$cgmapQi :: forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> Quaternion a -> u
gmapQ :: (forall d. Data d => d -> u) -> Quaternion a -> [u]
$cgmapQ :: forall a u.
Data a =>
(forall d. Data d => d -> u) -> Quaternion a -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Quaternion a -> r
$cgmapQr :: forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Quaternion a -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Quaternion a -> r
$cgmapQl :: forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Quaternion a -> r
gmapT :: (forall b. Data b => b -> b) -> Quaternion a -> Quaternion a
$cgmapT :: forall a.
Data a =>
(forall b. Data b => b -> b) -> Quaternion a -> Quaternion a
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Quaternion a))
$cdataCast2 :: forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Quaternion a))
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c (Quaternion a))
$cdataCast1 :: forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Quaternion a))
dataTypeOf :: Quaternion a -> DataType
$cdataTypeOf :: forall a. Data a => Quaternion a -> DataType
toConstr :: Quaternion a -> Constr
$ctoConstr :: forall a. Data a => Quaternion a -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Quaternion a)
$cgunfold :: forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Quaternion a)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Quaternion a -> c (Quaternion a)
$cgfoldl :: forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Quaternion a -> c (Quaternion a)
$cp1Data :: forall a. Data a => Typeable (Quaternion a)
Data
                             ,(forall x. Quaternion a -> Rep (Quaternion a) x)
-> (forall x. Rep (Quaternion a) x -> Quaternion a)
-> Generic (Quaternion a)
forall x. Rep (Quaternion a) x -> Quaternion a
forall x. Quaternion a -> Rep (Quaternion a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (Quaternion a) x -> Quaternion a
forall a x. Quaternion a -> Rep (Quaternion a) x
$cto :: forall a x. Rep (Quaternion a) x -> Quaternion a
$cfrom :: forall a x. Quaternion a -> Rep (Quaternion a) x
Generic,(forall a. Quaternion a -> Rep1 Quaternion a)
-> (forall a. Rep1 Quaternion a -> Quaternion a)
-> Generic1 Quaternion
forall a. Rep1 Quaternion a -> Quaternion a
forall a. Quaternion a -> Rep1 Quaternion a
forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
$cto1 :: forall a. Rep1 Quaternion a -> Quaternion a
$cfrom1 :: forall a. Quaternion a -> Rep1 Quaternion a
Generic1
#if defined(MIN_VERSION_template_haskell)
                             ,Quaternion a -> Q Exp
Quaternion a -> Q (TExp (Quaternion a))
(Quaternion a -> Q Exp)
-> (Quaternion a -> Q (TExp (Quaternion a))) -> Lift (Quaternion a)
forall a. Lift a => Quaternion a -> Q Exp
forall a. Lift a => Quaternion a -> Q (TExp (Quaternion a))
forall t. (t -> Q Exp) -> (t -> Q (TExp t)) -> Lift t
liftTyped :: Quaternion a -> Q (TExp (Quaternion a))
$cliftTyped :: forall a. Lift a => Quaternion a -> Q (TExp (Quaternion a))
lift :: Quaternion a -> Q Exp
$clift :: forall a. Lift a => Quaternion a -> Q Exp
Lift
#endif
                             )

instance Finite Quaternion where
  type Size Quaternion = 4
  toV :: Quaternion a -> V (Size Quaternion) a
toV (Quaternion a
a (V3 a
b a
c a
d)) = Vector a -> V 4 a
forall k (n :: k) a. Vector a -> V n a
V (Int -> [a] -> Vector a
forall a. Int -> [a] -> Vector a
V.fromListN Int
4 [a
a, a
b, a
c, a
d])
  fromV :: V (Size Quaternion) a -> Quaternion a
fromV (V Vector a
v) = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion (Vector a
v Vector a -> Int -> a
forall a. Vector a -> Int -> a
V.! Int
0) (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 (Vector a
v Vector a -> Int -> a
forall a. Vector a -> Int -> a
V.! Int
1) (Vector a
v Vector a -> Int -> a
forall a. Vector a -> Int -> a
V.! Int
2) (Vector a
v Vector a -> Int -> a
forall a. Vector a -> Int -> a
V.! Int
3))

instance Random a => Random (Quaternion a) where
  random :: g -> (Quaternion a, g)
random g
g = case g -> (a, g)
forall a g. (Random a, RandomGen g) => g -> (a, g)
random g
g of
    (a
a, g
g') -> case g -> (V3 a, g)
forall a g. (Random a, RandomGen g) => g -> (a, g)
random g
g' of
      (V3 a
b, g
g'') -> (a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
a V3 a
b, g
g'')
  randomR :: (Quaternion a, Quaternion a) -> g -> (Quaternion a, g)
randomR (Quaternion a
a V3 a
b, Quaternion a
c V3 a
d) g
g = case (a, a) -> g -> (a, g)
forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (a
a,a
c) g
g of
    (a
e, g
g') -> case (V3 a, V3 a) -> g -> (V3 a, g)
forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (V3 a
b,V3 a
d) g
g' of
      (V3 a
f, g
g'') -> (a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
e V3 a
f, g
g'')

instance Functor Quaternion where
  fmap :: (a -> b) -> Quaternion a -> Quaternion b
fmap a -> b
f (Quaternion a
e V3 a
v) = b -> V3 b -> Quaternion b
forall a. a -> V3 a -> Quaternion a
Quaternion (a -> b
f a
e) ((a -> b) -> V3 a -> V3 b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f V3 a
v)
  {-# INLINE fmap #-}
  a
a <$ :: a -> Quaternion b -> Quaternion a
<$ Quaternion b
_ = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
a (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
a a
a a
a)
  {-# INLINE (<$) #-}

instance Apply Quaternion where
  Quaternion a -> b
f V3 (a -> b)
fv <.> :: Quaternion (a -> b) -> Quaternion a -> Quaternion b
<.> Quaternion a
a V3 a
v = b -> V3 b -> Quaternion b
forall a. a -> V3 a -> Quaternion a
Quaternion (a -> b
f a
a) (V3 (a -> b)
fv V3 (a -> b) -> V3 a -> V3 b
forall (f :: * -> *) a b. Apply f => f (a -> b) -> f a -> f b
<.> V3 a
v)
  {-# INLINE (<.>) #-}

instance Applicative Quaternion where
  pure :: a -> Quaternion a
pure a
a = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
a (a -> V3 a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a)
  {-# INLINE pure #-}
  Quaternion a -> b
f V3 (a -> b)
fv <*> :: Quaternion (a -> b) -> Quaternion a -> Quaternion b
<*> Quaternion a
a V3 a
v = b -> V3 b -> Quaternion b
forall a. a -> V3 a -> Quaternion a
Quaternion (a -> b
f a
a) (V3 (a -> b)
fv V3 (a -> b) -> V3 a -> V3 b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> V3 a
v)
  {-# INLINE (<*>) #-}

instance Additive Quaternion where
  zero :: Quaternion a
zero = a -> Quaternion a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
0
  {-# INLINE zero #-}
  liftU2 :: (a -> a -> a) -> Quaternion a -> Quaternion a -> Quaternion a
liftU2 = (a -> a -> a) -> Quaternion a -> Quaternion a -> Quaternion a
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2
  {-# INLINE liftU2 #-}
  liftI2 :: (a -> b -> c) -> Quaternion a -> Quaternion b -> Quaternion c
liftI2 = (a -> b -> c) -> Quaternion a -> Quaternion b -> Quaternion c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2
  {-# INLINE liftI2 #-}

instance Bind Quaternion where
  Quaternion a
a (V3 a
b a
c a
d) >>- :: Quaternion a -> (a -> Quaternion b) -> Quaternion b
>>- a -> Quaternion b
f = b -> V3 b -> Quaternion b
forall a. a -> V3 a -> Quaternion a
Quaternion b
a' (b -> b -> b -> V3 b
forall a. a -> a -> a -> V3 a
V3 b
b' b
c' b
d') where
    Quaternion b
a' V3 b
_          = a -> Quaternion b
f a
a
    Quaternion b
_ (V3 b
b' b
_ b
_) = a -> Quaternion b
f a
b
    Quaternion b
_ (V3 b
_ b
c' b
_) = a -> Quaternion b
f a
c
    Quaternion b
_ (V3 b
_ b
_ b
d') = a -> Quaternion b
f a
d
  {-# INLINE (>>-) #-}

instance Monad Quaternion where
  return :: a -> Quaternion a
return = a -> Quaternion a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
  {-# INLINE return #-}
  -- the diagonal of a sedenion is super useful!
  Quaternion a
a (V3 a
b a
c a
d) >>= :: Quaternion a -> (a -> Quaternion b) -> Quaternion b
>>= a -> Quaternion b
f = b -> V3 b -> Quaternion b
forall a. a -> V3 a -> Quaternion a
Quaternion b
a' (b -> b -> b -> V3 b
forall a. a -> a -> a -> V3 a
V3 b
b' b
c' b
d') where
    Quaternion b
a' V3 b
_          = a -> Quaternion b
f a
a
    Quaternion b
_ (V3 b
b' b
_ b
_) = a -> Quaternion b
f a
b
    Quaternion b
_ (V3 b
_ b
c' b
_) = a -> Quaternion b
f a
c
    Quaternion b
_ (V3 b
_ b
_ b
d') = a -> Quaternion b
f a
d
  {-# INLINE (>>=) #-}

instance Ix a => Ix (Quaternion a) where
    {-# SPECIALISE instance Ix (Quaternion Int) #-}

    range :: (Quaternion a, Quaternion a) -> [Quaternion a]
range (Quaternion a
l1 V3 a
l2, Quaternion a
u1 V3 a
u2) =
      [ a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
i1 V3 a
i2 | a
i1 <- (a, a) -> [a]
forall a. Ix a => (a, a) -> [a]
range (a
l1,a
u1), V3 a
i2 <- (V3 a, V3 a) -> [V3 a]
forall a. Ix a => (a, a) -> [a]
range (V3 a
l2,V3 a
u2) ]
    {-# INLINE range #-}

    unsafeIndex :: (Quaternion a, Quaternion a) -> Quaternion a -> Int
unsafeIndex (Quaternion a
l1 V3 a
l2, Quaternion a
u1 V3 a
u2) (Quaternion a
i1 V3 a
i2) =
      (a, a) -> a -> Int
forall a. Ix a => (a, a) -> a -> Int
unsafeIndex (a
l1,a
u1) a
i1 Int -> Int -> Int
forall a. Num a => a -> a -> a
* (V3 a, V3 a) -> Int
forall a. Ix a => (a, a) -> Int
unsafeRangeSize (V3 a
l2,V3 a
u2) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (V3 a, V3 a) -> V3 a -> Int
forall a. Ix a => (a, a) -> a -> Int
unsafeIndex (V3 a
l2,V3 a
u2) V3 a
i2
    {-# INLINE unsafeIndex #-}

    inRange :: (Quaternion a, Quaternion a) -> Quaternion a -> Bool
inRange (Quaternion a
l1 V3 a
l2, Quaternion a
u1 V3 a
u2) (Quaternion a
i1 V3 a
i2) =
      (a, a) -> a -> Bool
forall a. Ix a => (a, a) -> a -> Bool
inRange (a
l1,a
u1) a
i1 Bool -> Bool -> Bool
&& (V3 a, V3 a) -> V3 a -> Bool
forall a. Ix a => (a, a) -> a -> Bool
inRange (V3 a
l2,V3 a
u2) V3 a
i2
    {-# INLINE inRange #-}

instance Representable Quaternion where
  type Rep Quaternion = E Quaternion
  tabulate :: (Rep Quaternion -> a) -> Quaternion a
tabulate Rep Quaternion -> a
f = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion (Rep Quaternion -> a
f Rep Quaternion
forall (t :: * -> *). Complicated t => E t
ee) (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 (Rep Quaternion -> a
f Rep Quaternion
forall (t :: * -> *). Complicated t => E t
ei) (Rep Quaternion -> a
f Rep Quaternion
forall (t :: * -> *). Hamiltonian t => E t
ej) (Rep Quaternion -> a
f Rep Quaternion
forall (t :: * -> *). Hamiltonian t => E t
ek))
  {-# INLINE tabulate #-}
  index :: Quaternion a -> Rep Quaternion -> a
index Quaternion a
xs (E l) = Getting a (Quaternion a) a -> Quaternion a -> a
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting a (Quaternion a) a
forall x. Lens' (Quaternion x) x
l Quaternion a
xs
  {-# INLINE index #-}

instance WithIndex.FunctorWithIndex (E Quaternion) Quaternion where
  imap :: (E Quaternion -> a -> b) -> Quaternion a -> Quaternion b
imap E Quaternion -> a -> b
f (Quaternion a
a (V3 a
b a
c a
d)) = b -> V3 b -> Quaternion b
forall a. a -> V3 a -> Quaternion a
Quaternion (E Quaternion -> a -> b
f E Quaternion
forall (t :: * -> *). Complicated t => E t
ee a
a) (V3 b -> Quaternion b) -> V3 b -> Quaternion b
forall a b. (a -> b) -> a -> b
$ b -> b -> b -> V3 b
forall a. a -> a -> a -> V3 a
V3 (E Quaternion -> a -> b
f E Quaternion
forall (t :: * -> *). Complicated t => E t
ei a
b) (E Quaternion -> a -> b
f E Quaternion
forall (t :: * -> *). Hamiltonian t => E t
ej a
c) (E Quaternion -> a -> b
f E Quaternion
forall (t :: * -> *). Hamiltonian t => E t
ek a
d)
  {-# INLINE imap #-}

instance WithIndex.FoldableWithIndex (E Quaternion) Quaternion where
  ifoldMap :: (E Quaternion -> a -> m) -> Quaternion a -> m
ifoldMap E Quaternion -> a -> m
f (Quaternion a
a (V3 a
b a
c a
d)) = E Quaternion -> a -> m
f E Quaternion
forall (t :: * -> *). Complicated t => E t
ee a
a m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` E Quaternion -> a -> m
f E Quaternion
forall (t :: * -> *). Complicated t => E t
ei a
b m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` E Quaternion -> a -> m
f E Quaternion
forall (t :: * -> *). Hamiltonian t => E t
ej a
c m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` E Quaternion -> a -> m
f E Quaternion
forall (t :: * -> *). Hamiltonian t => E t
ek a
d
  {-# INLINE ifoldMap #-}

instance WithIndex.TraversableWithIndex (E Quaternion) Quaternion where
  itraverse :: (E Quaternion -> a -> f b) -> Quaternion a -> f (Quaternion b)
itraverse E Quaternion -> a -> f b
f (Quaternion a
a (V3 a
b a
c a
d)) = b -> V3 b -> Quaternion b
forall a. a -> V3 a -> Quaternion a
Quaternion (b -> V3 b -> Quaternion b) -> f b -> f (V3 b -> Quaternion b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> E Quaternion -> a -> f b
f E Quaternion
forall (t :: * -> *). Complicated t => E t
ee a
a f (V3 b -> Quaternion b) -> f (V3 b) -> f (Quaternion b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (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
<$> E Quaternion -> a -> f b
f E Quaternion
forall (t :: * -> *). Complicated t => E t
ei a
b f (b -> b -> V3 b) -> f b -> f (b -> V3 b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> E Quaternion -> a -> f b
f E Quaternion
forall (t :: * -> *). Hamiltonian t => E t
ej a
c f (b -> V3 b) -> f b -> f (V3 b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> E Quaternion -> a -> f b
f E Quaternion
forall (t :: * -> *). Hamiltonian t => E t
ek a
d)
  {-# INLINE itraverse #-}

#if !MIN_VERSION_lens(5,0,0)
instance Lens.FunctorWithIndex     (E Quaternion) Quaternion where imap      = WithIndex.imap
instance Lens.FoldableWithIndex    (E Quaternion) Quaternion where ifoldMap  = WithIndex.ifoldMap
instance Lens.TraversableWithIndex (E Quaternion) Quaternion where itraverse = WithIndex.itraverse
#endif

type instance Index (Quaternion a) = E Quaternion
type instance IxValue (Quaternion a) = a

instance Ixed (Quaternion a) where
  ix :: Index (Quaternion a)
-> Traversal' (Quaternion a) (IxValue (Quaternion a))
ix Index (Quaternion a)
i = E Quaternion -> forall x. Lens' (Quaternion x) x
forall (t :: * -> *).
E t
-> forall x (f :: * -> *).
   Functor f =>
   (x -> f x) -> t x -> f (t x)
el Index (Quaternion a)
E Quaternion
i
  {-# INLINE ix #-}

instance Each (Quaternion a) (Quaternion b) a b where
  each :: (a -> f b) -> Quaternion a -> f (Quaternion b)
each = (a -> f b) -> Quaternion a -> f (Quaternion b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse
  {-# INLINE each #-}

instance Foldable Quaternion where
  foldMap :: (a -> m) -> Quaternion a -> m
foldMap a -> m
f (Quaternion a
e V3 a
v) = a -> m
f a
e m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` (a -> m) -> V3 a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f V3 a
v
  {-# INLINE foldMap #-}
  foldr :: (a -> b -> b) -> b -> Quaternion a -> b
foldr a -> b -> b
f b
z (Quaternion a
e V3 a
v) = a -> b -> b
f a
e ((a -> b -> b) -> b -> V3 a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr a -> b -> b
f b
z V3 a
v)
  {-# INLINE foldr #-}
  null :: Quaternion a -> Bool
null Quaternion a
_ = Bool
False
  length :: Quaternion a -> Int
length Quaternion a
_ = Int
4

instance Traversable Quaternion where
  traverse :: (a -> f b) -> Quaternion a -> f (Quaternion b)
traverse a -> f b
f (Quaternion a
e V3 a
v) = b -> V3 b -> Quaternion b
forall a. a -> V3 a -> Quaternion a
Quaternion (b -> V3 b -> Quaternion b) -> f b -> f (V3 b -> Quaternion b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
e f (V3 b -> Quaternion b) -> f (V3 b) -> f (Quaternion 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
v
  {-# INLINE traverse #-}

instance Storable a => Storable (Quaternion a) where
  sizeOf :: Quaternion a -> Int
sizeOf Quaternion a
_ = Int
4 Int -> Int -> Int
forall a. Num a => a -> a -> a
* a -> Int
forall a. Storable a => a -> Int
sizeOf (a
forall a. HasCallStack => a
undefined::a)
  {-# INLINE sizeOf #-}
  alignment :: Quaternion a -> Int
alignment Quaternion a
_ = a -> Int
forall a. Storable a => a -> Int
alignment (a
forall a. HasCallStack => a
undefined::a)
  {-# INLINE alignment #-}
  poke :: Ptr (Quaternion a) -> Quaternion a -> IO ()
poke Ptr (Quaternion a)
ptr (Quaternion a
e V3 a
v) = Ptr a -> a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke (Ptr (Quaternion a) -> Ptr a
forall a b. Ptr a -> Ptr b
castPtr Ptr (Quaternion a)
ptr) a
e IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                              Ptr (V3 a) -> V3 a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke (Ptr Any -> Ptr (V3 a)
forall a b. Ptr a -> Ptr b
castPtr (Ptr (Quaternion a)
ptr Ptr (Quaternion a) -> Int -> Ptr Any
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
sz)) V3 a
v
    where sz :: Int
sz = a -> Int
forall a. Storable a => a -> Int
sizeOf (a
forall a. HasCallStack => a
undefined::a)
  {-# INLINE poke #-}
  peek :: Ptr (Quaternion a) -> IO (Quaternion a)
peek Ptr (Quaternion a)
ptr = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion (a -> V3 a -> Quaternion a) -> IO a -> IO (V3 a -> Quaternion a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek (Ptr (Quaternion a) -> Ptr a
forall a b. Ptr a -> Ptr b
castPtr Ptr (Quaternion a)
ptr)
                        IO (V3 a -> Quaternion a) -> IO (V3 a) -> IO (Quaternion a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Ptr (V3 a) -> IO (V3 a)
forall a. Storable a => Ptr a -> IO a
peek (Ptr Any -> Ptr (V3 a)
forall a b. Ptr a -> Ptr b
castPtr (Ptr (Quaternion a)
ptr Ptr (Quaternion a) -> Int -> Ptr Any
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
sz))
    where sz :: Int
sz = a -> Int
forall a. Storable a => a -> Int
sizeOf (a
forall a. HasCallStack => a
undefined::a)
  {-# INLINE peek #-}

instance RealFloat a => Num (Quaternion a) where
  {-# SPECIALIZE instance Num (Quaternion Float) #-}
  {-# SPECIALIZE instance Num (Quaternion Double) #-}
  + :: Quaternion a -> Quaternion a -> Quaternion a
(+) = (a -> a -> a) -> Quaternion a -> Quaternion a -> Quaternion a
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Num a => a -> a -> a
(+)
  {-# INLINE (+) #-}
  (-) = (a -> a -> a) -> Quaternion a -> Quaternion a -> Quaternion a
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (-)
  {-# INLINE (-) #-}
  negate :: Quaternion a -> Quaternion a
negate = (a -> a) -> Quaternion a -> Quaternion a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Num a => a -> a
negate
  {-# INLINE negate #-}
  Quaternion a
s1 V3 a
v1 * :: Quaternion a -> Quaternion a -> Quaternion a
* Quaternion a
s2 V3 a
v2 = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion (a
s1a -> a -> a
forall a. Num a => a -> a -> a
*a
s2 a -> a -> a
forall a. Num a => a -> a -> a
- (V3 a
v1 V3 a -> V3 a -> a
forall (f :: * -> *) a. (Metric f, Num a) => f a -> f a -> a
`dot` V3 a
v2)) (V3 a -> Quaternion a) -> V3 a -> Quaternion a
forall a b. (a -> b) -> a -> b
$
                                        (V3 a
v1 V3 a -> V3 a -> V3 a
forall a. Num a => V3 a -> V3 a -> V3 a
`cross` V3 a
v2) V3 a -> V3 a -> V3 a
forall a. Num a => a -> a -> a
+ a
s1a -> V3 a -> V3 a
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^V3 a
v2 V3 a -> V3 a -> V3 a
forall a. Num a => a -> a -> a
+ a
s2a -> V3 a -> V3 a
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^V3 a
v1
  {-# INLINE (*) #-}
  fromInteger :: Integer -> Quaternion a
fromInteger Integer
x = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion (Integer -> a
forall a. Num a => Integer -> a
fromInteger Integer
x) V3 a
0
  {-# INLINE fromInteger #-}
  abs :: Quaternion a -> Quaternion a
abs Quaternion a
z = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion (Quaternion a -> a
forall (f :: * -> *) a. (Metric f, Floating a) => f a -> a
norm Quaternion a
z) V3 a
0
  {-# INLINE abs #-}
  signum :: Quaternion a -> Quaternion a
signum q :: Quaternion a
q@(Quaternion a
e (V3 a
i a
j a
k))
    | a
m a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0.0 = Quaternion a
q
    | Bool -> Bool
not (a -> Bool
forall a. RealFloat a => a -> Bool
isInfinite a
m Bool -> Bool -> Bool
|| a -> Bool
forall a. RealFloat a => a -> Bool
isNaN a
m) = Quaternion a
q Quaternion a -> a -> Quaternion a
forall (f :: * -> *) a.
(Functor f, Fractional a) =>
f a -> a -> f a
^/ a -> a
forall a. Floating a => a -> a
sqrt a
m
    | (a -> Bool) -> Quaternion a -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any a -> Bool
forall a. RealFloat a => a -> Bool
isNaN Quaternion a
q = Quaternion a
forall a. RealFloat a => Quaternion a
qNaN
    | Bool -> Bool
not (Bool
ii Bool -> Bool -> Bool
|| Bool
ij Bool -> Bool -> Bool
|| Bool
ik) = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
1 (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
0 a
0 a
0)
    | Bool -> Bool
not (Bool
ie Bool -> Bool -> Bool
|| Bool
ij Bool -> Bool -> Bool
|| Bool
ik) = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
0 (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
1 a
0 a
0)
    | Bool -> Bool
not (Bool
ie Bool -> Bool -> Bool
|| Bool
ii Bool -> Bool -> Bool
|| Bool
ik) = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
0 (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
0 a
1 a
0)
    | Bool -> Bool
not (Bool
ie Bool -> Bool -> Bool
|| Bool
ii Bool -> Bool -> Bool
|| Bool
ij) = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
0 (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
0 a
0 a
1)
    | Bool
otherwise = Quaternion a
forall a. RealFloat a => Quaternion a
qNaN
    where
      m :: a
m = Quaternion a -> a
forall (f :: * -> *) a. (Metric f, Num a) => f a -> a
quadrance Quaternion a
q
      ie :: Bool
ie = a -> Bool
forall a. RealFloat a => a -> Bool
isInfinite a
e
      ii :: Bool
ii = a -> Bool
forall a. RealFloat a => a -> Bool
isInfinite a
i
      ij :: Bool
ij = a -> Bool
forall a. RealFloat a => a -> Bool
isInfinite a
j
      ik :: Bool
ik = a -> Bool
forall a. RealFloat a => a -> Bool
isInfinite a
k
  {-# INLINE signum #-}

instance Hashable a => Hashable (Quaternion a) where
  hashWithSalt :: Int -> Quaternion a -> Int
hashWithSalt Int
s (Quaternion a
a V3 a
b) = Int
s Int -> a -> Int
forall a. Hashable a => Int -> a -> Int
`hashWithSalt` a
a Int -> V3 a -> Int
forall a. Hashable a => Int -> a -> Int
`hashWithSalt` V3 a
b
  {-# INLINE hashWithSalt #-}

instance Hashable1 Quaternion where
  liftHashWithSalt :: (Int -> a -> Int) -> Int -> Quaternion a -> Int
liftHashWithSalt Int -> a -> Int
h Int
s (Quaternion a
a V3 a
b) = (Int -> a -> Int) -> Int -> V3 a -> Int
forall (t :: * -> *) a.
Hashable1 t =>
(Int -> a -> Int) -> Int -> t a -> Int
liftHashWithSalt Int -> a -> Int
h (Int -> a -> Int
h Int
s a
a) V3 a
b
  {-# INLINE liftHashWithSalt #-}

qNaN :: RealFloat a => Quaternion a
qNaN :: Quaternion a
qNaN = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
fNaN (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
fNaN a
fNaN a
fNaN) where fNaN :: a
fNaN = a
0a -> a -> a
forall a. Fractional a => a -> a -> a
/a
0
{-# INLINE qNaN #-}

-- {-# RULES "abs/norm" abs x = Quaternion (norm x) 0 #-}
-- {-# RULES "signum/signorm" signum = signorm #-}

-- this will attempt to rewrite calls to abs to use norm intead when it is available.

instance RealFloat a => Fractional (Quaternion a) where
  {-# SPECIALIZE instance Fractional (Quaternion Float) #-}
  {-# SPECIALIZE instance Fractional (Quaternion Double) #-}
  Quaternion a
q0 (V3 a
q1 a
q2 a
q3) / :: Quaternion a -> Quaternion a -> Quaternion a
/ Quaternion a
r0 (V3 a
r1 a
r2 a
r3) =
    a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion (a
r0a -> a -> a
forall a. Num a => a -> a -> a
*a
q0a -> a -> a
forall a. Num a => a -> a -> a
+a
r1a -> a -> a
forall a. Num a => a -> a -> a
*a
q1a -> a -> a
forall a. Num a => a -> a -> a
+a
r2a -> a -> a
forall a. Num a => a -> a -> a
*a
q2a -> a -> a
forall a. Num a => a -> a -> a
+a
r3a -> a -> a
forall a. Num a => a -> a -> a
*a
q3)
               (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 (a
r0a -> a -> a
forall a. Num a => a -> a -> a
*a
q1a -> a -> a
forall a. Num a => a -> a -> a
-a
r1a -> a -> a
forall a. Num a => a -> a -> a
*a
q0a -> a -> a
forall a. Num a => a -> a -> a
-a
r2a -> a -> a
forall a. Num a => a -> a -> a
*a
q3a -> a -> a
forall a. Num a => a -> a -> a
+a
r3a -> a -> a
forall a. Num a => a -> a -> a
*a
q2)
                   (a
r0a -> a -> a
forall a. Num a => a -> a -> a
*a
q2a -> a -> a
forall a. Num a => a -> a -> a
+a
r1a -> a -> a
forall a. Num a => a -> a -> a
*a
q3a -> a -> a
forall a. Num a => a -> a -> a
-a
r2a -> a -> a
forall a. Num a => a -> a -> a
*a
q0a -> a -> a
forall a. Num a => a -> a -> a
-a
r3a -> a -> a
forall a. Num a => a -> a -> a
*a
q1)
                   (a
r0a -> a -> a
forall a. Num a => a -> a -> a
*a
q3a -> a -> a
forall a. Num a => a -> a -> a
-a
r1a -> a -> a
forall a. Num a => a -> a -> a
*a
q2a -> a -> a
forall a. Num a => a -> a -> a
+a
r2a -> a -> a
forall a. Num a => a -> a -> a
*a
q1a -> a -> a
forall a. Num a => a -> a -> a
-a
r3a -> a -> a
forall a. Num a => a -> a -> a
*a
q0))
               Quaternion a -> a -> Quaternion a
forall (f :: * -> *) a.
(Functor f, Fractional a) =>
f a -> a -> f a
^/ (a
r0a -> a -> a
forall a. Num a => a -> a -> a
*a
r0 a -> a -> a
forall a. Num a => a -> a -> a
+ a
r1a -> a -> a
forall a. Num a => a -> a -> a
*a
r1 a -> a -> a
forall a. Num a => a -> a -> a
+ a
r2a -> a -> a
forall a. Num a => a -> a -> a
*a
r2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
r3a -> a -> a
forall a. Num a => a -> a -> a
*a
r3)
  {-# INLINE (/) #-}
  recip :: Quaternion a -> Quaternion a
recip q :: Quaternion a
q@(Quaternion a
e V3 a
v) = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
e (V3 a -> V3 a
forall a. Num a => a -> a
negate V3 a
v) Quaternion a -> a -> Quaternion a
forall (f :: * -> *) a.
(Functor f, Fractional a) =>
f a -> a -> f a
^/ Quaternion a -> a
forall (f :: * -> *) a. (Metric f, Num a) => f a -> a
quadrance Quaternion a
q
  {-# INLINE recip #-}
  fromRational :: Rational -> Quaternion a
fromRational Rational
x = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion (Rational -> a
forall a. Fractional a => Rational -> a
fromRational Rational
x) V3 a
0
  {-# INLINE fromRational #-}

instance Metric Quaternion where
  Quaternion a
e V3 a
v dot :: Quaternion a -> Quaternion a -> a
`dot` Quaternion a
e' V3 a
v' = a
ea -> a -> a
forall a. Num a => a -> a -> a
*a
e' a -> a -> a
forall a. Num a => a -> a -> a
+ (V3 a
v V3 a -> V3 a -> a
forall (f :: * -> *) a. (Metric f, Num a) => f a -> f a -> a
`dot` V3 a
v')
  {-# INLINE dot #-}

-- | A vector space that includes the basis elements '_e' and '_i'
class Complicated t where
  _e, _i :: Lens' (t a) a

ee, ei :: Complicated t => E t
ee :: E t
ee = (forall x. Lens' (t x) x) -> E t
forall (t :: * -> *).
(forall x (f :: * -> *). Functor f => (x -> f x) -> t x -> f (t x))
-> E t
E forall x. Lens' (t x) x
forall (t :: * -> *) a. Complicated t => Lens' (t a) a
_e
ei :: E t
ei = (forall x. Lens' (t x) x) -> E t
forall (t :: * -> *).
(forall x (f :: * -> *). Functor f => (x -> f x) -> t x -> f (t x))
-> E t
E forall x. Lens' (t x) x
forall (t :: * -> *) a. Complicated t => Lens' (t a) a
_i

instance Complicated Complex where
  _e :: (a -> f a) -> Complex a -> f (Complex a)
_e a -> f a
f (a
a :+ a
b) = (a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
b) (a -> Complex a) -> f a -> f (Complex a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f a
f a
a
  {-# INLINE _e #-}
  _i :: (a -> f a) -> Complex a -> f (Complex a)
_i a -> f a
f (a
a :+ a
b) = (a
a a -> a -> Complex a
forall a. a -> a -> Complex a
:+) (a -> Complex a) -> f a -> f (Complex a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f a
f a
b
  {-# INLINE _i #-}

instance Complicated Quaternion where
  _e :: (a -> f a) -> Quaternion a -> f (Quaternion a)
_e a -> f a
f (Quaternion a
a V3 a
v) = (a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
`Quaternion` V3 a
v) (a -> Quaternion a) -> f a -> f (Quaternion a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f a
f a
a
  {-# INLINE _e #-}
  _i :: (a -> f a) -> Quaternion a -> f (Quaternion a)
_i a -> f a
f (Quaternion a
a V3 a
v) = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
a (V3 a -> Quaternion a) -> f (V3 a) -> f (Quaternion a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f a) -> V3 a -> f (V3 a)
forall (t :: * -> *) a. R1 t => Lens' (t a) a
_x a -> f a
f V3 a
v
  {-# INLINE _i #-}

-- | A vector space that includes the basis elements '_e', '_i', '_j' and '_k'
class Complicated t => Hamiltonian t where
  _j, _k :: Lens' (t a) a
  _ijk :: Lens' (t a) (V3 a)

ej, ek :: Hamiltonian t => E t
ej :: E t
ej = (forall x. Lens' (t x) x) -> E t
forall (t :: * -> *).
(forall x (f :: * -> *). Functor f => (x -> f x) -> t x -> f (t x))
-> E t
E forall x. Lens' (t x) x
forall (t :: * -> *) a. Hamiltonian t => Lens' (t a) a
_j
ek :: E t
ek = (forall x. Lens' (t x) x) -> E t
forall (t :: * -> *).
(forall x (f :: * -> *). Functor f => (x -> f x) -> t x -> f (t x))
-> E t
E forall x. Lens' (t x) x
forall (t :: * -> *) a. Hamiltonian t => Lens' (t a) a
_k

instance Hamiltonian Quaternion where
  _j :: (a -> f a) -> Quaternion a -> f (Quaternion a)
_j a -> f a
f (Quaternion a
a V3 a
v) = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
a (V3 a -> Quaternion a) -> f (V3 a) -> f (Quaternion a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f a) -> V3 a -> f (V3 a)
forall (t :: * -> *) a. R2 t => Lens' (t a) a
_y a -> f a
f V3 a
v
  {-# INLINE _j #-}
  _k :: (a -> f a) -> Quaternion a -> f (Quaternion a)
_k a -> f a
f (Quaternion a
a V3 a
v) = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
a (V3 a -> Quaternion a) -> f (V3 a) -> f (Quaternion a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f a) -> V3 a -> f (V3 a)
forall (t :: * -> *) a. R3 t => Lens' (t a) a
_z a -> f a
f V3 a
v
  {-# INLINE _k #-}
  _ijk :: (V3 a -> f (V3 a)) -> Quaternion a -> f (Quaternion a)
_ijk V3 a -> f (V3 a)
f (Quaternion a
a V3 a
v) = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
a (V3 a -> Quaternion a) -> f (V3 a) -> f (Quaternion a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> V3 a -> f (V3 a)
f V3 a
v
  {-# INLINE _ijk #-}

instance Distributive Quaternion where
  distribute :: f (Quaternion a) -> Quaternion (f a)
distribute f (Quaternion a)
f = f a -> V3 (f a) -> Quaternion (f a)
forall a. a -> V3 a -> Quaternion a
Quaternion ((Quaternion a -> a) -> f (Quaternion a) -> f a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Quaternion a
x V3 a
_) -> a
x) f (Quaternion a)
f) (V3 (f a) -> Quaternion (f a)) -> V3 (f a) -> Quaternion (f a)
forall a b. (a -> b) -> a -> b
$ f a -> f a -> f a -> V3 (f a)
forall a. a -> a -> a -> V3 a
V3
    ((Quaternion a -> a) -> f (Quaternion a) -> f a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Quaternion a
_ (V3 a
y a
_ a
_)) -> a
y) f (Quaternion a)
f)
    ((Quaternion a -> a) -> f (Quaternion a) -> f a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Quaternion a
_ (V3 a
_ a
z a
_)) -> a
z) f (Quaternion a)
f)
    ((Quaternion a -> a) -> f (Quaternion a) -> f a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Quaternion a
_ (V3 a
_ a
_ a
w)) -> a
w) f (Quaternion a)
f)
  {-# INLINE distribute #-}

instance (Conjugate a, RealFloat a) => Conjugate (Quaternion a) where
  conjugate :: Quaternion a -> Quaternion a
conjugate (Quaternion a
e V3 a
v) = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion (a -> a
forall a. Conjugate a => a -> a
conjugate a
e) (V3 a -> V3 a
forall a. Num a => a -> a
negate V3 a
v)
  {-# INLINE conjugate #-}

reimagine :: RealFloat a => a -> a -> Quaternion a -> Quaternion a
reimagine :: a -> a -> Quaternion a -> Quaternion a
reimagine a
r a
s (Quaternion a
_ V3 a
v)
  | a -> Bool
forall a. RealFloat a => a -> Bool
isNaN a
s Bool -> Bool -> Bool
|| a -> Bool
forall a. RealFloat a => a -> Bool
isInfinite a
s = let aux :: a -> a
aux a
0 = a
0
                                  aux a
x = a
s a -> a -> a
forall a. Num a => a -> a -> a
* a
x
                              in a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
r (a -> a
aux (a -> a) -> V3 a -> V3 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> V3 a
v)
  | Bool
otherwise = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
r (V3 a
vV3 a -> a -> V3 a
forall (f :: * -> *) a. (Functor f, Num a) => f a -> a -> f a
^*a
s)
{-# INLINE reimagine #-}

-- | quadrance of the imaginary component
qi :: Num a => Quaternion a -> a
qi :: Quaternion a -> a
qi (Quaternion a
_ V3 a
v) = V3 a -> a
forall (f :: * -> *) a. (Metric f, Num a) => f a -> a
quadrance V3 a
v
{-# INLINE qi #-}

-- | norm of the imaginary component
absi :: Floating a => Quaternion a -> a
absi :: Quaternion a -> a
absi = a -> a
forall a. Floating a => a -> a
sqrt (a -> a) -> (Quaternion a -> a) -> Quaternion a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Quaternion a -> a
forall a. Num a => Quaternion a -> a
qi
{-# INLINE absi #-}

-- | raise a 'Quaternion' to a scalar power
pow :: RealFloat a => Quaternion a -> a -> Quaternion a
pow :: Quaternion a -> a -> Quaternion a
pow Quaternion a
q a
t = Quaternion a -> Quaternion a
forall a. Floating a => a -> a
exp (a
t a -> Quaternion a -> Quaternion a
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^ Quaternion a -> Quaternion a
forall a. Floating a => a -> a
log Quaternion a
q)
{-# INLINE pow #-}

sqrte2pqiq :: (Floating a, Ord a) => a -> a -> a
sqrte2pqiq :: a -> a -> a
sqrte2pqiq a
e a
qiq -- = sqrt (e*e + qiq)
  | a
e a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< - a
1.5097698010472593e153 = -(a
qiqa -> a -> a
forall a. Fractional a => a -> a -> a
/a
e) a -> a -> a
forall a. Num a => a -> a -> a
- a
e
  | a
e a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
5.582399551122541e57      = a -> a
forall a. Floating a => a -> a
sqrt (a
ea -> a -> a
forall a. Num a => a -> a -> a
*a
e a -> a -> a
forall a. Num a => a -> a -> a
+ a
qiq) -- direct definition
  | Bool
otherwise                     = (a
qiqa -> a -> a
forall a. Fractional a => a -> a -> a
/a
e) a -> a -> a
forall a. Num a => a -> a -> a
+ a
e
-- {-# SPECIALIZE sqrte2pqiq :: Double -> Double -> Double #-}
-- {-# SPECIALIZE sqrte2pqiq :: Float -> Float -> Float #-}
#ifdef HERBIE
{-# ANN sqrte2pqiq "NoHerbie" #-}
#endif

tanrhs :: (Floating a, Ord a) => a -> a -> a -> a
tanrhs :: a -> a -> a -> a
tanrhs a
sai a
ai a
d -- = cosh ai * (sai / ai) / d -- improved from 6.04 bits of error to 0.19 bits
  | a
sai a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< -a
4.618902267687042e-52 = (a
sai a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
d a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
ai) a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
cosh a
ai
  | a
sai a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
1.038530535935153e-39 = (a -> a
forall a. Floating a => a -> a
cosh a
ai a -> a -> a
forall a. Num a => a -> a -> a
* a
sai) a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
ai a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
d
  | Bool
otherwise = (a
sai a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
d a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
ai) a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
cosh a
ai
-- {-# SPECIALIZE tanrhs :: Double -> Double -> Double -> Double #-}
-- {-# SPECIALIZE tanrhs :: Float -> Float -> Float -> Float #-}
#ifdef HERBIE
{-# ANN tanrhs "NoHerbie" #-}
#endif


-- ehh..
instance RealFloat a => Floating (Quaternion a) where
  {-# SPECIALIZE instance Floating (Quaternion Float) #-}
  {-# SPECIALIZE instance Floating (Quaternion Double) #-}
  pi :: Quaternion a
pi = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
forall a. Floating a => a
pi V3 a
0
  {-# INLINE pi #-}
  exp :: Quaternion a -> Quaternion a
exp q :: Quaternion a
q@(Quaternion a
e V3 a
v)
    | a
qiq a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion (a -> a
forall a. Floating a => a -> a
exp a
e) V3 a
v
    | a
ai <- a -> a
forall a. Floating a => a -> a
sqrt a
qiq, a
exe <- a -> a
forall a. Floating a => a -> a
exp a
e = a -> a -> Quaternion a -> Quaternion a
forall a. RealFloat a => a -> a -> Quaternion a -> Quaternion a
reimagine (a
exe a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
cos a
ai) (a
exe a -> a -> a
forall a. Num a => a -> a -> a
* (a -> a
forall a. Floating a => a -> a
sin a
ai a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
ai)) Quaternion a
q
    where qiq :: a
qiq = Quaternion a -> a
forall a. Num a => Quaternion a -> a
qi Quaternion a
q
  {-# INLINE exp #-}
  log :: Quaternion a -> Quaternion a
log q :: Quaternion a
q@(Quaternion a
e V3 a
v)
    | a
qiq a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 = if a
e a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
0
                 then a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion (a -> a
forall a. Floating a => a -> a
log a
e) V3 a
v                   -- Using v rather than 0 preserves negative zeros
                 else a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion (a -> a
forall a. Num a => a -> a
negate (a -> a
forall a. Floating a => a -> a
log (a -> a
forall a. Num a => a -> a
negate a
e))) V3 a
v -- negative scalar: negate quaternion, take log, negate again, preserves negative zeros
    | a
ai <- a -> a
forall a. Floating a => a -> a
sqrt a
qiq = a -> a -> Quaternion a -> Quaternion a
forall a. RealFloat a => a -> a -> Quaternion a -> Quaternion a
reimagine (a -> a
forall a. Floating a => a -> a
log a
m) (a -> a
forall a. Floating a => a -> a
acos (a
e a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
m) a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
ai) Quaternion a
q
    where qiq :: a
qiq = Quaternion a -> a
forall a. Num a => Quaternion a -> a
qi Quaternion a
q
          m :: a
m = a -> a -> a
forall a. (Floating a, Ord a) => a -> a -> a
sqrte2pqiq a
e a
qiq
  {-# INLINE log #-}

  Quaternion a
x ** :: Quaternion a -> Quaternion a -> Quaternion a
** Quaternion a
y = Quaternion a -> Quaternion a
forall a. Floating a => a -> a
exp (Quaternion a
y Quaternion a -> Quaternion a -> Quaternion a
forall a. Num a => a -> a -> a
* Quaternion a -> Quaternion a
forall a. Floating a => a -> a
log Quaternion a
x)
  {-# INLINE (**) #-}

  sqrt :: Quaternion a -> Quaternion a
sqrt q :: Quaternion a
q@(Quaternion a
e V3 a
v)
    | a
m   a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 = Quaternion a
q
    | a
qiq a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 = if a
e a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
0
                 then a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion (a -> a
forall a. Floating a => a -> a
sqrt a
e) V3 a
0
                 else a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
0 (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 (a -> a
forall a. Floating a => a -> a
sqrt (a -> a
forall a. Num a => a -> a
negate a
e)) a
0 a
0)
    | a
im <- a -> a
forall a. Floating a => a -> a
sqrt (a
0.5a -> a -> a
forall a. Num a => a -> a -> a
*(a
ma -> a -> a
forall a. Num a => a -> a -> a
-a
e)) a -> a -> a
forall a. Fractional a => a -> a -> a
/ a -> a
forall a. Floating a => a -> a
sqrt a
qiq = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion (a
0.5a -> a -> a
forall a. Num a => a -> a -> a
*(a
ma -> a -> a
forall a. Num a => a -> a -> a
+a
e)) (V3 a
vV3 a -> a -> V3 a
forall (f :: * -> *) a. (Functor f, Num a) => f a -> a -> f a
^*a
im)
    where qiq :: a
qiq = Quaternion a -> a
forall a. Num a => Quaternion a -> a
qi Quaternion a
q
          m :: a
m = a -> a -> a
forall a. (Floating a, Ord a) => a -> a -> a
sqrte2pqiq a
e a
qiq
  {-# INLINE sqrt #-}

  cos :: Quaternion a -> Quaternion a
cos q :: Quaternion a
q@(Quaternion a
e V3 a
v)
    | a
qiq a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion (a -> a
forall a. Floating a => a -> a
cos a
e) V3 a
v
    | a
ai <- a -> a
forall a. Floating a => a -> a
sqrt a
qiq = a -> a -> Quaternion a -> Quaternion a
forall a. RealFloat a => a -> a -> Quaternion a -> Quaternion a
reimagine (a -> a
forall a. Floating a => a -> a
cos a
e a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
cosh a
ai) (- a -> a
forall a. Floating a => a -> a
sin a
e a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
ai a -> a -> a
forall a. Fractional a => a -> a -> a
/ a -> a
forall a. Floating a => a -> a
sinh a
ai) Quaternion a
q -- 0.15 bits error
    where qiq :: a
qiq = Quaternion a -> a
forall a. Num a => Quaternion a -> a
qi Quaternion a
q
  {-# INLINE cos #-}

  sin :: Quaternion a -> Quaternion a
sin q :: Quaternion a
q@(Quaternion a
e V3 a
v)
    | a
qiq a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion (a -> a
forall a. Floating a => a -> a
sin a
e) V3 a
v
    | a
ai <- a -> a
forall a. Floating a => a -> a
sqrt a
qiq = a -> a -> Quaternion a -> Quaternion a
forall a. RealFloat a => a -> a -> Quaternion a -> Quaternion a
reimagine (a -> a
forall a. Floating a => a -> a
sin a
e a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
cosh a
ai) (a -> a
forall a. Floating a => a -> a
cos a
e a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
sinh a
ai a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
ai) Quaternion a
q
    where qiq :: a
qiq = Quaternion a -> a
forall a. Num a => Quaternion a -> a
qi Quaternion a
q
  {-# INLINE sin #-}

  tan :: Quaternion a -> Quaternion a
tan q :: Quaternion a
q@(Quaternion a
e V3 a
v)
    | a
qiq a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion (a -> a
forall a. Floating a => a -> a
tan a
e) V3 a
v
    | a
ai <- a -> a
forall a. Floating a => a -> a
sqrt a
qiq, a
ce <- a -> a
forall a. Floating a => a -> a
cos a
e, a
sai <- a -> a
forall a. Floating a => a -> a
sinh a
ai, a
d <- a
cea -> a -> a
forall a. Num a => a -> a -> a
*a
ce a -> a -> a
forall a. Num a => a -> a -> a
+ a
saia -> a -> a
forall a. Num a => a -> a -> a
*a
sai =
      a -> a -> Quaternion a -> Quaternion a
forall a. RealFloat a => a -> a -> Quaternion a -> Quaternion a
reimagine (a
ce a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
sin a
e a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
d) (a -> a -> a -> a
forall a. (Floating a, Ord a) => a -> a -> a -> a
tanrhs a
sai a
ai a
d) Quaternion a
q
    where qiq :: a
qiq = Quaternion a -> a
forall a. Num a => Quaternion a -> a
qi Quaternion a
q
  {-# INLINE tan #-}

  sinh :: Quaternion a -> Quaternion a
sinh q :: Quaternion a
q@(Quaternion a
e V3 a
v)
    | a
qiq a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion (a -> a
forall a. Floating a => a -> a
sinh a
e) V3 a
v
    | a
ai <- a -> a
forall a. Floating a => a -> a
sqrt a
qiq = a -> a -> Quaternion a -> Quaternion a
forall a. RealFloat a => a -> a -> Quaternion a -> Quaternion a
reimagine (a -> a
forall a. Floating a => a -> a
sinh a
e a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
cos a
ai) (a -> a
forall a. Floating a => a -> a
cosh a
e a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
sin a
ai a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
ai) Quaternion a
q
    where qiq :: a
qiq = Quaternion a -> a
forall a. Num a => Quaternion a -> a
qi Quaternion a
q
  {-# INLINE sinh #-}

  cosh :: Quaternion a -> Quaternion a
cosh q :: Quaternion a
q@(Quaternion a
e V3 a
v)
    | a
qiq a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion (a -> a
forall a. Floating a => a -> a
cosh a
e) V3 a
v
    | a
ai <- a -> a
forall a. Floating a => a -> a
sqrt a
qiq = a -> a -> Quaternion a -> Quaternion a
forall a. RealFloat a => a -> a -> Quaternion a -> Quaternion a
reimagine (a -> a
forall a. Floating a => a -> a
cosh a
e a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
cos a
ai) (a -> a
forall a. Floating a => a -> a
sin a
ai a -> a -> a
forall a. Num a => a -> a -> a
* (a -> a
forall a. Floating a => a -> a
sinh a
e a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
ai)) Quaternion a
q
    where qiq :: a
qiq = Quaternion a -> a
forall a. Num a => Quaternion a -> a
qi Quaternion a
q
  {-# INLINE cosh #-}

  tanh :: Quaternion a -> Quaternion a
tanh q :: Quaternion a
q@(Quaternion a
e V3 a
v)
    | a
qiq a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion (a -> a
forall a. Floating a => a -> a
tanh a
e) V3 a
v
    | a
ai <- a -> a
forall a. Floating a => a -> a
sqrt a
qiq, a
se <- a -> a
forall a. Floating a => a -> a
sinh a
e, a
cai <- a -> a
forall a. Floating a => a -> a
cos a
ai, a
d <- a
sea -> a -> a
forall a. Num a => a -> a -> a
*a
se a -> a -> a
forall a. Num a => a -> a -> a
+ a
caia -> a -> a
forall a. Num a => a -> a -> a
*a
cai =
      a -> a -> Quaternion a -> Quaternion a
forall a. RealFloat a => a -> a -> Quaternion a -> Quaternion a
reimagine (a -> a
forall a. Floating a => a -> a
cosh a
e a -> a -> a
forall a. Num a => a -> a -> a
* a
se a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
d) (a -> a -> a -> a
forall a. (Floating a, Ord a) => a -> a -> a -> a
tanhrhs a
cai a
ai a
d) Quaternion a
q
    where qiq :: a
qiq = Quaternion a -> a
forall a. Num a => Quaternion a -> a
qi Quaternion a
q
  {-# INLINE tanh #-}

  asin :: Quaternion a -> Quaternion a
asin = (Complex a -> Complex a) -> Quaternion a -> Quaternion a
forall a.
RealFloat a =>
(Complex a -> Complex a) -> Quaternion a -> Quaternion a
cut Complex a -> Complex a
forall a. Floating a => a -> a
asin
  {-# INLINE asin #-}
  acos :: Quaternion a -> Quaternion a
acos = (Complex a -> Complex a) -> Quaternion a -> Quaternion a
forall a.
RealFloat a =>
(Complex a -> Complex a) -> Quaternion a -> Quaternion a
cut Complex a -> Complex a
forall a. Floating a => a -> a
acos
  {-# INLINE acos #-}
  atan :: Quaternion a -> Quaternion a
atan = (Complex a -> Complex a) -> Quaternion a -> Quaternion a
forall a.
RealFloat a =>
(Complex a -> Complex a) -> Quaternion a -> Quaternion a
cut Complex a -> Complex a
forall a. Floating a => a -> a
atan
  {-# INLINE atan #-}

  asinh :: Quaternion a -> Quaternion a
asinh = (Complex a -> Complex a) -> Quaternion a -> Quaternion a
forall a.
RealFloat a =>
(Complex a -> Complex a) -> Quaternion a -> Quaternion a
cut Complex a -> Complex a
forall a. Floating a => a -> a
asinh
  {-# INLINE asinh #-}
  acosh :: Quaternion a -> Quaternion a
acosh = (Complex a -> Complex a) -> Quaternion a -> Quaternion a
forall a.
RealFloat a =>
(Complex a -> Complex a) -> Quaternion a -> Quaternion a
cut Complex a -> Complex a
forall a. Floating a => a -> a
acosh
  {-# INLINE acosh #-}
  atanh :: Quaternion a -> Quaternion a
atanh = (Complex a -> Complex a) -> Quaternion a -> Quaternion a
forall a.
RealFloat a =>
(Complex a -> Complex a) -> Quaternion a -> Quaternion a
cut Complex a -> Complex a
forall a. Floating a => a -> a
atanh
  {-# INLINE atanh #-}

tanhrhs :: (Floating a, Ord a) => a -> a -> a -> a
tanhrhs :: a -> a -> a -> a
tanhrhs a
cai a
ai a
d -- = cai * (sin ai / ai) / d
  | a
d a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= -a
4.2173720203427147e-29 Bool -> Bool -> Bool
&& a
d a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
4.446702369113811e64 = a
cai a -> a -> a
forall a. Fractional a => a -> a -> a
/ (a
d a -> a -> a
forall a. Num a => a -> a -> a
* (a
ai a -> a -> a
forall a. Fractional a => a -> a -> a
/ a -> a
forall a. Floating a => a -> a
sin a
ai))
  | Bool
otherwise                                                = a
cai a -> a -> a
forall a. Num a => a -> a -> a
* (a
1 a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
ai a -> a -> a
forall a. Fractional a => a -> a -> a
/ a -> a
forall a. Floating a => a -> a
sin a
ai) a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
d
-- {-# SPECIALIZE tanhrhs :: Double -> Double -> Double -> Double #-}
-- {-# SPECIALIZE tanhrhs :: Float -> Float -> Float -> Float #-}
#ifdef HERBIE
{-# ANN tanhrhs "NoHerbie" #-}
#endif

-- | Helper for calculating with specific branch cuts
cut :: RealFloat a => (Complex a -> Complex a) -> Quaternion a -> Quaternion a
cut :: (Complex a -> Complex a) -> Quaternion a -> Quaternion a
cut Complex a -> Complex a
f q :: Quaternion a
q@(Quaternion a
e (V3 a
_ a
y a
z))
  | a
qiq a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
a (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
b a
y a
z)
  | Bool
otherwise = a -> a -> Quaternion a -> Quaternion a
forall a. RealFloat a => a -> a -> Quaternion a -> Quaternion a
reimagine a
a (a
b a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
ai) Quaternion a
q
  where qiq :: a
qiq = Quaternion a -> a
forall a. Num a => Quaternion a -> a
qi Quaternion a
q
        ai :: a
ai = a -> a
forall a. Floating a => a -> a
sqrt a
qiq
        a
a :+ a
b = Complex a -> Complex a
f (a
e a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
ai)
{-# INLINE cut #-}

-- | Helper for calculating with specific branch cuts
cutWith :: RealFloat a => Complex a -> Quaternion a -> Quaternion a
cutWith :: Complex a -> Quaternion a -> Quaternion a
cutWith (a
r :+ a
im) q :: Quaternion a
q@(Quaternion a
e V3 a
v)
  | a
e a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
0 Bool -> Bool -> Bool
|| a
qiq a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 Bool -> Bool -> Bool
|| a -> Bool
forall a. RealFloat a => a -> Bool
isNaN a
qiq Bool -> Bool -> Bool
|| a -> Bool
forall a. RealFloat a => a -> Bool
isInfinite a
qiq = String -> Quaternion a
forall a. HasCallStack => String -> a
error String
"bad cut"
  | a
s <- a
im a -> a -> a
forall a. Fractional a => a -> a -> a
/ a -> a
forall a. Floating a => a -> a
sqrt a
qiq = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
r (V3 a
vV3 a -> a -> V3 a
forall (f :: * -> *) a. (Functor f, Num a) => f a -> a -> f a
^*a
s)
  where qiq :: a
qiq = Quaternion a -> a
forall a. Num a => Quaternion a -> a
qi Quaternion a
q
{-# INLINE cutWith #-}

-- | 'asin' with a specified branch cut.
asinq :: RealFloat a => Quaternion a -> Quaternion a -> Quaternion a
asinq :: Quaternion a -> Quaternion a -> Quaternion a
asinq q :: Quaternion a
q@(Quaternion a
e V3 a
_) Quaternion a
u
  | a
qiq a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
0.0 Bool -> Bool -> Bool
|| a
e a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= -a
1 Bool -> Bool -> Bool
&& a
e a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
1 = Quaternion a -> Quaternion a
forall a. Floating a => a -> a
asin Quaternion a
q
  | Bool
otherwise = Complex a -> Quaternion a -> Quaternion a
forall a. RealFloat a => Complex a -> Quaternion a -> Quaternion a
cutWith (Complex a -> Complex a
forall a. Floating a => a -> a
asin (a
e a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a -> a
forall a. Floating a => a -> a
sqrt a
qiq)) Quaternion a
u
  where qiq :: a
qiq = Quaternion a -> a
forall a. Num a => Quaternion a -> a
qi Quaternion a
q
{-# INLINE asinq #-}

-- | 'acos' with a specified branch cut.
acosq :: RealFloat a => Quaternion a -> Quaternion a -> Quaternion a
acosq :: Quaternion a -> Quaternion a -> Quaternion a
acosq q :: Quaternion a
q@(Quaternion a
e V3 a
_) Quaternion a
u
  | a
qiq a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
0.0 Bool -> Bool -> Bool
|| a
e a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= -a
1 Bool -> Bool -> Bool
&& a
e a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
1 = Quaternion a -> Quaternion a
forall a. Floating a => a -> a
acos Quaternion a
q
  | Bool
otherwise = Complex a -> Quaternion a -> Quaternion a
forall a. RealFloat a => Complex a -> Quaternion a -> Quaternion a
cutWith (Complex a -> Complex a
forall a. Floating a => a -> a
acos (a
e a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a -> a
forall a. Floating a => a -> a
sqrt a
qiq)) Quaternion a
u
  where qiq :: a
qiq = Quaternion a -> a
forall a. Num a => Quaternion a -> a
qi Quaternion a
q
{-# INLINE acosq #-}

-- | 'atan' with a specified branch cut.
atanq :: RealFloat a => Quaternion a -> Quaternion a -> Quaternion a
atanq :: Quaternion a -> Quaternion a -> Quaternion a
atanq q :: Quaternion a
q@(Quaternion a
e V3 a
_) Quaternion a
u
  | a
e a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
0.0 Bool -> Bool -> Bool
|| a
qiq a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= -a
1 Bool -> Bool -> Bool
&& a
qiq a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
1 = Quaternion a -> Quaternion a
forall a. Floating a => a -> a
atan Quaternion a
q
  | Bool
otherwise = Complex a -> Quaternion a -> Quaternion a
forall a. RealFloat a => Complex a -> Quaternion a -> Quaternion a
cutWith (Complex a -> Complex a
forall a. Floating a => a -> a
atan (a
e a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a -> a
forall a. Floating a => a -> a
sqrt a
qiq)) Quaternion a
u
  where qiq :: a
qiq = Quaternion a -> a
forall a. Num a => Quaternion a -> a
qi Quaternion a
q
{-# INLINE atanq #-}

-- | 'asinh' with a specified branch cut.
asinhq :: RealFloat a => Quaternion a -> Quaternion a -> Quaternion a
asinhq :: Quaternion a -> Quaternion a -> Quaternion a
asinhq q :: Quaternion a
q@(Quaternion a
e V3 a
_) Quaternion a
u
  | a
e a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
0.0 Bool -> Bool -> Bool
|| a
qiq a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= -a
1 Bool -> Bool -> Bool
&& a
qiq a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
1 = Quaternion a -> Quaternion a
forall a. Floating a => a -> a
asinh Quaternion a
q
  | Bool
otherwise = Complex a -> Quaternion a -> Quaternion a
forall a. RealFloat a => Complex a -> Quaternion a -> Quaternion a
cutWith (Complex a -> Complex a
forall a. Floating a => a -> a
asinh (a
e a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a -> a
forall a. Floating a => a -> a
sqrt a
qiq)) Quaternion a
u
  where qiq :: a
qiq = Quaternion a -> a
forall a. Num a => Quaternion a -> a
qi Quaternion a
q
{-# INLINE asinhq #-}

-- | 'acosh' with a specified branch cut.
acoshq :: RealFloat a => Quaternion a -> Quaternion a -> Quaternion a
acoshq :: Quaternion a -> Quaternion a -> Quaternion a
acoshq q :: Quaternion a
q@(Quaternion a
e V3 a
_) Quaternion a
u
  | a
qiq a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
0.0 Bool -> Bool -> Bool
|| a
e a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
1 = Quaternion a -> Quaternion a
forall a. Floating a => a -> a
asinh Quaternion a
q
  | Bool
otherwise = Complex a -> Quaternion a -> Quaternion a
forall a. RealFloat a => Complex a -> Quaternion a -> Quaternion a
cutWith (Complex a -> Complex a
forall a. Floating a => a -> a
acosh (a
e a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a -> a
forall a. Floating a => a -> a
sqrt a
qiq)) Quaternion a
u
  where qiq :: a
qiq = Quaternion a -> a
forall a. Num a => Quaternion a -> a
qi Quaternion a
q
{-# INLINE acoshq #-}

-- | 'atanh' with a specified branch cut.
atanhq :: RealFloat a => Quaternion a -> Quaternion a -> Quaternion a
atanhq :: Quaternion a -> Quaternion a -> Quaternion a
atanhq q :: Quaternion a
q@(Quaternion a
e V3 a
_) Quaternion a
u
  | a
qiq a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
0.0 Bool -> Bool -> Bool
|| a
e a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> -a
1 Bool -> Bool -> Bool
&& a
e a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
1 = Quaternion a -> Quaternion a
forall a. Floating a => a -> a
atanh Quaternion a
q
  | Bool
otherwise = Complex a -> Quaternion a -> Quaternion a
forall a. RealFloat a => Complex a -> Quaternion a -> Quaternion a
cutWith (Complex a -> Complex a
forall a. Floating a => a -> a
atanh (a
e a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a -> a
forall a. Floating a => a -> a
sqrt a
qiq)) Quaternion a
u
  where qiq :: a
qiq = Quaternion a -> a
forall a. Num a => Quaternion a -> a
qi Quaternion a
q
{-# INLINE atanhq #-}

-- | Spherical linear interpolation between two quaternions.

slerp :: RealFloat a => Quaternion a -> Quaternion a -> a -> Quaternion a
slerp :: Quaternion a -> Quaternion a -> a -> Quaternion a
slerp Quaternion a
q Quaternion a
p a
t
  | a
1.0 a -> a -> a
forall a. Num a => a -> a -> a
- a
cosphi a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
1e-8 = Quaternion a
q
  | Bool
otherwise           = ((a -> a
forall a. Floating a => a -> a
sin ((a
1a -> a -> a
forall a. Num a => a -> a -> a
-a
t)a -> a -> a
forall a. Num a => a -> a -> a
*a
phi) a -> Quaternion a -> Quaternion a
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^ Quaternion a
q) Quaternion a -> Quaternion a -> Quaternion a
forall a. Num a => a -> a -> a
+ a -> a
forall a. Floating a => a -> a
sin (a
ta -> a -> a
forall a. Num a => a -> a -> a
*a
phi) a -> Quaternion a -> Quaternion a
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^ Quaternion a -> Quaternion a
f Quaternion a
p) Quaternion a -> a -> Quaternion a
forall (f :: * -> *) a.
(Functor f, Fractional a) =>
f a -> a -> f a
^/ a -> a
forall a. Floating a => a -> a
sin a
phi
  where
    dqp :: a
dqp = Quaternion a -> Quaternion a -> a
forall (f :: * -> *) a. (Metric f, Num a) => f a -> f a -> a
dot Quaternion a
q Quaternion a
p
    (a
cosphi, Quaternion a -> Quaternion a
f) = if a
dqp a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 then (-a
dqp, Quaternion a -> Quaternion a
forall a. Num a => a -> a
negate) else (a
dqp, Quaternion a -> Quaternion a
forall a. a -> a
id)
    phi :: a
phi = a -> a
forall a. Floating a => a -> a
acos a
cosphi
{-# SPECIALIZE slerp :: Quaternion Float -> Quaternion Float -> Float -> Quaternion Float #-}
{-# SPECIALIZE slerp :: Quaternion Double -> Quaternion Double -> Double -> Quaternion Double #-}

-- | Apply a rotation to a vector.
rotate :: (Conjugate a, RealFloat a) => Quaternion a -> V3 a -> V3 a
rotate :: Quaternion a -> V3 a -> V3 a
rotate Quaternion a
q V3 a
v = V3 a
ijk where
  Quaternion a
_ V3 a
ijk = Quaternion a
q Quaternion a -> Quaternion a -> Quaternion a
forall a. Num a => a -> a -> a
* a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
0 V3 a
v Quaternion a -> Quaternion a -> Quaternion a
forall a. Num a => a -> a -> a
* Quaternion a -> Quaternion a
forall a. Conjugate a => a -> a
conjugate Quaternion a
q
{-# SPECIALIZE rotate :: Quaternion Float -> V3 Float -> V3 Float #-}
{-# SPECIALIZE rotate :: Quaternion Double -> V3 Double -> V3 Double #-}

instance (RealFloat a, Epsilon a) => Epsilon (Quaternion a) where
  nearZero :: Quaternion a -> Bool
nearZero = a -> Bool
forall a. Epsilon a => a -> Bool
nearZero (a -> Bool) -> (Quaternion a -> a) -> Quaternion a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Quaternion a -> a
forall (f :: * -> *) a. (Metric f, Num a) => f a -> a
quadrance
  {-# INLINE nearZero #-}

-- | @'axisAngle' axis theta@ builds a 'Quaternion' representing a
-- rotation of @theta@ radians about @axis@.
axisAngle :: (Epsilon a, Floating a) => V3 a -> a -> Quaternion a
axisAngle :: V3 a -> a -> Quaternion a
axisAngle V3 a
axis a
theta = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion (a -> a
forall a. Floating a => a -> a
cos a
half) (a -> a
forall a. Floating a => a -> a
sin a
half a -> V3 a -> V3 a
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^ V3 a -> V3 a
forall a (f :: * -> *).
(Floating a, Metric f, Epsilon a) =>
f a -> f a
normalize V3 a
axis)
  where half :: a
half = a
theta a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
2
{-# INLINE axisAngle #-}

data instance U.Vector    (Quaternion a) =  V_Quaternion !Int (U.Vector    a)
data instance U.MVector s (Quaternion a) = MV_Quaternion !Int (U.MVector s a)
instance U.Unbox a => U.Unbox (Quaternion a)

instance U.Unbox a => M.MVector U.MVector (Quaternion a) where
  basicLength :: MVector s (Quaternion a) -> Int
basicLength (MV_Quaternion n _) = Int
n
  basicUnsafeSlice :: Int -> Int -> MVector s (Quaternion a) -> MVector s (Quaternion a)
basicUnsafeSlice Int
m Int
n (MV_Quaternion _ v) = Int -> MVector s a -> MVector s (Quaternion a)
forall s a. Int -> MVector s a -> MVector s (Quaternion a)
MV_Quaternion Int
n (Int -> Int -> MVector s a -> MVector s a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
M.basicUnsafeSlice (Int
4Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
m) (Int
4Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
n) MVector s a
v)
  basicOverlaps :: MVector s (Quaternion a) -> MVector s (Quaternion a) -> Bool
basicOverlaps (MV_Quaternion _ v) (MV_Quaternion _ u) = MVector s a -> MVector s a -> Bool
forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> Bool
M.basicOverlaps MVector s a
v MVector s a
u
  basicUnsafeNew :: Int -> m (MVector (PrimState m) (Quaternion a))
basicUnsafeNew Int
n = (MVector (PrimState m) a -> MVector (PrimState m) (Quaternion a))
-> m (MVector (PrimState m) a)
-> m (MVector (PrimState m) (Quaternion a))
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (Int
-> MVector (PrimState m) a -> MVector (PrimState m) (Quaternion a)
forall s a. Int -> MVector s a -> MVector s (Quaternion a)
MV_Quaternion Int
n) (Int -> m (MVector (PrimState m) a)
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
Int -> m (v (PrimState m) a)
M.basicUnsafeNew (Int
4Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
n))
  basicUnsafeRead :: MVector (PrimState m) (Quaternion a) -> Int -> m (Quaternion a)
basicUnsafeRead (MV_Quaternion _ v) Int
i =
    do let o :: Int
o = Int
4Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
i
       a
x <- MVector (PrimState m) a -> Int -> m a
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> m a
M.basicUnsafeRead MVector (PrimState m) a
v Int
o
       a
y <- MVector (PrimState m) a -> Int -> m a
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> m a
M.basicUnsafeRead MVector (PrimState m) a
v (Int
oInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
       a
z <- MVector (PrimState m) a -> Int -> m a
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> m a
M.basicUnsafeRead MVector (PrimState m) a
v (Int
oInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
2)
       a
w <- MVector (PrimState m) a -> Int -> m a
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> m a
M.basicUnsafeRead MVector (PrimState m) a
v (Int
oInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
3)
       Quaternion a -> m (Quaternion a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
x (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
y a
z a
w))
  basicUnsafeWrite :: MVector (PrimState m) (Quaternion a) -> Int -> Quaternion a -> m ()
basicUnsafeWrite (MV_Quaternion _ v) Int
i (Quaternion a
x (V3 a
y a
z a
w)) =
    do let o :: Int
o = Int
4Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
i
       MVector (PrimState m) a -> Int -> a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> a -> m ()
M.basicUnsafeWrite MVector (PrimState m) a
v Int
o     a
x
       MVector (PrimState m) a -> Int -> a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> a -> m ()
M.basicUnsafeWrite MVector (PrimState m) a
v (Int
oInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) a
y
       MVector (PrimState m) a -> Int -> a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> a -> m ()
M.basicUnsafeWrite MVector (PrimState m) a
v (Int
oInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
2) a
z
       MVector (PrimState m) a -> Int -> a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> a -> m ()
M.basicUnsafeWrite MVector (PrimState m) a
v (Int
oInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
3) a
w
  basicInitialize :: MVector (PrimState m) (Quaternion a) -> m ()
basicInitialize (MV_Quaternion _ v) = MVector (PrimState m) a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> m ()
M.basicInitialize MVector (PrimState m) a
v

instance U.Unbox a => G.Vector U.Vector (Quaternion a) where
  basicUnsafeFreeze :: Mutable Vector (PrimState m) (Quaternion a)
-> m (Vector (Quaternion a))
basicUnsafeFreeze (MV_Quaternion n v) = (Vector a -> Vector (Quaternion a))
-> m (Vector a) -> m (Vector (Quaternion a))
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM ( Int -> Vector a -> Vector (Quaternion a)
forall a. Int -> Vector a -> Vector (Quaternion a)
V_Quaternion Int
n) (Mutable Vector (PrimState m) a -> m (Vector a)
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, PrimMonad m) =>
Mutable v (PrimState m) a -> m (v a)
G.basicUnsafeFreeze MVector (PrimState m) a
Mutable Vector (PrimState m) a
v)
  basicUnsafeThaw :: Vector (Quaternion a)
-> m (Mutable Vector (PrimState m) (Quaternion a))
basicUnsafeThaw   ( V_Quaternion n v) = (MVector (PrimState m) a -> MVector (PrimState m) (Quaternion a))
-> m (MVector (PrimState m) a)
-> m (MVector (PrimState m) (Quaternion a))
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (Int
-> MVector (PrimState m) a -> MVector (PrimState m) (Quaternion a)
forall s a. Int -> MVector s a -> MVector s (Quaternion a)
MV_Quaternion Int
n) (Vector a -> m (Mutable Vector (PrimState m) a)
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, PrimMonad m) =>
v a -> m (Mutable v (PrimState m) a)
G.basicUnsafeThaw   Vector a
v)
  basicLength :: Vector (Quaternion a) -> Int
basicLength       ( V_Quaternion n _) = Int
n
  basicUnsafeSlice :: Int -> Int -> Vector (Quaternion a) -> Vector (Quaternion a)
basicUnsafeSlice Int
m Int
n (V_Quaternion _ v) = Int -> Vector a -> Vector (Quaternion a)
forall a. Int -> Vector a -> Vector (Quaternion a)
V_Quaternion Int
n (Int -> Int -> Vector a -> Vector a
forall (v :: * -> *) a. Vector v a => Int -> Int -> v a -> v a
G.basicUnsafeSlice (Int
4Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
m) (Int
4Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
n) Vector a
v)
  basicUnsafeIndexM :: Vector (Quaternion a) -> Int -> m (Quaternion a)
basicUnsafeIndexM (V_Quaternion _ v) Int
i =
    do let o :: Int
o = Int
4Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
i
       a
x <- Vector a -> Int -> m a
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, Monad m) =>
v a -> Int -> m a
G.basicUnsafeIndexM Vector a
v Int
o
       a
y <- Vector a -> Int -> m a
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, Monad m) =>
v a -> Int -> m a
G.basicUnsafeIndexM Vector a
v (Int
oInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
       a
z <- Vector a -> Int -> m a
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, Monad m) =>
v a -> Int -> m a
G.basicUnsafeIndexM Vector a
v (Int
oInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
2)
       a
w <- Vector a -> Int -> m a
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, Monad m) =>
v a -> Int -> m a
G.basicUnsafeIndexM Vector a
v (Int
oInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
3)
       Quaternion a -> m (Quaternion a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
x (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
y a
z a
w))

instance MonadZip Quaternion where
  mzipWith :: (a -> b -> c) -> Quaternion a -> Quaternion b -> Quaternion c
mzipWith = (a -> b -> c) -> Quaternion a -> Quaternion b -> Quaternion c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2

instance MonadFix Quaternion where
  mfix :: (a -> Quaternion a) -> Quaternion a
mfix a -> Quaternion a
f = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion (let Quaternion a
a V3 a
_ = a -> Quaternion a
f a
a in a
a)
                      (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 (let Quaternion a
_ (V3 a
a a
_ a
_) = a -> Quaternion a
f a
a in a
a)
                          (let Quaternion a
_ (V3 a
_ a
a a
_) = a -> Quaternion a
f a
a in a
a)
                          (let Quaternion a
_ (V3 a
_ a
_ a
a) = a -> Quaternion a
f a
a in a
a))

instance NFData a => NFData (Quaternion a) where
  rnf :: Quaternion a -> ()
rnf (Quaternion a
a V3 a
b) = a -> ()
forall a. NFData a => a -> ()
rnf a
a () -> () -> ()
`seq` V3 a -> ()
forall a. NFData a => a -> ()
rnf V3 a
b

instance Serial1 Quaternion where
  serializeWith :: (a -> m ()) -> Quaternion a -> m ()
serializeWith a -> m ()
f (Quaternion a
a V3 a
b) = a -> m ()
f a
a m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (a -> m ()) -> V3 a -> m ()
forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
serializeWith a -> m ()
f V3 a
b
  deserializeWith :: m a -> m (Quaternion a)
deserializeWith m a
f = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion (a -> V3 a -> Quaternion a) -> m a -> m (V3 a -> Quaternion a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a
f m (V3 a -> Quaternion a) -> m (V3 a) -> m (Quaternion a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> m a -> m (V3 a)
forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadGet m) =>
m a -> m (f a)
deserializeWith m a
f

instance Serial a => Serial (Quaternion a) where
  serialize :: Quaternion a -> m ()
serialize = (a -> m ()) -> Quaternion a -> m ()
forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
serializeWith a -> m ()
forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
  deserialize :: m (Quaternion a)
deserialize = m a -> m (Quaternion a)
forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadGet m) =>
m a -> m (f a)
deserializeWith m a
forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Binary a => Binary (Quaternion a) where
  put :: Quaternion a -> Put
put = (a -> Put) -> Quaternion a -> Put
forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
serializeWith a -> Put
forall t. Binary t => t -> Put
Binary.put
  get :: Get (Quaternion a)
get = Get a -> Get (Quaternion a)
forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadGet m) =>
m a -> m (f a)
deserializeWith Get a
forall t. Binary t => Get t
Binary.get

instance Serialize a => Serialize (Quaternion a) where
  put :: Putter (Quaternion a)
put = (a -> PutM ()) -> Putter (Quaternion a)
forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
serializeWith a -> PutM ()
forall t. Serialize t => Putter t
Cereal.put
  get :: Get (Quaternion a)
get = Get a -> Get (Quaternion a)
forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadGet m) =>
m a -> m (f a)
deserializeWith Get a
forall t. Serialize t => Get t
Cereal.get

instance Eq1 Quaternion where
  liftEq :: (a -> b -> Bool) -> Quaternion a -> Quaternion b -> Bool
liftEq a -> b -> Bool
f (Quaternion a
a V3 a
b) (Quaternion b
c V3 b
d) = a -> b -> Bool
f a
a b
c Bool -> Bool -> Bool
&& (a -> b -> Bool) -> V3 a -> V3 b -> Bool
forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq a -> b -> Bool
f V3 a
b V3 b
d
instance Ord1 Quaternion where
  liftCompare :: (a -> b -> Ordering) -> Quaternion a -> Quaternion b -> Ordering
liftCompare a -> b -> Ordering
f (Quaternion a
a V3 a
b) (Quaternion b
c V3 b
d) = a -> b -> Ordering
f a
a b
c Ordering -> Ordering -> Ordering
forall a. Monoid a => a -> a -> a
`mappend` (a -> b -> Ordering) -> V3 a -> V3 b -> Ordering
forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare a -> b -> Ordering
f V3 a
b V3 b
d
instance Show1 Quaternion where
  liftShowsPrec :: (Int -> a -> ShowS)
-> ([a] -> ShowS) -> Int -> Quaternion a -> ShowS
liftShowsPrec Int -> a -> ShowS
f [a] -> ShowS
g Int
d (Quaternion a
a V3 a
b) = (Int -> a -> ShowS)
-> (Int -> V3 a -> ShowS) -> String -> Int -> a -> V3 a -> ShowS
forall a b.
(Int -> a -> ShowS)
-> (Int -> b -> ShowS) -> String -> Int -> a -> b -> ShowS
showsBinaryWith Int -> a -> ShowS
f ((Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> V3 a -> ShowS
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
liftShowsPrec Int -> a -> ShowS
f [a] -> ShowS
g) String
"Quaternion" Int
d a
a V3 a
b
instance Read1 Quaternion where
  liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Quaternion a)
liftReadsPrec Int -> ReadS a
f ReadS [a]
g = (String -> ReadS (Quaternion a)) -> Int -> ReadS (Quaternion a)
forall a. (String -> ReadS a) -> Int -> ReadS a
readsData ((String -> ReadS (Quaternion a)) -> Int -> ReadS (Quaternion a))
-> (String -> ReadS (Quaternion a)) -> Int -> ReadS (Quaternion a)
forall a b. (a -> b) -> a -> b
$ (Int -> ReadS a)
-> (Int -> ReadS (V3 a))
-> String
-> (a -> V3 a -> Quaternion a)
-> String
-> ReadS (Quaternion a)
forall a b t.
(Int -> ReadS a)
-> (Int -> ReadS b) -> String -> (a -> b -> t) -> String -> ReadS t
readsBinaryWith Int -> ReadS a
f ((Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (V3 a)
forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
liftReadsPrec Int -> ReadS a
f ReadS [a]
g) String
"Quaternion" a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion

instance Field1 (Quaternion a) (Quaternion a) a a where
  _1 :: (a -> f a) -> Quaternion a -> f (Quaternion a)
_1 a -> f a
f (Quaternion a
w V3 a
xyz) = a -> f a
f a
w f a -> (a -> Quaternion a) -> f (Quaternion a)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \a
w' -> a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
w' V3 a
xyz

instance Field2 (Quaternion a) (Quaternion a) a a where
  _2 :: (a -> f a) -> Quaternion a -> f (Quaternion a)
_2 a -> f a
f (Quaternion a
w (V3 a
x a
y a
z)) = a -> f a
f a
x f a -> (a -> Quaternion a) -> f (Quaternion a)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \a
x' -> a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
w (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
x' a
y a
z)

instance Field3 (Quaternion a) (Quaternion a) a a where
  _3 :: (a -> f a) -> Quaternion a -> f (Quaternion a)
_3 a -> f a
f (Quaternion a
w (V3 a
x a
y a
z)) = a -> f a
f a
y f a -> (a -> Quaternion a) -> f (Quaternion a)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \a
y' -> a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
w (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
x a
y' a
z)

instance Field4 (Quaternion a) (Quaternion a) a a where
  _4 :: (a -> f a) -> Quaternion a -> f (Quaternion a)
_4 a -> f a
f (Quaternion a
w (V3 a
x a
y a
z)) = a -> f a
f a
z f a -> (a -> Quaternion a) -> f (Quaternion a)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \a
z' -> a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
w (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
x a
y a
z')

instance Semigroup a => Semigroup (Quaternion a) where
 <> :: Quaternion a -> Quaternion a -> Quaternion a
(<>) = (a -> a -> a) -> Quaternion a -> Quaternion a -> Quaternion a
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Semigroup a => a -> a -> a
(<>)

instance Monoid a => Monoid (Quaternion a) where
  mempty :: Quaternion a
mempty = a -> Quaternion a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Monoid a => a
mempty
#if !(MIN_VERSION_base(4,11,0))
  mappend = liftA2 mappend
#endif

instance R1 Quaternion where
  _x :: (a -> f a) -> Quaternion a -> f (Quaternion a)
_x a -> f a
f (Quaternion a
w (V3 a
x a
y a
z)) = a -> f a
f a
x f a -> (a -> Quaternion a) -> f (Quaternion a)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \a
x' -> a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
w (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
x' a
y a
z)

instance R2 Quaternion where
  _y :: (a -> f a) -> Quaternion a -> f (Quaternion a)
_y a -> f a
f (Quaternion a
w (V3 a
x a
y a
z)) = a -> f a
f a
y f a -> (a -> Quaternion a) -> f (Quaternion a)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \a
y' -> a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
w (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
x a
y' a
z)
  _xy :: (V2 a -> f (V2 a)) -> Quaternion a -> f (Quaternion a)
_xy V2 a -> f (V2 a)
f (Quaternion a
w (V3 a
x a
y a
z)) = V2 a -> f (V2 a)
f (a -> a -> V2 a
forall a. a -> a -> V2 a
V2 a
x a
y) f (V2 a) -> (V2 a -> Quaternion a) -> f (Quaternion a)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \(V2 a
x' a
y') -> a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
w (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
x' a
y' a
z)

instance R3 Quaternion where
  _z :: (a -> f a) -> Quaternion a -> f (Quaternion a)
_z a -> f a
f (Quaternion a
w (V3 a
x a
y a
z)) = a -> f a
f a
z f a -> (a -> Quaternion a) -> f (Quaternion a)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \a
z' -> a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
w (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
x a
y a
z')
  _xyz :: (V3 a -> f (V3 a)) -> Quaternion a -> f (Quaternion a)
_xyz V3 a -> f (V3 a)
f (Quaternion a
w V3 a
xyz) = a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
w (V3 a -> Quaternion a) -> f (V3 a) -> f (Quaternion a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> V3 a -> f (V3 a)
f V3 a
xyz

instance R4 Quaternion where
  _w :: (a -> f a) -> Quaternion a -> f (Quaternion a)
_w a -> f a
f (Quaternion a
w V3 a
xyz) = a -> f a
f a
w f a -> (a -> Quaternion a) -> f (Quaternion a)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \a
w' -> a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
w' V3 a
xyz
  _xyzw :: (V4 a -> f (V4 a)) -> Quaternion a -> f (Quaternion a)
_xyzw V4 a -> f (V4 a)
f (Quaternion a
w (V3 a
x a
y a
z)) = V4 a -> f (V4 a)
f (a -> a -> a -> a -> V4 a
forall a. a -> a -> a -> a -> V4 a
V4 a
x a
y a
z a
w) f (V4 a) -> (V4 a -> Quaternion a) -> f (Quaternion a)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \(V4 a
x' a
y' a
z' a
w') -> a -> V3 a -> Quaternion a
forall a. a -> V3 a -> Quaternion a
Quaternion a
w' (a -> a -> a -> V3 a
forall a. a -> a -> a -> V3 a
V3 a
x' a
y' a
z')