[Date Index] [Thread Index] [Author Index]
quaternions, second plea for help
This is a followup to my request for a non-List implementation of quaternions. Someone suggested that I show my feeble attempts and let people see if they can boost me over the hill. First I tried a straight forward entry of the multiplication table: i i ^= -1; j j ^= -1; k k ^= -1; i j ^= k; j k ^= i; k i ^= j; j i ^= -k; k j ^= -i; i k ^= -j; i/: i^2 = -1; j/: j^2 = -1; k/: k^2 = -1; Some rules work OK: i i -1 i j k But the non-commutative ones won't, because Mma follows the earlier rules first and "knows" that Times is commutative: j i k So next I tried using NonCommutativeMultiply, which would seem "made" for the job: i ** i ^= -1; j ** j ^= -1; k ** k ^= -1; i ** j ^= k; j ** k ^= i; k ** i ^= j; j ** i ^= -k; k ** j ^= -i; i ** k ^= -j; Now *all* the rules indeed work: i ** i -1 i ** j k j ** i -k But when I "go for the gold": q = q0 + q1 i + q2 j + q3 k; p = p0 + p1 i + p2 j + p3 k; p ** q (p0 + i p1 + j p2 + k p3) ** (q0 + i q1 + j q2 + k q3) Mma doesn't apply the distributative law by default with the ** operator. In fact, some reflection/trials reveal that at a more basic level, I also have to give simplification rules for things like: i p1 ** j q2 (because how can Mma know that p1, q2, etc. are "intended" to be reals?). I am hoping that someone out there has some experience with using ** or with constructing other abstract data types, because it is clear to me that running around plugging one gap after the other in a non-systematic way is the road to ugliness.