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, 29 Jun 2021 23:10:28 +0200- Define matrix indexed by partitionshttps://ask.sagemath.org/question/57814/define-matrix-indexed-by-partitions/ I want to define the following matrix in sage: $J$ is of size Partitions(n), and $J_{\lambda, \mu} = 1$ if $\lambda'=\mu$ and $0$ else.
Can someone please help? Tue, 29 Jun 2021 21:00:56 +0200https://ask.sagemath.org/question/57814/define-matrix-indexed-by-partitions/
- Answer by tmonteil for <p>I want to define the following matrix in sage: $J$ is of size Partitions(n), and $J_{\lambda, \mu} = 1$ if $\lambda'=\mu$ and $0$ else.
Can someone please help? </p>
https://ask.sagemath.org/question/57814/define-matrix-indexed-by-partitions/?answer=57815#post-id-57815I am not sure about the notation $\lambda'$ so let me assume that it is the conjugate.
First, to build a matrix, you have to index the partitions, which is easy with the `enumerate` function:
sage: n = 6
sage: P = Partitions(n)
sage: m = matrix(ZZ, P.cardinality())
sage: list(enumerate(P))
[(0, [6]),
(1, [5, 1]),
(2, [4, 2]),
(3, [4, 1, 1]),
(4, [3, 3]),
(5, [3, 2, 1]),
(6, [3, 1, 1, 1]),
(7, [2, 2, 2]),
(8, [2, 2, 1, 1]),
(9, [2, 1, 1, 1, 1]),
(10, [1, 1, 1, 1, 1, 1])]
In the other way, given a partition, you need to be able to find its index, which is doable with this dictionary:
sage: part_to_int = {p:i for i,p in enumerate(P)}
Now, you can construct your matrix:
sage: for i,p in enumerate(P):
....: j = part_to_int[p.conjugate()]
....: m[i,j] = 1
You have:
sage: m
[0 0 0 0 0 0 0 0 0 0 1]
[0 0 0 0 0 0 0 0 0 1 0]
[0 0 0 0 0 0 0 0 1 0 0]
[0 0 0 0 0 0 1 0 0 0 0]
[0 0 0 0 0 0 0 1 0 0 0]
[0 0 0 0 0 1 0 0 0 0 0]
[0 0 0 1 0 0 0 0 0 0 0]
[0 0 0 0 1 0 0 0 0 0 0]
[0 0 1 0 0 0 0 0 0 0 0]
[0 1 0 0 0 0 0 0 0 0 0]
[1 0 0 0 0 0 0 0 0 0 0]
sage: m^2
[1 0 0 0 0 0 0 0 0 0 0]
[0 1 0 0 0 0 0 0 0 0 0]
[0 0 1 0 0 0 0 0 0 0 0]
[0 0 0 1 0 0 0 0 0 0 0]
[0 0 0 0 1 0 0 0 0 0 0]
[0 0 0 0 0 1 0 0 0 0 0]
[0 0 0 0 0 0 1 0 0 0 0]
[0 0 0 0 0 0 0 1 0 0 0]
[0 0 0 0 0 0 0 0 1 0 0]
[0 0 0 0 0 0 0 0 0 1 0]
[0 0 0 0 0 0 0 0 0 0 1]
If $\lambda'$ does not denote the conjugate, you can adapt by replacing `p.conjugate()` with your favorite function.Tue, 29 Jun 2021 21:48:31 +0200https://ask.sagemath.org/question/57814/define-matrix-indexed-by-partitions/?answer=57815#post-id-57815
- Comment by mathstudent for <p>I am not sure about the notation $\lambda'$ so let me assume that it is the conjugate.</p>
<p>First, to build a matrix, you have to index the partitions, which is easy with the <code>enumerate</code> function:</p>
<pre><code>sage: n = 6
sage: P = Partitions(n)
sage: m = matrix(ZZ, P.cardinality())
sage: list(enumerate(P))
[(0, [6]),
(1, [5, 1]),
(2, [4, 2]),
(3, [4, 1, 1]),
(4, [3, 3]),
(5, [3, 2, 1]),
(6, [3, 1, 1, 1]),
(7, [2, 2, 2]),
(8, [2, 2, 1, 1]),
(9, [2, 1, 1, 1, 1]),
(10, [1, 1, 1, 1, 1, 1])]
</code></pre>
<p>In the other way, given a partition, you need to be able to find its index, which is doable with this dictionary:</p>
<pre><code>sage: part_to_int = {p:i for i,p in enumerate(P)}
</code></pre>
<p>Now, you can construct your matrix:</p>
<pre><code>sage: for i,p in enumerate(P):
....: j = part_to_int[p.conjugate()]
....: m[i,j] = 1
</code></pre>
<p>You have:</p>
<pre><code>sage: m
[0 0 0 0 0 0 0 0 0 0 1]
[0 0 0 0 0 0 0 0 0 1 0]
[0 0 0 0 0 0 0 0 1 0 0]
[0 0 0 0 0 0 1 0 0 0 0]
[0 0 0 0 0 0 0 1 0 0 0]
[0 0 0 0 0 1 0 0 0 0 0]
[0 0 0 1 0 0 0 0 0 0 0]
[0 0 0 0 1 0 0 0 0 0 0]
[0 0 1 0 0 0 0 0 0 0 0]
[0 1 0 0 0 0 0 0 0 0 0]
[1 0 0 0 0 0 0 0 0 0 0]
sage: m^2
[1 0 0 0 0 0 0 0 0 0 0]
[0 1 0 0 0 0 0 0 0 0 0]
[0 0 1 0 0 0 0 0 0 0 0]
[0 0 0 1 0 0 0 0 0 0 0]
[0 0 0 0 1 0 0 0 0 0 0]
[0 0 0 0 0 1 0 0 0 0 0]
[0 0 0 0 0 0 1 0 0 0 0]
[0 0 0 0 0 0 0 1 0 0 0]
[0 0 0 0 0 0 0 0 1 0 0]
[0 0 0 0 0 0 0 0 0 1 0]
[0 0 0 0 0 0 0 0 0 0 1]
</code></pre>
<p>If $\lambda'$ does not denote the conjugate, you can adapt by replacing <code>p.conjugate()</code> with your favorite function.</p>
https://ask.sagemath.org/question/57814/define-matrix-indexed-by-partitions/?comment=57816#post-id-57816This was exactly what I was looking for. Thank you so much.Tue, 29 Jun 2021 22:30:34 +0200https://ask.sagemath.org/question/57814/define-matrix-indexed-by-partitions/?comment=57816#post-id-57816
- Answer by Nicolas M. ThiĆ©ry for <p>I want to define the following matrix in sage: $J$ is of size Partitions(n), and $J_{\lambda, \mu} = 1$ if $\lambda'=\mu$ and $0$ else.
Can someone please help? </p>
https://ask.sagemath.org/question/57814/define-matrix-indexed-by-partitions/?answer=57819#post-id-57819Potential alternatives:
- Install pandas in your Sage installation, and use a pandas DataFrame (https://fr.wikipedia.org/wiki/Pandas)
- Define a FreeModule indexed by pairs of partitionsTue, 29 Jun 2021 23:10:28 +0200https://ask.sagemath.org/question/57814/define-matrix-indexed-by-partitions/?answer=57819#post-id-57819