Skip to content

Commit 73b51e6

Browse files
author
Kasper Peeters
committed
Provide an ExNode::_sympy_ method so that sympy functions can accept ExNode (they already accepted Ex).
1 parent 74ec8c5 commit 73b51e6

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

core/pythoncdb/py_ex.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ namespace cadabra {
290290
return str.str();
291291
}
292292

293-
294293
pybind11::object Ex_as_sympy(Ex_ptr ex)
295294
{
296295
// Generate a string which can be parsed by Sympy.
@@ -306,6 +305,11 @@ namespace cadabra {
306305
return ret;
307306
}
308307

308+
pybind11::object ExNode_as_sympy(const ExNode& exnode)
309+
{
310+
return Ex_as_sympy(exnode.ex);
311+
}
312+
309313
std::string Ex_as_sympy_string(Ex_ptr ex)
310314
{
311315
// Check to see if the expression is a scalar without dummy indices.
@@ -692,6 +696,7 @@ namespace cadabra {
692696
.def("__setitem__", &ExNode::setitem_iterator)
693697
.def("_latex_", &ExNode::_latex_)
694698
.def("__str__", &ExNode::__str__)
699+
.def("_sympy_", &ExNode_as_sympy)
695700
.def("terms", &ExNode::terms, "Return an ExNode iterator over all terms at the level of the current ExNode.")
696701
.def("factors", &ExNode::factors, "Return an ExNode iterator over all factors at the level of the current ExNode.")
697702
.def("own_indices", &ExNode::own_indices, "Return an ExNode iterator over all indices which are not inherited from child nodes.")

tests/sympy_cdb.cdb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ def test01():
44
__cdbkernel__=create_scope()
55
ex:=3x + x**2 + a x**2 + b x;
66
q=SympyBridge(ex)
7-
q.from_sympy( str(sympy.collect(q.to_sympy(), str($x$))) )
7+
q.from_sympy( str(sympy.collect(q.to_sympy(), $x$)) )
88
tst:= x**2 (a + 1) + x ( b + 3) - @(ex);
99
assert(tst==0)
1010
print("Test 01 passed")

0 commit comments

Comments
 (0)