Extend the limit function/method call syntax ?

48 views
Skip to first unread message

Emmanuel Charpentier

unread,
Oct 3, 2024, 11:44:12 AM10/3/24
to sage-support

Motivation in this ask.sagemath.org question.

sage: X=var("x", n=3) sage: F=sum(X) ; F x0 + x1 + x2 sage: F.limit(x0=3) x1 + x2 + 3

So far, so good. But

sage: F.limit(X[0]=3) Cell In[9], line 1 F.limit(X[Integer(0)]=Integer(3)) ^ SyntaxError: expression cannot contain assignment, perhaps you meant "=="?

Indeed, the current limit function and method get their arguments (variable and value) by analysing a single named argument, whose name must be a literal. From limit?? :

def limit(ex, dir=None, taylor=False, algorithm='maxima', **argv):

[ Docstring elided ]

if not isinstance(ex, Expression): ex = SR(ex) if len(argv) != 1: raise ValueError("call the limit function like this, e.g. limit(expr, x= 2).") else: k, = argv.keys() v = var(k) a = argv[k]

[ Then proceeds to keyword management. ]

This call syntax :

  • must be kept for reverse compatibility’s sake, but
  • must be extended to accept at minimum limit(variable, value, ...) syntax.

But we may take inspiration from the subs call syntax, which accepts more flexible calls ; for example, a “dictionary” argument may be useful to implement multi-variable limits.

Advice ?

Nils Bruin

unread,
Oct 3, 2024, 3:29:50 PM10/3/24
to sage-support
On Thursday 3 October 2024 at 04:44:12 UTC-7 Emmanuel Charpentier wrote:
sage: F.limit(X[0]=3) Cell In[9], line 1 F.limit(X[Integer(0)]=Integer(3)) ^ SyntaxError: expression cannot contain assignment, perhaps you meant "=="?

Indeed, the current limit function and method get their arguments (variable and value) by analysing a single named argument, whose name must be a literal. From limit?? :

The restriction here is python's processing of optional arguments. These must have simple names.  You can already use
"""
limit(x0,**{str(X[0]):1})
"""
now, which isn't beautiful but at least it establishes the function. Allowing an explicit dictionary argument would likely open up a whole slew of other issues. It looks to me like we're missing a very simple form for limit that should be the primitive to all of this:
limit( f, x, 1)
i.e.: expression to take limit of, variable with respect to take limit, limit value. Then one could just write limit( F, X[0], 0) and one would get the default processing of arguments for python for free. We wouldn't rely on the programmatic pun of using python identifiers to stand in for our symbolic variables.
Further convenience routines that use syntactic sugar to make things resemble more mathematical notation can then be built on top, but at least the general primitive is available underneath.
It may be hard to figure out a way to make such a routine available under an easily found name while remaining compatible with what exists now.

Nils Bruin

unread,
Oct 3, 2024, 4:07:29 PM10/3/24
to sage-support
The function in question is sort-of available as sage.functions.other.symbolic_limit (which is an inert form, so it requires a further "simplify").

Nils Bruin

unread,
Oct 7, 2024, 1:00:11 AM10/7/24
to sage-support
Following up on this, I think we can do this with relatively small impact:


previously,

limit(1/x,'+',x=0)

did work, but the recommended way to spell this was

limit(1/x,x=0,dir="+")

so I'm not sure if we need to provide a special case and intercept this.


Reply all
Reply to author
Forward
0 new messages