| 1 |
/*----------------------------------------------------------------------------- |
| 2 |
| Copyright (c) 2013-2026, Nucleic Development Team. |
| 3 |
| |
| 4 |
| Distributed under the terms of the Modified BSD License. |
| 5 |
| |
| 6 |
| The full license is in the file LICENSE, distributed with this software. |
| 7 |
|----------------------------------------------------------------------------*/ |
| 8 |
#include <cppy/cppy.h> |
| 9 |
#include <sstream> |
| 10 |
#include "symbolics.h" |
| 11 |
#include "types.h" |
| 12 |
#include "util.h" |
| 13 |
|
| 14 |
|
| 15 |
namespace kiwisolver |
| 16 |
{ |
| 17 |
|
| 18 |
|
| 19 |
namespace |
| 20 |
{ |
| 21 |
|
| 22 |
|
| 23 |
PyObject* |
| 24 |
Term_new( PyTypeObject* type, PyObject* args, PyObject* kwargs ) |
| 25 |
{ |
| 26 |
static const char *kwlist[] = { "variable", "coefficient", 0 }; |
| 27 |
PyObject* pyvar; |
| 28 |
PyObject* pycoeff = 0; |
| 29 |
if( !PyArg_ParseTupleAndKeywords( |
| 30 |
args, kwargs, "O|O:__new__", const_cast<char**>( kwlist ), |
| 31 |
&pyvar, &pycoeff ) ) |
| 32 |
return 0; |
| 33 |
if( !Variable::TypeCheck( pyvar ) ) |
| 34 |
return cppy::type_error( pyvar, "Variable" ); |
| 35 |
double coefficient = 1.0; |
| 36 |
if( pycoeff && !convert_to_double( pycoeff, coefficient ) ) |
| 37 |
return 0; |
| 38 |
PyObject* pyterm = PyType_GenericNew( type, args, kwargs ); |
| 39 |
if( !pyterm ) |
| 40 |
return 0; |
| 41 |
Term* self = reinterpret_cast<Term*>( pyterm ); |
| 42 |
self->variable = cppy::incref( pyvar ); |
| 43 |
self->coefficient = coefficient; |
| 44 |
return pyterm; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
void |
| 49 |
Term_clear( Term* self ) |
| 50 |
{ |
| 51 |
Py_CLEAR( self->variable ); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
int |
| 56 |
Term_traverse( Term* self, visitproc visit, void* arg ) |
| 57 |
{ |
| 58 |
Py_VISIT( self->variable ); |
| 59 |
#if PY_VERSION_HEX >= 0x03090000 |
| 60 |
// This was not needed before Python 3.9 (Python issue 35810 and 40217) |
| 61 |
Py_VISIT(Py_TYPE(self)); |
| 62 |
#endif |
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
void |
| 68 |
Term_dealloc( Term* self ) |
| 69 |
{ |
| 70 |
PyObject_GC_UnTrack( self ); |
| 71 |
Term_clear( self ); |
| 72 |
Py_TYPE( self )->tp_free( pyobject_cast( self ) ); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
PyObject* |
| 77 |
Term_repr( Term* self ) |
| 78 |
{ |
| 79 |
std::stringstream stream; |
| 80 |
stream << self->coefficient << " * "; |
| 81 |
ACQUIRE_GLOBAL_LOCK(); |
| 82 |
std::string name = reinterpret_cast<Variable*>( self->variable )->variable.name(); |
| 83 |
RELEASE_GLOBAL_LOCK(); |
| 84 |
stream << name; |
| 85 |
return PyUnicode_FromString( stream.str().c_str() ); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
PyObject* |
| 90 |
Term_variable( Term* self ) |
| 91 |
{ |
| 92 |
return cppy::incref( self->variable ); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
PyObject* |
| 97 |
Term_coefficient( Term* self ) |
| 98 |
{ |
| 99 |
return PyFloat_FromDouble( self->coefficient ); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
PyObject* |
| 104 |
Term_value( Term* self ) |
| 105 |
{ |
| 106 |
Variable* pyvar = reinterpret_cast<Variable*>( self->variable ); |
| 107 |
ACQUIRE_GLOBAL_LOCK(); |
| 108 |
double value = pyvar->variable.value(); |
| 109 |
RELEASE_GLOBAL_LOCK(); |
| 110 |
return PyFloat_FromDouble( self->coefficient * value ); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
PyObject* |
| 115 |
Term_add( PyObject* first, PyObject* second ) |
| 116 |
{ |
| 117 |
return BinaryInvoke<BinaryAdd, Term>()( first, second ); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
PyObject* |
| 122 |
Term_sub( PyObject* first, PyObject* second ) |
| 123 |
{ |
| 124 |
return BinaryInvoke<BinarySub, Term>()( first, second ); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
PyObject* |
| 129 |
Term_mul( PyObject* first, PyObject* second ) |
| 130 |
{ |
| 131 |
return BinaryInvoke<BinaryMul, Term>()( first, second ); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
PyObject* |
| 136 |
Term_div( PyObject* first, PyObject* second ) |
| 137 |
{ |
| 138 |
return BinaryInvoke<BinaryDiv, Term>()( first, second ); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
PyObject* |
| 143 |
Term_neg( PyObject* value ) |
| 144 |
{ |
| 145 |
return UnaryInvoke<UnaryNeg, Term>()( value ); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
PyObject* |
| 150 |
Term_richcmp( PyObject* first, PyObject* second, int op ) |
| 151 |
{ |
| 152 |
switch( op ) |
| 153 |
{ |
| 154 |
case Py_EQ: |
| 155 |
return BinaryInvoke<CmpEQ, Term>()( first, second ); |
| 156 |
case Py_LE: |
| 157 |
return BinaryInvoke<CmpLE, Term>()( first, second ); |
| 158 |
case Py_GE: |
| 159 |
return BinaryInvoke<CmpGE, Term>()( first, second ); |
| 160 |
default: |
| 161 |
break; |
| 162 |
} |
| 163 |
PyErr_Format( |
| 164 |
PyExc_TypeError, |
| 165 |
"unsupported operand type(s) for %s: " |
| 166 |
"'%.100s' and '%.100s'", |
| 167 |
pyop_str( op ), |
| 168 |
Py_TYPE( first )->tp_name, |
| 169 |
Py_TYPE( second )->tp_name |
| 170 |
); |
| 171 |
return 0; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
static PyMethodDef |
| 176 |
Term_methods[] = { |
| 177 |
{ "variable", ( PyCFunction )Term_variable, METH_NOARGS, |
| 178 |
"Get the variable for the term." }, |
| 179 |
{ "coefficient", ( PyCFunction )Term_coefficient, METH_NOARGS, |
| 180 |
"Get the coefficient for the term." }, |
| 181 |
{ "value", ( PyCFunction )Term_value, METH_NOARGS, |
| 182 |
"Get the value for the term." }, |
| 183 |
{ 0 } // sentinel |
| 184 |
}; |
| 185 |
|
| 186 |
|
| 187 |
static PyType_Slot Term_Type_slots[] = { |
| 188 |
{ Py_tp_dealloc, void_cast( Term_dealloc ) }, /* tp_dealloc */ |
| 189 |
{ Py_tp_traverse, void_cast( Term_traverse ) }, /* tp_traverse */ |
| 190 |
{ Py_tp_clear, void_cast( Term_clear ) }, /* tp_clear */ |
| 191 |
{ Py_tp_repr, void_cast( Term_repr ) }, /* tp_repr */ |
| 192 |
{ Py_tp_richcompare, void_cast( Term_richcmp ) }, /* tp_richcompare */ |
| 193 |
{ Py_tp_methods, void_cast( Term_methods ) }, /* tp_methods */ |
| 194 |
{ Py_tp_new, void_cast( Term_new ) }, /* tp_new */ |
| 195 |
{ Py_tp_alloc, void_cast( PyType_GenericAlloc ) }, /* tp_alloc */ |
| 196 |
{ Py_tp_free, void_cast( PyObject_GC_Del ) }, /* tp_free */ |
| 197 |
{ Py_nb_add, void_cast( Term_add ) }, /* nb_add */ |
| 198 |
{ Py_nb_subtract, void_cast( Term_sub ) }, /* nb_subatract */ |
| 199 |
{ Py_nb_multiply, void_cast( Term_mul ) }, /* nb_multiply */ |
| 200 |
{ Py_nb_negative, void_cast( Term_neg ) }, /* nb_negative */ |
| 201 |
{ Py_nb_true_divide, void_cast( Term_div ) }, /* nb_true_divide */ |
| 202 |
{ 0, 0 }, |
| 203 |
}; |
| 204 |
|
| 205 |
|
| 206 |
} // namespace |
| 207 |
|
| 208 |
|
| 209 |
// Initialize static variables (otherwise the compiler eliminates them) |
| 210 |
PyTypeObject* Term::TypeObject = NULL; |
| 211 |
|
| 212 |
|
| 213 |
PyType_Spec Term::TypeObject_Spec = { |
| 214 |
"kiwisolver.Term", /* tp_name */ |
| 215 |
sizeof( Term ), /* tp_basicsize */ |
| 216 |
0, /* tp_itemsize */ |
| 217 |
Py_TPFLAGS_DEFAULT| |
| 218 |
Py_TPFLAGS_HAVE_GC| |
| 219 |
Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 220 |
Term_Type_slots /* slots */ |
| 221 |
}; |
| 222 |
|
| 223 |
|
| 224 |
bool Term::Ready() |
| 225 |
{ |
| 226 |
// The reference will be handled by the module to which we will add the type |
| 227 |
TypeObject = pytype_cast( PyType_FromSpec( &TypeObject_Spec ) ); |
| 228 |
if( !TypeObject ) |
| 229 |
{ |
| 230 |
return false; |
| 231 |
} |
| 232 |
return true; |
| 233 |
} |
| 234 |
|
| 235 |
} // namespace kiwisolver |