ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 20 Aug 2019 15:42:42 -0500Checking identity of two combinatorial expressionshttp://ask.sagemath.org/question/47535/checking-identity-of-two-combinatorial-expressions/I have a reason to believe that a certain combinatorial identity holds for even integers $n, k$ that satisfies $2 \leq k \leq n/2$ and $n\geq 4$. To test it in Sage, I denote the expression on the right- and lefthandside as the functions RHS$(n,k)$ and LHS$(n,k)$ respectively and check if they agree for a variety of values $n$ and $k$.
def t(n,k):
sum=0
for i in range(n-2*k+1):
sum+=binomial(n-2*k,i)
return n/k*sum*binomial(n-4,2*k-4)*catalan_number(k-2)
def LHS(n,k):
return n/2*((n/2+1-k)/(n/2+1)*t(n/2+1,k/2)+2*(k/2+1)/(n/2+1)*t(n/2+1,k/2+1))
def RHS(n,k):
return (n/k)*(2**(n/2-k))*binomial(n/2-2,k-2)*binomial(k-2,k/2-1)
But when run the following code
for n in range(6,12,2):
for k in range(2,floor(n/2)+1,2):
print("n="+str(n)+", k="+str(k))
print(bool(LHS(n,k)==RHS(n,k)))
I get the output
n=6, k=2
True
n=8, k=2
True
n=8, k=4
True
n=10, k=2
True
n=10, k=4
False
which would indicate that the identity does not hold for $n=10$, $k=4$. However, when I write
print(bool(LHS(10,4)==RHS(10,4)))
I get the output TRUE (I also double checked this by hand, and both sides agree and are equal to $30$ in this case).
1. Why does the code in the double loop yield the wrong answer? EDIT: answered by rburing
2. Is there a better way to check equalities similar to this in Sage?joakim_uhlinTue, 20 Aug 2019 15:42:42 -0500http://ask.sagemath.org/question/47535/sage cannot get the simplest form on the limit expression with binomial and exponential operationshttp://ask.sagemath.org/question/25267/sage-cannot-get-the-simplest-form-on-the-limit-expression-with-binomial-and-exponential-operations/considering the flowing code:
————————————————————————
from sage.symbolic.assumptions import GenericDeclaration;
var('lamda,n,k,p');
decl1 = GenericDeclaration(k, 'integer');
decl1.assume();
assume(k>0,k>1,n>k,lamda>0,lamda<n);
ep=binomial(n,k)*p^k*(1-p)^(n-k);
ep=ep.subs_expr(p==lamda/n);
ep=ep.subs_expr(binomial(n,k)==factorial(n)/factorial(k)/factorial(n-k));
ep=limit(ep,n=oo);
ep.simplify_full();
————————————————————————
the result is:
lamda^k*limit((-(lamda - n)/n)^n*factorial(n)/((-lamda + n)^k*factorial(-k + n)), n, +Infinity)/factorial(k)
————————————————————————
it is not the simplest form. In fact , sage will give me the simplest form as below when I paste the result above to sage again.
————————————————————————
lamda^k*e^(-lamda)/factorial(k)
————————————————————————
the question is how can i get the simplest form directly?
Mr.GanChengSun, 14 Dec 2014 05:52:34 -0600http://ask.sagemath.org/question/25267/Sage Math Cloud Plotting Error, ValueError: cannot convert x to inthttp://ask.sagemath.org/question/31427/sage-math-cloud-plotting-error-valueerror-cannot-convert-x-to-int/ I'm using SageMathCloud to compute a summation
sum([.25^(100-i) * .75^(i) * binomial(100,i) for i in range(0, 63)])
0.00274614363169321
but am not able to plot it
plot(sum([.25^(100-i) * .75^(i) * binomial(100,i) for i in range(0, x)]),(x,1,63))
Error in lines 1-1
Traceback (most recent call last):
File "/projects/sage/sage-6.9/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 905, in execute
exec compile(block+'\n', '', 'single') in namespace, locals
File "", line 1, in <module>
File "sage/symbolic/expression.pyx", line 1084, in sage.symbolic.expression.Expression.__int__ (/projects/sage/sage-6.9/src/build/cythonized/sage/symbolic/expression.cpp:8329)
raise ValueError("cannot convert %s to int" % self)
ValueError: cannot convert x to int
What am I doing wrong here?Joshua BurkhartWed, 09 Dec 2015 02:58:40 -0600http://ask.sagemath.org/question/31427/Binomial Coefficients are inconsistent with Partition Coefficientshttp://ask.sagemath.org/question/28740/binomial-coefficients-are-inconsistent-with-partition-coefficients/In Sage:
binomial(-1,-1) = 0
I have complaint about this before: [ask-sage](http://ask.sagemath.org/question/24418/the-value-of-binomial-1-1-is/) and proposed the natural binomial(x,x) = 1 for all x.
I discussed the arguments in detail at sagemath-track where I opened a [ticket](http://trac.sagemath.org/ticket/17123).
One answer was: "Having binomial(z, z) != 1 is collateral damage."
There is also the damage of inconsistency.
For me inconsistency is what this code shows:
PartitionCoefficient = lambda p: mul(binomial(p[j], p[j+1]) for j in range(len(p)-1))
for n in (0..6):
for k in (0..n):
P = Partitions(n, max_part=k, inner=[k])
print sum(PartitionCoefficient(p) for p in P), binomial(n-1,k-1)
1 0
0 0
1 1
0 0
1 1
1 1
0 0
1 1
2 2
1 1
0 0
1 1
3 3
3 3
1 1
0 0
1 1
4 4
6 6
4 4
1 1
0 0
1 1
5 5
10 10
10 10
5 5
1 1
Another said: "Apparently Maple and Mathematica use the proposed
version. Is that sufficient justification to change our version?"
*Question:* Is the internal consistency of Sage enough justification?Peter LuschnyFri, 31 Jul 2015 07:18:25 -0500http://ask.sagemath.org/question/28740/Difference between sum and for loophttp://ask.sagemath.org/question/10669/difference-between-sum-and-for-loop/Hi,
I'm currently using sage to calculate some double sums over two variables, where s runs from 1 to t-1, and t runs from 2 to 2*x+2*n+1. I initially used two nested for loops inside the following function:
def qentr(i,j,k,x,n):
s,t,sm = var('s,t,sm')
sm = 0
for t in range(2,2*x+2*n+2):
for s in range(1,t):
sm = sm + h(i,s,k,x,n)*h(j,t,k,x,n) - h(j,s,k,x,n)*h(i,t,k,x,n)
return sm
where i, j, k, x, n are all nonnegative integers. This gives me the correct values for what I am enumerating, for instance qentr(1,3,3,1,2) = 96, which is right. For some reason when I replace these for loops with two nested sums, say,
sum(sum(h(i,s,k,x,n)*h(j,t,k,x,n)-h(i,t,k,x,n)*h(j,s,k,x,n),s,1,t-1),t,2,2*x+2*n+1)
this no longer gives me the correct values. If I replace the two nested loops with this sum I get qentr(1,3,3,1,2)=196....
I wanted to replace the loops with sum in order to return an expression in x, the only way I can think to do this is with sum but this does not yield the right expression. Does anyone know why this happens? Does anyone know an alternative way I can get the function qentr() to return a polynomial in x?
The function h from above is the following:
def h(i,j,k,x,n):
r = var('r')
return binomial(j-1,i-1)-2*sum(binomial(r+i-k,i-k)*binomial(j-i-r+k-2,k-2),r,x+n+(k+1)/2+1-i,j-i)
Cheers!tommytricksMon, 28 Oct 2013 07:04:05 -0500http://ask.sagemath.org/question/10669/Binomial in a Summationhttp://ask.sagemath.org/question/10384/binomial-in-a-summation/sage: var('k')
k
sage: sum(binomial(5,k),k,0,5)
I get an error "cannot convert k to int"
But if instead I try something like sum(binomial(n,k),k,0,n), everything is just fine.
Anyone know how I can get around this?david8381Thu, 25 Jul 2013 03:28:29 -0500http://ask.sagemath.org/question/10384/How do I generate a random number according to the binomial distribution?http://ask.sagemath.org/question/8848/how-do-i-generate-a-random-number-according-to-the-binomial-distribution/I would like to generate a random integer according to the binomial distribution. That is, I would like to generate a Bin(n,p) random value. That's number between 0 and n in which the probability we get the value k is `C(n,k)p^k(1-p)^(n-k)`.
Here is an inefficient method (which requires n calls to `random()` ):
def bin_rv(n,p=0.5):
"""
Generate a binomial random variable with parameters n,p.
"""
return sum( random() < p for _ in range(n))
Is there a better way to do this (already built into Sage, I hope)?
(In Matlab, this can be done via the `binord` function and in R with the `rbinom` function.)Ed ScheinermanMon, 02 Apr 2012 08:50:18 -0500http://ask.sagemath.org/question/8848/