Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion symengine/lib/pywrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "pywrapper.h"
#include <symengine/serialize-cereal.h>
#include <symengine/symengine_exception.h>

#if PY_MAJOR_VERSION >= 3
#define PyInt_FromLong PyLong_FromLong
Expand Down Expand Up @@ -243,7 +244,27 @@ RCP<const Basic> PyFunction::create(const vec_basic &x) const {
}

RCP<const Number> PyFunction::eval(long bits) const {
return pyfunction_class_->get_py_module()->eval_(pyobject_, bits);
RCP<const Number> result
= pyfunction_class_->get_py_module()->eval_(pyobject_, bits);
if (!result.is_null() && !PyErr_Occurred()) {
return result;
}

bool not_implemented = result.is_null();
if (PyErr_Occurred()) {
not_implemented
= PyErr_ExceptionMatches(PyExc_NotImplementedError) != 0;
}
PyErr_Clear();

if (not_implemented) {
// The Python function has no numerical value (e.g. it has free
// symbols). Signal the caller that it should stay unevaluated.
throw NotImplementedError(
"PyFunction cannot be evaluated to a number");
}

throw SymEngineException("Python function evaluation failed");
}

RCP<const Basic> PyFunction::diff_impl(const RCP<const Symbol> &s) const {
Expand Down
1 change: 1 addition & 0 deletions symengine/lib/symengine.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ cdef extern from "<symengine/number.h>" namespace "SymEngine":
pass
cdef cppclass NumberWrapper(Basic):
pass
bool is_a_Number(const Basic &b) nogil
cdef tribool is_zero(const Basic &x) nogil
cdef tribool is_positive(const Basic &x) nogil
cdef tribool is_negative(const Basic &x) nogil
Expand Down
7 changes: 6 additions & 1 deletion symengine/lib/symengine_wrapper.in.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2900,8 +2900,13 @@ cdef PyObject* symengine_to_sympy(rcp_const_basic o1):
return <PyObject*>(t)

cdef RCP[const symengine.Number] sympy_eval(PyObject* o1, long bits):
cdef Basic X
prec = max(1, int(round(bits/3.3219280948873626)-1))
cdef Number X = sympify((<object>o1).n(prec))
result = (<object>o1).n(prec)
X = sympify(result)
if not symengine.is_a_Number(deref(X.thisptr)):
raise NotImplementedError(
"expression cannot be evaluated to a number: %r" % (result,))
return symengine.rcp_static_cast_Number(X.thisptr)

cdef RCP[const symengine.Number] sage_eval(PyObject* o1, long bits):
Expand Down
11 changes: 10 additions & 1 deletion symengine/tests/test_sympy_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
KroneckerDelta, LeviCivita, erf, erfc, lowergamma, uppergamma,
loggamma, beta, polygamma, sign, floor, ceiling, conjugate, And,
Or, Not, Xor, Piecewise, Interval, EmptySet, FiniteSet, Contains,
Union, Complement, UniversalSet, Reals, Rationals, Integers)
Union, Complement, UniversalSet, Reals, Rationals, Integers,
PyFunction)
import unittest

# Note: We test _sympy_() for SymEngine -> SymPy conversion, as those are
Expand Down Expand Up @@ -854,3 +855,11 @@ def test_sympy_roundtrip():
_check_sympy_roundtrip(x+y)
_check_sympy_roundtrip(x**y)
_check_sympy_roundtrip(d)


@unittest.skipIf(not have_sympy, "SymPy not installed")
def test_evalf_symbolic_pyfunction():
x = Symbol("x")
f = sympify(sympy.besselj(x, 1))
assert isinstance(f, PyFunction)
assert f.n() == f