1 ! Copyright (C) 2005, 2007 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays kernel math math.functions math.vectors sequences ;
6 ! Everybody's favorite non-commutative skew field, the quaternions!
8 ! Quaternions are represented as pairs of complex numbers, using the
9 ! identity: (a+bi)+(c+di)j = a+bi+cj+dk.
13 : ** ( x y -- z ) conjugate * ; inline
15 : 2q ( u v -- u' u'' v' v'' ) [ first2 ] bi@ ; inline
17 : q*a ( u v -- a ) 2q swapd ** [ * ] dip - ; inline
19 : q*b ( u v -- b ) 2q [ ** swap ] dip * + ; inline
24 [ q*a ] [ q*b ] 2bi 2array ;
26 : qconjugate ( u -- u' )
27 first2 [ conjugate ] [ neg ] bi* 2array ;
30 qconjugate dup norm-sq v/n ;
42 first3 rect> [ 0 swap rect> ] dip 2array ;
45 first2 [ imaginary-part ] dip >rect 3array ;
60 : (euler) ( theta unit -- q )
61 [ -0.5 * [ cos c>q ] [ sin ] bi ] dip n*v v- ;
65 : euler ( phi theta psi -- q )
66 [ qi (euler) ] [ qj (euler) ] [ qk (euler) ] tri* q* q* ;