sagemath / sage

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

Improve interface to SymPy solvers #24142

Open rwst opened 6 years ago

rwst commented 6 years ago

At the moment with solve(...,algorithm='sympy') the SymPy function solveset is called for single expressions, and solve for systems. However, it turns out that solve can handle some single-expression tasks where solveset fails. This ticket should add code that analyzes solveset output and calls solve in case no solution was found. This probably includes simplifying the returned ConditionSets.

References:

Depends on #24171

CC: @mforets

Component: symbolics

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

rwst commented 6 years ago

Dependencies: #22322

rwst commented 6 years ago
comment:2

These sets can be returned by solveset: FiniteSet, EmptySet, Interval, Intersection, Union, ConditionSet, ImageSet, Complement, plus the algebraic sets of numbers like ZZ, RR, CC. We can directly translate FiniteSet, EmptySet, Interval. ConditionSet is a AND-combination of a relation and a set, so this can be translated into list format too if the set is finite/interval. Often RR/CC are given as set, and they may be redundant (CC is default with no domain= option given, and RR is implicated with any inequality as condition).

rwst commented 6 years ago
comment:3

Should we translate to sage.set as optional output?

rwst commented 6 years ago
comment:4

Let's use sage.set when we can't express the solveset result as list, and later support an option to always use sage.set. As to calling solve do this when EmptySet or the original expression is returned.

rwst commented 6 years ago
comment:5

Possibilities:

A further problem is that output is different between SymPy's solve and solveset. The relation/boolean notation of solve can be translated to set notation for the reals however (see #24156).

rwst commented 6 years ago
comment:6

Replying to @rwst:

ConditionSet is a AND-combination of a relation and a set, so this can be translated into list format too if the set is finite/interval.

Not so, it is a AND-combination of a relation and an element statement, i.e. symbol in set. This means it cannot be expressed in list format.

rwst commented 6 years ago

Changed dependencies from #22322 to #22322, #24171

rwst commented 6 years ago
comment:8

As an example this result set from SymPy:

In [9]: solveset(abs(x) - n, x, S.Reals)
Out[9]: ([0, ∞) ∩ {n}) ∪ ((-∞, 0] ∩ {-n})

is with #24156 and #24171 constructible in Sage:

sage: from sage.sets.set import Set_object_union, Set_object_intersection
sage: _ = var('n')
sage: Set_object_union(Set_object_intersection(RealSet(x>=0),Set([n])), Set_obje
....: ct_intersection(RealSet(x<=0),Set([-n])))
Set-theoretic union of Set-theoretic intersection of [0, +oo) and {n} and Set-theoretic intersection of (-oo, 0] and {-n}
kcrisman commented 3 years ago
comment:10

See possibly also #31926 or some of its constituent tickets.

mkoeppe commented 3 years ago

Changed dependencies from #22322, #24171 to #24171

mkoeppe commented 2 years ago

Description changed:

--- 
+++ 
@@ -1 +1,4 @@
 At the moment with `solve(...,algorithm='sympy')` the SymPy function `solveset` is called for single expressions, and `solve` for systems. However, it turns out that `solve` can handle some single-expression tasks where `solveset` fails. This ticket should add code that analyzes `solveset` output and calls `solve` in case no solution was found. This probably includes simplifying the returned `ConditionSet`s.
+
+References:
+- https://groups.google.com/g/sympy/c/v_YLkX4QuRY/m/50uHEGbHBAAJ (Mar 2022)