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.Fri, 09 Jun 2017 22:36:07 +0200generation of certain matriceshttps://ask.sagemath.org/question/37860/generation-of-certain-matrices/I'd like to create a list of roughly 100-1000 $2 \times 2$ matrices $[A_1,A_2,...,A_N]$ that have the following properties:
1. $\det A_j = 1$
2. The entries of each $A_j$ are in an imaginary quadratic integer ring, such as $\mathbb{Z}[i]$, or $\mathbb{Z}[\sqrt{-2}]$
For example, the matrix
$$\begin{bmatrix}
1&2i \\\\
0&1
\end{bmatrix}$$
fits the above specifications when the ring is $\mathbb{Z}[i]$.
I know that I probably want to run some kind of loop over the entries of the matrix, but I'm not sure how to do this. Perhaps I want to initially treat the matrices as lists of length 4, and then run an iterative loop over the lists. Then, when the above specifications are met, that list is stored somewhere else. I think I'd also like to put a bound on the "size" of the matrix entries, but that should be easy to do afterwards.
Thanks! Thu, 08 Jun 2017 19:26:31 +0200https://ask.sagemath.org/question/37860/generation-of-certain-matrices/Comment by kcrisman for <p>I'd like to create a list of roughly 100-1000 $2 \times 2$ matrices $[A_1,A_2,...,A_N]$ that have the following properties:</p>
<ol>
<li><p>$\det A_j = 1$ </p></li>
<li><p>The entries of each $A_j$ are in an imaginary quadratic integer ring, such as $\mathbb{Z}[i]$, or $\mathbb{Z}[\sqrt{-2}]$</p></li>
</ol>
<p>For example, the matrix
$$\begin{bmatrix}
1&2i \\
0&1
\end{bmatrix}$$
fits the above specifications when the ring is $\mathbb{Z}[i]$.</p>
<p>I know that I probably want to run some kind of loop over the entries of the matrix, but I'm not sure how to do this. Perhaps I want to initially treat the matrices as lists of length 4, and then run an iterative loop over the lists. Then, when the above specifications are met, that list is stored somewhere else. I think I'd also like to put a bound on the "size" of the matrix entries, but that should be easy to do afterwards.</p>
<p>Thanks! </p>
https://ask.sagemath.org/question/37860/generation-of-certain-matrices/?comment=37862#post-id-37862Hmm, there is a `random_matrix` command which might be helpful here.Thu, 08 Jun 2017 19:35:09 +0200https://ask.sagemath.org/question/37860/generation-of-certain-matrices/?comment=37862#post-id-37862Answer by kcrisman for <p>I'd like to create a list of roughly 100-1000 $2 \times 2$ matrices $[A_1,A_2,...,A_N]$ that have the following properties:</p>
<ol>
<li><p>$\det A_j = 1$ </p></li>
<li><p>The entries of each $A_j$ are in an imaginary quadratic integer ring, such as $\mathbb{Z}[i]$, or $\mathbb{Z}[\sqrt{-2}]$</p></li>
</ol>
<p>For example, the matrix
$$\begin{bmatrix}
1&2i \\
0&1
\end{bmatrix}$$
fits the above specifications when the ring is $\mathbb{Z}[i]$.</p>
<p>I know that I probably want to run some kind of loop over the entries of the matrix, but I'm not sure how to do this. Perhaps I want to initially treat the matrices as lists of length 4, and then run an iterative loop over the lists. Then, when the above specifications are met, that list is stored somewhere else. I think I'd also like to put a bound on the "size" of the matrix entries, but that should be easy to do afterwards.</p>
<p>Thanks! </p>
https://ask.sagemath.org/question/37860/generation-of-certain-matrices/?answer=37863#post-id-37863Or you could use the `algorithm='unimodular'` option to `random_matrix`.
sage: random_matrix(GaussianIntegers(),2,algorithm='unimodular')
[-429*I + 54 -I - 10]
[ 43*I - 1 1]
sage: random_matrix(GaussianIntegers(),2,algorithm='unimodular')
[ 6*I - 7 -7*I - 12]
[ I - 1 -I - 2]
sage: random_matrix(GaussianIntegers(),2,algorithm='unimodular')
[ -6*I + 29 -221*I - 103]
[ I + 9 -74*I - 9]
You'd need to define your other rings and plop them in the right spot, of course. Edit: [such as follows](http://sagecell.sagemath.org/?z=eJwNybEKwyAQgOE9kHe4TaXSwW6FrF2E5g0qFo050BNOpXn8-o__Z2GD9yjfyC-MOcjrY-AGRoNoQq3LbqfbOyMlVw-H1GOK3OSkozIQIAF7SlE-1HNdYNbO-pPzhVpc8Z3xkrvVRvucKmM_yyYGYalhZM9CqT_t2Sio&lang=sage), though there are probably better ways to get this ring. In particular, be careful since the integral closure of $\mathbb{Z}[\sqrt{n}]$ isn't always equal to it, but depends on the residue class of `n` (mod 4), if I recall correctly.
K = NumberField(x^2 + 2, 's')
OK = K.ring_of_integers()
for n in range(3):
show(random_matrix(OK,2,algorithm='unimodular'))Thu, 08 Jun 2017 19:36:11 +0200https://ask.sagemath.org/question/37860/generation-of-certain-matrices/?answer=37863#post-id-37863Comment by Daniel L for <p>Or you could use the <code>algorithm='unimodular'</code> option to <code>random_matrix</code>.</p>
<pre><code>sage: random_matrix(GaussianIntegers(),2,algorithm='unimodular')
[-429*I + 54 -I - 10]
[ 43*I - 1 1]
sage: random_matrix(GaussianIntegers(),2,algorithm='unimodular')
[ 6*I - 7 -7*I - 12]
[ I - 1 -I - 2]
sage: random_matrix(GaussianIntegers(),2,algorithm='unimodular')
[ -6*I + 29 -221*I - 103]
[ I + 9 -74*I - 9]
</code></pre>
<p>You'd need to define your other rings and plop them in the right spot, of course. Edit: <a href="http://sagecell.sagemath.org/?z=eJwNybEKwyAQgOE9kHe4TaXSwW6FrF2E5g0qFo050BNOpXn8-o__Z2GD9yjfyC-MOcjrY-AGRoNoQq3LbqfbOyMlVw-H1GOK3OSkozIQIAF7SlE-1HNdYNbO-pPzhVpc8Z3xkrvVRvucKmM_yyYGYalhZM9CqT_t2Sio&lang=sage">such as follows</a>, though there are probably better ways to get this ring. In particular, be careful since the integral closure of $\mathbb{Z}[\sqrt{n}]$ isn't always equal to it, but depends on the residue class of <code>n</code> (mod 4), if I recall correctly.</p>
<pre><code>K = NumberField(x^2 + 2, 's')
OK = K.ring_of_integers()
for n in range(3):
show(random_matrix(OK,2,algorithm='unimodular'))
</code></pre>
https://ask.sagemath.org/question/37860/generation-of-certain-matrices/?comment=37875#post-id-37875This works well. It is a little bit annoying using the symbol 's' instead of the complex number sqrt(-2). Sage does seem to be able to work with it a little bit. For instance,
`M; M[1,0]; M[1,0][0]; M[1,0][1];` returns
`[ s - 1 4*s + 19]`
`[ -s - 1 11*s - 12]`
`-s - 1`
`-1`
`-1`Thu, 08 Jun 2017 22:18:03 +0200https://ask.sagemath.org/question/37860/generation-of-certain-matrices/?comment=37875#post-id-37875Comment by dan_fulea for <p>Or you could use the <code>algorithm='unimodular'</code> option to <code>random_matrix</code>.</p>
<pre><code>sage: random_matrix(GaussianIntegers(),2,algorithm='unimodular')
[-429*I + 54 -I - 10]
[ 43*I - 1 1]
sage: random_matrix(GaussianIntegers(),2,algorithm='unimodular')
[ 6*I - 7 -7*I - 12]
[ I - 1 -I - 2]
sage: random_matrix(GaussianIntegers(),2,algorithm='unimodular')
[ -6*I + 29 -221*I - 103]
[ I + 9 -74*I - 9]
</code></pre>
<p>You'd need to define your other rings and plop them in the right spot, of course. Edit: <a href="http://sagecell.sagemath.org/?z=eJwNybEKwyAQgOE9kHe4TaXSwW6FrF2E5g0qFo050BNOpXn8-o__Z2GD9yjfyC-MOcjrY-AGRoNoQq3LbqfbOyMlVw-H1GOK3OSkozIQIAF7SlE-1HNdYNbO-pPzhVpc8Z3xkrvVRvucKmM_yyYGYalhZM9CqT_t2Sio&lang=sage">such as follows</a>, though there are probably better ways to get this ring. In particular, be careful since the integral closure of $\mathbb{Z}[\sqrt{n}]$ isn't always equal to it, but depends on the residue class of <code>n</code> (mod 4), if I recall correctly.</p>
<pre><code>K = NumberField(x^2 + 2, 's')
OK = K.ring_of_integers()
for n in range(3):
show(random_matrix(OK,2,algorithm='unimodular'))
</code></pre>
https://ask.sagemath.org/question/37860/generation-of-certain-matrices/?comment=37896#post-id-37896There are no problems for me... For instance:
sage: K.<s> = QuadraticField( -2 )
sage: OK = K.ring_of_integers()
sage: M = random_matrix( OK, 2, algorithm = 'unimodular' )
sage: M
[ -s + 9 3*s + 10]
[ -s -s + 1]
sage: M[0,0]
-s + 9
sage: M[0,1]
3*s + 10
sage: M[1,0]
-s
sage: M[1,1]
-s + 1
sage: a = M[0,0]
sage: a.list()
[9, -1]
sage: a[0]
9
sage: a[1]
-1
It seems that the entry $M_{00}=9-s = 9s^0+ (-1)s^1$ is stored as a list / as a vector, and asking for its components, `a[0]` and `a[1]`, implicitly forces the conversion to a list.Fri, 09 Jun 2017 21:38:50 +0200https://ask.sagemath.org/question/37860/generation-of-certain-matrices/?comment=37896#post-id-37896Comment by kcrisman for <p>Or you could use the <code>algorithm='unimodular'</code> option to <code>random_matrix</code>.</p>
<pre><code>sage: random_matrix(GaussianIntegers(),2,algorithm='unimodular')
[-429*I + 54 -I - 10]
[ 43*I - 1 1]
sage: random_matrix(GaussianIntegers(),2,algorithm='unimodular')
[ 6*I - 7 -7*I - 12]
[ I - 1 -I - 2]
sage: random_matrix(GaussianIntegers(),2,algorithm='unimodular')
[ -6*I + 29 -221*I - 103]
[ I + 9 -74*I - 9]
</code></pre>
<p>You'd need to define your other rings and plop them in the right spot, of course. Edit: <a href="http://sagecell.sagemath.org/?z=eJwNybEKwyAQgOE9kHe4TaXSwW6FrF2E5g0qFo050BNOpXn8-o__Z2GD9yjfyC-MOcjrY-AGRoNoQq3LbqfbOyMlVw-H1GOK3OSkozIQIAF7SlE-1HNdYNbO-pPzhVpc8Z3xkrvVRvucKmM_yyYGYalhZM9CqT_t2Sio&lang=sage">such as follows</a>, though there are probably better ways to get this ring. In particular, be careful since the integral closure of $\mathbb{Z}[\sqrt{n}]$ isn't always equal to it, but depends on the residue class of <code>n</code> (mod 4), if I recall correctly.</p>
<pre><code>K = NumberField(x^2 + 2, 's')
OK = K.ring_of_integers()
for n in range(3):
show(random_matrix(OK,2,algorithm='unimodular'))
</code></pre>
https://ask.sagemath.org/question/37860/generation-of-certain-matrices/?comment=37898#post-id-37898I'm sure there is a way to get the symbol to "look better" but I don't know how to do this as I typically only use the Gaussian integers, for which there is already the built-in symbol `I`.Fri, 09 Jun 2017 22:36:07 +0200https://ask.sagemath.org/question/37860/generation-of-certain-matrices/?comment=37898#post-id-37898