# How to do symbolic computation with quaternions

I need to do symbolic computations with quaternions. Ideally I'd like to enter something like

Q.<i,j,k> = QuaternionAlgebra(QQ,-1,-1)
a=var('a')
b=var('b')
c=var('c')
q = a*i+b*j+c*k


But it doesn't work : TypeError: unsupported operand parent(s) for '*': 'Symbolic Ring' and 'Quaternion Algebra (-1, -1) with base ring Rational Field'

Looks like SAGE doesn't know how to multiply a variable and a quaternion. Or should I specify the types of the variables ? How ?

edit retag close merge delete

Sort by » oldest newest most voted

You should define Q as a QuaternionAlgebra over the symbolic ring SR instead of over the rationals:

sage: Q.<i,j,k> = QuaternionAlgebra(SR, -1, -1)
sage: q = a*i + b*j + c*k
sage: q
a*i + b*j + c*k
sage: q^2
-a^2 - b^2 - c^2

more
N.<c,d,a1,a2,a3,a4,b1,b2,b3,b4,s> = QQ[]
H.<i,j,k> = QuaternionAlgebra(c,d)
a = a1 + a2 * i + a3 * j + a4 * k
b = b1 + b2 * i + b3 * j + b4 * k
a+b
a*b


Seems to give better results

Now I just need to figure out how to define vector and rotate it with quaternion.

more