Skip to content

Commit 0d81664

Browse files
committed
Add get_indices() and get_dummy() to Indices class in python
1 parent 126a8b7 commit 0d81664

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

core/pythoncdb/py_properties.cc

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ namespace cadabra {
164164

165165
template <typename PropT, typename... ParentTs>
166166
std::shared_ptr<BoundProperty<PropT, ParentTs...>> BoundProperty<PropT, ParentTs...>::get_from_kernel(Ex::iterator it, const std::string& label, bool ignore_parent_rel)
167-
167+
168168
{
169169
int tmp;
170170
auto res = get_kernel_from_scope()->properties.get_with_pattern<PropT>(
@@ -204,19 +204,19 @@ namespace cadabra {
204204
{
205205
return BoundPropertyBase::str_();
206206
}
207-
207+
208208
template <typename PropT, typename... ParentTs>
209209
std::string BoundProperty<PropT, ParentTs...>::latex_() const
210210
{
211211
return BoundPropertyBase::latex_();
212212
}
213-
213+
214214
template <typename PropT, typename... ParentTs>
215215
std::string BoundProperty<PropT, ParentTs...>::repr_() const
216216
{
217217
return BoundPropertyBase::str_();
218218
}
219-
219+
220220
template <typename BoundPropT>
221221
typename BoundPropT::py_type def_abstract_prop(pybind11::module& m, const std::string& name)
222222
{
@@ -304,9 +304,30 @@ namespace cadabra {
304304
return ret;
305305
}
306306

307+
std::vector<Ex> indices_get_all(const Indices* indices, bool include_wildcards)
308+
{
309+
auto kernel = get_kernel_from_scope();
310+
auto its = kernel->properties.pats.equal_range(indices);
311+
312+
std::vector<Ex> res;
313+
for (auto it = its.first; it != its.second; ++it) {
314+
if (it->second->obj.begin()->is_autodeclare_wildcard() && !include_wildcards)
315+
continue;
316+
res.push_back(it->second->obj);
317+
}
318+
319+
return res;
320+
}
321+
322+
Ex indices_get_dummy(const Indices* indices, const Ex_ptr& ex)
323+
{
324+
IndexClassifier ic(*get_kernel_from_scope());
325+
return ic.get_dummy(indices, ex->begin());
326+
}
327+
307328
void init_properties(py::module& m)
308329
{
309-
330+
310331
m.def("properties", &list_properties);
311332

312333
py::class_<BoundPropertyBase, std::shared_ptr<BoundPropertyBase>>(m, "Property")
@@ -381,6 +402,9 @@ namespace cadabra {
381402
.def_property_readonly("explicit_form", [](const Py_ImplicitIndex & p) { return p.get_prop()->explicit_form; });
382403
def_prop<Py_ImaginaryI>(m);
383404
auto py_indices = def_prop<Py_Indices>(m)
405+
.def("get_indices", [](const Py_Indices& p, bool wc) { return indices_get_all(p.get_prop(), wc); },
406+
py::arg("include_wildcards") = false)
407+
.def("get_dummy", [](const Py_Indices& p, const Ex_ptr& ex) { return indices_get_dummy(p.get_prop(), ex);})
384408
.def_property_readonly("set_name", [](const Py_Indices & p) { return p.get_prop()->set_name; })
385409
.def_property_readonly("parent_name", [](const Py_Indices & p) { return p.get_prop()->parent_name; })
386410
.def_property_readonly("values", [](const Py_Indices & p) { return p.get_prop()->values; });

0 commit comments

Comments
 (0)