-
Notifications
You must be signed in to change notification settings - Fork 8
Open
Description
We can use SymPy to provide missing functionalities in SymEngine.
s4basic_as_py <- function(x, convert=FALSE) {
symengine_py_module <- reticulate::import("symengine", convert=FALSE)
## Convert external pointer to pycapsule
cap <- reticulate::r_to_py(x@ptr, convert=convert)
## Convert to symengine py object
ans <- symengine_py_module$lib$symengine_wrapper$sympify_pycapsule(cap)
ans
}
s4basic_from_py <- function(x) {
symengine_py_module <- reticulate::import("symengine", convert=FALSE)
ans <- symengine:::s4basic()
symengine_py_module$lib$symengine_wrapper$assign_to_pycapsule(
reticulate::r_to_py(ans@ptr), x)
ans
}
r_to_py.Basic <- function(x, convert=FALSE) {
s4basic_as_py(x, convert)
}
py_to_r.symengine.lib.symengine_wrapper.Basic <- function(x) {
s4basic_from_py(x)
}
py_to_r.sympy.core.basic.Basic <- function(x) {
s4basic_from_py(x)
}
Then this provides nice seamless integration through reticulate:
x <- S("x")
sympy <- reticulate::import("sympy")
sympy$integrate(x^2L/2L)
# (Mul) (1/6)*x^3
sympy$integrate(x^2L/2L, tuple(x, 1L, 2L))
# (Rational) 7/6
The symengine object in R is first passed to python through pycapsule, then converted to symengine.py object. Thanks to symengine.py's compatibility layer, sympy functions can work on them and the result is seamlessly converted back to R's symengine object.
Related issues: symengine/symengine.py#319, rstudio/reticulate#660
Metadata
Metadata
Assignees
Labels
No labels