Skip to content

Commit e8a1c93

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 e8a1c93

File tree

2 files changed

+8
-328
lines changed

2 files changed

+8
-328
lines changed

pycommute/expression.cpp

Lines changed: 6 additions & 145 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
//
@@ -237,20 +186,15 @@ class gen_type_trampoline : public gen_type {
237186
}
238187
};
239188

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;
245-
};
246-
247189
//
248190
// Register generator<dyn_indices>
249191
//
250192

251-
template<typename Gen>
252-
void register_generator(py::module_ & m, Gen & g) {
253-
g
193+
void register_generator(py::module_ & m) {
194+
195+
py::class_<gen_type, gen_type_trampoline>(m, "Generator",
196+
"Abstract algebra generator"
197+
)
254198
// Algebra ID
255199
.def_property_readonly("algebra_id",
256200
&gen_type::algebra_id,
@@ -262,70 +206,6 @@ void register_generator(py::module_ & m, Gen & g) {
262206
},
263207
"Indices carried by this generator."
264208
)
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-
)
329209
// Comparison operators
330210
.def("__eq__",
331211
[](gen_type const& g1, gen_type const& g2){ return g1 == g2; },
@@ -349,14 +229,6 @@ This method should be overridden in derived classes.)eol",
349229
)
350230
// String representation
351231
.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-
);
360232
}
361233

362234
////////////////////////////////////////////////////////////////////////////////
@@ -966,17 +838,6 @@ PYBIND11_MODULE(expression, m) {
966838

967839
register_dyn_indices(m);
968840

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-
980841
//
981842
// Algebra IDs
982843
//
@@ -985,7 +846,7 @@ PYBIND11_MODULE(expression, m) {
985846
m.attr("BOSON") = boson;
986847
m.attr("SPIN") = spin;
987848

988-
register_generator(m, g);
849+
register_generator(m);
989850

990851
register_generator_fermion(m);
991852
register_generator_boson(m);

0 commit comments

Comments
 (0)