Skip to content

Commit 9065a72

Browse files
committed
Remove wrapper for linear_function and generator methods that use it
Those methods are meant to be called only by Python classes derived from `generator`. Supporting derived generator classes is too painful with current pybind11 (pybind/pybind11#1566), so I have decided to disallow this functionality altogether.
1 parent 3200831 commit 9065a72

File tree

2 files changed

+8
-352
lines changed

2 files changed

+8
-352
lines changed

pycommute/expression.cpp

Lines changed: 6 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -126,57 +126,6 @@ void register_dyn_indices(py::module_ & m) {
126126

127127
////////////////////////////////////////////////////////////////////////////////
128128

129-
//
130-
// Register generator::linear_function_t
131-
//
132-
133-
void register_linear_function(py::module_ & m) {
134-
135-
auto copy_terms = [](auto const& terms) {
136-
std::vector<std::pair<std::unique_ptr<gen_type>, double>> res;
137-
res.reserve(terms.size());
138-
for(auto const& t : terms)
139-
res.emplace_back(t.first->clone(), t.second);
140-
return res;
141-
};
142-
143-
py::class_<gen_type::linear_function_t>(m, "LinearFunctionGen",
144-
"Linear combination of algebra generators plus a constant term"
145-
)
146-
.def(py::init<>(), "Construct a function that is identically zero.")
147-
.def(py::init<double>(), "Construct a constant.", py::arg("const_term"))
148-
.def(py::init([copy_terms](
149-
double const_term,
150-
std::vector<std::pair<const gen_type*, double>> const& terms) {
151-
return std::make_unique<gen_type::linear_function_t>(
152-
const_term,
153-
std::move(copy_terms(terms))
154-
);
155-
}),
156-
"Construct from a constant term and a list of coefficient/generator pairs.",
157-
py::arg("const_term"),
158-
py::arg("terms")
159-
)
160-
.def_readwrite("const_term",
161-
&gen_type::linear_function_t::const_term,
162-
"Constant term.")
163-
.def_property("terms",
164-
[copy_terms](gen_type::linear_function_t const& f) {
165-
return copy_terms(f.terms);
166-
},
167-
[copy_terms](gen_type::linear_function_t & f,
168-
std::vector<std::pair<const gen_type*, double>> const& terms) {
169-
f.terms = std::move(copy_terms(terms));
170-
},
171-
"List of pairs of algebra generators and their respective coefficients."
172-
)
173-
.def_property_readonly("vanishing", &gen_type::linear_function_t::vanishing,
174-
"Is this linear function identically zero?"
175-
);
176-
}
177-
178-
////////////////////////////////////////////////////////////////////////////////
179-
180129
//
181130
// Helper classes for abstract base generator<dyn_indices>
182131
//
@@ -211,46 +160,17 @@ class gen_type_trampoline : public gen_type {
211160
double swap_with(gen_type const& g2, linear_function_t & f) const override {
212161
PYBIND11_OVERRIDE_PURE(double, gen_type, swap_with, g2, f);
213162
}
214-
215-
bool simplify_prod(gen_type const& g2, linear_function_t & f) const override {
216-
PYBIND11_OVERRIDE(bool, gen_type, simplify_prod, g2, f);
217-
}
218-
219-
bool reduce_power(int power, linear_function_t & f) const override {
220-
PYBIND11_OVERRIDE(bool, gen_type, reduce_power, power, f);
221-
}
222-
223-
void conj(linear_function_t & f) const override {
224-
PYBIND11_OVERRIDE(void, gen_type, conj, f);
225-
}
226-
227-
bool equal(gen_type const& g) const override {
228-
PYBIND11_OVERRIDE(bool, gen_type, equal, g);
229-
}
230-
231-
bool less(gen_type const& g) const override {
232-
PYBIND11_OVERRIDE(bool, gen_type, less, g);
233-
}
234-
235-
bool greater(gen_type const& g) const override {
236-
PYBIND11_OVERRIDE(bool, gen_type, greater, g);
237-
}
238-
};
239-
240-
class gen_type_publicist : public gen_type {
241-
public:
242-
using gen_type::equal;
243-
using gen_type::less;
244-
using gen_type::greater;
245163
};
246164

247165
//
248166
// Register generator<dyn_indices>
249167
//
250168

251-
template<typename Gen>
252-
void register_generator(py::module_ & m, Gen & g) {
253-
g
169+
void register_generator(py::module_ & m) {
170+
171+
py::class_<gen_type, gen_type_trampoline>(m, "Generator",
172+
"Abstract algebra generator"
173+
)
254174
// Algebra ID
255175
.def_property_readonly("algebra_id",
256176
&gen_type::algebra_id,
@@ -262,70 +182,6 @@ void register_generator(py::module_ & m, Gen & g) {
262182
},
263183
"Indices carried by this generator."
264184
)
265-
// Product transformation methods
266-
.def("swap_with", &gen_type::swap_with, R"eol(
267-
Given a pair of generators g1 = 'self' and g2 such that g1 > g2, swap_with()
268-
must signal what transformation g1 * g2 -> c * g2 * g1 + f(g) should be applied
269-
to the product g1 * g2 to put it into the canonical order.
270-
swap_with() returns the constant 'c' and writes the linear function f(g) into
271-
'f'. 'c' is allowed to be zero.
272-
273-
This method should be overridden in derived classes.)eol",
274-
py::arg("g2"),
275-
py::arg("f")
276-
)
277-
.def("simplify_prod", &gen_type::simplify_prod, R"eol(
278-
Given a pair of generators g1 = 'self' and g2 such that g1 * g2 is in the
279-
canonical order (g1 <= g2), optionally apply a simplifying transformation
280-
g1 * g2 -> f(g). If a simplification is actually possible, simplify_prod()
281-
must return True and write the linear function f(g) into 'f'.
282-
Otherwise return False.
283-
284-
This method should be overridden in derived classes.)eol",
285-
py::arg("g2"),
286-
py::arg("f")
287-
)
288-
.def("reduce_power", &gen_type::reduce_power, R"eol(
289-
Given a generator g1 = 'self' and a power > 2, optionally apply a simplifying
290-
transformation g1^power -> f(g). If a simplification is actually possible,
291-
reduce_power() must return True and write the linear function f(g) into 'f'.
292-
Otherwise return False.
293-
294-
N.B. Simplifications for power = 2 must be carried out by simplify_prod().
295-
296-
This method should be overridden in derived classes.)eol",
297-
py::arg("power"), py::arg("f")
298-
)
299-
// Comparison methods
300-
.def("equal", &gen_type_publicist::equal, R"eol(
301-
Determine whether two generators 'self' and 'g' belonging to the same algebra
302-
are equal.
303-
304-
This method should be overridden in derived classes.)eol",
305-
py::arg("g")
306-
)
307-
.def("less", &gen_type_publicist::less, R"eol(
308-
Determine whether two generators 'self' and 'g' belonging to the same algebra
309-
satisfy self < g.
310-
311-
This method should be overridden in derived classes.)eol",
312-
py::arg("g")
313-
)
314-
.def("greater", &gen_type_publicist::greater, R"eol(
315-
Determine whether two generators 'self' and 'g' belonging to the same algebra
316-
satisfy self > g.
317-
318-
This method should be overridden in derived classes.)eol",
319-
py::arg("g")
320-
)
321-
// Hermitian conjugate
322-
.def("conj", &gen_type::conj, R"eol(
323-
Return the Hermitian conjugate of this generator as a linear function of
324-
generators via 'f'.
325-
326-
This method should be overridden in derived classes.)eol",
327-
py::arg("f")
328-
)
329185
// Comparison operators
330186
.def("__eq__",
331187
[](gen_type const& g1, gen_type const& g2){ return g1 == g2; },
@@ -349,14 +205,6 @@ This method should be overridden in derived classes.)eol",
349205
)
350206
// String representation
351207
.def("__repr__", &print<gen_type>);
352-
353-
// Swap generators of potentially different algebras
354-
m.def("swap_with", &swap_with<dyn_indices>, R"eol(
355-
Check if 'g1' and 'g2' belong to the same algebra and call g1.swap_with(g2, f)
356-
accordingly. Generators of different algebras always commute, and for such
357-
generators swap_with() returns 1 and sets 'f' to the trivial function.)eol",
358-
py::arg("g1"), py::arg("g2"), py::arg("f")
359-
);
360208
}
361209

362210
////////////////////////////////////////////////////////////////////////////////
@@ -966,17 +814,6 @@ PYBIND11_MODULE(expression, m) {
966814

967815
register_dyn_indices(m);
968816

969-
//
970-
// Register generator<dyn_indices> early on so that pybind11 knows about this
971-
// type at the point where generator::linear_function_t is wrapped.
972-
//
973-
974-
py::class_<gen_type, gen_type_trampoline> g(m, "Generator",
975-
"Abstract algebra generator"
976-
);
977-
978-
register_linear_function(m);
979-
980817
//
981818
// Algebra IDs
982819
//
@@ -985,7 +822,7 @@ PYBIND11_MODULE(expression, m) {
985822
m.attr("BOSON") = boson;
986823
m.attr("SPIN") = spin;
987824

988-
register_generator(m, g);
825+
register_generator(m);
989826

990827
register_generator_fermion(m);
991828
register_generator_boson(m);

0 commit comments

Comments
 (0)