Skip to content

Commit

Permalink
Fix a bug in accessing the Weight value in Python. Fix an error with …
Browse files Browse the repository at this point in the history
…setting the multiplier of a node from Python.
  • Loading branch information
Kasper Peeters committed Dec 1, 2023
1 parent 2ac0254 commit 135885d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 5 deletions.
4 changes: 2 additions & 2 deletions core/ExNode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ void ExNode::set_multiplier(pybind11::object mult)
if(!ex->is_valid(it))
throw ConsistencyException("Cannot set the multiplier of an iterator before the first 'next'.");

pybind11::object mpq = pybind11::module::import("gmpy2").attr("mpq");
multiply(it->multiplier, pybind11::cast<int>(mult));
set(it->multiplier, multiplier_t(mult.attr("numerator").cast<long>(),
mult.attr("denominator").cast<long>()) );
}


Expand Down
6 changes: 6 additions & 0 deletions core/Storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,12 @@ namespace cadabra {
num=rat_set.insert(fac).first;
}

void set(rset_t::iterator& num, multiplier_t fac)
{
fac.canonicalize();
num=rat_set.insert(fac).first;
}

void add(rset_t::iterator& num, multiplier_t fac)
{
fac+=*num;
Expand Down
3 changes: 2 additions & 1 deletion core/Storage.hh
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ namespace cadabra {
void one(rset_t::iterator&);
void flip_sign(rset_t::iterator&);
void half(rset_t::iterator&);

void set(rset_t::iterator&, multiplier_t);

/// \ingroup core
///
/// Basic storage class for symbolic mathemematical expressions. The
Expand Down
6 changes: 5 additions & 1 deletion core/algorithms/substitute.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ substitute::substitute(const Kernel& k, Ex& tr, Ex& args_, bool partial)
{
if(args.is_empty())
throw ArgumentException("substitute: Replacement rule is an empty expression.");


Stopwatch sw;
sw.start();
cadabra::do_list(args, args.begin(), [&](Ex::iterator arrow) {
//args.print_recursive_treeform(std::cerr, arrow);
if(*arrow->name!="\\arrow" && *arrow->name!="\\equals")
Expand Down Expand Up @@ -81,6 +83,8 @@ substitute::substitute(const Kernel& k, Ex& tr, Ex& args_, bool partial)
}
return true;
});
sw.stop();
std::cerr << "preparation took " << sw << std::endl;
}

bool substitute::can_apply(iterator st)
Expand Down
9 changes: 8 additions & 1 deletion core/pythoncdb/py_properties.cc
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,14 @@ namespace cadabra {
def_abstract_prop<Py_DependsBase>(m, "DependsBase")
.def("dependencies", [](const Py_DependsBase & p) { return p.get_prop()->dependencies(p.get_kernel(), p.get_it()); });
def_abstract_prop<Py_WeightBase>(m, "WeightBase")
.def("value", [](const Py_WeightBase & p, const std::string& forcedLabel) { return p.get_prop()->value(p.get_kernel(), p.get_it(), forcedLabel); });
.def("value", [](const Py_WeightBase & p, const std::string& forcedLabel) {
// This is mpq_class, convert to the Python equivalent.
pybind11::object mpq = pybind11::module::import("gmpy2").attr("mpq");
auto m = p.get_prop()->value(p.get_kernel(), p.get_it(), forcedLabel);
pybind11::object mult = mpq(m.get_num().get_si(), m.get_den().get_si());
return mult;
});

def_abstract_prop<Py_DifferentialFormBase>(m, "DifferentialFormBase")
.def("degree", [](const Py_DifferentialFormBase & p) { return p.get_prop()->degree(p.get_props(), p.get_it()); });

Expand Down
15 changes: 15 additions & 0 deletions tests/programming.cdb
Original file line number Diff line number Diff line change
Expand Up @@ -341,5 +341,20 @@ def test16():
{i,j,k}::Indices(isospin, position=independent).
assert($\Lambda_{a}$.matches($\Lambda_{i}$)==False)
assert($\Lambda_{a}$.matches($\Lambda^{i}$)==False)
print("Test 16 passed")

test16()

def test17():
x::Weight(value=42, label=field);
tst1 = Weight.get($x$, label="field").value("field")
assert(tst1==42)
print("Test 17a passed")
ex:= 3 a;
ex.top().multiplier = tst1
tst2:= 42 a - @(ex);
assert(tst2==0)
print("Test 17b passed")

test17()

0 comments on commit 135885d

Please sign in to comment.