Using SymEngine in SymPy

134 views
Skip to first unread message

Isuru Fernando

unread,
Apr 28, 2016, 1:23:09 AM4/28/16
to sy...@googlegroups.com, symengine, py...@googlegroups.com
Hi,

Now that SymEngine has enough methods ported from sympy.core, it can be used in other parts of sympy.

For example, I've been adding methods to symengine.py to mirror the SymPy API so that `n_link_pendulum_on_cart` from PyDy can run without any conversions from SymEngine to SymPy and vice versa. We chose sympy.physics and PyDy because it uses less amount of things from other sympy modules.

For those who are unfamiliar let me explain how SymEngine and SymPy can interoperate. In almost all functions in sympy, sympify is called on inputs before doing anything. Symengine objects when sympified are converted into sympy and therefore can be used in those functions. Similarly in symengine, all methods call symengine's sympify which would convert sympy objects to symengine. One additional thing that symengine do is that when it goes through a sympy expression's tree and finds a function not defined in symengine like Ynm, then it will create a PyFunction object to wrap the Ynm object. Logic is there so that methods like subs, eval, hash, compare can fallback to Python.

This is okay, but doesn't really help with sympy's speed as symengine objects are converted to sympy as soon as a sympy function is called. To avoid these conversions, I've started a PR here. What it does is it looks for an environment variable USE_SYMENGINE and if found it uses symengine's methods instead of sympy methods.

import os
if os.environ.get('USE_SYMENGINE'):
    from symengine import sympify, diff, sin, cos, Matrix, symbols, UndefFunction as Function
else:
    from sympy import sympify, diff, sin, cos, Matrix, symbols, Function


Advantages over sympy is that symengine is faster. Symengine keeps objects in data structures that are used to creating them. For example, an Add keeps the dictionary of coefficients (output of expr.as_coefficients_dict()). This structure is used throughout symengine without flattening it to a list of Mul objects. So this leads to a better time complexity than sympy.

Also since symengine is written in C++, true threading is also possible. I've tried using OpenMP for embarrassingly parallel algorithms like taking the jacobian of a matrix and differentiating all elements of a matrix and it gives a good speedup for `n_link_pendulum_on_cart`.

One question I have is how do we test this? Is this something that should be tested in symengine or sympy? Since symengine is written in C++ and the default C++ compiler is different on different platforms, symengine is tested on Linux (gcc, clang), OS X (gcc, AppleClang) and Windows (MSVC, MinGW, MinGW-w64) with different compilers using Travis-CI and Appveyor.

Jason was okay to merge the PR as an undocumented feature, but what should be done to support this officially?


Thanks,

Isuru Fernando

Ondřej Čertík

unread,
Apr 28, 2016, 1:02:57 PM4/28/16
to symengine, sympy, py...@googlegroups.com
Good questions. I also started this wiki which has a plan how to do that:

https://github.com/symengine/symengine/wiki/SymPy-core-upgrade-to-SymEngine

And it talks about how to test it. The approach explained in the wiki
is a general approach, that I have used for another project. It works.

The approach you just described in your email is simplified --- No
need for "old_core_api.py" and the compatibility layer. The symengine
Python wrappers themselves are compatible with SymPy. The problem is
that this might introduce a slow down, if the SymPy's API forces you
to do some conversions, say from a list to a dictionary, while it
would be faster to change the API and keep things as a dictionary.

So we can try your approach, as it is simpler. But if it forces us to
introduce some slow downs in the SymEngine Python wrappers, in order
to be compatible with SymPy, we'll have to switch to my more general
approach, outlined in the wiki.

Ondrej

>
>
> Thanks,
>
> Isuru Fernando
>
> --
> You received this message because you are subscribed to the Google Groups
> "symengine" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to symengine+...@googlegroups.com.
> To post to this group, send email to syme...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/symengine/CA%2B01voP_-pOjKm40hbLzsryzWD%3DsnX_-A-GQYaFGL_%3DEO2ehVA%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages