ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 10 Jun 2013 11:39:06 -0500How to compute an iterated product in Sagehttp://ask.sagemath.org/question/10219/how-to-compute-an-iterated-product-in-sage/I am wondering about how to define a function `f(n, k, i)` that will take inputs `n`, `k`, `i` and give me the following product:
$$
\prod_{\ell = 1}^i {n + k - \ell \choose k}
$$
So for $i = 2$, this would look like ${n + k - 1 \choose k}\cdot {n + k - 2 \choose k}$. Would I use a for loop to iteratively add to the product until all `i` terms were accounted for?Mon, 10 Jun 2013 10:58:24 -0500http://ask.sagemath.org/question/10219/how-to-compute-an-iterated-product-in-sage/Answer by tmonteil for <p>I am wondering about how to define a function <code>f(n, k, i)</code> that will take inputs <code>n</code>, <code>k</code>, <code>i</code> and give me the following product:
$$
\prod_{\ell = 1}^i {n + k - \ell \choose k}
$$
So for $i = 2$, this would look like ${n + k - 1 \choose k}\cdot {n + k - 2 \choose k}$. Would I use a for loop to iteratively add to the product until all <code>i</code> terms were accounted for?</p>
http://ask.sagemath.org/question/10219/how-to-compute-an-iterated-product-in-sage/?answer=15054#post-id-15054You can use the `prod()` function (see prod? for some help):
sage: f = lambda n, k, i : prod([binomial(n+k-l,k) for l in range(1,i+1)])
sage: f(3,4,2)
75
This also works symbolically:
sage: var('n,k')
(n, k)
sage: f(n,k,2)
binomial(k + n - 2, k)*binomial(k + n - 1, k)
Mon, 10 Jun 2013 11:39:06 -0500http://ask.sagemath.org/question/10219/how-to-compute-an-iterated-product-in-sage/?answer=15054#post-id-15054