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.Mon, 19 Oct 2015 03:15:41 +0200How can I define a function with quaternion argument, and other non-vector inputhttps://ask.sagemath.org/question/30025/how-can-i-define-a-function-with-quaternion-argument-and-other-non-vector-input/I've been using Sage for 3 weeks so please bear with me if this is an ignorant question.
There are lots of great tools for studying properties of quaternion algebras
http://doc.sagemath.org/html/en/reference/quat_algebras/sage/algebras/quatalg/quaternion_algebra.html
and also for doing computations in these algebras
http://doc.sagemath.org/html/en/reference/quat_algebras/sage/algebras/quatalg/quaternion_algebra_element.html
But I am interested in working with functions that move between quaternion algebras and other spaces.
For instance let's say I write
K.<d> = NumberField(x^2 + 1)
A.<i,j,k> = QuaternionAlgebra(K,-1,-1)
And let's say I want a function
$$f:A\rightarrow M_2(K), w+xi+yj+zij\mapsto (w,x,y,z).$$
How can define this function in such a way that I can enter something like
f(2*i+i*j)
and get as output
(0,2,0,1)
?
**This particular function is not very useful or interesting, I'm just using this to keep it simple. I'd also be interested in defining functions that go from one quaternion algebra to another, or from a quaternion algebra to a matrix ring, in such a way that I can compose these things.** It seems functions are generally expected to have vector input, but there is not always a straightforward way of turning elements of a domain into vectors (there is for matrices, but even this is tedious).
I feel there is probably a general principle addressing things like this but I'm not finding it.
Thu, 15 Oct 2015 04:44:52 +0200https://ask.sagemath.org/question/30025/how-can-i-define-a-function-with-quaternion-argument-and-other-non-vector-input/Answer by fidbc for <p>I've been using Sage for 3 weeks so please bear with me if this is an ignorant question.</p>
<p>There are lots of great tools for studying properties of quaternion algebras
<a href="http://doc.sagemath.org/html/en/reference/quat_algebras/sage/algebras/quatalg/quaternion_algebra.html">http://doc.sagemath.org/html/en/refer...</a>
and also for doing computations in these algebras
<a href="http://doc.sagemath.org/html/en/reference/quat_algebras/sage/algebras/quatalg/quaternion_algebra_element.html">http://doc.sagemath.org/html/en/refer...</a></p>
<p>But I am interested in working with functions that move between quaternion algebras and other spaces.
For instance let's say I write</p>
<pre><code>K.<d> = NumberField(x^2 + 1)
A.<i,j,k> = QuaternionAlgebra(K,-1,-1)
</code></pre>
<p>And let's say I want a function
$$f:A\rightarrow M_2(K), w+xi+yj+zij\mapsto (w,x,y,z).$$
How can define this function in such a way that I can enter something like</p>
<pre><code>f(2*i+i*j)
</code></pre>
<p>and get as output</p>
<pre><code>(0,2,0,1)
</code></pre>
<p>?</p>
<p><strong>This particular function is not very useful or interesting, I'm just using this to keep it simple. I'd also be interested in defining functions that go from one quaternion algebra to another, or from a quaternion algebra to a matrix ring, in such a way that I can compose these things.</strong> It seems functions are generally expected to have vector input, but there is not always a straightforward way of turning elements of a domain into vectors (there is for matrices, but even this is tedious).</p>
<p>I feel there is probably a general principle addressing things like this but I'm not finding it.</p>
https://ask.sagemath.org/question/30025/how-can-i-define-a-function-with-quaternion-argument-and-other-non-vector-input/?answer=30026#post-id-30026 You can just use the `list` or `vector` functions
x=2*i+i*j
list(x)
# produces [0,2,0,1]
vector(x)
# produces (0,2,0,1)Thu, 15 Oct 2015 05:35:51 +0200https://ask.sagemath.org/question/30025/how-can-i-define-a-function-with-quaternion-argument-and-other-non-vector-input/?answer=30026#post-id-30026Comment by j0equ1nn for <p>You can just use the <code>list</code> or <code>vector</code> functions</p>
<pre><code>x=2*i+i*j
list(x)
# produces [0,2,0,1]
vector(x)
# produces (0,2,0,1)
</code></pre>
https://ask.sagemath.org/question/30025/how-can-i-define-a-function-with-quaternion-argument-and-other-non-vector-input/?comment=30028#post-id-30028Thanks but I think you're missing the fact that I'm interested in arbitrary functions that can take quaternion input, not just that example. What if I wanted for instance the (actually useful) function $$f(w+xi+yj+zij)=\begin{matrix}w+x & y+z \\ y-z & w-x\end{matrix}$&? I guess I could define it like $f(w,x,y,z)$, then for quaternion $q$ I could evaluate it like
f(list(q)[0],list(q)[1],list(q)[2],list(q)[3])
but I quickly run into trouble when doing composition or evaluating algebraic expressions.Thu, 15 Oct 2015 05:58:38 +0200https://ask.sagemath.org/question/30025/how-can-i-define-a-function-with-quaternion-argument-and-other-non-vector-input/?comment=30028#post-id-30028Comment by nbruin for <p>You can just use the <code>list</code> or <code>vector</code> functions</p>
<pre><code>x=2*i+i*j
list(x)
# produces [0,2,0,1]
vector(x)
# produces (0,2,0,1)
</code></pre>
https://ask.sagemath.org/question/30025/how-can-i-define-a-function-with-quaternion-argument-and-other-non-vector-input/?comment=30029#post-id-30029So is the problem convenience for writing the formula? The following may not be very readable, but it uses the parameter unpacking capabilities of python to give a concise way of writing the answer:
sage: C=lambda u: (lambda w,x,y,z:(w+x,y+z,y-z,w-x))(*list(u))
sage: C(2*i+i*j)
(2, 1, -1, -2)
perhaps a little more readable:
def C(u):
w,x,y,z=list(u)
return (w+x,y+z,y-z,w-x)Thu, 15 Oct 2015 06:13:39 +0200https://ask.sagemath.org/question/30025/how-can-i-define-a-function-with-quaternion-argument-and-other-non-vector-input/?comment=30029#post-id-30029Comment by j0equ1nn for <p>You can just use the <code>list</code> or <code>vector</code> functions</p>
<pre><code>x=2*i+i*j
list(x)
# produces [0,2,0,1]
vector(x)
# produces (0,2,0,1)
</code></pre>
https://ask.sagemath.org/question/30025/how-can-i-define-a-function-with-quaternion-argument-and-other-non-vector-input/?comment=30100#post-id-30100@nbuin: I'm not seeing how this would allow input/output of non-vectors. For instance, to get an output from this $C$ function, one needs to put it in the form
C((w,x,y,z))
I thought perhaps doing this would make
C(w+x*i+y*j+z*i*j)
work, or at least
C(list(w+x*i+y*j+z*i*j))
but no it seems I still cannot use quaternion input. Also, I really wanted that output to be a $2\times2$ matrix, not another vector. (My LaTeX code did not translate in the comment.)Mon, 19 Oct 2015 02:10:41 +0200https://ask.sagemath.org/question/30025/how-can-i-define-a-function-with-quaternion-argument-and-other-non-vector-input/?comment=30100#post-id-30100Answer by nbruin for <p>I've been using Sage for 3 weeks so please bear with me if this is an ignorant question.</p>
<p>There are lots of great tools for studying properties of quaternion algebras
<a href="http://doc.sagemath.org/html/en/reference/quat_algebras/sage/algebras/quatalg/quaternion_algebra.html">http://doc.sagemath.org/html/en/refer...</a>
and also for doing computations in these algebras
<a href="http://doc.sagemath.org/html/en/reference/quat_algebras/sage/algebras/quatalg/quaternion_algebra_element.html">http://doc.sagemath.org/html/en/refer...</a></p>
<p>But I am interested in working with functions that move between quaternion algebras and other spaces.
For instance let's say I write</p>
<pre><code>K.<d> = NumberField(x^2 + 1)
A.<i,j,k> = QuaternionAlgebra(K,-1,-1)
</code></pre>
<p>And let's say I want a function
$$f:A\rightarrow M_2(K), w+xi+yj+zij\mapsto (w,x,y,z).$$
How can define this function in such a way that I can enter something like</p>
<pre><code>f(2*i+i*j)
</code></pre>
<p>and get as output</p>
<pre><code>(0,2,0,1)
</code></pre>
<p>?</p>
<p><strong>This particular function is not very useful or interesting, I'm just using this to keep it simple. I'd also be interested in defining functions that go from one quaternion algebra to another, or from a quaternion algebra to a matrix ring, in such a way that I can compose these things.</strong> It seems functions are generally expected to have vector input, but there is not always a straightforward way of turning elements of a domain into vectors (there is for matrices, but even this is tedious).</p>
<p>I feel there is probably a general principle addressing things like this but I'm not finding it.</p>
https://ask.sagemath.org/question/30025/how-can-i-define-a-function-with-quaternion-argument-and-other-non-vector-input/?answer=30104#post-id-30104From your question and comments I understand you are interested in defining functions from the "quaternion algebra"
sage: K.<d> = NumberField(x^2 + 1)
sage: A.<i,j,k> = QuaternionAlgebra(K,-1,-1)
(note that -1 is a square in K, so this algebra is split: it's isomorphic to GL(2,K))
For instance, you might be interested in the function that sends the quaternion algebra element w+x*i+y*j+z*k to the 2x2 matrix with rows [w+x,y+z] and [y-z,w-x]. The following is a way of defining such a function.
sage: def C(u):
....: w,x,y,z=list(u)
....: return matrix(2,2,[w+x,y+z,y-z,w-x])
with the above commands you can do things like:
sage: C(2*i+i*j)
[ 2 1]
[-1 -2]
sage: C(3+5*i+d*j+11*k)
[ 8 d + 11]
[d - 11 -2]
Note that `w+x*i+y*j+z*k` is not an element of A:
sage: w+x*i+y*j+z*k
NameError: name 'w' is not defined
You can't even construct the element, let alone apply C to it. If you want quaternions with `w,x,y,z` in the coefficients, you'll have to declare a quaternion algebra over a ring that contains these elements, e.g., a multivariate function field. The "function" C is really only a "function" in the python/programming language sense, not a mathematical construct. You may notice that the definition of C never referenced the fact that our original A is defined over K, or in fact A at all. It only depends on the fact that its parameter u can be taken apart by `list` into 4 coefficients that can be stuffed into a matrix. So we don't even have to change the definition of C.
sage: P.<w,x,y,z>=QQ[]
sage: L=P.fraction_field()
sage: A.<i,j,k> = QuaternionAlgebra(L,-1,-1)
sage: C(w+x*i+y*j+z*k)
[w + x y + z]
[y - z w - x]
Mon, 19 Oct 2015 02:48:39 +0200https://ask.sagemath.org/question/30025/how-can-i-define-a-function-with-quaternion-argument-and-other-non-vector-input/?answer=30104#post-id-30104Comment by j0equ1nn for <p>From your question and comments I understand you are interested in defining functions from the "quaternion algebra"</p>
<pre><code>sage: K.<d> = NumberField(x^2 + 1)
sage: A.<i,j,k> = QuaternionAlgebra(K,-1,-1)
</code></pre>
<p>(note that -1 is a square in K, so this algebra is split: it's isomorphic to GL(2,K))
For instance, you might be interested in the function that sends the quaternion algebra element w+x<em>i+y</em>j+z*k to the 2x2 matrix with rows [w+x,y+z] and [y-z,w-x]. The following is a way of defining such a function.</p>
<pre><code>sage: def C(u):
....: w,x,y,z=list(u)
....: return matrix(2,2,[w+x,y+z,y-z,w-x])
</code></pre>
<p>with the above commands you can do things like:</p>
<pre><code>sage: C(2*i+i*j)
[ 2 1]
[-1 -2]
sage: C(3+5*i+d*j+11*k)
[ 8 d + 11]
[d - 11 -2]
</code></pre>
<p>Note that <code>w+x*i+y*j+z*k</code> is not an element of A:</p>
<pre><code>sage: w+x*i+y*j+z*k
NameError: name 'w' is not defined
</code></pre>
<p>You can't even construct the element, let alone apply C to it. If you want quaternions with <code>w,x,y,z</code> in the coefficients, you'll have to declare a quaternion algebra over a ring that contains these elements, e.g., a multivariate function field. The "function" C is really only a "function" in the python/programming language sense, not a mathematical construct. You may notice that the definition of C never referenced the fact that our original A is defined over K, or in fact A at all. It only depends on the fact that its parameter u can be taken apart by <code>list</code> into 4 coefficients that can be stuffed into a matrix. So we don't even have to change the definition of C.</p>
<pre><code>sage: P.<w,x,y,z>=QQ[]
sage: L=P.fraction_field()
sage: A.<i,j,k> = QuaternionAlgebra(L,-1,-1)
sage: C(w+x*i+y*j+z*k)
[w + x y + z]
[y - z w - x]
</code></pre>
https://ask.sagemath.org/question/30025/how-can-i-define-a-function-with-quaternion-argument-and-other-non-vector-input/?comment=30105#post-id-30105Great, this is what I was looking for. I think I can get the various functions I want by fiddling with what you showed me. (I would upvote your answer but I am new here and need to have points before I can upvote.)Mon, 19 Oct 2015 03:15:41 +0200https://ask.sagemath.org/question/30025/how-can-i-define-a-function-with-quaternion-argument-and-other-non-vector-input/?comment=30105#post-id-30105