예제 #1
0
def test_numpy_transl():
    if not numpy:
        skip("numpy not installed.")

    from sympy.utilities.lambdify import NUMPY_TRANSLATIONS
    for sym, nump in NUMPY_TRANSLATIONS.items():
        assert sym in sympy.__dict__
        assert nump in numpy.__dict__
예제 #2
0
def test_numpy_transl():
    if not numpy:
        skip("numpy not installed.")

    from sympy.utilities.lambdify import NUMPY_TRANSLATIONS
    for sym, nump in NUMPY_TRANSLATIONS.items():
        assert sym in sympy.__dict__
        assert nump in numpy.__dict__
예제 #3
0
def callback_factory(args, expr, module, use_numba=False):
    if module == 'numpy':
        from sympy.utilities.lambdify import NUMPY_TRANSLATIONS as TRANSLATIONS
        from sympy.printing.lambdarepr import NumPyPrinter as Printer

        def lambdarepr(_x):
            return Printer().doprint(_x)
    else:
        from sympy.printing.lambdarepr import lambdarepr
        if module == 'mpmath':
            from sympy.utilities.lambdify import MPMATH_TRANSLATIONS as TRANSLATIONS
        elif module == 'sympy':
            TRANSLATIONS = {}
        else:
            raise NotImplementedError("Lambdify does not yet support %s" % module)

    mod = __import__(module)
    from sympy import IndexedBase, Symbol
    x = IndexedBase('x')
    indices = [Symbol('..., %d' % i) for i in range(len(args))]
    dummy_subs = dict(zip(args, [x[i] for i in indices]))
    dummified = expr.xreplace(dummy_subs)
    estr = lambdarepr(dummified)

    namespace = mod.__dict__.copy()

    # e.g. NumPyPrinter incomplete: https://github.com/sympy/sympy/issues/11023
    # we need to read translations from lambdify
    for k, v in TRANSLATIONS.items():
        namespace[k] = namespace[v]

    if module != 'mpmath':
        namespace['Abs'] = abs

    func = eval('lambda x: %s' % estr, namespace)
    if use_numba:
        from numba import njit
        func = njit(func)
    if module == 'numpy':
        def wrapper(x):
            return func(mod.asarray(x, dtype=mod.float64))
    else:
        wrapper = func
    wrapper.__doc__ = estr

    return wrapper
예제 #4
0
def test_lambdify_transl():
    from sympy.utilities.lambdify import NUMPY_TRANSLATIONS
    for sym, mat in NUMPY_TRANSLATIONS.items():
        assert sym in sympy.__dict__
        assert mat in numpy.__dict__
예제 #5
0
def test_lambdify_transl():
    from sympy.utilities.lambdify import NUMPY_TRANSLATIONS
    for sym, mat in NUMPY_TRANSLATIONS.iteritems():
        assert sym in sympy.functions.__dict__ or sym in ("Matrix", )
        assert mat in numpy.__dict__
예제 #6
0
def test_lambdify_transl():
    from sympy.utilities.lambdify import NUMPY_TRANSLATIONS
    for sym, mat in NUMPY_TRANSLATIONS.items():
        assert sym in sympy.__dict__
        assert mat in numpy.__dict__
예제 #7
0
파일: nbvars.py 프로젝트: wsumfest-cpu/exa
.. _sympy: http://docs.sympy.org/latest/index.html
.. _symengine: https://github.com/symengine/symengine
.. _numba: http://numba.pydata.org/
"""
import six
import numpy as np
import sympy as sy
import numba as nb
from warnings import warn
from platform import system
from sympy.utilities.lambdify import NUMPY_TRANSLATIONS, NUMPY_DEFAULT


npvars = vars(np)
npvars.update(NUMPY_DEFAULT)
npvars.update({k: getattr(np, v) for k, v in NUMPY_TRANSLATIONS.items()})
if "linux" in system().lower():
    jitkwargs = dict(nopython=True, nogil=True, parallel=True)
    veckwargs = dict(nopython=True, target="parallel")
else:
    jitkwargs = dict(nopython=True, nogil=True, parallel=False, cache=True)
    veckwargs = dict(nopython=True, target="cpu")


def numbafy(fn, args, compiler="jit", **nbkws):
    """
    Compile a string, sympy expression or symengine expression using numba.

    Not all functions are supported by Python's numerical package (numpy). For
    difficult cases, valid Python code (as string) may be more suitable than
    symbolic expressions coming from sympy, symengine, etc. When compiling
예제 #8
0
파일: test_numpy.py 프로젝트: fperez/sympy
def test_lambdify_transl():
    from sympy.utilities.lambdify import NUMPY_TRANSLATIONS
    for sym, mat in NUMPY_TRANSLATIONS.iteritems():
        assert sym in sympy.functions.__dict__ or sym in ("Matrix", )
        assert mat in numpy.__dict__
예제 #9
0
파일: nbvars.py 프로젝트: tjduigna/exa
.. _symengine: https://github.com/symengine/symengine
.. _numba: http://numba.pydata.org/
"""
import six
#import ast
import numpy as np
import sympy as sy
import numba as nb
from warnings import warn
from platform import system
from sympy.utilities.lambdify import NUMPY_TRANSLATIONS, NUMPY_DEFAULT


npvars = vars(np)
npvars.update(NUMPY_DEFAULT)
npvars.update({k: getattr(np, v) for k, v in NUMPY_TRANSLATIONS.items()})
if "linux" in system().lower():
    jitkwargs = dict(nopython=True, nogil=True, parallel=True)
    veckwargs = dict(nopython=True, target="parallel")
else:
    jitkwargs = dict(nopython=True, nogil=True, parallel=False, cache=True)
    veckwargs = dict(nopython=True, target="cpu")


def numbafy(fn, args, compiler="jit", **nbkws):
    """
    Compile a string, sympy expression or symengine expression using numba.

    Not all functions are supported by Python's numerical package (numpy). For
    difficult cases, valid Python code (as string) may be more suitable than
    symbolic expressions coming from sympy, symengine, etc. When compiling