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, 19 Dec 2017 13:51:53 +0100Matrix Permutationshttps://ask.sagemath.org/question/40180/matrix-permutations/ Hello,
how do I get all permutations of a Matrix?
For the following exercise I would like to get all permutations of a Matrix with integer values in the range $0$ to $9$. There should be $10^4$ permutations. And then generate the permutations of tuples with three matrices. $10^{12}$ permutations and then I would filter the list somehow for the invertible matrices.
Is there a better way?
Exercise:
Let $S, T, U \in \mathbb{R}^{2x2}$ such that, $S_{i, j}, T_{i, j}, U_{i, j} \in \{0,1, \cdots, 9\}$
How many of the products $S \cdot T \cdot U$ are invertible?
In the Sage reference for permutation I couldn't find any thing.
Thanks
PS: The curly braces couldn't be escaped properly in the LaTex part ... \\{0,1, \cdots, 9\\}Fri, 15 Dec 2017 18:34:07 +0100https://ask.sagemath.org/question/40180/matrix-permutations/Answer by dan_fulea for <p>Hello,</p>
<p>how do I get all permutations of a Matrix?</p>
<p>For the following exercise I would like to get all permutations of a Matrix with integer values in the range $0$ to $9$. There should be $10^4$ permutations. And then generate the permutations of tuples with three matrices. $10^{12}$ permutations and then I would filter the list somehow for the invertible matrices.</p>
<p>Is there a better way?</p>
<p>Exercise:
Let $S, T, U \in \mathbb{R}^{2x2}$ such that, $S_{i, j}, T_{i, j}, U_{i, j} \in {0,1, \cdots, 9}$
How many of the products $S \cdot T \cdot U$ are invertible?</p>
<p>In the Sage reference for permutation I couldn't find any thing.</p>
<p>Thanks</p>
<p>PS: The curly braces couldn't be escaped properly in the LaTex part ... \{0,1, \cdots, 9\}</p>
https://ask.sagemath.org/question/40180/matrix-permutations/?answer=40183#post-id-40183For the exercise, to loop among the list of all 2x2 matrices with entries in [0..9] the suitable mathematical construction is the **cartesian product** of four copies of [0..9]. This may be achieved like this:
sage: R = [0..9]
sage: C = cartesian_product( (R,R,R,R) )
sage: len(C)
10000
sage: C.random_element()
(6, 5, 4, 7)
One can build the matrix with the las entries (last value is `_` in the sage interpreter) like this:
sage: _
(6, 5, 4, 7)
sage: matrix( ZZ, 2, 2, list(_) )
[6 5]
[4 7]
So the number of invertible matrices $S$ of the given shape is:
sage: R = [0..9]
sage: C = cartesian_product( (R,R,R,R) )
sage: len( [ c for c in C if matrix( ZZ, 2, 2, list(c) ).det() ] )
9430
The product $STU$ is invertible iff each matrix in it is invertible. So the exercise is solved in one more line. It is a good idea to use structure in mathematics, when it is possible. Brute force can then confirm the result.
Note: Permutations are not involved in the exercise. Inn order to get info on this or an other mathematical object, just *try* the first letters of it in the sage interpreter, which has autocompletion. We can try here `perm[TAB]` and/or `Perm[TAB]`. In the second case we get a list of classes / constructors, among them we can put a question mark before or after...
sage: Permutation?
to get more information on this. The sage interpreter is a good tool to learn `sage`.
Sat, 16 Dec 2017 15:22:24 +0100https://ask.sagemath.org/question/40180/matrix-permutations/?answer=40183#post-id-40183Comment by Asg for <p>For the exercise, to loop among the list of all 2x2 matrices with entries in [0..9] the suitable mathematical construction is the <strong>cartesian product</strong> of four copies of [0..9]. This may be achieved like this:</p>
<pre><code>sage: R = [0..9]
sage: C = cartesian_product( (R,R,R,R) )
sage: len(C)
10000
sage: C.random_element()
(6, 5, 4, 7)
</code></pre>
<p>One can build the matrix with the las entries (last value is <code>_</code> in the sage interpreter) like this:</p>
<pre><code>sage: _
(6, 5, 4, 7)
sage: matrix( ZZ, 2, 2, list(_) )
[6 5]
[4 7]
</code></pre>
<p>So the number of invertible matrices $S$ of the given shape is:</p>
<pre><code>sage: R = [0..9]
sage: C = cartesian_product( (R,R,R,R) )
sage: len( [ c for c in C if matrix( ZZ, 2, 2, list(c) ).det() ] )
9430
</code></pre>
<p>The product $STU$ is invertible iff each matrix in it is invertible. So the exercise is solved in one more line. It is a good idea to use structure in mathematics, when it is possible. Brute force can then confirm the result.</p>
<p>Note: Permutations are not involved in the exercise. Inn order to get info on this or an other mathematical object, just <em>try</em> the first letters of it in the sage interpreter, which has autocompletion. We can try here <code>perm[TAB]</code> and/or <code>Perm[TAB]</code>. In the second case we get a list of classes / constructors, among them we can put a question mark before or after...</p>
<pre><code>sage: Permutation?
</code></pre>
<p>to get more information on this. The sage interpreter is a good tool to learn <code>sage</code>.</p>
https://ask.sagemath.org/question/40180/matrix-permutations/?comment=40187#post-id-40187Thanks for your help.
I tried it as follow based on your code, but it doesn't work.
I use CoCalc. It shows the green bar for computation, but it doesn't stop and doesn't return any results.
R = [0,1,2,3,4,5,6,7,8,9]
C = cartesian_product((R,R,R,R)).list()
invertibleMatrices = [matrix(ZZ, 2,2,list(c)) for c in C if matrix(ZZ, 2,2,list(c)).det()]
invertibleMatrices3Tuples = cartesian_product((invertibleMatrices, invertibleMatrices, invertibleMatrices)).list()
invertibleSTUProducts = [c for c in invertibleMatrices3Tuples if c[0]*c[1]*c[2].det()]
len(invertibleSTUProducts)
BTW: What do you mean by *"It is a good idea to use structure in mathematics"*?Sat, 16 Dec 2017 19:19:34 +0100https://ask.sagemath.org/question/40180/matrix-permutations/?comment=40187#post-id-40187Comment by vdelecroix for <p>For the exercise, to loop among the list of all 2x2 matrices with entries in [0..9] the suitable mathematical construction is the <strong>cartesian product</strong> of four copies of [0..9]. This may be achieved like this:</p>
<pre><code>sage: R = [0..9]
sage: C = cartesian_product( (R,R,R,R) )
sage: len(C)
10000
sage: C.random_element()
(6, 5, 4, 7)
</code></pre>
<p>One can build the matrix with the las entries (last value is <code>_</code> in the sage interpreter) like this:</p>
<pre><code>sage: _
(6, 5, 4, 7)
sage: matrix( ZZ, 2, 2, list(_) )
[6 5]
[4 7]
</code></pre>
<p>So the number of invertible matrices $S$ of the given shape is:</p>
<pre><code>sage: R = [0..9]
sage: C = cartesian_product( (R,R,R,R) )
sage: len( [ c for c in C if matrix( ZZ, 2, 2, list(c) ).det() ] )
9430
</code></pre>
<p>The product $STU$ is invertible iff each matrix in it is invertible. So the exercise is solved in one more line. It is a good idea to use structure in mathematics, when it is possible. Brute force can then confirm the result.</p>
<p>Note: Permutations are not involved in the exercise. Inn order to get info on this or an other mathematical object, just <em>try</em> the first letters of it in the sage interpreter, which has autocompletion. We can try here <code>perm[TAB]</code> and/or <code>Perm[TAB]</code>. In the second case we get a list of classes / constructors, among them we can put a question mark before or after...</p>
<pre><code>sage: Permutation?
</code></pre>
<p>to get more information on this. The sage interpreter is a good tool to learn <code>sage</code>.</p>
https://ask.sagemath.org/question/40180/matrix-permutations/?comment=40188#post-id-40188As it is written: "The product STU is invertible if and only if each of the matrices S, T and U is invertible." So there is no need to compute all the products (that would be too long to terminate anyway).Sat, 16 Dec 2017 21:07:40 +0100https://ask.sagemath.org/question/40180/matrix-permutations/?comment=40188#post-id-40188Comment by Asg for <p>For the exercise, to loop among the list of all 2x2 matrices with entries in [0..9] the suitable mathematical construction is the <strong>cartesian product</strong> of four copies of [0..9]. This may be achieved like this:</p>
<pre><code>sage: R = [0..9]
sage: C = cartesian_product( (R,R,R,R) )
sage: len(C)
10000
sage: C.random_element()
(6, 5, 4, 7)
</code></pre>
<p>One can build the matrix with the las entries (last value is <code>_</code> in the sage interpreter) like this:</p>
<pre><code>sage: _
(6, 5, 4, 7)
sage: matrix( ZZ, 2, 2, list(_) )
[6 5]
[4 7]
</code></pre>
<p>So the number of invertible matrices $S$ of the given shape is:</p>
<pre><code>sage: R = [0..9]
sage: C = cartesian_product( (R,R,R,R) )
sage: len( [ c for c in C if matrix( ZZ, 2, 2, list(c) ).det() ] )
9430
</code></pre>
<p>The product $STU$ is invertible iff each matrix in it is invertible. So the exercise is solved in one more line. It is a good idea to use structure in mathematics, when it is possible. Brute force can then confirm the result.</p>
<p>Note: Permutations are not involved in the exercise. Inn order to get info on this or an other mathematical object, just <em>try</em> the first letters of it in the sage interpreter, which has autocompletion. We can try here <code>perm[TAB]</code> and/or <code>Perm[TAB]</code>. In the second case we get a list of classes / constructors, among them we can put a question mark before or after...</p>
<pre><code>sage: Permutation?
</code></pre>
<p>to get more information on this. The sage interpreter is a good tool to learn <code>sage</code>.</p>
https://ask.sagemath.org/question/40180/matrix-permutations/?comment=40190#post-id-40190I don't think that I do so.
I perform the following steps:
1. I put all invertible Matrices in the variable invertibleMatrices.
2. I try to build all tuple-combinations with 3 matrices and put it in the variable invertibleMatrices3Tuples.
3. I build the products of matrices in each tuple and see if the product is invertible and put them all in the variable invertibleSTUProducts.
Did I miss something?Sat, 16 Dec 2017 21:55:27 +0100https://ask.sagemath.org/question/40180/matrix-permutations/?comment=40190#post-id-40190Comment by vdelecroix for <p>For the exercise, to loop among the list of all 2x2 matrices with entries in [0..9] the suitable mathematical construction is the <strong>cartesian product</strong> of four copies of [0..9]. This may be achieved like this:</p>
<pre><code>sage: R = [0..9]
sage: C = cartesian_product( (R,R,R,R) )
sage: len(C)
10000
sage: C.random_element()
(6, 5, 4, 7)
</code></pre>
<p>One can build the matrix with the las entries (last value is <code>_</code> in the sage interpreter) like this:</p>
<pre><code>sage: _
(6, 5, 4, 7)
sage: matrix( ZZ, 2, 2, list(_) )
[6 5]
[4 7]
</code></pre>
<p>So the number of invertible matrices $S$ of the given shape is:</p>
<pre><code>sage: R = [0..9]
sage: C = cartesian_product( (R,R,R,R) )
sage: len( [ c for c in C if matrix( ZZ, 2, 2, list(c) ).det() ] )
9430
</code></pre>
<p>The product $STU$ is invertible iff each matrix in it is invertible. So the exercise is solved in one more line. It is a good idea to use structure in mathematics, when it is possible. Brute force can then confirm the result.</p>
<p>Note: Permutations are not involved in the exercise. Inn order to get info on this or an other mathematical object, just <em>try</em> the first letters of it in the sage interpreter, which has autocompletion. We can try here <code>perm[TAB]</code> and/or <code>Perm[TAB]</code>. In the second case we get a list of classes / constructors, among them we can put a question mark before or after...</p>
<pre><code>sage: Permutation?
</code></pre>
<p>to get more information on this. The sage interpreter is a good tool to learn <code>sage</code>.</p>
https://ask.sagemath.org/question/40180/matrix-permutations/?comment=40191#post-id-40191yes you didSat, 16 Dec 2017 22:00:53 +0100https://ask.sagemath.org/question/40180/matrix-permutations/?comment=40191#post-id-40191Comment by Asg for <p>For the exercise, to loop among the list of all 2x2 matrices with entries in [0..9] the suitable mathematical construction is the <strong>cartesian product</strong> of four copies of [0..9]. This may be achieved like this:</p>
<pre><code>sage: R = [0..9]
sage: C = cartesian_product( (R,R,R,R) )
sage: len(C)
10000
sage: C.random_element()
(6, 5, 4, 7)
</code></pre>
<p>One can build the matrix with the las entries (last value is <code>_</code> in the sage interpreter) like this:</p>
<pre><code>sage: _
(6, 5, 4, 7)
sage: matrix( ZZ, 2, 2, list(_) )
[6 5]
[4 7]
</code></pre>
<p>So the number of invertible matrices $S$ of the given shape is:</p>
<pre><code>sage: R = [0..9]
sage: C = cartesian_product( (R,R,R,R) )
sage: len( [ c for c in C if matrix( ZZ, 2, 2, list(c) ).det() ] )
9430
</code></pre>
<p>The product $STU$ is invertible iff each matrix in it is invertible. So the exercise is solved in one more line. It is a good idea to use structure in mathematics, when it is possible. Brute force can then confirm the result.</p>
<p>Note: Permutations are not involved in the exercise. Inn order to get info on this or an other mathematical object, just <em>try</em> the first letters of it in the sage interpreter, which has autocompletion. We can try here <code>perm[TAB]</code> and/or <code>Perm[TAB]</code>. In the second case we get a list of classes / constructors, among them we can put a question mark before or after...</p>
<pre><code>sage: Permutation?
</code></pre>
<p>to get more information on this. The sage interpreter is a good tool to learn <code>sage</code>.</p>
https://ask.sagemath.org/question/40180/matrix-permutations/?comment=40193#post-id-40193I think, now I get your point:
The 1st step remains and the I'll do this:
CountOfInvertibleMatrices3Tuples = len(invertibleMatrices)^3
CountOfInvertibleMatrices3Tuples
838561807000
Is it now ok?
ThanksSat, 16 Dec 2017 22:19:56 +0100https://ask.sagemath.org/question/40180/matrix-permutations/?comment=40193#post-id-40193Comment by vdelecroix for <p>For the exercise, to loop among the list of all 2x2 matrices with entries in [0..9] the suitable mathematical construction is the <strong>cartesian product</strong> of four copies of [0..9]. This may be achieved like this:</p>
<pre><code>sage: R = [0..9]
sage: C = cartesian_product( (R,R,R,R) )
sage: len(C)
10000
sage: C.random_element()
(6, 5, 4, 7)
</code></pre>
<p>One can build the matrix with the las entries (last value is <code>_</code> in the sage interpreter) like this:</p>
<pre><code>sage: _
(6, 5, 4, 7)
sage: matrix( ZZ, 2, 2, list(_) )
[6 5]
[4 7]
</code></pre>
<p>So the number of invertible matrices $S$ of the given shape is:</p>
<pre><code>sage: R = [0..9]
sage: C = cartesian_product( (R,R,R,R) )
sage: len( [ c for c in C if matrix( ZZ, 2, 2, list(c) ).det() ] )
9430
</code></pre>
<p>The product $STU$ is invertible iff each matrix in it is invertible. So the exercise is solved in one more line. It is a good idea to use structure in mathematics, when it is possible. Brute force can then confirm the result.</p>
<p>Note: Permutations are not involved in the exercise. Inn order to get info on this or an other mathematical object, just <em>try</em> the first letters of it in the sage interpreter, which has autocompletion. We can try here <code>perm[TAB]</code> and/or <code>Perm[TAB]</code>. In the second case we get a list of classes / constructors, among them we can put a question mark before or after...</p>
<pre><code>sage: Permutation?
</code></pre>
<p>to get more information on this. The sage interpreter is a good tool to learn <code>sage</code>.</p>
https://ask.sagemath.org/question/40180/matrix-permutations/?comment=40194#post-id-40194Yes! Congratulations!Sat, 16 Dec 2017 22:30:36 +0100https://ask.sagemath.org/question/40180/matrix-permutations/?comment=40194#post-id-40194Answer by vdelecroix for <p>Hello,</p>
<p>how do I get all permutations of a Matrix?</p>
<p>For the following exercise I would like to get all permutations of a Matrix with integer values in the range $0$ to $9$. There should be $10^4$ permutations. And then generate the permutations of tuples with three matrices. $10^{12}$ permutations and then I would filter the list somehow for the invertible matrices.</p>
<p>Is there a better way?</p>
<p>Exercise:
Let $S, T, U \in \mathbb{R}^{2x2}$ such that, $S_{i, j}, T_{i, j}, U_{i, j} \in {0,1, \cdots, 9}$
How many of the products $S \cdot T \cdot U$ are invertible?</p>
<p>In the Sage reference for permutation I couldn't find any thing.</p>
<p>Thanks</p>
<p>PS: The curly braces couldn't be escaped properly in the LaTex part ... \{0,1, \cdots, 9\}</p>
https://ask.sagemath.org/question/40180/matrix-permutations/?answer=40198#post-id-40198It is possible to make an even less computational solution than in @dan_fulea answer. As already mentioned, we simply want to count the solutions of `a d - b c != 0` where `a, b, c, d` are elements of `{0, 1, ..., 9}`. This is easily obtained from the number of solutions of `ad - bc = 0` which itself can be obtained by solving `x y = s` for all possible values of `s`. For example
- `xy = 0` has 19 solutions (from which we get `19 * 19 = 361` matrices)
- `xy = 1` has 1 solution (from which we get `1` matrix)
- `xy = 6` has 4 solutions (from which we get `6 * 6 = 36` matrices)
- etc
In Sage
sage: P = set(i * j for i in range(10) for j in range(10))
sage: sum(sum(x * y == s for x in range(10) for y in range(10))**2 for s in P)
570
sage: 10**4 - 570
9430
Here 570 is the number of non-invertible matrices with entries in `{0, 1, ..., 9}`. Now, we can even be more clever. The number of solutions for `xy = s` is, excepted for `s = 0`, the number of divisors `d` of `s` that belongs to {1, ..., 9} and so that `s/d` also beongs to {1, ..., 9}. We recover 570 with
sage: Q = set(i * j for i in range(1, 10) for j in range(1, 10))
sage: 19^2 + sum(sum(s%d == 0 and s < 10*d for d in range(1,10))**2 for s in Q)
570Sun, 17 Dec 2017 11:14:46 +0100https://ask.sagemath.org/question/40180/matrix-permutations/?answer=40198#post-id-40198Comment by dan_fulea for <p>It is possible to make an even less computational solution than in <a href="/users/23512/dan_fulea/">@dan_fulea</a> answer. As already mentioned, we simply want to count the solutions of <code>a d - b c != 0</code> where <code>a, b, c, d</code> are elements of <code>{0, 1, ..., 9}</code>. This is easily obtained from the number of solutions of <code>ad - bc = 0</code> which itself can be obtained by solving <code>x y = s</code> for all possible values of <code>s</code>. For example</p>
<ul>
<li><code>xy = 0</code> has 19 solutions (from which we get <code>19 * 19 = 361</code> matrices)</li>
<li><code>xy = 1</code> has 1 solution (from which we get <code>1</code> matrix)</li>
<li><code>xy = 6</code> has 4 solutions (from which we get <code>6 * 6 = 36</code> matrices)</li>
<li>etc</li>
</ul>
<p>In Sage</p>
<pre><code>sage: P = set(i * j for i in range(10) for j in range(10))
sage: sum(sum(x * y == s for x in range(10) for y in range(10))**2 for s in P)
570
sage: 10**4 - 570
9430
</code></pre>
<p>Here 570 is the number of non-invertible matrices with entries in <code>{0, 1, ..., 9}</code>. Now, we can even be more clever. The number of solutions for <code>xy = s</code> is, excepted for <code>s = 0</code>, the number of divisors <code>d</code> of <code>s</code> that belongs to {1, ..., 9} and so that <code>s/d</code> also beongs to {1, ..., 9}. We recover 570 with</p>
<pre><code>sage: Q = set(i * j for i in range(1, 10) for j in range(1, 10))
sage: 19^2 + sum(sum(s%d == 0 and s < 10*d for d in range(1,10))**2 for s in Q)
570
</code></pre>
https://ask.sagemath.org/question/40180/matrix-permutations/?comment=40203#post-id-40203Thanks for the post! I must confess, this was the first solution i wanted to type, then saw that there is a little combinatorial point to *explain* when we place the `a,b,c,d` elements with `ad == bc` on the two diagonals in case `a == d` and/or `b == c`. Since the cartesian product was already an issue, i decided to avoid combinatorics. This means then to compute for each possible product the number of the ways to produce it. Of course, we do not have $10^4$ cases, since for each possible product we can count the pairs. At most $10^2$ cases. But then i was generous to get all $10^4$. The argument is not better, but it better illustrates the cartesian product as a construction. I was really glad to see it in action for $S,T,U$ again. (Although the bits needed are too many.)Sun, 17 Dec 2017 18:33:29 +0100https://ask.sagemath.org/question/40180/matrix-permutations/?comment=40203#post-id-40203Comment by dan_fulea for <p>It is possible to make an even less computational solution than in <a href="/users/23512/dan_fulea/">@dan_fulea</a> answer. As already mentioned, we simply want to count the solutions of <code>a d - b c != 0</code> where <code>a, b, c, d</code> are elements of <code>{0, 1, ..., 9}</code>. This is easily obtained from the number of solutions of <code>ad - bc = 0</code> which itself can be obtained by solving <code>x y = s</code> for all possible values of <code>s</code>. For example</p>
<ul>
<li><code>xy = 0</code> has 19 solutions (from which we get <code>19 * 19 = 361</code> matrices)</li>
<li><code>xy = 1</code> has 1 solution (from which we get <code>1</code> matrix)</li>
<li><code>xy = 6</code> has 4 solutions (from which we get <code>6 * 6 = 36</code> matrices)</li>
<li>etc</li>
</ul>
<p>In Sage</p>
<pre><code>sage: P = set(i * j for i in range(10) for j in range(10))
sage: sum(sum(x * y == s for x in range(10) for y in range(10))**2 for s in P)
570
sage: 10**4 - 570
9430
</code></pre>
<p>Here 570 is the number of non-invertible matrices with entries in <code>{0, 1, ..., 9}</code>. Now, we can even be more clever. The number of solutions for <code>xy = s</code> is, excepted for <code>s = 0</code>, the number of divisors <code>d</code> of <code>s</code> that belongs to {1, ..., 9} and so that <code>s/d</code> also beongs to {1, ..., 9}. We recover 570 with</p>
<pre><code>sage: Q = set(i * j for i in range(1, 10) for j in range(1, 10))
sage: 19^2 + sum(sum(s%d == 0 and s < 10*d for d in range(1,10))**2 for s in Q)
570
</code></pre>
https://ask.sagemath.org/question/40180/matrix-permutations/?comment=40204#post-id-40204It is a good homework to try to implement **this idea** for counting $3\times 3$ invertible matrices with entries among $0,1,2,3,4,5,6,7,8,9$. The $10^9$-steps loop would not be my solution. (Of course, there is no structural meaning for the number obtained after much work, just an exercise. The experience will eventually pay later many times the dividends...)Sun, 17 Dec 2017 18:37:42 +0100https://ask.sagemath.org/question/40180/matrix-permutations/?comment=40204#post-id-40204Comment by Asg for <p>It is possible to make an even less computational solution than in <a href="/users/23512/dan_fulea/">@dan_fulea</a> answer. As already mentioned, we simply want to count the solutions of <code>a d - b c != 0</code> where <code>a, b, c, d</code> are elements of <code>{0, 1, ..., 9}</code>. This is easily obtained from the number of solutions of <code>ad - bc = 0</code> which itself can be obtained by solving <code>x y = s</code> for all possible values of <code>s</code>. For example</p>
<ul>
<li><code>xy = 0</code> has 19 solutions (from which we get <code>19 * 19 = 361</code> matrices)</li>
<li><code>xy = 1</code> has 1 solution (from which we get <code>1</code> matrix)</li>
<li><code>xy = 6</code> has 4 solutions (from which we get <code>6 * 6 = 36</code> matrices)</li>
<li>etc</li>
</ul>
<p>In Sage</p>
<pre><code>sage: P = set(i * j for i in range(10) for j in range(10))
sage: sum(sum(x * y == s for x in range(10) for y in range(10))**2 for s in P)
570
sage: 10**4 - 570
9430
</code></pre>
<p>Here 570 is the number of non-invertible matrices with entries in <code>{0, 1, ..., 9}</code>. Now, we can even be more clever. The number of solutions for <code>xy = s</code> is, excepted for <code>s = 0</code>, the number of divisors <code>d</code> of <code>s</code> that belongs to {1, ..., 9} and so that <code>s/d</code> also beongs to {1, ..., 9}. We recover 570 with</p>
<pre><code>sage: Q = set(i * j for i in range(1, 10) for j in range(1, 10))
sage: 19^2 + sum(sum(s%d == 0 and s < 10*d for d in range(1,10))**2 for s in Q)
570
</code></pre>
https://ask.sagemath.org/question/40180/matrix-permutations/?comment=40232#post-id-40232Thanks for your help.
@vdelecroix I'll try to comprehend your solution ...Tue, 19 Dec 2017 13:51:53 +0100https://ask.sagemath.org/question/40180/matrix-permutations/?comment=40232#post-id-40232