Skip to content

Commit

Permalink
Fix test which relied on implicit string fallback for sympy functions…
Browse files Browse the repository at this point in the history
…; see sympy/sympy#18066. We need a wrapper function for sympy's collect function to make this cleaner.
  • Loading branch information
Kasper Peeters committed Aug 4, 2024
1 parent 59010ab commit 74ec8c5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
9 changes: 6 additions & 3 deletions core/SympyCdb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,21 @@ pybind11::object sympy::SympyBridge::export_ex()
pybind11::module sympy_parser = pybind11::module::import("sympy.parsing.sympy_parser");
auto parse = sympy_parser.attr("parse_expr");
#ifdef DEBUG
std::cerr << str.str() << std::endl;
std::cerr << "sympy::SympyBridge::export_ex: " << str.str() << std::endl;
#endif
pybind11::object ret = parse(str.str());

#ifdef DEBUG
std::cerr << "sympy::SympyBridge::export_ex: succes" << std::endl;
#endif

return ret;
}

void sympy::SympyBridge::import_ex(const std::string& s)
{
preparse_import(s);
#ifdef DEBUG
std::cerr << s << std::endl;
std::cerr << "sympy::SympyBridge::import_ex: " << s << std::endl;
#endif
auto ptr = std::make_shared<Ex>();
cadabra::Parser parser(ptr);
Expand Down
8 changes: 4 additions & 4 deletions core/pythoncdb/py_ex.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ namespace cadabra {

/// \ingroup pythoncore
///
/// Add two expressions, adding a top-level \sum node if required.
/// Add two expressions, adding a top-level '\sum' node if required.

Ex_ptr Ex_add(const Ex_ptr ex1, const ExNode ex2);
Ex_ptr Ex_add(const Ex_ptr ex1, const Ex_ptr ex2);
Ex_ptr Ex_add(const Ex_ptr ex1, const Ex_ptr ex2, Ex::iterator top2);

/// \ingroup pythoncore
///
/// Multiply two expressions, adding a top-level \prod node if required.
/// Multiply two expressions, adding a top-level '\prod' node if required.
Ex_ptr Ex_mul(const Ex_ptr ex1, const Ex_ptr ex2);
Ex_ptr Ex_mul(const Ex_ptr ex1, const Ex_ptr ex2, Ex::iterator top2);

/// \ingroup pythoncore
///
/// Subtract two expressions, adding a top-level \sum node if required.
/// Subtract two expressions, adding a top-level '\sum' node if required.
Ex_ptr Ex_sub(const Ex_ptr ex1, const ExNode ex2);
Ex_ptr Ex_sub(const Ex_ptr ex1, const Ex_ptr ex2);
Ex_ptr Ex_sub(const Ex_ptr ex1, const Ex_ptr ex2, Ex::iterator top2);
Expand Down Expand Up @@ -109,4 +109,4 @@ namespace cadabra {
void call_post_process(Kernel& kernel, Ex_ptr ex);

void init_ex(pybind11::module& m);
}
}
14 changes: 7 additions & 7 deletions tests/sympy_cdb.cdb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def test01():
__cdbkernel__=create_scope()
ex:=3x + x**2 + a x**2 + b x;
q=SympyBridge(ex)
q.from_sympy( str(sympy.collect(q.to_sympy(), $x$)) )
q.from_sympy( str(sympy.collect(q.to_sympy(), str($x$))) )
tst:= x**2 (a + 1) + x ( b + 3) - @(ex);
assert(tst==0)
print("Test 01 passed")
Expand All @@ -13,10 +13,10 @@ test01()

def test02():
__cdbkernel__=create_scope()
{r,t}::Coordinate;
f::Depends(r,t);
{r,t}::Coordinate.
f::Depends(r,t).
ex:=f*A;
\partial{#}::PartialDerivative;
\partial{#}::PartialDerivative.
ex:= \partial_{r}{ f + (\sin(r)**2)**2 };
map_sympy(ex)
tst:= \partial_{r}{f} + 4 \sin(r)**3 \cos(r) - @(ex);
Expand All @@ -27,9 +27,9 @@ test02()

def test03():
__cdbkernel__=create_scope()
\tau::Coordinate;
\partial{#}::Derivative(\tau);
r::Depends(\tau);
\tau::Coordinate.
\partial{#}::Derivative(\tau).
r::Depends(\tau).
ex:=\partial{r};
q=SympyBridge(ex)
q.from_sympy(str(q.to_sympy()))
Expand Down

0 comments on commit 74ec8c5

Please sign in to comment.