Extended sympy interface in SageMath 9.4

77 views
Skip to first unread message

Matthias Köppe

unread,
Aug 24, 2021, 7:48:34 PM8/24/21
to sympy
Some here on the list may be interested in SageMath's extended sympy interface merged in the just-released Sage version 9.4. 

In particular, all mathematical sets and algebraic structures ("Parents") of SageMath are now accessible to SymPy by way of a wrapper class SageSet, which implements the SymPy Set API.


Oscar Benjamin

unread,
Aug 24, 2021, 8:21:02 PM8/24/21
to sympy
Hi Matthias,

That's excellent.

Is there a way to run those same examples from Python or do they need
to be run from the SageMath application?

Also since SymPy switched from Travis to GitHub Actions for CI I
haven't figured out how to make a setup that tests the latest master
SymPy as part of Sage. Currently I don't think we have any CI tests
for this. Do you have an idea how to set that up?

Oscar
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/da0fcd9c-0990-4e0d-a378-faee02e97673n%40googlegroups.com.

Matthias Köppe

unread,
Aug 24, 2021, 8:48:32 PM8/24/21
to sympy
On Tuesday, August 24, 2021 at 5:21:02 PM UTC-7 Oscar wrote:
Is there a way to run those same examples from Python or do they need
to be run from the SageMath application?

Only one of the examples shown in the release notes uses Sage's preparser syntax. So when Sage is installed, you can also use plain python for the same functionality.  Here is an example session in plain python:

$ ./sage -python

Python 3.9.6 (default, Jun 29 2021, 05:25:02)
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
from sage.all import var, cos, sin, matrix, pi

>>> x, y = var('x y')
>>> M = matrix([[sin(x), cos(x)], [-cos(x), sin(x)]]); M
[ sin(x)  cos(x)]
[-cos(x)  sin(x)]
 >>> sM = M._sympy_(); sM
Matrix([
[ sin(x), cos(x)],
[-cos(x), sin(x)]])
>>> sM.subs(x, pi/4)           # computation in SymPy
Matrix([
[ sqrt(2)/2, sqrt(2)/2],
[-sqrt(2)/2, sqrt(2)/2]])

>>> from sage.all import WeylGroup
>>> W = WeylGroup(["A",1,1])
>>> sW = W._sympy_(); sW
SageSet(Weyl Group of type ['A', 1, 1] (as a matrix group acting on the root space))
>>> sW.is_finite_set
False
>>> sW.is_iterable
True
>>> sB3 = WeylGroup(["B", 3])._sympy_(); sB3
SageSet(Weyl Group of type ['B', 3] (as a matrix group acting on the ambient space))
>>> len(sB3)
48



Matthias Köppe

unread,
Aug 24, 2021, 8:54:21 PM8/24/21
to sympy
On Tuesday, August 24, 2021 at 5:21:02 PM UTC-7 Oscar wrote:

Also since SymPy switched from Travis to GitHub Actions for CI I
haven't figured out how to make a setup that tests the latest master
SymPy as part of Sage. Currently I don't think we have any CI tests
for this. Do you have an idea how to set that up?


Several projects upstream from Sage have accepted PRs that add integration/portability testing using Sage's GH Actions infrastructure. Take a look at https://github.com/wbhart/flint2/blob/flint-2.8/.github/workflows/ci-sage.yml
If this looks like something that you'd like for SymPy, let me know; I can send you a PR.


Oscar Benjamin

unread,
Aug 24, 2021, 9:32:51 PM8/24/21
to sympy
I just downloaded SageMath 9.4 on OSX from here:
https://github.com/3-manifolds/Sage_macOS/releases

I ran most of the examples fine but there was a problem with the Weyl group example:

sage: from sage.all import WeylGroup                                            

sage: W = WeylGroup(["A",1,1])                                                  

Assertion failed: ((STATE_SLOTS_SIZE - StateNextFreeOffset) >= size), function RegisterModuleState, file src/modules.c, line 106.

---------------------------------------------------------------------------

KeyError                                  Traceback (most recent call last)

/var/tmp/sage-9.4-current/local/lib/python3.9/site-packages/sage/misc/cachefunc.pyx in sage.misc.cachefunc.CachedFunction.__call__ (build/cythonized/sage/misc/cachefunc.c:5974)()

    995             try:

--> 996                 return self.cache[k]

    997             except TypeError:  # k is not hashable


/var/tmp/sage-9.4-current/local/lib/python3.9/site-packages/sage/misc/weak_dict.pyx in sage.misc.weak_dict.WeakValueDictionary.__getitem__ (build/cythonized/sage/misc/weak_dict.c:3736)()

    703         if wr == NULL:

--> 704             raise KeyError(k)

    705         out = PyWeakref_GetObject(wr)


KeyError: ((<class 'sage.combinat.root_system.weyl_group.WeylGroup_gens'>, Root space over the Rational Field of the Root system of type ['A', 1, 1], None), ())


During handling of the above exception, another exception occurred:


RuntimeError                              Traceback (most recent call last)

<ipython-input-37-3528b02e71d0> in <module>

----> 1 W = WeylGroup(["A",Integer(1),Integer(1)])


/var/tmp/sage-9.4-current/local/lib/python3.9/site-packages/sage/misc/lazy_import.pyx in sage.misc.lazy_import.LazyImport.__call__ (build/cythonized/sage/misc/lazy_import.c:4036)()

    360             True

    361         """

--> 362         return self.get_object()(*args, **kwds)

    363 

    364     def __repr__(self):


/var/tmp/sage-9.4-current/local/lib/python3.9/site-packages/sage/combinat/root_system/weyl_group.py in WeylGroup(x, prefix, implementation)

    207     if ct.is_finite():

    208         return WeylGroup_gens(ct.root_system().ambient_space(), prefix=prefix)

--> 209     return WeylGroup_gens(ct.root_system().root_space(), prefix=prefix)

    210 

    211 


/var/tmp/sage-9.4-current/local/lib/python3.9/site-packages/sage/misc/classcall_metaclass.pyx in sage.misc.classcall_metaclass.ClasscallMetaclass.__call__ (build/cythonized/sage/misc/classcall_metaclass.c:1761)()

    320         """

    321         if cls.classcall is not None:

--> 322             return cls.classcall(cls, *args, **kwds)

    323         else:

    324             # Fast version of type.__call__(cls, *args, **kwds)


/var/tmp/sage-9.4-current/local/lib/python3.9/site-packages/sage/combinat/root_system/weyl_group.py in __classcall__(cls, domain, prefix)

    215     @staticmethod

    216     def __classcall__(cls, domain, prefix=None):

--> 217         return super(WeylGroup_gens, cls).__classcall__(cls, domain, prefix)

    218 

    219     def __init__(self, domain, prefix):


/var/tmp/sage-9.4-current/local/lib/python3.9/site-packages/sage/misc/cachefunc.pyx in sage.misc.cachefunc.CachedFunction.__call__ (build/cythonized/sage/misc/cachefunc.c:6100)()

    999                 return self.cache[k]

   1000         except KeyError:

-> 1001             w = self.f(*args, **kwds)

   1002             self.cache[k] = w

   1003             return w


/var/tmp/sage-9.4-current/local/lib/python3.9/site-packages/sage/structure/unique_representation.py in __classcall__(cls, *args, **options)

   1005             True

   1006         """

-> 1007         instance = typecall(cls, *args, **options)

   1008         assert isinstance( instance, cls )

   1009         if instance.__class__.__reduce__ == CachedRepresentation.__reduce__:


/var/tmp/sage-9.4-current/local/lib/python3.9/site-packages/sage/misc/classcall_metaclass.pyx in sage.misc.classcall_metaclass.typecall (build/cythonized/sage/misc/classcall_metaclass.c:2223)()

    484             TypeError: Argument 'cls' has incorrect type (expected type, got classobj)

    485     """

--> 486     return (<PyTypeObject*>type).tp_call(cls, args, kwds)

    487 

    488 # Class for timing::


/var/tmp/sage-9.4-current/local/lib/python3.9/site-packages/sage/combinat/root_system/weyl_group.py in __init__(self, domain, prefix)

    243                        for i in self.index_set()]

    244         from sage.libs.all import libgap

--> 245         libgap_group = libgap.Group(gens_matrix)

    246         degree = ZZ(self.domain().dimension())

    247         ring = self.domain().base_ring()


/var/tmp/sage-9.4-current/local/lib/python3.9/site-packages/sage/libs/gap/libgap.pyx in sage.libs.gap.libgap.Gap.__getattr__ (build/cythonized/sage/libs/gap/libgap.c:6806)()

    698 

    699         try:

--> 700             g = self.eval(name)

    701         except ValueError:

    702             raise AttributeError(f'No such attribute: {name}.')


/var/tmp/sage-9.4-current/local/lib/python3.9/site-packages/sage/libs/gap/libgap.pyx in sage.libs.gap.libgap.Gap.eval (build/cythonized/sage/libs/gap/libgap.c:4241)()

    397             gap_command = str(gap_command._libgap_init_())

    398 

--> 399         initialize()

    400         elem = make_any_gap_element(self, gap_eval(gap_command))

    401 


/var/tmp/sage-9.4-current/local/lib/python3.9/site-packages/sage/libs/gap/util.pyx in sage.libs.gap.util.initialize (build/cythonized/sage/libs/gap/util.c:5619)()

    287     argv[argc] = NULL

    288 

--> 289     sig_on()

    290     # Initialize GAP but disable their SIGINT handler

    291     GAP_Initialize(argc, argv, gasman_callback, error_handler,


RuntimeError: Aborted



--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.

Oscar Benjamin

unread,
Aug 24, 2021, 9:33:55 PM8/24/21
to sympy
I will take a look. That definitely sounds like something that we would want.


Oscar 

Matthias Köppe

unread,
Aug 24, 2021, 9:37:41 PM8/24/21
to sympy
On Tuesday, August 24, 2021 at 6:32:51 PM UTC-7 Oscar wrote:
I just downloaded SageMath 9.4 on OSX from here:
https://github.com/3-manifolds/Sage_macOS/releases

I ran most of the examples fine but there was a problem with the Weyl group example:

sage: from sage.all import WeylGroup                                            

sage: W = WeylGroup(["A",1,1])                                                  

Assertion failed: ((STATE_SLOTS_SIZE - StateNextFreeOffset) >= size), function RegisterModuleState, file src/modules.c, line 106.


Thanks for testing! I have forwarded this to the person who is making the mac binaries; I have not tried them myself yet.
 

Aaron Meurer

unread,
Aug 24, 2021, 9:39:45 PM8/24/21
to sympy
From the page: "also, Sage attaches _sage_ methods to various SymPy
classes, which provide the opposite conversion."

SymPy also has various _sage_ methods, but mainly only in the core.
Should we keep these in SymPy? It sounds like you are already adding a
bunch of your own, so maybe we should just move them all to the sage
codebase. We could also move them all to the SymPy codebase too, if
that sounds more preferable. I would assume having them in Sage is
better because that puts it closer to the Sage developers who
understand that code, and also the Sage CI that tests against the
latest development version of Sage.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/da0fcd9c-0990-4e0d-a378-faee02e97673n%40googlegroups.com.

Matthias Köppe

unread,
Aug 24, 2021, 9:53:23 PM8/24/21
to sympy
On Tuesday, August 24, 2021 at 6:39:45 PM UTC-7 asme...@gmail.com wrote:
From the page: "also, Sage attaches _sage_ methods to various SymPy
classes, which provide the opposite conversion."

SymPy also has various _sage_ methods, but mainly only in the core.
Should we keep these in SymPy? It sounds like you are already adding a
bunch of your own, so maybe we should just move them all to the sage
codebase. We could also move them all to the SymPy codebase too, if
that sounds more preferable. I would assume having them in Sage is
better 

Our file adding the methods apparently has been in Sage since Sage 8.1 (2017), https://trac.sagemath.org/ticket/24006, which references https://github.com/sympy/sympy/issues/13430

I would think that, unless more _sage_ methods have been added on the SymPy side, the _sage_ methods can just be removed from SymPy. There is no point trying to support Sage <= 8.0; because these old versions did not support Python 3 as far as I know.



Reply all
Reply to author
Forward
0 new messages