Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide symbolic sum function with evalf #9424

Closed
burcin opened this issue Jul 4, 2010 · 32 comments
Closed

Provide symbolic sum function with evalf #9424

burcin opened this issue Jul 4, 2010 · 32 comments

Comments

@burcin
Copy link

burcin commented Jul 4, 2010

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

Here is the thread:

http://groups.google.com/group/sage-support/t/615b15ca638c9652

See also #15346

Depends on #17759

CC: @sagetrac-whuss @kcrisman @eviatarbach

Component: symbolics

Author: Ralf Stephan

Branch/Commit: 4f7b161

Reviewer: Daniel Krenn

Issue created by migration from https://trac.sagemath.org/ticket/9424

@burcin burcin added this to the sage-5.11 milestone Jul 4, 2010
@burcin burcin self-assigned this Jul 4, 2010
@jdemeyer jdemeyer modified the milestones: sage-5.11, sage-5.12 Aug 13, 2013
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.1, sage-6.2 Jan 30, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.2, sage-6.3 May 6, 2014
@rwst
Copy link

rwst commented Jul 26, 2014

comment:7

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,
     please use python summation...

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]

@rwst rwst removed this from the sage-6.3 milestone Jul 26, 2014
@kcrisman
Copy link
Member

kcrisman commented Aug 1, 2014

comment:8

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 :)

@kcrisman kcrisman added this to the sage-6.3 milestone Aug 1, 2014
@nbruin
Copy link
Contributor

nbruin commented Aug 1, 2014

comment:9

Burcin is spot-on:

sage: S=(I(10).operands()[0].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".

@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.3, sage-6.4 Aug 10, 2014
@kcrisman
Copy link
Member

comment:12

A relevant ask.sagemath question.

Another one.

Maybe it's time we fixed this.

@rwst

This comment has been minimized.

@rwst
Copy link

rwst commented Dec 1, 2014

comment:13

Replying to @kcrisman:

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

@rwst rwst changed the title numerical evaluation of symbolic sums FP evaluation of symbolic sums fails Dec 1, 2014
@rwst

This comment has been minimized.

@rwst rwst added t: bug and removed t: enhancement labels Dec 4, 2014
@rwst
Copy link

rwst commented Feb 24, 2015

comment:26

Fixing these doctests will need the solutions found for fixing #17849 (or vice versa).

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 28, 2015

Branch pushed to git repo; I updated commit sha1. New commits:

5a1ac7eMerge branch 'develop' into t/9424/provide_symbolic_sum_function_with_evalf
9375510Merge branch 'develop' into t/17759/public/17759
16aa81d17759: handle hold=True and hypergeometric
1f4edf2Merge branch 'public/17759' of trac.sagemath.org:sage into t/9424/provide_symbolic_sum_function_with_evalf
27a16929424: delegate to superclass; fix doctest

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 28, 2015

Changed commit from 32bca6a to 27a1692

@rwst rwst modified the milestones: sage-6.4, sage-6.6 Feb 28, 2015
@rwst
Copy link

rwst commented May 25, 2015

Changed branch from u/rws/provide_symbolic_sum_function_with_evalf to u/rws/9424

@rwst
Copy link

rwst commented May 25, 2015

New commits:

fa7328517759: convenience class symbolic ExpressionTreeWalker(Converter)
b213d69Merge branch 'u/rws/provide_symbolic_sum_function_with_evalf' of trac.sagemath.org:sage into tmp2

@rwst
Copy link

rwst commented May 25, 2015

Changed commit from 27a1692 to b213d69

@rwst rwst modified the milestones: sage-6.6, sage-6.8 May 25, 2015
@mezzarobba
Copy link
Member

comment:31

TestsFailed (says the patchbot)

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 11, 2015

Branch pushed to git repo; I updated commit sha1. New commits:

985fdb4Merge branch 'develop' into t/9424/9424
4f7b1619424: make ceil/floor accept kwds; fix doctests

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 11, 2015

Changed commit from b213d69 to 4f7b161

@dkrenn
Copy link
Contributor

dkrenn commented Apr 30, 2016

Reviewer: Daniel Krenn

@dkrenn
Copy link
Contributor

dkrenn commented Apr 30, 2016

comment:34

LGTM

@vbraun
Copy link
Member

vbraun commented May 1, 2016

Changed branch from u/rws/9424 to 4f7b161

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants