Opened 11 years ago

Closed 6 years ago

# symbolic sum gives wrong answer

Reported by: Owned by: Henryk.Trappmann burcin major sage-6.5 calculus Ralf Stephan, Karl-Dieter Crisman Karl-Dieter Crisman, Ralf Stephan N/A b8569fe b8569fe48be69b143ea64205cb6f018547b67805

### Description

```sage: (n,k,j)=var('n,k,j')
sage: sum(binomial(n,k)*binomial(k-1,j)*(-1)**(k-1-j),k,j+1,n)
0
sage: (n,j)=(5,3)
sage: sum(binomial(n,k)*binomial(k-1,j)*(-1)**(k-1-j) for k in range(j
+1,n+1))
1
```

The above sum should be 1 for n>=j and 0 otherwise.

From kcrisman: This appears to be a bug in Maxima.

```(%i1) load(simplify_sum);
<snip>
(%i3) simplify_sum(sum(binomial(n,k)*binomial(k-1,j)*(-1)**(k-1-j),k,j
+1,n));

(%o3)                                  0
(%i4) simplify_sum(sum(binomial(5,k)*binomial(k-1,3)*(-1)**(k-1-3),k,
4,5));
(%o4)                                  1
(%i5) 5*1*1+1*4*(-1);
(%o5)                                  1
```

### comment:1 Changed 11 years ago by kcrisman

• Component changed from basic arithmetic to calculus
• Owner changed from AlexGhitza to burcin
• Report Upstream changed from N/A to Reported upstream. Developers acknowledge bug.

This is now Maxima bug 3036579.

### comment:2 Changed 10 years ago by kcrisman

Maxima 5.23.2 still has this, and no movement on the original bug report.

### comment:3 Changed 9 years ago by kcrisman

• Report Upstream changed from Reported upstream. Developers acknowledge bug. to Fixed upstream, in a later stable release.

The bug report got updated over a year ago.

In the current Sage:

```(%i1) load(simplify_sum);
(%o1) /Users/karl-dietercrisman/Downloads/sage-5.0/local/share/maxima/5.26.0/s\
hare/contrib/solve_rec/simplify_sum.mac
(%i2) simplify_sum(sum(binomial(n,k)*binomial(k-1,j)*(-1)**(k-1-j),k,j +1,n));

Is  j + 1  positive, negative, or zero?

pos;
(%o2)                                  1
```

So just need a doctest.

```sage: (n,k,j)=var('n,k,j')
sage: sum(binomial(n,k)*binomial(k-1,j)*(-1)**(k-1-j),k,j+1,n)
-sum((-1)^(-j + k)*binomial(k - 1, j)*binomial(n, k), k, j + 1, n)
sage: assume(j>-1)
sage: sum(binomial(n,k)*binomial(k-1,j)*(-1)**(k-1-j),k,j+1,n)
1
sage: forget()
sage: assume(n>=j)
sage: sum(binomial(n,k)*binomial(k-1,j)*(-1)**(k-1-j),k,j+1,n)
-sum((-1)^(-j + k)*binomial(k - 1, j)*binomial(n, k), k, j + 1, n)
sage: forget()
sage: assume(j==-1)
sage: sum(binomial(n,k)*binomial(k-1,j)*(-1)**(k-1-j),k,j+1,n)
1
sage: forget()
sage: assume(j<-1)
sage: sum(binomial(n,k)*binomial(k-1,j)*(-1)**(k-1-j),k,j+1,n)
-sum((-1)^(-j + k)*binomial(k - 1, j)*binomial(n, k), k, j + 1, n)
```

Was the original report here wrong? Maxima currently says that the sign of `j+1` is all that matters, which sort of makes sense

### comment:4 Changed 6 years ago by rws

• Milestone set to sage-duplicate/invalid/wontfix
• Status changed from new to needs_review

Now it's no longer solved at all, i.e., the sum is returned. Since there is no longer an erroneous result the ticket can be closed I think.

### comment:5 Changed 6 years ago by kcrisman

• Status changed from needs_review to needs_info

All closures like this should be doctested - in case the bad behavior returns.

### comment:6 Changed 6 years ago by rws

• Branch set to u/rws/symbolic_sum_gives_wrong_answer

### comment:7 Changed 6 years ago by rws

• Authors set to Ralf Stephan
• Commit set to 5479dd1c8e524759f71933421e4557ac2d36189b
• Milestone changed from sage-duplicate/invalid/wontfix to sage-6.5
• Report Upstream changed from Fixed upstream, in a later stable release. to N/A
• Status changed from needs_info to needs_review

New commits:

 ​5479dd1 `9635: doctest`

### comment:8 Changed 6 years ago by kcrisman

• Authors changed from Ralf Stephan to Ralf Stephan, Karl-Dieter Crisman
• Branch changed from u/rws/symbolic_sum_gives_wrong_answer to u/kcrisman/ticket/9635
• Commit changed from 5479dd1c8e524759f71933421e4557ac2d36189b to b8569fe48be69b143ea64205cb6f018547b67805
• Reviewers set to Karl-Dieter Crisman

Actually, in this case the doctest I pasted above is absolutely correct. One still has to assume the right thing for it to return anything other than the original sum! Your part is fine, you can just review the additional ones (unless you think they are too much).

New commits:

 ​b8569fe `Add full doctest`

### comment:9 Changed 6 years ago by rws

Somehow I still expect results like `a (for x<0); b (for x==0); c (else)` but I digress...

### comment:10 Changed 6 years ago by rws

• Reviewers changed from Karl-Dieter Crisman to Karl-Dieter Crisman, Ralf Stephan
• Status changed from needs_review to positive_review

Tests OK, is fine.

### comment:11 Changed 6 years ago by kcrisman

Somehow I still expect results like a (for x<0); b (for x==0); c (else) but I digress...

That isn't possible with the current Maxima setup (at least not in a useful way, given the crazy number of branches Maxima gives us) but perhaps via sympy? That would be a very, very good improvement.

### comment:12 Changed 6 years ago by vbraun

• Branch changed from u/kcrisman/ticket/9635 to b8569fe48be69b143ea64205cb6f018547b67805
• Resolution set to fixed
• Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.