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.Tue, 10 Mar 2020 23:38:33 +0100Unimodular matrices with additional restrictionshttps://ask.sagemath.org/question/50203/unimodular-matrices-with-additional-restrictions/I'd like to generate some unimodular matrices over ZZ with some restrictions:
1. the entries are between -2 and 2
2. the matrix is symmetric: $A = A^t$
Item (1) above can be addressed with the option `upper_bound`. Simply searching for matrices satisfying (1) and (2),
for j in [1..1000]:
A = random_matrix(ZZ,3,3, algorithm = 'unimodular', upper_bound = 3, max_tries = 1000)
if A == A.transpose():
A
becomes extremely inefficient, especially as the dimension grows.
Is there a way to enforce items (1),(2) within the unimodular algorithm? Or another workaround?
Edit: I originally also wanted the diagonal entries fixed at 2, but this is a bit too restrictive.Mon, 09 Mar 2020 18:56:08 +0100https://ask.sagemath.org/question/50203/unimodular-matrices-with-additional-restrictions/Comment by Daniel L for <p>I'd like to generate some unimodular matrices over ZZ with some restrictions:</p>
<ol>
<li>the entries are between -2 and 2</li>
<li>the matrix is symmetric: $A = A^t$</li>
</ol>
<p>Item (1) above can be addressed with the option <code>upper_bound</code>. Simply searching for matrices satisfying (1) and (2),</p>
<pre><code>for j in [1..1000]:
A = random_matrix(ZZ,3,3, algorithm = 'unimodular', upper_bound = 3, max_tries = 1000)
if A == A.transpose():
A
</code></pre>
<p>becomes extremely inefficient, especially as the dimension grows.
Is there a way to enforce items (1),(2) within the unimodular algorithm? Or another workaround?</p>
<p>Edit: I originally also wanted the diagonal entries fixed at 2, but this is a bit too restrictive.</p>
https://ask.sagemath.org/question/50203/unimodular-matrices-with-additional-restrictions/?comment=50207#post-id-50207Good point. This certainly works if the dimension is 8 e.g.
the $E_8$ root lattice gram matrix:
[E8 lattice wiki](https://en.wikipedia.org/wiki/E8_lattice). I edited the post. Thank youTue, 10 Mar 2020 03:34:19 +0100https://ask.sagemath.org/question/50203/unimodular-matrices-with-additional-restrictions/?comment=50207#post-id-50207Comment by Juanjo for <p>I'd like to generate some unimodular matrices over ZZ with some restrictions:</p>
<ol>
<li>the entries are between -2 and 2</li>
<li>the matrix is symmetric: $A = A^t$</li>
</ol>
<p>Item (1) above can be addressed with the option <code>upper_bound</code>. Simply searching for matrices satisfying (1) and (2),</p>
<pre><code>for j in [1..1000]:
A = random_matrix(ZZ,3,3, algorithm = 'unimodular', upper_bound = 3, max_tries = 1000)
if A == A.transpose():
A
</code></pre>
<p>becomes extremely inefficient, especially as the dimension grows.
Is there a way to enforce items (1),(2) within the unimodular algorithm? Or another workaround?</p>
<p>Edit: I originally also wanted the diagonal entries fixed at 2, but this is a bit too restrictive.</p>
https://ask.sagemath.org/question/50203/unimodular-matrices-with-additional-restrictions/?comment=50206#post-id-50206Are you sure that your restrictions are correct? For example, let
$$A=\begin{pmatrix} 2 & a & b \\\\ a & 2 & c \\\\ b & c & 2 \end{pmatrix},$$
with $a,b,c\in\mathbb{Z}$.
Since
$$\det(A)= 8+2(abc-a^2-b^2-c^2),$$
$A$ will be unimodular if and only if
$$a^2+b^2+c^2-abc=\frac{7}{2},$$
which is impossible because $a^2+b^2+c^2-abc\in\mathbb{Z}$.Tue, 10 Mar 2020 03:00:33 +0100https://ask.sagemath.org/question/50203/unimodular-matrices-with-additional-restrictions/?comment=50206#post-id-50206Answer by Juanjo for <p>I'd like to generate some unimodular matrices over ZZ with some restrictions:</p>
<ol>
<li>the entries are between -2 and 2</li>
<li>the matrix is symmetric: $A = A^t$</li>
</ol>
<p>Item (1) above can be addressed with the option <code>upper_bound</code>. Simply searching for matrices satisfying (1) and (2),</p>
<pre><code>for j in [1..1000]:
A = random_matrix(ZZ,3,3, algorithm = 'unimodular', upper_bound = 3, max_tries = 1000)
if A == A.transpose():
A
</code></pre>
<p>becomes extremely inefficient, especially as the dimension grows.
Is there a way to enforce items (1),(2) within the unimodular algorithm? Or another workaround?</p>
<p>Edit: I originally also wanted the diagonal entries fixed at 2, but this is a bit too restrictive.</p>
https://ask.sagemath.org/question/50203/unimodular-matrices-with-additional-restrictions/?answer=50217#post-id-50217I have adapted an idea I have found in [Stackexchange](https://math.stackexchange.com/questions/336829/generation-of-unimodular-matrices-with-bounded-elements). It is based on the following facts:
* Let $J_k$ be the identity matrix of order $n$ with the null elements in the $k$-th row replaced by random integers. Then $J_k$ is unimodular, i.e. $\lvert J_k\rvert=1$.
* If $A$ is an unimodular symmetric matrix, so it is $J_k^T A J_k$.
Thus, one can start with $A=I_n$, choose a row $k$ at random, construct $J_k$, compute $J_k^T A J_k$ and replace $A$ by that matrix. This process can be repeated as many times as wanted. The result is an unimodular symmetric matrix. The following code implements this idea:
def unimod_sym_matrix(n, niter=1, min_int=-1, max_int=1):
A = identity_matrix(n)
rows = random_vector(niter, x=0, y=n)
for k in rows:
J = identity_matrix(n)
J[k,:] = random_vector(n, x=min_int, y=max_int+1)
J[k,k] = 1
A = J.transpose()*A*J
return A
Observe that, to get $J_k$ , random integers are taken from `min_int` to `max_int`, both included. Likewise, `niter` is the number of matrices $J_k$ that are constructed.
For example, the following code
set_random_seed(1000)
A = unimod_sym_matrix(6,3); show(A)
A = unimod_sym_matrix(6); show(A)
yields
$\\begin{pmatrix}
2 & -2 & -1 & 0 & 1 & 0 \\\\
-2 & 9 & 4 & 1 & -3 & 3 \\\\
-1 & 4 & 2 & 0 & -1 & 2 \\\\
0 & 1 & 0 & 2 & -1 & -1 \\\\
1 & -3 & -1 & -1 & 2 & 1 \\\\
0 & 3 & 2 & -1 & 1 & 6
\\end{pmatrix}$
$\\begin{pmatrix}
2 & 0 & 1 & -1 & -1 & -1 \\\\
0 & 1 & 0 & 0 & 0 & 0 \\\\
1 & 0 & 2 & -1 & -1 & -1 \\\\
-1 & 0 & -1 & 1 & 1 & 1 \\\\
-1 & 0 & -1 & 1 & 2 & 1 \\\\
-1 & 0 & -1 & 1 & 1 & 2
\\end{pmatrix}$
I have included `set_random_seed` just for the sake of reproducibility of the above output.
Since you want matrix entries between $-2$ and $2$, you had better use the default values for `niter`, `min_int` and `max_int`.Tue, 10 Mar 2020 23:38:33 +0100https://ask.sagemath.org/question/50203/unimodular-matrices-with-additional-restrictions/?answer=50217#post-id-50217