ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 24 Dec 2015 18:47:53 +0100Computing in a quaternion algebra over a complex field?https://ask.sagemath.org/question/31808/computing-in-a-quaternion-algebra-over-a-complex-field/ If I enter
Q.<i,j,k> = QuaternionAlgebra(CC,1,1)
there is no problem. I can then type something like
(2+i+j)*(3*i-j)
and get the appropriate answer
2.00000000000000 + 6.00000000000000*i + (-2.00000000000000)*j + (-4.00000000000000)*k
The problem comes when I want to use non-real numbers. If I type
i*sqrt(-1)
then I get
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-0b500ba4930f> in <module>()
1 Q = QuaternionAlgebra(CC,Integer(1),Integer(1), names=('i', 'j', 'k',)); (i, j, k,) = Q._first_ngens(3)
----> 2 i*sqrt(-Integer(1))
/home/sc_serv/sage/local/lib/python2.7/site-packages/sage/structure/element.so in sage.structure.element.RingElement.__mul__ (/home/sc_serv/sage/src/build/cythonized/sage/structure/element.c:17265)()
/home/sc_serv/sage/local/lib/python2.7/site-packages/sage/structure/coerce.so in sage.structure.coerce.CoercionModel_cache_maps.bin_op (/home/sc_serv/sage/src/build/cythonized/sage/structure/coerce.c:9721)()
TypeError: unsupported operand parent(s) for '*': 'Quaternion Algebra (1.00000000000000, 1.00000000000000) with base ring Complex Field with 53 bits of precision' and 'Symbolic Ring'
As a matter of fact, something similar happens if I type
i*sqrt(2)
which is not imaginary... I also tried typing
I*i
and got a similar error message. Since I am supposedly working in the quaternion algebra over the complex numbers, how do I specify arbitrary complex coefficients?Wed, 23 Dec 2015 18:15:45 +0100https://ask.sagemath.org/question/31808/computing-in-a-quaternion-algebra-over-a-complex-field/Answer by vdelecroix for <p>If I enter</p>
<pre><code>Q.<i,j,k> = QuaternionAlgebra(CC,1,1)
</code></pre>
<p>there is no problem. I can then type something like </p>
<pre><code>(2+i+j)*(3*i-j)
</code></pre>
<p>and get the appropriate answer</p>
<pre><code>2.00000000000000 + 6.00000000000000*i + (-2.00000000000000)*j + (-4.00000000000000)*k
</code></pre>
<p>The problem comes when I want to use non-real numbers. If I type</p>
<pre><code>i*sqrt(-1)
</code></pre>
<p>then I get</p>
<pre><code>---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-0b500ba4930f> in <module>()
1 Q = QuaternionAlgebra(CC,Integer(1),Integer(1), names=('i', 'j', 'k',)); (i, j, k,) = Q._first_ngens(3)
----> 2 i*sqrt(-Integer(1))
/home/sc_serv/sage/local/lib/python2.7/site-packages/sage/structure/element.so in sage.structure.element.RingElement.__mul__ (/home/sc_serv/sage/src/build/cythonized/sage/structure/element.c:17265)()
/home/sc_serv/sage/local/lib/python2.7/site-packages/sage/structure/coerce.so in sage.structure.coerce.CoercionModel_cache_maps.bin_op (/home/sc_serv/sage/src/build/cythonized/sage/structure/coerce.c:9721)()
TypeError: unsupported operand parent(s) for '*': 'Quaternion Algebra (1.00000000000000, 1.00000000000000) with base ring Complex Field with 53 bits of precision' and 'Symbolic Ring'
</code></pre>
<p>As a matter of fact, something similar happens if I type</p>
<pre><code>i*sqrt(2)
</code></pre>
<p>which is not imaginary... I also tried typing</p>
<pre><code>I*i
</code></pre>
<p>and got a similar error message. Since I am supposedly working in the quaternion algebra over the complex numbers, how do I specify arbitrary complex coefficients?</p>
https://ask.sagemath.org/question/31808/computing-in-a-quaternion-algebra-over-a-complex-field/?answer=31811#post-id-31811You should use
sage: sage: Q.<i,j,k> = QuaternionAlgebra(CC,1,1)
sage: i * CC(-1).sqrt()
1.00000000000000*I*i
sage: j * CC(2).sqrt()
1.41421356237310*j
The reason it fails with your code is that the function sqrt returns symbolic objects when called with non square integers
sage: s = sqrt(2)
sage: print s ,type(s)
sqrt(2) <type 'sage.symbolic.expression.Expression'>
But the symbolic world has nothing such as generalized quaternions and hence it fails. As you wanted a result in your algebra just avoid symbolic objects.
Note that the following works fine
sage: sqrt(CC(-1))
1.00000000000000*I
sage: parent(_)
Complex Field with 53 bits of precision
To be compared with
sage: sqrt(-1)
I
sage: parent(_)
Symbolic RingWed, 23 Dec 2015 19:58:33 +0100https://ask.sagemath.org/question/31808/computing-in-a-quaternion-algebra-over-a-complex-field/?answer=31811#post-id-31811Comment by j0equ1nn for <p>You should use</p>
<pre><code>sage: sage: Q.<i,j,k> = QuaternionAlgebra(CC,1,1)
sage: i * CC(-1).sqrt()
1.00000000000000*I*i
sage: j * CC(2).sqrt()
1.41421356237310*j
</code></pre>
<p>The reason it fails with your code is that the function sqrt returns symbolic objects when called with non square integers</p>
<pre><code>sage: s = sqrt(2)
sage: print s ,type(s)
sqrt(2) <type 'sage.symbolic.expression.Expression'>
</code></pre>
<p>But the symbolic world has nothing such as generalized quaternions and hence it fails. As you wanted a result in your algebra just avoid symbolic objects.</p>
<p>Note that the following works fine</p>
<pre><code>sage: sqrt(CC(-1))
1.00000000000000*I
sage: parent(_)
Complex Field with 53 bits of precision
</code></pre>
<p>To be compared with</p>
<pre><code>sage: sqrt(-1)
I
sage: parent(_)
Symbolic Ring
</code></pre>
https://ask.sagemath.org/question/31808/computing-in-a-quaternion-algebra-over-a-complex-field/?comment=31830#post-id-31830Cool. In light of this I prefer to include
Im = CC(-1).sqrt()
rather than typing that every time, though I'd still need to worry about irrational square roots and whatnot. I guess the quaternion algebra stuff is designed more for computing properties of the algebras than for computations within them?Thu, 24 Dec 2015 14:53:47 +0100https://ask.sagemath.org/question/31808/computing-in-a-quaternion-algebra-over-a-complex-field/?comment=31830#post-id-31830Comment by vdelecroix for <p>You should use</p>
<pre><code>sage: sage: Q.<i,j,k> = QuaternionAlgebra(CC,1,1)
sage: i * CC(-1).sqrt()
1.00000000000000*I*i
sage: j * CC(2).sqrt()
1.41421356237310*j
</code></pre>
<p>The reason it fails with your code is that the function sqrt returns symbolic objects when called with non square integers</p>
<pre><code>sage: s = sqrt(2)
sage: print s ,type(s)
sqrt(2) <type 'sage.symbolic.expression.Expression'>
</code></pre>
<p>But the symbolic world has nothing such as generalized quaternions and hence it fails. As you wanted a result in your algebra just avoid symbolic objects.</p>
<p>Note that the following works fine</p>
<pre><code>sage: sqrt(CC(-1))
1.00000000000000*I
sage: parent(_)
Complex Field with 53 bits of precision
</code></pre>
<p>To be compared with</p>
<pre><code>sage: sqrt(-1)
I
sage: parent(_)
Symbolic Ring
</code></pre>
https://ask.sagemath.org/question/31808/computing-in-a-quaternion-algebra-over-a-complex-field/?comment=31832#post-id-31832@j0equ1nn people who wrote the quaternion code were careful about speed. So I wouldn't say so. Though, I think that it was more designed to be used with coefficients in number fields rather than with floating points.Thu, 24 Dec 2015 18:47:53 +0100https://ask.sagemath.org/question/31808/computing-in-a-quaternion-algebra-over-a-complex-field/?comment=31832#post-id-31832