sagemath / sage

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

Meta-ticket: Use the SymPy assumptions facility #31958

Open mkoeppe opened 3 years ago

mkoeppe commented 3 years ago

SymPy has a clean and general design for assumptions. https://docs.sympy.org/latest/modules/assumptions/index.html

We should connect to it. Based on #31926 (#24171, #31931, #31938), we should be able to express everything in it that Sage's assumptions can do, so perhaps we can replace what we have in Sage completely by SymPy.

Tickets:

Symptoms:

CC: @egourgoulhon

Component: symbolics

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

mkoeppe commented 3 years ago

Description changed:

--- 
+++ 
@@ -11,4 +11,5 @@
 - #27998 Solving a single equation for multiple variables doesn't work with assumptions
 - #29938 comparison with infinity does not take assumptions into account
 - #25972 solve does not obey assumptions for inequalities
-
+- #30793 Sage may ignore the imaginary part of variables not explicitly declared complex
+- see also https://trac.sagemath.org/wiki/symbolics
mkoeppe commented 3 years ago

Description changed:

--- 
+++ 
@@ -11,5 +11,6 @@
 - #27998 Solving a single equation for multiple variables doesn't work with assumptions
 - #29938 comparison with infinity does not take assumptions into account
 - #25972 solve does not obey assumptions for inequalities
+- #24334 sympy symbol -> `SR` drops assumptions
 - #30793 Sage may ignore the imaginary part of variables not explicitly declared complex
 - see also https://trac.sagemath.org/wiki/symbolics
mkoeppe commented 3 years ago

Description changed:

--- 
+++ 
@@ -1,5 +1,7 @@
 SymPy has a clean and general design for assumptions.
 https://docs.sympy.org/latest/modules/assumptions/index.html
+
+- https://docs.sympy.org/latest/modules/assumptions/assume.html?highlight=assumptionscontext#sympy.assumptions.assume.AssumptionsContext

 We should connect to it. Based on #31926 (#24171, #31931, #31938), we should be able to express everything in it that Sage's assumptions can do, so perhaps we can replace what we have in Sage completely by SymPy.
mkoeppe commented 3 years ago
comment:6

One issue with using sympy for the implementation is that loading it would add quite a bit to the start up time:

$ time ./sage -c quit

real    0m1.620s
user    0m1.249s
sys 0m0.450s
$ time ./sage -c "import sympy"

real    0m1.980s
user    0m1.570s
sys 0m0.514s
williamstein commented 1 year ago

Here is a closely related example to #27998, though it is a bit worse, since instead of an error, you get the wrong answer.

var("x,y")
assume(x>0)
assume(y>0)
print("x^2=1 and y^2=1")
show(solve([x^2==1, y^2==1], x, y))

OUTPUT:

(x, y)
x^2=1 and y^2=1
[[x == 1, y == 1], [x == -1, y == 1], [x == 1, y == -1], [x == -1, y == -1]]

Then answer should be [[x == 1, y == 1]].

Note that sympy of course works perfectly fine for this problem, so this is a vote for this ticket:

import sympy
x = sympy.Symbol('x', positive=True)
y = sympy.Symbol('y', positive=True)
sympy.solve([x^2-1, y^2-1], x, y)

OUTPUT: 
[(1, 1)]

More details: https://cocalc.com/wstein/support/assumptions/files/bug.sagews