Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix getting string representation of sage objects and fix tests #373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2021
Merged
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
17 changes: 6 additions & 11 deletions symengine/lib/pywrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,12 @@ RCP<const Number> PyNumber::eval(long bits) const {
}

std::string PyNumber::__str__() const {
PyObject* temp;
std::string str;
#if PY_MAJOR_VERSION > 2
temp = PyUnicode_AsUTF8String(pyobject_);
str = std::string(PyBytes_AsString(temp));
#else
temp = PyObject_Str(pyobject_);
str = std::string(PyString_AsString(temp));
#endif
Py_XDECREF(temp);
return str;
Py_ssize_t size;
PyObject *pystr = PyObject_Str(pyobject_);
const char* data = PyUnicode_AsUTF8AndSize(pystr, &size);
std::string result = std::string(data, size);
Py_XDECREF(pystr);
return result;
}

// PyFunctionClass
Expand Down
9 changes: 6 additions & 3 deletions symengine/lib/symengine_wrapper.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2690,7 +2690,7 @@ class FunctionSymbol(Function):
def _sage_(self):
import sage.all as sage
name = self.get_name()
return sage.function(name, *self.args_as_sage())
return sage.function(name)(*self.args_as_sage())

def func(self, *values):
name = self.get_name()
Expand All @@ -2711,7 +2711,7 @@ cdef rcp_const_basic pynumber_to_symengine(PyObject* o1):

cdef PyObject* symengine_to_sage(rcp_const_basic o1):
import sage.all as sage
t = sage.SR(c2py(o1)._sage_())
t = c2py(o1)._sage_()
Py_XINCREF(<PyObject*>t)
return <PyObject*>(t)

Expand Down Expand Up @@ -2765,7 +2765,10 @@ cdef class PyNumber(Number):

def _sage_(self):
import sage.all as sage
return sage.SR(self.pyobject())
res = self.pyobject()
if hasattr(res, '_sage_'):
return res._sage_()
return res

def pyobject(self):
return <object>deref(symengine.rcp_static_cast_PyNumber(self.thisptr)).get_py_object()
Expand Down
10 changes: 3 additions & 7 deletions symengine/tests/test_sage.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ def test_sage_conversions():
assert cos(x1)._sage_() == sage.cos(x)
assert cos(x1) == sympify(sage.cos(x))

assert function_symbol('f', x1, y1)._sage_() == sage.function('f', x, y)
assert function_symbol('f', x1, y1)._sage_() == sage.function('f')(x, y)
assert (function_symbol('f', 2 * x1, x1 + y1).diff(x1)._sage_() ==
sage.function('f', 2 * x, x + y).diff(x))
sage.function('f')(2 * x, x + y).diff(x))

assert LambertW(x1) == LambertW(x)
assert LambertW(x1)._sage_() == sage.lambert_w(x)
Expand Down Expand Up @@ -142,11 +142,7 @@ def test_sage_conversions():
b = b + 8
assert isinstance(b, PyNumber)
assert b._sage_() == a

a = a + x
b = b + x
assert isinstance(b, Add)
assert b._sage_() == a
assert str(a) == str(b)

# Sage Function
e = x1 + wrap_sage_function(sage.log_gamma(x))
Expand Down
1 change: 1 addition & 0 deletions symengine/tests/test_sympy_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,7 @@ def test_pynumber():
assert isinstance(b, PyNumber)
assert b == a # Check equality via SymEngine
assert a == b # Check equality via SymPy
assert str(a) == str(b)

a = 1 - a
b = 1 - b
Expand Down