File: treelp.cpp

package info (click to toggle)
regina-normal 7.4.1-1.1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 154,244 kB
  • sloc: cpp: 295,026; xml: 9,992; sh: 1,344; python: 1,225; perl: 616; ansic: 138; makefile: 26
file content (201 lines) | stat: -rw-r--r-- 9,393 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

/**************************************************************************
 *                                                                        *
 *  Regina - A Normal Surface Theory Calculator                           *
 *  Python Interface                                                      *
 *                                                                        *
 *  Copyright (c) 1999-2025, Ben Burton                                   *
 *  For further details contact Ben Burton (bab@debian.org).              *
 *                                                                        *
 *  This program is free software; you can redistribute it and/or         *
 *  modify it under the terms of the GNU General Public License as        *
 *  published by the Free Software Foundation; either version 2 of the    *
 *  License, or (at your option) any later version.                       *
 *                                                                        *
 *  As an exception, when this program is distributed through (i) the     *
 *  App Store by Apple Inc.; (ii) the Mac App Store by Apple Inc.; or     *
 *  (iii) Google Play by Google Inc., then that store may impose any      *
 *  digital rights management, device limits and/or redistribution        *
 *  restrictions that are required by its terms of service.               *
 *                                                                        *
 *  This program is distributed in the hope that it will be useful, but   *
 *  WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *  General Public License for more details.                              *
 *                                                                        *
 *  You should have received a copy of the GNU General Public License     *
 *  along with this program. If not, see <https://www.gnu.org/licenses/>. *
 *                                                                        *
 **************************************************************************/

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "enumerate/treelp.h"
#include "triangulation/dim3.h"
#include "../helpers.h"
#include "../docstrings/enumerate/treelp.h"

using pybind11::overload_cast;
using regina::Integer;
using regina::LPData;
using regina::LPInitialTableaux;
using regina::LPMatrix;
using regina::LPSystem;
using regina::NormalEncoding;
using regina::Triangulation;

using regina::LPConstraintNone;
using regina::LPConstraintEulerPositive;
using regina::LPConstraintEulerZero;
using regina::LPConstraintNonSpun;

template <class LPConstraint>
void addLPInitialTableaux(pybind11::module_& m, const char* name) {
    RDOC_SCOPE_BEGIN(LPInitialTableaux)

    using Tableaux = LPInitialTableaux<LPConstraint>;

    auto c = pybind11::class_<Tableaux>(m, name, rdoc_scope)
        .def(pybind11::init<const Triangulation<3>&, NormalEncoding, bool>(),
            pybind11::arg(), pybind11::arg(),
            pybind11::arg("enumeration") = true,
            rdoc::__init)
        .def(pybind11::init<const Tableaux&>(), rdoc::__copy)
        .def("swap", &Tableaux::swap, rdoc::swap)
        .def("tri", &Tableaux::tri,
            pybind11::return_value_policy::reference_internal, rdoc::tri)
        .def("system", &Tableaux::system, rdoc::system)
        .def("rank", &Tableaux::rank, rdoc::rank)
        .def("columns", &Tableaux::columns, rdoc::columns)
        .def("coordinateColumns", &Tableaux::coordinateColumns,
            rdoc::coordinateColumns)
        .def("columnPerm", [](const Tableaux& t) {
            const size_t* perm = t.columnPerm();

            pybind11::list ans;
            for (size_t i = 0; i < t.columns(); ++i)
                ans.append(perm[i]);
            return ans;
        }, rdoc::columnPerm)
        .def("multColByRow", &Tableaux::template multColByRow<Integer>,
            rdoc::multColByRow)
        .def("multColByRowOct", &Tableaux::template multColByRowOct<Integer>,
            rdoc::multColByRowOct)
        .def("fillInitialTableaux",
            &Tableaux::template fillInitialTableaux<Integer>,
            rdoc::fillInitialTableaux)
        ;
    regina::python::add_output(c);
    // We need to think more about what a comparison between tableaux should
    // test.  In the meantime, don't make a decision we might regret later.
    regina::python::disable_eq_operators(c);

    regina::python::add_global_swap<Tableaux>(m, rdoc::global_swap);

    RDOC_SCOPE_END
}

template <class LPConstraint>
void addLPData(pybind11::module_& m, const char* name) {
    RDOC_SCOPE_BEGIN(LPData)

    using Data = LPData<LPConstraint, Integer>;

    auto c = pybind11::class_<Data>(m, name, rdoc_scope)
        .def(pybind11::init<>(), rdoc::__default)
        .def("swap", &Data::swap, rdoc::swap)
        .def("reserve", &Data::reserve, rdoc::reserve)
        .def("initStart", &Data::initStart, rdoc::initStart)
        .def("initClone", &Data::initClone, rdoc::initClone)
        .def("columns", &Data::columns, rdoc::columns)
        .def("coordinateColumns", &Data::coordinateColumns,
            rdoc::coordinateColumns)
        .def("isFeasible", &Data::isFeasible, rdoc::isFeasible)
        .def("isActive", &Data::isActive, rdoc::isActive)
        .def("sign", &Data::sign, rdoc::sign)
        .def("constrainZero", &Data::constrainZero, rdoc::constrainZero)
        .def("constrainPositive", &Data::constrainPositive,
            rdoc::constrainPositive)
        .def("constrainOct", &Data::constrainOct, rdoc::constrainOct)
        .def("extractSolution", [](const Data& d, const std::vector<int>& t) {
            // Currently LPData does not give us an easy way to extract the
            // expected length of the type vector, and so we cannot sanity-check
            // the size of t right now.  Probably we should add an access
            // function to LPData that lets us view the original tableaux.
            char* types = new char[t.size()];
            std::copy(t.begin(), t.end(), types);
            auto ans = d.template extractSolution<regina::VectorInt>(types);
            delete[] types;
            return ans;
        }, rdoc::extractSolution)
        ;
    regina::python::add_output(c);
    // We need to think more about what a comparison between tableaux should
    // test.  Do we just compare basis indices?  Do we do a deep comparison of
    // all the internal data?  Let's not force a decision right now.
    regina::python::disable_eq_operators(c);

    regina::python::add_global_swap<Data>(m, rdoc::global_swap);

    RDOC_SCOPE_END
}

void addTreeLP(pybind11::module_& m) {
    RDOC_SCOPE_BEGIN(LPMatrix)

    auto c = pybind11::class_<LPMatrix<Integer>>(m, "LPMatrix", rdoc_scope)
        .def(pybind11::init<>(), rdoc::__default)
        .def(pybind11::init<size_t, size_t>(), rdoc::__init)
        .def("swap", &LPMatrix<Integer>::swap, rdoc::swap)
        .def("reserve", &LPMatrix<Integer>::reserve, rdoc::reserve)
        .def("initClone", &LPMatrix<Integer>::initClone, rdoc::initClone)
        .def("initIdentity", &LPMatrix<Integer>::initIdentity,
            rdoc::initIdentity)
        .def("entry", overload_cast<size_t, size_t>(&LPMatrix<Integer>::entry),
            pybind11::return_value_policy::reference_internal, rdoc::entry)
        .def("set", [](LPMatrix<Integer>& m, size_t row, size_t col,
                const regina::Integer& value){
            m.entry(row, col) = value;
        }, rdoc::set)
        .def("rows", &LPMatrix<Integer>::rows, rdoc::rows)
        .def("columns", &LPMatrix<Integer>::columns, rdoc::columns)
        .def("swapRows", &LPMatrix<Integer>::swapRows, rdoc::swapRows)
        .def("combRow", &LPMatrix<Integer>::combRow, rdoc::combRow)
        .def("combRowAndNorm", &LPMatrix<Integer>::combRowAndNorm,
            rdoc::combRowAndNorm)
        .def("negateRow", &LPMatrix<Integer>::negateRow, rdoc::negateRow)
        ;
    regina::python::add_output(c);
    regina::python::add_eq_operators(c, rdoc::__eq);

    regina::python::add_global_swap<LPMatrix<Integer>>(m, rdoc::global_swap);

    RDOC_SCOPE_SWITCH(LPSystem)

    auto s = pybind11::class_<LPSystem>(m, "LPSystem", rdoc_scope)
        .def(pybind11::init<NormalEncoding>(), rdoc::__init)
        .def(pybind11::init<const LPSystem&>(), rdoc::__copy)
        .def("normal", &LPSystem::normal, rdoc::normal)
        .def("angle", &LPSystem::angle, rdoc::angle)
        .def("standard", &LPSystem::standard, rdoc::standard)
        .def("quad", &LPSystem::quad, rdoc::quad)
        .def("coords", &LPSystem::coords, rdoc::coords)
        ;
    regina::python::add_output(s);
    regina::python::add_eq_operators(s, rdoc::__eq);

    addLPInitialTableaux<LPConstraintNone>(m, "LPInitialTableaux");
    addLPInitialTableaux<LPConstraintEulerPositive>(m,
        "LPInitialTableaux_EulerPositive");
    addLPInitialTableaux<LPConstraintEulerZero>(m,
        "LPInitialTableaux_EulerZero");
    addLPInitialTableaux<LPConstraintNonSpun>(m, "LPInitialTableaux_NonSpun");

    addLPData<LPConstraintNone>(m, "LPData");
    addLPData<LPConstraintEulerPositive>(m, "LPData_EulerPositive");
    addLPData<LPConstraintEulerZero>(m, "LPData_EulerZero");
    addLPData<LPConstraintNonSpun>(m, "LPData_NonSpun");

    RDOC_SCOPE_END
}