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, 16 Feb 2021 16:13:09 +0100Computing dimensionshttps://ask.sagemath.org/question/55725/computing-dimensions/I have the following routine divided into 3 steps:
1)
p = dict()
P = Permutations(3)
for i in (1 .. 3):
for u in (1 .. 3):
p[i, u] = [1 if P[m][i-1] == u else 0
for m in (0 .. 5)]
2)
for i in (1 .. 3):
for u in (1 .. 3):
for j in (1 .. 3):
for v in (1 .. 3):
if i != j and u != v:
p[i, j, u, v] = [1 if P[m][i-1] == u and P[m][j-1] == v else 0
for m in (0 .. 5)]
3)
A1 = matrix([p[i, u]
for i in (1 .. 3)
for u in (1 .. 3)])
A2 = matrix([p[i, j, u, v]
for i in (1 .. 3) for j in (1 .. 3) if i != j
for u in (1 .. 3) for v in (1 .. 3) if u != v])
C1 = A1.stack(A2)
E1 = A1.right_kernel()
E2 = C1.right_kernel()
d1 = E1.dimension()
d2 = E2.dimension()
I'm interested in dimensions `d1` and `d2`.
If I go to permutations of 4, I will need to adjust all `for` loops
to `(1 .. 4)` and to add one more step similar to step 2 to construct
one more matrix like
A3 = matrix([p[i, j, k, u, v, w]
for i in (1 .. 4) for j in (1 .. 4)
for u in (1 .. 4) for v in (1 .. 4)
for k in (1 .. 4) for w in (1 .. 4)
if i != j and i != k and j != k
and u != v and u != w and v != w])
and also
C2 = C1.stack(A3)
E3 = C2.right_kernel()
d3 = E3.dimension()
So... I was wondering if there is some "easy" way to put this code
in some kind of "generic" program. I mean, I could repeat the whole
idea for permutations of 4 or 5 and so on, but it is not very practical.
Any suggestions would be great.Tue, 16 Feb 2021 00:54:19 +0100https://ask.sagemath.org/question/55725/computing-dimensions/Answer by slelievre for <p>I have the following routine divided into 3 steps:</p>
<p>1)</p>
<pre><code>p = dict()
P = Permutations(3)
for i in (1 .. 3):
for u in (1 .. 3):
p[i, u] = [1 if P[m][i-1] == u else 0
for m in (0 .. 5)]
</code></pre>
<p>2)</p>
<pre><code>for i in (1 .. 3):
for u in (1 .. 3):
for j in (1 .. 3):
for v in (1 .. 3):
if i != j and u != v:
p[i, j, u, v] = [1 if P[m][i-1] == u and P[m][j-1] == v else 0
for m in (0 .. 5)]
</code></pre>
<p>3)</p>
<pre><code>A1 = matrix([p[i, u]
for i in (1 .. 3)
for u in (1 .. 3)])
A2 = matrix([p[i, j, u, v]
for i in (1 .. 3) for j in (1 .. 3) if i != j
for u in (1 .. 3) for v in (1 .. 3) if u != v])
C1 = A1.stack(A2)
E1 = A1.right_kernel()
E2 = C1.right_kernel()
d1 = E1.dimension()
d2 = E2.dimension()
</code></pre>
<p>I'm interested in dimensions <code>d1</code> and <code>d2</code>.</p>
<p>If I go to permutations of 4, I will need to adjust all <code>for</code> loops
to <code>(1 .. 4)</code> and to add one more step similar to step 2 to construct
one more matrix like</p>
<pre><code>A3 = matrix([p[i, j, k, u, v, w]
for i in (1 .. 4) for j in (1 .. 4)
for u in (1 .. 4) for v in (1 .. 4)
for k in (1 .. 4) for w in (1 .. 4)
if i != j and i != k and j != k
and u != v and u != w and v != w])
</code></pre>
<p>and also</p>
<pre><code>C2 = C1.stack(A3)
E3 = C2.right_kernel()
d3 = E3.dimension()
</code></pre>
<p>So... I was wondering if there is some "easy" way to put this code
in some kind of "generic" program. I mean, I could repeat the whole
idea for permutations of 4 or 5 and so on, but it is not very practical.
Any suggestions would be great.</p>
https://ask.sagemath.org/question/55725/computing-dimensions/?answer=55726#post-id-55726Here is a refactoring of the code as a function of `n`,
called `dimensions` for lack of a better name.
The documentation could better describe what sequence
of dimensions is being computed.
def dimensions(n):
r"""
Return the sequence of dimensions for this `n`.
EXAMPLES::
sage: dimensions(3)
[1, 0]
sage: dimensions(4)
[14, 1, 0]
sage: dimensions(5)
[103, 42, 1, 0]
"""
P = Permutations(n)
N = P.cardinality()
C = matrix(ZZ, 0, N)
d = []
for k in range(1, n):
Arr = Arrangements([1 .. n], k)
A = matrix(ZZ, [[all(p[i - 1] == u for i, u in zip(ii, uu))
for p in P] for ii in Arr for uu in Arr])
C = C.stack(A)
d.append(N - C.rank())
return d
No surprise, it takes longer and longer as `n` grows:
sage: %time dimensions(3)
CPU times: user 4.18 ms, sys: 202 µs, total: 4.39 ms
Wall time: 4.29 ms
[1, 0]
sage: %time dimensions(4)
CPU times: user 169 ms, sys: 5.79 ms, total: 175 ms
Wall time: 189 ms
[14, 1, 0]
sage: %time dimensions(5)
CPU times: user 14.1 s, sys: 103 ms, total: 14.2 s
Wall time: 14.3 s
[103, 42, 1, 0]Tue, 16 Feb 2021 02:26:30 +0100https://ask.sagemath.org/question/55725/computing-dimensions/?answer=55726#post-id-55726Comment by phcosta for <p>Here is a refactoring of the code as a function of <code>n</code>,
called <code>dimensions</code> for lack of a better name.</p>
<p>The documentation could better describe what sequence
of dimensions is being computed.</p>
<pre><code>def dimensions(n):
r"""
Return the sequence of dimensions for this `n`.
EXAMPLES::
sage: dimensions(3)
[1, 0]
sage: dimensions(4)
[14, 1, 0]
sage: dimensions(5)
[103, 42, 1, 0]
"""
P = Permutations(n)
N = P.cardinality()
C = matrix(ZZ, 0, N)
d = []
for k in range(1, n):
Arr = Arrangements([1 .. n], k)
A = matrix(ZZ, [[all(p[i - 1] == u for i, u in zip(ii, uu))
for p in P] for ii in Arr for uu in Arr])
C = C.stack(A)
d.append(N - C.rank())
return d
</code></pre>
<p>No surprise, it takes longer and longer as <code>n</code> grows:</p>
<pre><code>sage: %time dimensions(3)
CPU times: user 4.18 ms, sys: 202 µs, total: 4.39 ms
Wall time: 4.29 ms
[1, 0]
sage: %time dimensions(4)
CPU times: user 169 ms, sys: 5.79 ms, total: 175 ms
Wall time: 189 ms
[14, 1, 0]
sage: %time dimensions(5)
CPU times: user 14.1 s, sys: 103 ms, total: 14.2 s
Wall time: 14.3 s
[103, 42, 1, 0]
</code></pre>
https://ask.sagemath.org/question/55725/computing-dimensions/?comment=55736#post-id-55736Many thanks. Do you have any suggestions of references to learn a bit more about programming?Tue, 16 Feb 2021 15:36:08 +0100https://ask.sagemath.org/question/55725/computing-dimensions/?comment=55736#post-id-55736Comment by slelievre for <p>Here is a refactoring of the code as a function of <code>n</code>,
called <code>dimensions</code> for lack of a better name.</p>
<p>The documentation could better describe what sequence
of dimensions is being computed.</p>
<pre><code>def dimensions(n):
r"""
Return the sequence of dimensions for this `n`.
EXAMPLES::
sage: dimensions(3)
[1, 0]
sage: dimensions(4)
[14, 1, 0]
sage: dimensions(5)
[103, 42, 1, 0]
"""
P = Permutations(n)
N = P.cardinality()
C = matrix(ZZ, 0, N)
d = []
for k in range(1, n):
Arr = Arrangements([1 .. n], k)
A = matrix(ZZ, [[all(p[i - 1] == u for i, u in zip(ii, uu))
for p in P] for ii in Arr for uu in Arr])
C = C.stack(A)
d.append(N - C.rank())
return d
</code></pre>
<p>No surprise, it takes longer and longer as <code>n</code> grows:</p>
<pre><code>sage: %time dimensions(3)
CPU times: user 4.18 ms, sys: 202 µs, total: 4.39 ms
Wall time: 4.29 ms
[1, 0]
sage: %time dimensions(4)
CPU times: user 169 ms, sys: 5.79 ms, total: 175 ms
Wall time: 189 ms
[14, 1, 0]
sage: %time dimensions(5)
CPU times: user 14.1 s, sys: 103 ms, total: 14.2 s
Wall time: 14.3 s
[103, 42, 1, 0]
</code></pre>
https://ask.sagemath.org/question/55725/computing-dimensions/?comment=55737#post-id-55737I would recommend the book [*Computational mathematics with SageMath*](http://sagebook.gforge.inria.fr/) which is released under a Creative commons license and can be downloaded for free in pdf form in three languages or purchased in paper form in two languages.Tue, 16 Feb 2021 16:13:09 +0100https://ask.sagemath.org/question/55725/computing-dimensions/?comment=55737#post-id-55737