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.Sun, 22 Sep 2019 18:16:36 +0200Defining q-binomial coefficients $\binom{n}{k}_q$ symbolic in $n, k$https://ask.sagemath.org/question/48013/defining-q-binomial-coefficients-binomnk_q-symbolic-in-n-k/I would like to verify certain identities involving sums of [$q$-binomial coefficients $\binom{n}{k}_q$](https://en.wikipedia.org/wiki/Gaussian_binomial_coefficient) and as such, I would like to treat the $q$-binomial coefficients as if they were symbolic in $n$ and $k$. But the version of $q$-binomial coefficients that are implemented in Sage cannot be used as a symbolic variable in $n$ and $k$ according to [this thread](https://ask.sagemath.org/question/36788/symbolic-sum-with-q_binomial/). So, to this end I defined my own:
var('q,n,k')
qint = lambda n: sum([q^i for i in range(n)])
qfac = lambda n: 1 if n==0 else prod([qint(i) for i in range(1,n+1)])
qbin = lambda n,k:qfac(n)/(qfac(k)*qfac(n-k))
But if I write
sum(qbin(n,k),k,0,n)
I get the following error:
ValueError: cannot convert n + 1 to int
Is there a way to do these types of summations in Sage? I should note that, for my purposes, it would be enough to limit myself to the case when $n$ and $k$ are non-negative integers.
EDIT: As an example, I would like to prove something like the $q$-binomial theorem, which says
$$\sum_{k=0}^n q^{k(k-1)/2} \binom{n}{k}_q x^k = \prod_{k=0}^{n-1} (1+q^k x)$$
EDIT2: Fixed a typo.Sat, 21 Sep 2019 16:37:29 +0200https://ask.sagemath.org/question/48013/defining-q-binomial-coefficients-binomnk_q-symbolic-in-n-k/Answer by rburing for <p>I would like to verify certain identities involving sums of <a href="https://en.wikipedia.org/wiki/Gaussian_binomial_coefficient">$q$-binomial coefficients $\binom{n}{k}_q$</a> and as such, I would like to treat the $q$-binomial coefficients as if they were symbolic in $n$ and $k$. But the version of $q$-binomial coefficients that are implemented in Sage cannot be used as a symbolic variable in $n$ and $k$ according to <a href="https://ask.sagemath.org/question/36788/symbolic-sum-with-q_binomial/">this thread</a>. So, to this end I defined my own:</p>
<pre><code>var('q,n,k')
qint = lambda n: sum([q^i for i in range(n)])
qfac = lambda n: 1 if n==0 else prod([qint(i) for i in range(1,n+1)])
qbin = lambda n,k:qfac(n)/(qfac(k)*qfac(n-k))
</code></pre>
<p>But if I write </p>
<pre><code>sum(qbin(n,k),k,0,n)
</code></pre>
<p>I get the following error:</p>
<pre><code>ValueError: cannot convert n + 1 to int
</code></pre>
<p>Is there a way to do these types of summations in Sage? I should note that, for my purposes, it would be enough to limit myself to the case when $n$ and $k$ are non-negative integers.</p>
<p>EDIT: As an example, I would like to prove something like the $q$-binomial theorem, which says </p>
<p>$$\sum_{k=0}^n q^{k(k-1)/2} \binom{n}{k}_q x^k = \prod_{k=0}^{n-1} (1+q^k x)$$</p>
<p>EDIT2: Fixed a typo.</p>
https://ask.sagemath.org/question/48013/defining-q-binomial-coefficients-binomnk_q-symbolic-in-n-k/?answer=48025#post-id-48025If you want to make a symbolic sum then all the terms should be symbolic. Your example does not work because `qbin(n,k)` is not defined for symbolic `n`.
What you can do is to make `qbin` a symbolic function with custom typesetting:
var('q')
def qbin_latex(self, n, k):
return '{' + str(n) + ' \choose ' + str(k) + '}_{' + str(q) + '}'
qbin = function('qbin', nargs=2, print_latex_func=qbin_latex)
var('k,n')
show(sum(qbin(n,k),k,0,n))
$$\sum_{k=0}^{n} {n \choose k}_{q}$$
To check the identities that you are interested in, you will probably have to do some substitutions by hand, and/or pass an `evalf_func` parameter to `function` in the definition of `qbin`. Have a look at the [documentation for symbolic functions](http://doc.sagemath.org/html/en/reference/calculus/sage/calculus/var.html#sage.calculus.var.function).Sun, 22 Sep 2019 11:02:01 +0200https://ask.sagemath.org/question/48013/defining-q-binomial-coefficients-binomnk_q-symbolic-in-n-k/?answer=48025#post-id-48025Comment by joakim_uhlin for <p>If you want to make a symbolic sum then all the terms should be symbolic. Your example does not work because <code>qbin(n,k)</code> is not defined for symbolic <code>n</code>.</p>
<p>What you can do is to make <code>qbin</code> a symbolic function with custom typesetting:</p>
<pre><code>var('q')
def qbin_latex(self, n, k):
return '{' + str(n) + ' \choose ' + str(k) + '}_{' + str(q) + '}'
qbin = function('qbin', nargs=2, print_latex_func=qbin_latex)
var('k,n')
show(sum(qbin(n,k),k,0,n))
</code></pre>
<p>$$\sum_{k=0}^{n} {n \choose k}_{q}$$</p>
<p>To check the identities that you are interested in, you will probably have to do some substitutions by hand, and/or pass an <code>evalf_func</code> parameter to <code>function</code> in the definition of <code>qbin</code>. Have a look at the <a href="http://doc.sagemath.org/html/en/reference/calculus/sage/calculus/var.html#sage.calculus.var.function">documentation for symbolic functions</a>.</p>
https://ask.sagemath.org/question/48013/defining-q-binomial-coefficients-binomnk_q-symbolic-in-n-k/?comment=48030#post-id-48030I will take a look at the documentation and try to make sense of it. What I am really looking for is a way to verify an identity of the form RHS(q,n,k)=LHS(q,n,k), involving some q-binomial coefficients that depend on n and k. Something like the [q-binomial theorem](http://mathworld.wolfram.com/q-BinomialTheorem.html). It seems to me that evalf_func evaluates a function numerically, so I could only confirm the identity for many values of q, n, k which is not exactly what I want.Sun, 22 Sep 2019 18:16:36 +0200https://ask.sagemath.org/question/48013/defining-q-binomial-coefficients-binomnk_q-symbolic-in-n-k/?comment=48030#post-id-48030