functions for quaternion calculation,version 1.0
peter.vlasschaert@gmail.com,24/10/2018

load ' packages '
==============

kill(all)$
load(vect)$
load(lrats)$

p:declare([a1,a2,a3,a4,b1,b2,b3,b4],scalar);

definition :multiplication of two quaternions  'qmult2'
============================================
qa = a1+a2*i+a3*j+a4*k
qb =b1+b1*i+b2*j+b3*k
============================================
=>  qa.qb ,         use qmult2 you need 'p' : see above.

qmult2(a1,a2,a3,a4,b1,b2,b3,b4,h):=(qa:a1+a2*i+a3*j+a4*k,qb:b1+b1*i+b2*j+b3*k,h1:expand(qa.qb),
                                                                h2:[i.j=k,j.i=-k,j.k=i,k.j=-i,k.i=j,i.k=-j,i.i=-1,j.j=-1,k.k=-1],h:subst(h2,h1))$

example :
---------------

qc:qmult2(1,1,1,1,1,1,1,1,h);

definition : coef. from quaternion 'coefq'
=================================

coefq(qq,ss):=(s1:subst([i=0,j=0,k=0],qq),s2:coeff(qq,i),s3:coeff(qq,j),s4:coeff(qq,k),ss:[s1,s2,s3,s4])$

example : 'use qc'
--------------

m1:coefq(qc,ss);

m11:m1[1];

definition : norm of quaternion  'normq'
=================================

normq(a1,a2,a3,a4,tt):=( tt:sqrt((a1)^2+(a2)^2+(a3)^2+(a4)^2))$

example : ' use qc '
---------------

normq(m1[1],m1[2],m1[3],m1[4],tt);

rem : norm : two-norm 'distance norm'

definition : conjugate of quaternion  'conjugateq'
=========================================

conjugateq(a1,a2,a3,a4,uu):=(uu:[a1,-a2,-a3,-a4])$

example : ' use qc '
---------------

conjugateq(m1[1],m1[2],m1[3],m1[4],uu);

rem : complex conjugate :  z = a+b*i => conjugate (z)  = a-b*i
rem : quaternion with j=k=0  
result : [a,b,0,0]  => [a,-b,0,0]

definition : inverse of quaternion  'inverseq'
=========================================

inverseq(a1,a2,a3,a4,vv):=( d:sqrt((a1)^2+(a2)^2+(a3)^2+(a4)^2),vv:1/d^2* [a1,-a2,-a3,-a4] )$

rem : every nonzero quaternion has a inverse.
rem : inverse of quaternion = congjugate(qc)/(normq(qc))^2

example : ' use qc '
---------------

m2:inverseq(m1[1],m1[2],m1[3],m1[4],vv);

check this : with use 'qmult2'  ( example )
=========

qmult2(m1[1],m1[2],m1[3],m1[4],m2[1],m2[2],m2[3],m2[4],hh);

rem : Quaternions do not form field because multiplication is not commutative

----------------------------------------------------------------------------------------------------------------------