sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.45k stars 481 forks source link

asymptotic_expansions.SingularityAnalysis fails with some parameters #34028

Open mezzarobba opened 2 years ago

mezzarobba commented 2 years ago
sage: asymptotic_expansions.SingularityAnalysis('n', alpha=2/5, beta=1, precision=2)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/co/sage/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/sage/misc/cachefunc.pyx in sage.misc.cachefunc.CachedFunction.__call__ (build/cythonized/sage/misc/cachefunc.c:6061)()
    995             try:
--> 996                 return self.cache[k]
    997             except TypeError:  # k is not hashable

KeyError: ((0, 1), ())

During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
<ipython-input-3-0851315862c4> in <module>
----> 1 asymptotic_expansions.SingularityAnalysis('n', alpha=Integer(2)/Integer(5), beta=Integer(1), precision=Integer(2))

~/co/sage/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/sage/rings/asymptotic/asymptotic_expansion_generators.py in SingularityAnalysis(var, zeta, alpha, beta, delta, precision, normalized)
   1031         for (k, r) in it:
   1032             result += binomial(beta, r) * \
-> 1033                 sum(L[(k, ell)] * (-1)**ell *
   1034                     inverse_gamma_derivative(ell, r)
   1035                     for ell in srange(k, 2*k+1)

~/co/sage/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/sage/rings/asymptotic/asymptotic_expansion_generators.py in <genexpr>(.0)
   1032             result += binomial(beta, r) * \
   1033                 sum(L[(k, ell)] * (-1)**ell *
-> 1034                     inverse_gamma_derivative(ell, r)
   1035                     for ell in srange(k, 2*k+1)
   1036                     if (k, ell) in L) * \

~/co/sage/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/sage/misc/cachefunc.pyx in sage.misc.cachefunc.CachedFunction.__call__ (build/cythonized/sage/misc/cachefunc.c:6189)()
    999                 return self.cache[k]
   1000         except KeyError:
-> 1001             w = self.f(*args, **kwds)
   1002             self.cache[k] = w
   1003             return w

~/co/sage/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/sage/rings/asymptotic/asymptotic_expansion_generators.py in inverse_gamma_derivative(shift, r)
    945                 result = iga*falling_factorial(alpha-1, shift)
    946             else:
--> 947                 result = limit((1/gamma(s)).diff(s, r), s=alpha-shift)
    948
    949             try:

~/co/sage/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/sage/calculus/calculus.py in limit(ex, dir, taylor, algorithm, **argv)
   1415     if algorithm == 'maxima':
   1416         if dir is None:
-> 1417             l = maxima.sr_limit(ex, v, a)
   1418         elif dir in dir_plus:
   1419             l = maxima.sr_limit(ex, v, a, 'plus')

~/co/sage/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/sage/interfaces/maxima_lib.py in sr_limit(self, expr, v, a, dir)
    985             elif dir == "minus":
    986                 L.append(max_minus)
--> 987             return max_to_sr(maxima_eval(([max_limit], L)))
    988         except RuntimeError as error:
    989             s = str(error)

~/co/sage/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/sage/interfaces/maxima_lib.py in max_to_sr(expr)
   1708             op=max_op_dict[op_max]
   1709         max_args=cdr(expr)
-> 1710         args=[max_to_sr(a) for a in max_args]
   1711         return op(*args)
   1712     elif expr.symbolp():

~/co/sage/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/sage/interfaces/maxima_lib.py in <listcomp>(.0)
   1708             op=max_op_dict[op_max]
   1709         max_args=cdr(expr)
-> 1710         args=[max_to_sr(a) for a in max_args]
   1711         return op(*args)
   1712     elif expr.symbolp():

~/co/sage/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/sage/interfaces/maxima_lib.py in max_to_sr(expr)
   1708             op=max_op_dict[op_max]
   1709         max_args=cdr(expr)
-> 1710         args=[max_to_sr(a) for a in max_args]
   1711         return op(*args)
   1712     elif expr.symbolp():

~/co/sage/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/sage/interfaces/maxima_lib.py in <listcomp>(.0)
   1708             op=max_op_dict[op_max]
   1709         max_args=cdr(expr)
-> 1710         args=[max_to_sr(a) for a in max_args]
   1711         return op(*args)
   1712     elif expr.symbolp():

~/co/sage/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/sage/interfaces/maxima_lib.py in max_to_sr(expr)
   1708             op=max_op_dict[op_max]
   1709         max_args=cdr(expr)
-> 1710         args=[max_to_sr(a) for a in max_args]
   1711         return op(*args)
   1712     elif expr.symbolp():

~/co/sage/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/sage/interfaces/maxima_lib.py in <listcomp>(.0)
   1708             op=max_op_dict[op_max]
   1709         max_args=cdr(expr)
-> 1710         args=[max_to_sr(a) for a in max_args]
   1711         return op(*args)
   1712     elif expr.symbolp():

~/co/sage/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/sage/interfaces/maxima_lib.py in max_to_sr(expr)
   1702                 op=sage_expr.operator()
   1703             if op in sage_op_dict:
-> 1704                 raise RuntimeError("Encountered operator mismatch in maxima-to-sr translation")
   1705             max_op_dict[op_max]=op
   1706             sage_op_dict[op]=op_max

RuntimeError: Encountered operator mismatch in maxima-to-sr translation

CC: @cheuberg @behackl @dkrenn

Component: asymptotic expansions

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

behackl commented 2 years ago
comment:2

Looks like some sort of problem with limit and psi:

sage: expr = (1/gamma(x)).diff(2)(x=2/5); expr
psi(2/5)^2/gamma(2/5) - psi(1, 2/5)/gamma(2/5)
sage: expr.limit(x=42)
Traceback (most recent call last):
...
RuntimeError: Encountered operator mismatch in maxima-to-sr translation
sage: psi(x).limit(x=42)
-euler_gamma + 85691034670497533/19914562703599200
sage: psi(x).limit(x=2/5)
Traceback (most recent call last):
...
RuntimeError: Encountered operator mismatch in maxima-to-sr translation
sage: psi(2/5).limit(x=42)
Traceback (most recent call last):
...
RuntimeError: Encountered operator mismatch in maxima-to-sr translation
fchapoton commented 2 years ago
comment:3

Nevertheless, the following works

sage: f=psi(x)
sage: maxima(f).limit(x,5/4).sage()
-euler_gamma - 1/2*pi - 3*log(2) + 4
sage: maxima(f).limit(x,6/5).sage()
-euler_gamma - 1/4*(sqrt(5) + 1)*log(1/2*sqrt(5) + 5/2) + 1/4*(sqrt(5) - 1)*log(-1/2*sqrt(5) + 5/2) - 1/10*pi*sqrt(10*sqrt(5) + 25) - log(5) + 5

and

sage: maxima(f).limit(x,2/5)
(2*cos((8*%pi)/5)*log(2-2*cos((4*%pi)/5)) +2*cos((4*%pi)/5)*log(2-2*cos((2*%pi)/5))-%pi*cot((2*%pi)/5)-2*log(5) -2*%gamma) /2
sage: maxima(f).limit(x,2/5).sage()
-euler_gamma + 1/4*(sqrt(5) - 1)*log(1/2*sqrt(5) + 5/2) - 1/4*(sqrt(5) + 1)*log(-1/2*sqrt(5) + 5/2) - 1/10*pi*sqrt(-10*sqrt(5) + 25) - log(5)
fchapoton commented 12 months ago

trying to pinpoint the issue, one finds

sage: from sage.interfaces.maxima_lib import *
sage: bad = maxima_eval('((%COT SIMP) ((MTIMES SIMP) ((RAT SIMP) 1 5) $%PI))')
sage: max_to_sr(bad)
fchapoton commented 12 months ago

and then

ipdb> p maxima(expr)
cot(%pi/5)
ipdb> p expr
<ECL: ((%COT SIMP) ((MTIMES SIMP) ((RAT SIMP) 1 5) $%PI))>
ipdb> p SR(maxima(expr))
1/5*sqrt(10*sqrt(5) + 25)

so this is an issue of not recognizing the baby after it has been simplified...