module Numeric.Quaternion.QDouble
( QDouble, Quater (..)
) where
import GHC.Exts
import Data.Coerce (coerce)
import Numeric.Array
import Numeric.DataFrame.Type
import Numeric.Commons
import Numeric.Dimensions
import Numeric.Scalar
import Numeric.Vector
import Numeric.Matrix
import qualified Numeric.DataFrame.ST as ST
import qualified Numeric.Dimensions.Traverse.ST as ST
import qualified Control.Monad.ST as ST
import Numeric.Quaternion.Class
type QDouble = Quater Double
instance Quaternion Double where
newtype Quater Double = QDouble DoubleX4
packQ (D# x) (D# y) (D# z) (D# w) = QDouble (DoubleX4# x y z w)
unpackQ (QDouble (DoubleX4# x y z w)) = (D# x, D# y, D# z, D# w)
fromVecNum (KnownDataFrame (DoubleX3# x y z)) (D# w) = QDouble (DoubleX4# x y z w)
fromVec4 = coerce
toVec4 = coerce
square q = D# (qdot q)
im (QDouble (DoubleX4# x y z _)) = QDouble (DoubleX4# x y z 0.0##)
re (QDouble (DoubleX4# _ _ _ w)) = QDouble (DoubleX4# 0.0## 0.0## 0.0## w)
imVec (QDouble (DoubleX4# x y z _)) = KnownDataFrame (DoubleX3# x y z)
taker (QDouble (DoubleX4# _ _ _ w)) = D# w
takei (QDouble (DoubleX4# x _ _ _)) = D# x
takej (QDouble (DoubleX4# _ y _ _)) = D# y
takek (QDouble (DoubleX4# _ _ z _)) = D# z
conjugate (QDouble (DoubleX4# x y z w)) = QDouble (DoubleX4#
(negateDouble# x)
(negateDouble# y)
(negateDouble# z) w)
rotScale (QDouble (DoubleX4# i j k t))
(KnownDataFrame (DoubleX3# x y z))
= let l = t*##t -## i*##i -## j*##j -## k*##k
d = 2.0## *## ( i*##x +## j*##y +## k*##z)
t2 = t *## 2.0##
in KnownDataFrame
( DoubleX3#
(l*##x +## d*##i +## t2 *## (z*##j -## y*##k))
(l*##y +## d*##j +## t2 *## (x*##k -## z*##i))
(l*##z +## d*##k +## t2 *## (y*##i -## x*##j))
)
getRotScale _ (KnownDataFrame (DoubleX3# 0.0## 0.0## 0.0##))
= QDouble (DoubleX4# 0.0## 0.0## 0.0## 0.0##)
getRotScale (KnownDataFrame (DoubleX3# 0.0## 0.0## 0.0##)) _
= case infty of D# x -> QDouble (DoubleX4# x x x x)
getRotScale a@(KnownDataFrame (DoubleX3# a1 a2 a3))
b@(KnownDataFrame (DoubleX3# b1 b2 b3))
= let ma = sqrtDouble# (a1*##a1 +## a2*##a2 +## a3*##a3)
mb = sqrtDouble# (b1*##b1 +## b2*##b2 +## b3*##b3)
d = a1*##b1 +## a2*##b2 +## a3*##b3
c = sqrtDouble# (ma*##mb +## d)
ma2 = ma *## sqrtDouble# 2.0##
r = 1.0## /## (ma2 *## c)
in case cross a b of
KnownDataFrame (DoubleX3# 0.0## 0.0## 0.0##) ->
if isTrue# (d >## 0.0##)
then QDouble (DoubleX4# 0.0## 0.0## 0.0## (sqrtDouble# (mb /## ma)))
else QDouble (DoubleX4# 0.0## 0.0## (sqrtDouble# (mb /## ma)) 0.0##)
KnownDataFrame (DoubleX3# t1 t2 t3) -> QDouble
( DoubleX4#
(t1 *## r)
(t2 *## r)
(t3 *## r)
(c /## ma2)
)
axisRotation (KnownDataFrame (DoubleX3# 0.0## 0.0## 0.0##)) _
= QDouble (DoubleX4# 0.0## 0.0## 0.0## 1.0##)
axisRotation (KnownDataFrame (DoubleX3# x y z)) (D# a)
= let c = cosDouble# (a *## 0.5##)
s = sinDouble# (a *## 0.5##)
/## sqrtDouble# (x*##x +## y*##y +## z*##z)
in QDouble
( DoubleX4#
(x *## s)
(y *## s)
(z *## s)
c
)
qArg (QDouble (DoubleX4# x y z w))
= case atan2 (D# (sqrtDouble# (x*##x +## y*##y +## z*##z)))
(D# w) of
D# a -> D# (a *## 2.0##)
fromMatrix33 m
= let d =
( ix 0# m *## ( ix 4# m *## ix 8# m -## ix 5# m *## ix 7# m )
-## ix 1# m *## ( ix 3# m *## ix 8# m -## ix 5# m *## ix 6# m )
+## ix 2# m *## ( ix 3# m *## ix 7# m -## ix 4# m *## ix 6# m )
) **## 0.33333333333333333333333333333333##
in QDouble
( DoubleX4#
(sqrtDouble# (max# 0.0## (d +## ix 0# m -## ix 4# m -## ix 8# m )) *## sign# (ix 5# m -## ix 7# m) *## 0.5##)
(sqrtDouble# (max# 0.0## (d -## ix 0# m +## ix 4# m -## ix 8# m )) *## sign# (ix 6# m -## ix 2# m) *## 0.5##)
(sqrtDouble# (max# 0.0## (d -## ix 0# m -## ix 4# m +## ix 8# m )) *## sign# (ix 1# m -## ix 3# m) *## 0.5##)
(sqrtDouble# (max# 0.0## (d +## ix 0# m +## ix 4# m +## ix 8# m )) *## 0.5##)
)
fromMatrix44 m
= let d =
( ix 0# m *## ( ix 5# m *## ix 10# m -## ix 6# m *## ix 9# m )
-## ix 1# m *## ( ix 4# m *## ix 10# m -## ix 6# m *## ix 8# m )
+## ix 2# m *## ( ix 4# m *## ix 9# m -## ix 5# m *## ix 8# m )
) **## 0.33333333333333333333333333333333##
c = 0.5## /## ix 15# m
in QDouble
( DoubleX4#
(sqrtDouble# (max# 0.0## (d +## ix 0# m -## ix 5# m -## ix 10# m )) *## sign# (ix 6# m -## ix 9# m) *## c)
(sqrtDouble# (max# 0.0## (d -## ix 0# m +## ix 5# m -## ix 10# m )) *## sign# (ix 8# m -## ix 2# m) *## c)
(sqrtDouble# (max# 0.0## (d -## ix 0# m -## ix 5# m +## ix 10# m )) *## sign# (ix 1# m -## ix 4# m) *## c)
(sqrtDouble# (max# 0.0## (d +## ix 0# m +## ix 5# m +## ix 10# m )) *## c)
)
toMatrix33 (QDouble (DoubleX4# 0.0## 0.0## 0.0## w)) = diag (scalar (D# (w *## w)))
toMatrix33 (QDouble (DoubleX4# x' y' z' w')) =
let x = scalar (D# x')
y = scalar (D# y')
z = scalar (D# z')
w = scalar (D# w')
x2 = x * x
y2 = y * y
z2 = z * z
w2 = w * w
l2 = x2 + y2 + z2 + w2
in ST.runST $ do
df <- ST.newDataFrame
ST.writeDataFrameOff df 0 $ l2 2*(z2 + y2)
ST.writeDataFrameOff df 1 $ 2*(x*y + z*w)
ST.writeDataFrameOff df 2 $ 2*(x*z y*w)
ST.writeDataFrameOff df 3 $ 2*(x*y z*w)
ST.writeDataFrameOff df 4 $ l2 2*(z2 + x2)
ST.writeDataFrameOff df 5 $ 2*(y*z + x*w)
ST.writeDataFrameOff df 6 $ 2*(x*z + y*w)
ST.writeDataFrameOff df 7 $ 2*(y*z x*w)
ST.writeDataFrameOff df 8 $ l2 2*(y2 + x2)
ST.unsafeFreezeDataFrame df
toMatrix44 (QDouble (DoubleX4# 0.0## 0.0## 0.0## w)) = ST.runST $ do
df <- ST.newDataFrame
ST.overDimOff_ (dim :: Dim '[4,4]) (\i -> ST.writeDataFrameOff df (I# i) 0) 0# 1#
let w2 = scalar (D# (w *## w))
ST.writeDataFrameOff df 0 w2
ST.writeDataFrameOff df 5 w2
ST.writeDataFrameOff df 10 w2
ST.writeDataFrameOff df 15 1
ST.unsafeFreezeDataFrame df
toMatrix44 (QDouble (DoubleX4# x' y' z' w')) =
let x = scalar (D# x')
y = scalar (D# y')
z = scalar (D# z')
w = scalar (D# w')
x2 = x * x
y2 = y * y
z2 = z * z
w2 = w * w
l2 = x2 + y2 + z2 + w2
in ST.runST $ do
df <- ST.newDataFrame
ST.writeDataFrameOff df 0 $ l2 2*(z2 + y2)
ST.writeDataFrameOff df 1 $ 2*(x*y + z*w)
ST.writeDataFrameOff df 2 $ 2*(x*z y*w)
ST.writeDataFrameOff df 3 0
ST.writeDataFrameOff df 4 $ 2*(x*y z*w)
ST.writeDataFrameOff df 5 $ l2 2*(z2 + x2)
ST.writeDataFrameOff df 6 $ 2*(y*z + x*w)
ST.writeDataFrameOff df 7 0
ST.writeDataFrameOff df 8 $ 2*(x*z + y*w)
ST.writeDataFrameOff df 9 $ 2*(y*z x*w)
ST.writeDataFrameOff df 10 $ l2 2*(y2 + x2)
ST.writeDataFrameOff df 11 0
ST.writeDataFrameOff df 12 0
ST.writeDataFrameOff df 13 0
ST.writeDataFrameOff df 14 0
ST.writeDataFrameOff df 15 1
ST.unsafeFreezeDataFrame df
qdot :: QDouble -> Double#
qdot (QDouble (DoubleX4# x y z w)) = (x *## x) +##
(y *## y) +##
(z *## z) +##
(w *## w)
infty :: Double
infty = read "Infinity"
max# :: Double# -> Double# -> Double#
max# a b | isTrue# (a >## b) = a
| otherwise = b
sign# :: Double# -> Double#
sign# a | isTrue# (a >## 0.0##) = 1.0##
| isTrue# (a <## 0.0##) = negateDouble# 1.0##
| otherwise = 0.0##
instance Num QDouble where
QDouble a + QDouble b
= QDouble (a + b)
QDouble a QDouble b
= QDouble (a b)
QDouble (DoubleX4# a1 a2 a3 a4) * QDouble (DoubleX4# b1 b2 b3 b4)
= QDouble
( DoubleX4#
((a4 *## b1) +##
(a1 *## b4) +##
(a2 *## b3) -##
(a3 *## b2)
)
((a4 *## b2) -##
(a1 *## b3) +##
(a2 *## b4) +##
(a3 *## b1)
)
((a4 *## b3) +##
(a1 *## b2) -##
(a2 *## b1) +##
(a3 *## b4)
)
((a4 *## b4) -##
(a1 *## b1) -##
(a2 *## b2) -##
(a3 *## b3)
)
)
negate (QDouble a) = QDouble (negate a)
abs q = QDouble (DoubleX4# 0.0## 0.0## 0.0## (sqrtDouble# (qdot q)))
signum q@(QDouble (DoubleX4# x y z w))
= case qdot q of
0.0## -> QDouble (DoubleX4# 0.0## 0.0## 0.0## 0.0##)
qd -> case 1.0## /## sqrtDouble# qd of
s -> QDouble
( DoubleX4#
(x *## s)
(y *## s)
(z *## s)
(w *## s)
)
fromInteger n = case fromInteger n of
D# x -> QDouble (DoubleX4# 0.0## 0.0## 0.0## x)
instance Fractional QDouble where
recip q@(QDouble (DoubleX4# x y z w)) = case 1.0## /## qdot q of
c -> QDouble
( DoubleX4#
(x *## c)
(y *## c)
(z *## c)
(negateDouble# (w *## c))
)
a / b = a * recip b
fromRational q = case fromRational q of
D# x -> QDouble (DoubleX4# 0.0## 0.0## 0.0## x)
instance Floating QDouble where
pi = QDouble (DoubleX4# 0.0## 0.0## 0.0##
3.141592653589793##)
exp (QDouble (DoubleX4# x y z w))
= case (# (x *## x) +##
(y *## y) +##
(z *## z)
, expDouble# w
#) of
(# 0.0##, et #) -> QDouble (DoubleX4# 0.0## 0.0## 0.0## et)
(# mv2, et #) -> case sqrtDouble# mv2 of
mv -> case et *## sinDouble# mv
/## mv of
l -> QDouble
( DoubleX4#
(x *## l)
(y *## l)
(z *## l)
(et *## cosDouble# mv)
)
log (QDouble (DoubleX4# x y z w))
= case (x *## x) +##
(y *## y) +##
(z *## z) of
0.0## -> if isTrue# (w >=## 0.0##)
then QDouble (DoubleX4# 0.0## 0.0## 0.0## (logDouble# w))
else QDouble (DoubleX4# 3.141592653589793## 0.0## 0.0##
(logDouble# (negateDouble# w)))
mv2 -> case (# sqrtDouble# (mv2 +## (w *## w))
, sqrtDouble# mv2
#) of
(# mq, mv #) -> case atan2 (D# mv) (D# w) / D# mv of
D# l -> QDouble
( DoubleX4#
(x *## l)
(y *## l)
(z *## l)
(logDouble# mq)
)
sqrt (QDouble (DoubleX4# x y z w))
= case (x *## x) +##
(y *## y) +##
(z *## z) of
0.0## -> if isTrue# (w >=## 0.0##)
then QDouble (DoubleX4# 0.0## 0.0## 0.0## (sqrtDouble# w))
else QDouble (DoubleX4# (sqrtDouble# (negateDouble# w)) 0.0## 0.0## 0.0##)
mv2 ->
let mq = sqrtDouble# (mv2 +## w *## w)
l2 = sqrtDouble# mq
tq = w /## (mq *## 2.0##)
sina = sqrtDouble# (0.5## -## tq) *## l2 /## sqrtDouble# mv2
in QDouble
( DoubleX4#
(x *## sina)
(y *## sina)
(z *## sina)
(sqrtDouble# (0.5## +## tq) *## l2)
)
sin (QDouble (DoubleX4# x y z w))
= case (x *## x) +##
(y *## y) +##
(z *## z) of
0.0## -> QDouble (DoubleX4# 0.0## 0.0## 0.0## (sinDouble# w))
mv2 -> case sqrtDouble# mv2 of
mv -> case cosDouble# w *## sinhDouble# mv
/## mv of
l -> QDouble
( DoubleX4#
(x *## l)
(y *## l)
(z *## l)
(sinDouble# w *## coshDouble# mv)
)
cos (QDouble (DoubleX4# x y z w))
= case (x *## x) +##
(y *## y) +##
(z *## z) of
0.0## -> QDouble (DoubleX4# 0.0## 0.0## 0.0## (cosDouble# w))
mv2 -> case sqrtDouble# mv2 of
mv -> case sinDouble# w *## sinhDouble# mv
/## negateDouble# mv of
l -> QDouble
( DoubleX4#
(x *## l)
(y *## l)
(z *## l)
(cosDouble# w *## coshDouble# mv)
)
tan (QDouble (DoubleX4# x y z w))
= case (x *## x) +##
(y *## y) +##
(z *## z) of
0.0## -> QDouble (DoubleX4# 0.0## 0.0## 0.0## (tanDouble# w))
mv2 ->
let mv = sqrtDouble# mv2
chv = coshDouble# mv
shv = sinhDouble# mv
ct = cosDouble# w
st = sinDouble# w
cq = 1.0## /##
( (ct *## ct *## chv *## chv)
+##
(st *## st *## shv *## shv)
)
l = chv *## shv *## cq
/## mv
in QDouble
( DoubleX4#
(x *## l)
(y *## l)
(z *## l)
(ct *## st *## cq)
)
sinh (QDouble (DoubleX4# x y z w))
= case (x *## x) +##
(y *## y) +##
(z *## z) of
0.0## -> QDouble (DoubleX4# 0.0## 0.0## 0.0## (sinhDouble# w))
mv2 -> case sqrtDouble# mv2 of
mv -> case coshDouble# w *## sinDouble# mv
/## mv of
l -> QDouble
( DoubleX4#
(x *## l)
(y *## l)
(z *## l)
(sinhDouble# w *## cosDouble# mv)
)
cosh (QDouble (DoubleX4# x y z w))
= case (x *## x) +##
(y *## y) +##
(z *## z) of
0.0## -> QDouble (DoubleX4# 0.0## 0.0## 0.0## (coshDouble# w))
mv2 -> case sqrtDouble# mv2 of
mv -> case sinhDouble# w *## sinDouble# mv
/## mv of
l -> QDouble
( DoubleX4#
(x *## l)
(y *## l)
(z *## l)
(coshDouble# w *## cosDouble# mv)
)
tanh (QDouble (DoubleX4# x y z w))
= case (x *## x) +##
(y *## y) +##
(z *## z) of
0.0## -> QDouble (DoubleX4# 0.0## 0.0## 0.0## (tanhDouble# w))
mv2 ->
let mv = sqrtDouble# mv2
cv = cosDouble# mv
sv = sinDouble# mv
cht = coshDouble# w
sht = sinhDouble# w
cq = 1.0## /##
( (cht *## cht *## cv *## cv)
+##
(sht *## sht *## sv *## sv)
)
l = cv *## sv *## cq
/## mv
in QDouble
( DoubleX4#
(x *## l)
(y *## l)
(z *## l)
(cht *## sht *## cq)
)
asin q = i * log (i*q + sqrt (1 q*q))
where
i = case signum . im $ q of
0 -> QDouble (DoubleX4# 1.0## 0.0## 0.0## 0.0##)
i' -> i'
acos q = pi/2 asin q
atan q@(QDouble (DoubleX4# _ _ _ w))
= if square imq == 0
then QDouble (DoubleX4# 0.0## 0.0## 0.0## (atanDouble# w))
else i / 2 * log ( (i + q) / (i q) )
where
i = signum imq
imq = im q
asinh q = log (q + sqrt (q*q + 1))
acosh q = log (q + sqrt (q*q 1))
atanh q = 0.5 * log ((1+q)/(1q))
instance Eq QDouble where
QDouble a == QDouble b = a == b
QDouble a /= QDouble b = a /= b
instance Show QDouble where
show (QDouble (DoubleX4# x y z w)) =
show (D# w) ++ ss x ++ "i"
++ ss y ++ "j"
++ ss z ++ "k"
where
ss a# = case D# a# of
a -> if a >= 0 then " + " ++ show a
else " - " ++ show (negate a)