_sympy_ methods for some parent classes
mkoeppe opened this issue · 29 comments
We add _sympy_ methods to various parent classes, returning sympy Integers, Reals, Complexes, ProductSet.
These methods override the generic _sympy_ method provided by #31938.
Part of #31926 Meta-ticket: Connect Sage sets to sympy sets
Depends on #31938
Depends on #31877
CC: @tscrim @kcrisman @certik @videlec
Component: interfaces
Author: Matthias Koeppe
Branch/Commit: 32cdd5c
Reviewer: Karl-Dieter Crisman
Issue created by migration from https://trac.sagemath.org/ticket/31931
New commits:
9fcf32e | Sets.CartesianProducts.ParentMethods, FreeModule_ambient, IntegerRing_class, InternalRealInterval, RealSet, NonNegativeIntegers, IntegerRing_class, PositiveIntegers, RationalField: Add `_sympy_` methods |
Author: Matthias Koeppe
Nice - if patchbot says yes this this seems to be good.
Question: Do these reimport into Sage as one might expect (does the diagram commute in either direction)? If so, some tests for that might be appropriate. Also, with the products, what happens if one of the parts doesn't have a Sympy version - presumably there is an error message, but is it a useful one?
Replying to @kcrisman:
Do these reimport into Sage as one might expect (does the diagram commute in either direction)?
No, the other direction of conversion is not implemented yet, this would be part of #31935. The closest we have is sage.interfaces.sympy.sympy_set_to_list, which converts from various sympy set types to lists of Sage SR relation expressions.
Replying to @kcrisman:
with the products, what happens if one of the parts doesn't have a Sympy version - presumably there is an error message, but is it a useful one?
It fails with an unpleasant error message because SymPy tries too hard to make sense of it. Example:
sage: F = FiniteEnumeratedSets().example()
sage: cartesian_product([F, F])._sympy_()
SymPyDeprecationWarning:
String fallback in sympify has been deprecated since SymPy 1.6. Use
sympify(str(obj)) or sympy.core.sympify.converter or obj._sympy_
instead. See https://github.com/sympy/sympy/issues/18066 for more
info.
SymPyDeprecationWarning(.....)
SyntaxError: invalid syntax (<string>, line 1)
During handling of the above exception, another exception occurred:
SympifyError: Sympify of expression 'could not parse 'An example of a finite enumerated set: {1,2,3}'' failed, because of exception being raised:
SyntaxError: invalid syntax (<string>, line 1)
#31938 will provide all Sage sets with a _sympy_ method.
Replying to @mkoeppe:
Replying to @kcrisman:
with the products, what happens if one of the parts doesn't have a Sympy version - presumably there is an error message, but is it a useful one?
It fails with an unpleasant error message because SymPy tries too hard to make sense of it. Example:
I agree this isn't as helpful to the end user. What I'd formally recommend as a review, until #31938 is merged, is to document this type of error as a doctest so it is at least searchable if someone comes across it.
Description changed:
---
+++
@@ -1,4 +1,6 @@
We add `_sympy_` methods to various parent classes, returning sympy `Integers`, `Reals`, `Complexes`, `ProductSet`.
+
+These methods override the generic `_sympy_` method provided by #31938.
Part of #31926 Meta-ticket: Connect Sage sets to sympy sets
Now, after merging #31938, this example works:
sage: F = FiniteEnumeratedSets().example()
sage: cartesian_product([F, F])._sympy_()
ProductSet(SageSet(An example of a finite enumerated set: {1,2,3}), SageSet(An example of a finite enumerated set: {1,2,3}))
Branch pushed to git repo; I updated commit sha1. New commits:
93fbb2b | Call sympy_init in all added `_sympy_` methods |
6e5cac6 | sage.interfaces.sympy_wrapper, Sets.ParentMethods._sympy_: New |
3cac256 | sage.interfaces.sympy_wrapper: Add doctests |
eef604e | SageSet: Finish docstrings; handle symbolic _contains |
2baae58 | Sets.ParentMethods._sympy_: Call sympy_init |
153b3e5 | Merge #31938 |
Now, after merging #31938, this example works:
Nice. I am not reviewing that one, but anyway that was my only comment on this one.
sage -t --long --random-seed=0 src/sage/sets/real_set.py # 7 doctests failed
because there is no ambient() method called via the is_universe() method.
Now to wait for the patchbot one more time.
Reviewer: Karl-Dieter Crisman
Patchbots seem stuck on #31928,
see #31928 comment:1.
Setting to positive as per comment:13.
Thanks!
Changed branch from u/mkoeppe/_sympy__methods_for_some_parent_classes to 32cdd5c