Debian Codegraph @

py/src/expression.cpp

raw
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 namespace
19 {
20
21 PyObject*
22 Expression_new( PyTypeObject* type, PyObject* args, PyObject* kwargs )
23 {
24 static const char *kwlist[] = { "terms", "constant", 0 };
25 PyObject* pyterms;
26 PyObject* pyconstant = 0;
27 if( !PyArg_ParseTupleAndKeywords(
28 args, kwargs, "O|O:__new__", const_cast<char**>( kwlist ),
29 &pyterms, &pyconstant ) )
30 return 0;
31 cppy::ptr terms( PySequence_Tuple( pyterms ) );
32 if( !terms )
33 return 0;
34 Py_ssize_t end = PyTuple_GET_SIZE( terms.get() );
35 for( Py_ssize_t i = 0; i < end; ++i )
36 {
37 PyObject* item = PyTuple_GET_ITEM( terms.get(), i );
38 if( !Term::TypeCheck( item ) )
39 return cppy::type_error( item, "Term" );
40 }
41 double constant = 0.0;
42 if( pyconstant && !convert_to_double( pyconstant, constant ) )
43 return 0;
44 PyObject* pyexpr = PyType_GenericNew( type, args, kwargs );
45 if( !pyexpr )
46 return 0;
47 Expression* self = reinterpret_cast<Expression*>( pyexpr );
48 self->terms = terms.release();
49 self->constant = constant;
50 return pyexpr;
51 }
52
53
54 void
55 Expression_clear( Expression* self )
56 {
57 Py_CLEAR( self->terms );
58 }
59
60
61 int
62 Expression_traverse( Expression* self, visitproc visit, void* arg )
63 {
64 Py_VISIT( self->terms );
65 #if PY_VERSION_HEX >= 0x03090000
66 // This was not needed before Python 3.9 (Python issue 35810 and 40217)
67 Py_VISIT(Py_TYPE(self));
68 #endif
69 return 0;
70 }
71
72
73 void
74 Expression_dealloc( Expression* self )
75 {
76 PyObject_GC_UnTrack( self );
77 Expression_clear( self );
78 Py_TYPE( self )->tp_free( pyobject_cast( self ) );
79 }
80
81
82 PyObject*
83 Expression_repr( Expression* self )
84 {
85 std::stringstream stream;
86 Py_ssize_t end = PyTuple_GET_SIZE( self->terms );
87 for( Py_ssize_t i = 0; i < end; ++i )
88 {
89 PyObject* item = PyTuple_GET_ITEM( self->terms, i );
90 Term* term = reinterpret_cast<Term*>( item );
91 stream << term->coefficient << " * ";
92 ACQUIRE_GLOBAL_LOCK();
93 std::string name = reinterpret_cast<Variable*>( term->variable )->variable.name();
94 RELEASE_GLOBAL_LOCK();
95 stream << name;
96 stream << " + ";
97 }
98 stream << self->constant;
99 return PyUnicode_FromString( stream.str().c_str() );
100 }
101
102
103 PyObject*
104 Expression_terms( Expression* self )
105 {
106 return cppy::incref( self->terms );
107 }
108
109
110 PyObject*
111 Expression_constant( Expression* self )
112 {
113 return PyFloat_FromDouble( self->constant );
114 }
115
116
117 PyObject*
118 Expression_value( Expression* self )
119 {
120 double result = self->constant;
121 Py_ssize_t size = PyTuple_GET_SIZE( self->terms );
122 for( Py_ssize_t i = 0; i < size; ++i )
123 {
124 PyObject* item = PyTuple_GET_ITEM( self->terms, i );
125 Term* term = reinterpret_cast<Term*>( item );
126 Variable* pyvar = reinterpret_cast<Variable*>( term->variable );
127 ACQUIRE_GLOBAL_LOCK();
128 double value = pyvar->variable.value();
129 RELEASE_GLOBAL_LOCK();
130 result += term->coefficient * value;
131 }
132 return PyFloat_FromDouble( result );
133 }
134
135
136 PyObject*
137 Expression_add( PyObject* first, PyObject* second )
138 {
139 return BinaryInvoke<BinaryAdd, Expression>()( first, second );
140 }
141
142
143 PyObject*
144 Expression_sub( PyObject* first, PyObject* second )
145 {
146 return BinaryInvoke<BinarySub, Expression>()( first, second );
147 }
148
149
150 PyObject*
151 Expression_mul( PyObject* first, PyObject* second )
152 {
153 return BinaryInvoke<BinaryMul, Expression>()( first, second );
154 }
155
156
157 PyObject*
158 Expression_div( PyObject* first, PyObject* second )
159 {
160 return BinaryInvoke<BinaryDiv, Expression>()( first, second );
161 }
162
163
164 PyObject*
165 Expression_neg( PyObject* value )
166 {
167 return UnaryInvoke<UnaryNeg, Expression>()( value );
168 }
169
170
171 PyObject*
172 Expression_richcmp( PyObject* first, PyObject* second, int op )
173 {
174 switch( op )
175 {
176 case Py_EQ:
177 return BinaryInvoke<CmpEQ, Expression>()( first, second );
178 case Py_LE:
179 return BinaryInvoke<CmpLE, Expression>()( first, second );
180 case Py_GE:
181 return BinaryInvoke<CmpGE, Expression>()( first, second );
182 default:
183 break;
184 }
185 PyErr_Format(
186 PyExc_TypeError,
187 "unsupported operand type(s) for %s: "
188 "'%.100s' and '%.100s'",
189 pyop_str( op ),
190 Py_TYPE( first )->tp_name,
191 Py_TYPE( second )->tp_name
192 );
193 return 0;
194 }
195
196
197 static PyMethodDef
198 Expression_methods[] = {
199 { "terms", ( PyCFunction )Expression_terms, METH_NOARGS,
200 "Get the tuple of terms for the expression." },
201 { "constant", ( PyCFunction )Expression_constant, METH_NOARGS,
202 "Get the constant for the expression." },
203 { "value", ( PyCFunction )Expression_value, METH_NOARGS,
204 "Get the value for the expression." },
205 { 0 } // sentinel
206 };
207
208
209 static PyType_Slot Expression_Type_slots[] = {
210 { Py_tp_dealloc, void_cast( Expression_dealloc ) }, /* tp_dealloc */
211 { Py_tp_traverse, void_cast( Expression_traverse ) }, /* tp_traverse */
212 { Py_tp_clear, void_cast( Expression_clear ) }, /* tp_clear */
213 { Py_tp_repr, void_cast( Expression_repr ) }, /* tp_repr */
214 { Py_tp_richcompare, void_cast( Expression_richcmp ) }, /* tp_richcompare */
215 { Py_tp_methods, void_cast( Expression_methods ) }, /* tp_methods */
216 { Py_tp_new, void_cast( Expression_new ) }, /* tp_new */
217 { Py_tp_alloc, void_cast( PyType_GenericAlloc ) }, /* tp_alloc */
218 { Py_tp_free, void_cast( PyObject_GC_Del ) }, /* tp_free */
219 { Py_nb_add, void_cast( Expression_add ) }, /* nb_add */
220 { Py_nb_subtract, void_cast( Expression_sub ) }, /* nb_sub */
221 { Py_nb_multiply, void_cast( Expression_mul ) }, /* nb_mul */
222 { Py_nb_negative, void_cast( Expression_neg ) }, /* nb_neg */
223 { Py_nb_true_divide, void_cast( Expression_div ) }, /* nb_div */
224 { 0, 0 },
225 };
226
227
228 } // namespace
229
230
231 // Initialize static variables (otherwise the compiler eliminates them)
232 PyTypeObject* Expression::TypeObject = NULL;
233
234
235 PyType_Spec Expression::TypeObject_Spec = {
236 "kiwisolver.Expression", /* tp_name */
237 sizeof( Expression ), /* tp_basicsize */
238 0, /* tp_itemsize */
239 Py_TPFLAGS_DEFAULT|
240 Py_TPFLAGS_HAVE_GC|
241 Py_TPFLAGS_BASETYPE, /* tp_flags */
242 Expression_Type_slots /* slots */
243 };
244
245
246 bool Expression::Ready()
247 {
248 // The reference will be handled by the module to which we will add the type
249 TypeObject = pytype_cast( PyType_FromSpec( &TypeObject_Spec ) );
250 if( !TypeObject )
251 {
252 return false;
253 }
254 return true;
255 }
256
257 } // namesapce kiwisolver