Bug#1055815: python3-sympy: nsimplify() fails with: KeyError: 'extended_nonnegative'

Kingsley G. Morse Jr. kingsley at loaner.com
Sat Nov 11 22:57:47 GMT 2023


Package: python3-sympy
Version: 1.12-6
Severity: normal

Thanks for maintaining Debian's cool python3-sympy
package.

If you happen to have the time, and are so
inclined, feel free to share any thoughts you may
happen to have on whether it might be improved to
not crash when asked to nsimplify() the
following...

    (1/(1 - (0.5*Y + 1)**1.5583266249))**6.5


I can replicate the crash with 

    python3-sympy   1.12-6

    python3         3.11.4-5+b1 

by copying and pasting the following line to a
bash shell prompt....

    $ python3 -c 'import sympy ; sympy.nsimplify(sympy.sympify("(1/(1 - (0.5*Y + 1)**1.5583266249))**6.5"))'


It seems to me it should not crash, but it fails
with

    Traceback (most recent call last):
      File "/usr/lib/python3/dist-packages/sympy/core/assumptions.py", line 499, in getit
        return self._assumptions[fact]
               ~~~~~~~~~~~~~~~~~^^^^^^
    KeyError: 'extended_nonnegative'

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/usr/lib/python3/dist-packages/sympy/simplify/simplify.py", line 1446, in nsimplify
        return _real_to_rational(expr, tolerance, rational_conversion)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3/dist-packages/sympy/simplify/simplify.py", line 1582, in _real_to_rational
        return p.subs(reps, simultaneous=True)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3/dist-packages/sympy/core/basic.py", line 1036, in subs
        return rv.xreplace(reps)
               ^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3/dist-packages/sympy/core/basic.py", line 1230, in xreplace
        value, _ = self._xreplace(rule)
                   ^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3/dist-packages/sympy/core/basic.py", line 1252, in _xreplace
        return self.func(*args), True
               ^^^^^^^^^^^^^^^^
      File "/usr/lib/python3/dist-packages/sympy/core/cache.py", line 72, in wrapper
        retval = cfunc(*args, **kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3/dist-packages/sympy/core/power.py", line 371, in __new__
        obj = b._eval_power(e)
              ^^^^^^^^^^^^^^^^
      File "/usr/lib/python3/dist-packages/sympy/core/power.py", line 471, in _eval_power
        elif re(b).is_extended_nonnegative and (abs(e) < 2) == True:
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3/dist-packages/sympy/core/assumptions.py", line 503, in getit
        return _ask(fact, self)
               ^^^^^^^^^^^^^^^^
      File "/usr/lib/python3/dist-packages/sympy/core/assumptions.py", line 559, in _ask
        fact_i_value = handler_i(obj)
                       ^^^^^^^^^^^^^^
      File "/usr/lib/python3/dist-packages/sympy/core/add.py", line 863, in _eval_is_extended_negative
        v = _monotonic_sign(a)
            ^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3/dist-packages/sympy/core/exprtools.py", line 76, in _monotonic_sign
        if not self.is_Add and self.as_numer_denom()[1].is_number:
                               ^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3/dist-packages/sympy/core/mul.py", line 1234, in as_numer_denom
        numers, denoms = list(zip(*[f.as_numer_denom() for f in self.args]))
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3/dist-packages/sympy/core/mul.py", line 1234, in <listcomp>
        numers, denoms = list(zip(*[f.as_numer_denom() for f in self.args]))
                                    ^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3/dist-packages/sympy/core/power.py", line 1584, in as_numer_denom
        return self.func(n, exp), self.func(d, exp)
                                  ^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3/dist-packages/sympy/core/cache.py", line 72, in wrapper
        retval = cfunc(*args, **kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3/dist-packages/sympy/core/power.py", line 371, in __new__
        obj = b._eval_power(e)
              ^^^^^^^^^^^^^^^^
      File "/usr/lib/python3/dist-packages/sympy/core/numbers.py", line 2351, in _eval_power
        x, xexact = integer_nthroot(abs(self.p), expt.q)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3/dist-packages/sympy/core/power.py", line 83, in integer_nthroot
        x, t = gmpy.iroot(y, n)
               ^^^^^^^^^^^^^^^^
    ValueError: n must be > 0


I think it's possible to do better.

Version 16.0.5-4 of another computer algebra
package called "mathomatic" does not crash on the
same example:

    $ mathomatic -e "(1/(1 - (0.5*Y + 1)**1.5583266249))**6.5" simplify

    1-> (1/(1 - (0.5*Y + 1)**1.5583266249))**6.5

                     1               13
    #1: ----------------------------^--
               Y                     2
        (1 - ((- + 1)^1.5583266249))
               2

    1-> simplify

                        1
    #1: ---------------------------------
                Y                     13
        ((1 - ((- + 1)^1.5583266249))^--)
                2                     2


Thank you and kind regards,
Kingsley

-- System Information:
Debian Release: trixie/sid
  APT prefers unstable-debug
  APT policy: (500, 'unstable-debug'), (500, 'unstable')
merged-usr: no
Architecture: i386 (i686)

Kernel: Linux 6.5.0-1-686-pae (SMP w/2 CPU threads; PREEMPT)
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8), LANGUAGE not set
Shell: /bin/sh linked to /bin/dash
Init: systemd (via /run/systemd/system)
LSM: AppArmor: enabled

Versions of packages python3-sympy depends on:
ii  python3         3.11.4-5+b1
ii  python3-mpmath  1.2.1-1

Versions of packages python3-sympy recommends:
ii  isympy-common  1.7.1-2
ii  python3-numpy  1:1.24.2-1
ii  python3-pil    9.4.0-1.1+b1

Versions of packages python3-sympy suggests:
pn  dvipng               <none>
pn  python-sympy-doc     <none>
pn  texlive-fonts-extra  <none>

-- no debconf information



More information about the debian-science-maintainers mailing list