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.Wed, 08 Dec 2010 10:43:41 +0100A Combinatorics Problem - Product Rule Indiceshttps://ask.sagemath.org/question/7792/a-combinatorics-problem-product-rule-indices/I have a particular combinatorics problem where I would like to generate lists that look like this:
* (n=1): <tt>[[1],[]], [[],[1]]</tt>
* (n=2): <tt>[[1,2],[]], [[1],[2]], [[2],[1]], [[],[1,2]]</tt>
* (n=3): <tt>[[1,2,3],[]], [[1,2],[3]], [[1,3],[2]], [[2,3],[1]], [[1],[2,3]], [[2],[1,3]], [[3],[1,2]], [[],[1,2,3]]</tt>
These sorts of combinations come from taking derivatives with respect to different variables of a product of two functions. Using subscripts $1,2,3$ to denote differentiation with respect to the variables $z_1,z_2,z_3$, respectively, I'm looking at computations of the form:
* $\partial_{z_1} (fg) = f_1g + f g_1$
* $\partial_{z_1} \partial_{z_2} (fg) = f_{12}g + f_1 g_2 + f_2 g_1 + f g_{12}$
* $\partial_{z_1} \partial_{z_2} \partial_{z_3} (fg) = f_{123}g + f_{12}g_3 + f_{13}g_2 + f_{23}g_1 + f_1g_{23} + f_2g_{13} + f_3g_{12} + f g_{123}$
Is there a quick way to generate such a list in Sage? I'm not actually looking to perform these symbolic derivatives. I just used the differentiation to demonstrate where these combinations come from. (And check with you whether or not I'm computing them correctly.)Tue, 07 Dec 2010 14:38:21 +0100https://ask.sagemath.org/question/7792/a-combinatorics-problem-product-rule-indices/Answer by cswiercz for <p>I have a particular combinatorics problem where I would like to generate lists that look like this:</p>
<ul>
<li>(n=1): <tt>[[1],[]], [[],[1]]</tt></li>
<li>(n=2): <tt>[[1,2],[]], [[1],[2]], [[2],[1]], [[],[1,2]]</tt></li>
<li>(n=3): <tt>[[1,2,3],[]], [[1,2],[3]], [[1,3],[2]], [[2,3],[1]], [[1],[2,3]], [[2],[1,3]], [[3],[1,2]], [[],[1,2,3]]</tt></li>
</ul>
<p>These sorts of combinations come from taking derivatives with respect to different variables of a product of two functions. Using subscripts $1,2,3$ to denote differentiation with respect to the variables $z_1,z_2,z_3$, respectively, I'm looking at computations of the form:</p>
<ul>
<li>$\partial_{z_1} (fg) = f_1g + f g_1$</li>
<li>$\partial_{z_1} \partial_{z_2} (fg) = f_{12}g + f_1 g_2 + f_2 g_1 + f g_{12}$</li>
<li>$\partial_{z_1} \partial_{z_2} \partial_{z_3} (fg) = f_{123}g + f_{12}g_3 + f_{13}g_2 + f_{23}g_1 + f_1g_{23} + f_2g_{13} + f_3g_{12} + f g_{123}$</li>
</ul>
<p>Is there a quick way to generate such a list in Sage? I'm not actually looking to perform these symbolic derivatives. I just used the differentiation to demonstrate where these combinations come from. (And check with you whether or not I'm computing them correctly.)</p>
https://ask.sagemath.org/question/7792/a-combinatorics-problem-product-rule-indices/?answer=11827#post-id-11827I solved my own problem. The solution uses the function `partitions_set`. However, it only generates half of the derivatives I want. The other half is obtained by transposing / reversing the outputs:
sage: derivs = [1,2]
sage: for s in partitions_set(derivs,2):
print s
s.reverse()
print s
[[1], [2]]
[[2], [1]]
sage: s = [derivs, []]; print s
[[1, 2], []]
sage: s.reverse(); print s
[[], [1, 2]]
For the case when $n=3$ I get what I want as well:
sage: derivs = [1,2,3]
sage: for s in partitions_set(derivs,2):
print s
s.reverse()
print s
[[1], [2, 3]]
[[2, 3], [1]]
[[1, 2], [3]]
[[3], [1, 2]]
[[1, 3], [2]]
[[2], [1, 3]]
sage: s = [derivs, []]; print s
[[1, 2, 3], []]
sage: s.reverse(); print s
[[], [1, 2, 3]]
At the risk of sounding full of myself I'm going to answer my own question. Thanks to everyone who thought about it, though.Tue, 07 Dec 2010 15:18:53 +0100https://ask.sagemath.org/question/7792/a-combinatorics-problem-product-rule-indices/?answer=11827#post-id-11827Comment by cswiercz for <p>I solved my own problem. The solution uses the function <code>partitions_set</code>. However, it only generates half of the derivatives I want. The other half is obtained by transposing / reversing the outputs:</p>
<pre><code>sage: derivs = [1,2]
sage: for s in partitions_set(derivs,2):
print s
s.reverse()
print s
[[1], [2]]
[[2], [1]]
sage: s = [derivs, []]; print s
[[1, 2], []]
sage: s.reverse(); print s
[[], [1, 2]]
</code></pre>
<p>For the case when $n=3$ I get what I want as well:</p>
<pre><code>sage: derivs = [1,2,3]
sage: for s in partitions_set(derivs,2):
print s
s.reverse()
print s
[[1], [2, 3]]
[[2, 3], [1]]
[[1, 2], [3]]
[[3], [1, 2]]
[[1, 3], [2]]
[[2], [1, 3]]
sage: s = [derivs, []]; print s
[[1, 2, 3], []]
sage: s.reverse(); print s
[[], [1, 2, 3]]
</code></pre>
<p>At the risk of sounding full of myself I'm going to answer my own question. Thanks to everyone who thought about it, though.</p>
https://ask.sagemath.org/question/7792/a-combinatorics-problem-product-rule-indices/?comment=22441#post-id-22441I'm not sure if that's necessary. I just didn't want to appear that I was spamming ask.sage or trying to increase my karma. I was also, in part, trying to make a joke but I'm terrible at those. :)Tue, 07 Dec 2010 15:55:33 +0100https://ask.sagemath.org/question/7792/a-combinatorics-problem-product-rule-indices/?comment=22441#post-id-22441Comment by Evgeny for <p>I solved my own problem. The solution uses the function <code>partitions_set</code>. However, it only generates half of the derivatives I want. The other half is obtained by transposing / reversing the outputs:</p>
<pre><code>sage: derivs = [1,2]
sage: for s in partitions_set(derivs,2):
print s
s.reverse()
print s
[[1], [2]]
[[2], [1]]
sage: s = [derivs, []]; print s
[[1, 2], []]
sage: s.reverse(); print s
[[], [1, 2]]
</code></pre>
<p>For the case when $n=3$ I get what I want as well:</p>
<pre><code>sage: derivs = [1,2,3]
sage: for s in partitions_set(derivs,2):
print s
s.reverse()
print s
[[1], [2, 3]]
[[2, 3], [1]]
[[1, 2], [3]]
[[3], [1, 2]]
[[1, 3], [2]]
[[2], [1, 3]]
sage: s = [derivs, []]; print s
[[1, 2, 3], []]
sage: s.reverse(); print s
[[], [1, 2, 3]]
</code></pre>
<p>At the risk of sounding full of myself I'm going to answer my own question. Thanks to everyone who thought about it, though.</p>
https://ask.sagemath.org/question/7792/a-combinatorics-problem-product-rule-indices/?comment=22442#post-id-22442what if we make questions appear anonymous?Tue, 07 Dec 2010 15:45:15 +0100https://ask.sagemath.org/question/7792/a-combinatorics-problem-product-rule-indices/?comment=22442#post-id-22442Comment by Jason Bandlow for <p>I solved my own problem. The solution uses the function <code>partitions_set</code>. However, it only generates half of the derivatives I want. The other half is obtained by transposing / reversing the outputs:</p>
<pre><code>sage: derivs = [1,2]
sage: for s in partitions_set(derivs,2):
print s
s.reverse()
print s
[[1], [2]]
[[2], [1]]
sage: s = [derivs, []]; print s
[[1, 2], []]
sage: s.reverse(); print s
[[], [1, 2]]
</code></pre>
<p>For the case when $n=3$ I get what I want as well:</p>
<pre><code>sage: derivs = [1,2,3]
sage: for s in partitions_set(derivs,2):
print s
s.reverse()
print s
[[1], [2, 3]]
[[2, 3], [1]]
[[1, 2], [3]]
[[3], [1, 2]]
[[1, 3], [2]]
[[2], [1, 3]]
sage: s = [derivs, []]; print s
[[1, 2, 3], []]
sage: s.reverse(); print s
[[], [1, 2, 3]]
</code></pre>
<p>At the risk of sounding full of myself I'm going to answer my own question. Thanks to everyone who thought about it, though.</p>
https://ask.sagemath.org/question/7792/a-combinatorics-problem-product-rule-indices/?comment=22437#post-id-22437See also OrderedSetPartitions, which may be helpful.Wed, 08 Dec 2010 10:43:41 +0100https://ask.sagemath.org/question/7792/a-combinatorics-problem-product-rule-indices/?comment=22437#post-id-22437Comment by Evgeny for <p>I solved my own problem. The solution uses the function <code>partitions_set</code>. However, it only generates half of the derivatives I want. The other half is obtained by transposing / reversing the outputs:</p>
<pre><code>sage: derivs = [1,2]
sage: for s in partitions_set(derivs,2):
print s
s.reverse()
print s
[[1], [2]]
[[2], [1]]
sage: s = [derivs, []]; print s
[[1, 2], []]
sage: s.reverse(); print s
[[], [1, 2]]
</code></pre>
<p>For the case when $n=3$ I get what I want as well:</p>
<pre><code>sage: derivs = [1,2,3]
sage: for s in partitions_set(derivs,2):
print s
s.reverse()
print s
[[1], [2, 3]]
[[2, 3], [1]]
[[1, 2], [3]]
[[3], [1, 2]]
[[1, 3], [2]]
[[2], [1, 3]]
sage: s = [derivs, []]; print s
[[1, 2, 3], []]
sage: s.reverse(); print s
[[], [1, 2, 3]]
</code></pre>
<p>At the risk of sounding full of myself I'm going to answer my own question. Thanks to everyone who thought about it, though.</p>
https://ask.sagemath.org/question/7792/a-combinatorics-problem-product-rule-indices/?comment=22440#post-id-22440that's the thing I'm not sure why people should worry about these issues, it's kind of silly IMO.Tue, 07 Dec 2010 16:02:24 +0100https://ask.sagemath.org/question/7792/a-combinatorics-problem-product-rule-indices/?comment=22440#post-id-22440Comment by cswiercz for <p>I solved my own problem. The solution uses the function <code>partitions_set</code>. However, it only generates half of the derivatives I want. The other half is obtained by transposing / reversing the outputs:</p>
<pre><code>sage: derivs = [1,2]
sage: for s in partitions_set(derivs,2):
print s
s.reverse()
print s
[[1], [2]]
[[2], [1]]
sage: s = [derivs, []]; print s
[[1, 2], []]
sage: s.reverse(); print s
[[], [1, 2]]
</code></pre>
<p>For the case when $n=3$ I get what I want as well:</p>
<pre><code>sage: derivs = [1,2,3]
sage: for s in partitions_set(derivs,2):
print s
s.reverse()
print s
[[1], [2, 3]]
[[2, 3], [1]]
[[1, 2], [3]]
[[3], [1, 2]]
[[1, 3], [2]]
[[2], [1, 3]]
sage: s = [derivs, []]; print s
[[1, 2, 3], []]
sage: s.reverse(); print s
[[], [1, 2, 3]]
</code></pre>
<p>At the risk of sounding full of myself I'm going to answer my own question. Thanks to everyone who thought about it, though.</p>
https://ask.sagemath.org/question/7792/a-combinatorics-problem-product-rule-indices/?comment=22439#post-id-22439In that case I'm done worrying about it.Tue, 07 Dec 2010 16:15:08 +0100https://ask.sagemath.org/question/7792/a-combinatorics-problem-product-rule-indices/?comment=22439#post-id-22439