|
17 | 17 | * along with LibPoly. If not, see <http://www.gnu.org/licenses/>. |
18 | 18 | */ |
19 | 19 |
|
20 | | -#define PY_SSIZE_T_CLEAN |
21 | | -#include "Python.h" |
| 20 | +#include "python.h" |
22 | 21 |
|
23 | | -#if PY_MAJOR_VERSION >= 3 |
24 | | -#include "polypy3.c" |
25 | | -#else |
26 | | -#include "polypy2.c" |
| 22 | +#include "polypyInteger.h" |
| 23 | +#include "polypyVariable.h" |
| 24 | +#include "polypyVariableOrder.h" |
| 25 | +#include "polypyUPolynomial.h" |
| 26 | +#include "polypyAlgebraicNumber.h" |
| 27 | +#include "polypyPolynomial.h" |
| 28 | +#include "polypyAssignment.h" |
| 29 | +#include "polypyValue.h" |
| 30 | +#include "polypyInterval.h" |
| 31 | +#include "polypyFeasibilitySet.h" |
| 32 | + |
| 33 | +static PyObject* |
| 34 | +Trace_enable(PyObject* self, PyObject* args) { |
| 35 | +#ifndef NDEBUG |
| 36 | + const char *tag; |
| 37 | + if (!PyArg_ParseTuple(args, "s", &tag)) { |
| 38 | + return NULL; |
| 39 | + } |
| 40 | + lp_trace_enable(tag); |
| 41 | +#endif |
| 42 | + Py_RETURN_NONE; |
| 43 | +} |
| 44 | + |
| 45 | +static PyObject* |
| 46 | +Trace_disable(PyObject* self, PyObject* args) { |
| 47 | +#ifndef NDEBUG |
| 48 | + const char *tag; |
| 49 | + if (!PyArg_ParseTuple(args, "s", &tag)) { |
| 50 | + return NULL; |
| 51 | + } |
| 52 | + lp_trace_disable(tag); |
| 53 | +#endif |
| 54 | + Py_RETURN_NONE; |
| 55 | +} |
| 56 | + |
| 57 | +static PyObject* |
| 58 | +Stats_print(PyObject* self) { |
| 59 | + lp_stats_print(stdout); |
| 60 | + Py_RETURN_NONE; |
| 61 | +} |
| 62 | + |
| 63 | +static PyMethodDef polypy_methods[] = { |
| 64 | + {"trace_enable", (PyCFunction)Trace_enable, METH_VARARGS, "Enables tracing for the given tag"}, |
| 65 | + {"trace_disable", (PyCFunction)Trace_disable, METH_VARARGS, "Disables tracing for the given tag"}, |
| 66 | + {"stats_print", (PyCFunction)Stats_print, METH_NOARGS, "Prints the statistics"}, |
| 67 | + {NULL} /* Sentinel */ |
| 68 | +}; |
| 69 | + |
| 70 | + |
| 71 | +static struct PyModuleDef polypymodule = { |
| 72 | + PyModuleDef_HEAD_INIT, |
| 73 | + "polypy", |
| 74 | + NULL, |
| 75 | + 0, // sizeof polypy struct |
| 76 | + polypy_methods, |
| 77 | + NULL, |
| 78 | + NULL, |
| 79 | + NULL |
| 80 | +}; |
| 81 | + |
| 82 | +#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ |
| 83 | +#define PyMODINIT_FUNC void |
27 | 84 | #endif |
| 85 | +PyMODINIT_FUNC |
| 86 | +PyInit_polypy(void) |
| 87 | +{ |
| 88 | + PyObject* m; |
| 89 | + |
| 90 | + if (PyType_Ready(&CoefficientRingType) < 0) |
| 91 | + return NULL; |
| 92 | + if (PyType_Ready(&VariableType) < 0) |
| 93 | + return NULL; |
| 94 | + if (PyType_Ready(&VariableOrderType) < 0) |
| 95 | + return NULL; |
| 96 | + if (PyType_Ready(&AssignmentType) < 0) |
| 97 | + return NULL; |
| 98 | + if (PyType_Ready(&PolynomialType) < 0) |
| 99 | + return NULL; |
| 100 | + if (PyType_Ready(&UPolynomialType) < 0) |
| 101 | + return NULL; |
| 102 | + if (PyType_Ready(&AlgebraicNumberType) < 0) |
| 103 | + return NULL; |
| 104 | + if (PyType_Ready(&ValueType) < 0) |
| 105 | + return NULL; |
| 106 | + if (PyType_Ready(&IntervalType) < 0) |
| 107 | + return NULL; |
| 108 | + if (PyType_Ready(&FeasibilitySetType) < 0) |
| 109 | + return NULL; |
| 110 | + |
| 111 | + m = PyModule_Create(&polypymodule); |
| 112 | + |
| 113 | + // Initialize the library |
| 114 | + lp_set_output_language(LP_OUTPUT_PYTHON); |
| 115 | + |
| 116 | + Py_INCREF(&CoefficientRingType); |
| 117 | + PyModule_AddObject(m, "CoefficientRing", (PyObject*)&CoefficientRingType); |
| 118 | + |
| 119 | + PyObject* PyZ = PyCoefficientRing_create(lp_Z); |
| 120 | + Py_INCREF(PyZ); |
| 121 | + PyModule_AddObject(m, "Z", PyZ); |
| 122 | + |
| 123 | + Py_INCREF(&VariableType); |
| 124 | + PyModule_AddObject(m, "Variable", (PyObject*)&VariableType); |
| 125 | + |
| 126 | + Py_INCREF(&VariableOrderType); |
| 127 | + PyModule_AddObject(m, "VariableOrder", (PyObject*)&VariableOrderType); |
| 128 | + |
| 129 | + PyObject* variable_order = VariableOrder_create(VariableOrder_get_default_order()); |
| 130 | + Py_INCREF(variable_order); |
| 131 | + PyModule_AddObject(m, "variable_order", variable_order); |
| 132 | + |
| 133 | + Py_INCREF(&PolynomialType); |
| 134 | + PyModule_AddObject(m, "Polynomial", (PyObject*)&PolynomialType); |
| 135 | + |
| 136 | + // Sign conditions |
| 137 | + PyObject* Py_SGN_LT_0 = PyLong_FromLong(LP_SGN_LT_0); |
| 138 | + PyObject* Py_SGN_LE_0 = PyLong_FromLong(LP_SGN_LE_0); |
| 139 | + PyObject* Py_SGN_EQ_0 = PyLong_FromLong(LP_SGN_EQ_0); |
| 140 | + PyObject* Py_SGN_NE_0 = PyLong_FromLong(LP_SGN_NE_0); |
| 141 | + PyObject* Py_SGN_GT_0 = PyLong_FromLong(LP_SGN_GT_0); |
| 142 | + PyObject* Py_SGN_GE_0 = PyLong_FromLong(LP_SGN_GE_0); |
| 143 | + PyModule_AddObject(m, "SGN_LT_0", Py_SGN_LT_0); |
| 144 | + PyModule_AddObject(m, "SGN_LE_0", Py_SGN_LE_0); |
| 145 | + PyModule_AddObject(m, "SGN_EQ_0", Py_SGN_EQ_0); |
| 146 | + PyModule_AddObject(m, "SGN_NE_0", Py_SGN_NE_0); |
| 147 | + PyModule_AddObject(m, "SGN_GT_0", Py_SGN_GT_0); |
| 148 | + PyModule_AddObject(m, "SGN_GE_0", Py_SGN_GE_0); |
| 149 | + |
| 150 | + Py_INCREF(&AssignmentType); |
| 151 | + PyModule_AddObject(m, "Assignment", (PyObject*)&AssignmentType); |
| 152 | + |
| 153 | + Py_INCREF(&UPolynomialType); |
| 154 | + PyModule_AddObject(m, "UPolynomial", (PyObject*)&UPolynomialType); |
| 155 | + |
| 156 | + int x_coeff[2] = { 0, 1 }; |
| 157 | + lp_upolynomial_t* x_poly = lp_upolynomial_construct_from_int(lp_Z, 1, x_coeff); |
| 158 | + PyObject* x = PyUPolynomial_create(x_poly); |
| 159 | + Py_INCREF(x); |
| 160 | + PyModule_AddObject(m, "x", x); |
| 161 | + |
| 162 | + Py_INCREF(&AlgebraicNumberType); |
| 163 | + PyModule_AddObject(m, "AlgebraicNumber", (PyObject*)&AlgebraicNumberType); |
| 164 | + |
| 165 | + Py_INCREF(&ValueType); |
| 166 | + PyModule_AddObject(m, "Value", (PyObject*)&ValueType); |
| 167 | + |
| 168 | + // Sign conditions |
| 169 | + lp_value_t value_inf_pos, value_inf_neg; |
| 170 | + lp_value_construct(&value_inf_pos, LP_VALUE_PLUS_INFINITY, NULL); |
| 171 | + lp_value_construct(&value_inf_neg, LP_VALUE_MINUS_INFINITY, NULL); |
| 172 | + PyObject* inf_pos = PyValue_create(&value_inf_pos); |
| 173 | + PyObject* inf_neg = PyValue_create(&value_inf_neg); |
| 174 | + PyModule_AddObject(m, "INFINITY_POS", inf_pos); |
| 175 | + PyModule_AddObject(m, "INFINITY_NEG", inf_neg); |
| 176 | + Py_INCREF(inf_pos); |
| 177 | + Py_INCREF(inf_neg); |
| 178 | + lp_value_destruct(&value_inf_pos); |
| 179 | + lp_value_destruct(&value_inf_neg); |
| 180 | + |
| 181 | + Py_INCREF(&IntervalType); |
| 182 | + PyModule_AddObject(m, "Interval", (PyObject*)&IntervalType); |
| 183 | + |
| 184 | + Py_INCREF(&FeasibilitySetType); |
| 185 | + PyModule_AddObject(m, "FeasibilitySet", (PyObject*)&FeasibilitySetType); |
| 186 | + |
| 187 | + return m; |
| 188 | +} |
0 commit comments