Opened 11 years ago

# FP evaluation of symbolic sums fails — at Version 14

Reported by: Owned by: burcin burcin major sage-6.8 symbolics whuss, kcrisman, eviatarbach N/A

Symbolics sums returned from maxima cannot be numerically evaluated, since they don't define an `_evalf_()` method.

This was reported by dirkd on sage-support:

```Why is evaluating this expression problematical?

y1(x)=x^2;y2(x)=5-x;
a0=1;an=3;Delta=(an-a0)/n;p(k)=a0+(k-1/2)*Delta;
I(n)=sum(abs(y2(p(k))-y1(p(k)))*Delta,k,1,n);
N(I(10))

SAGE respons:
<snipped traceback>
File "expression.pyx", line 3797, in
sage.symbolic.expression.Expression.n (sage/symbolic/expression.cpp:
17022)
TypeError: cannot evaluate symbolic expression numerically
```

### comment:4 Changed 8 years ago by jdemeyer

• Milestone changed from sage-5.11 to sage-5.12

### comment:5 Changed 8 years ago by vbraun_spam

• Milestone changed from sage-6.1 to sage-6.2

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

• Milestone changed from sage-6.2 to sage-6.3

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

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

Your problem is threefold: 1. you use `n` both as sum endpoint and variable, 2. Sage `sum` is only intended with symbolic endpoints, and 3. the need to use Python summation may require defining a Python function instead of a Sage symbolic function:

```sage: sum?
...
Warning: This function only works with symbolic expressions. To sum any
other objects like list elements or function return values,

sage: I(n)=sum(abs(y2(p(k))-y1(p(k)))*Delta,k,1,n);
sage: I
n |--> 2*sum(abs(-4*k^2 - 3*(2*k - 1)*n + 3*n^2 + 4*k - 1), k, 1, n)/n^3

sage: def I(n):
....:     return (2*sum(abs(-4*k^2 - 3*(2*k - 1)*n + 3*n^2 + 4*k - 1) for k in range(1,n+1))/n^3)
....:
sage: I(10)
1301/250
sage: [I(i) for i in range(1,11)]
[2, 5, 46/9, 5, 26/5, 277/54, 254/49, 665/128, 418/81, 1301/250]
```

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

• Milestone changed from sage-duplicate/invalid/wontfix to sage-6.3
• Type changed from defect to enhancement

I don't think any of these invalidate the ticket; the point is to extend the behavior. Why is 1. a problem? This seems like it should be a nice function to me. See Burcin's reply in the thread:

```> If I leave out the N( )-operator on the last line the block evaluates
> to
>
>
> 1/500*sum(abs(-4*k^2 - 56*k + 329), k, 1, 10)
>
> which can be evaluated in a new inputbox. Why not in one step?
The result returned from maxima uses a symbolic function object created
on the fly. This is quite different from the sum() function
available on the command line, and unfortunately, it doesn't define a
numerical evaluation function, _evalf_().
```

Burcin knows this code very well, so I would be surprised if he misdiagnosed this. But I figure maybe changing to enhancement will appease everyone :)

### comment:9 Changed 7 years ago by nbruin

Burcin is spot-on:

```sage: S=(I(10).operands().operator()); S
sum
sage: type(S)
<class 'sage.symbolic.function_factory.NewSymbolicFunction'>
```

(Note the "New", not "BuiltIn?" or similar. It's a completely generic placeholder) we just need a symbolic function hooked up that can do some mildly intelligent evaluation when asked for it.

Incidentally, we can just map back to maxima and do the right thing there:

```sage: maxima_calculus(I(10))
('sum(abs(4*_SAGE_VAR_k^2+56*_SAGE_VAR_k-329),_SAGE_VAR_k,1,10))/500
sage: SR(maxima_calculus(I(10)).simplify_sum())
1301/250
```

(I haven't checked if it's correct). You can see why the "simplify_sum" is required: the newly created "sum" function in SR is linked to the inert "'sum".

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

• Status changed from needs_review to needs_work

### comment:11 Changed 7 years ago by vbraun_spam

• Milestone changed from sage-6.3 to sage-6.4

### comment:12 follow-up: ↓ 13 Changed 7 years ago by kcrisman

Maybe it's time we fixed this.

### comment:13 in reply to: ↑ 12 Changed 7 years ago by rws

• Description modified (diff)
• Summary changed from numerical evaluation of symbolic sums to FP evaluation of symbolic sums fails

Maybe it's time we fixed this.

It is not clear if forcing people to use `N()` and getting a float, even if there is an integer simplification of the sum, is the right thing to do. Granted, the error thrown on `N(I(10))` is a bug, and this ticket is about it. Here is a minimal example:

```sage: (k,n) = var('k,n')
sage: f(n)=sum(abs(-k*k+n),k,1,n)
sage: f(n=8)
sum(abs(-k^2 + 8), k, 1, 8)
sage: N(f(8))
```

However, I would expect `f(n=8).simplify()` or `.expand()` to give me the result `162`, and this is #17422

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

• Description modified (diff)
• Type changed from enhancement to defect
Note: See TracTickets for help on using tickets.