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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
/*-----------------------------------------------------------------------------
| Copyright (c) 2013-2019, Nucleic Development Team.
|
| Distributed under the terms of the Modified BSD License.
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
#include <sstream>
#include <cppy/cppy.h>
#include "symbolics.h"
#include "types.h"
#include "util.h"
namespace kiwisolver
{
namespace
{
PyObject*
Expression_new( PyTypeObject* type, PyObject* args, PyObject* kwargs )
{
static const char *kwlist[] = { "terms", "constant", 0 };
PyObject* pyterms;
PyObject* pyconstant = 0;
if( !PyArg_ParseTupleAndKeywords(
args, kwargs, "O|O:__new__", const_cast<char**>( kwlist ),
&pyterms, &pyconstant ) )
return 0;
cppy::ptr terms( PySequence_Tuple( pyterms ) );
if( !terms )
return 0;
Py_ssize_t end = PyTuple_GET_SIZE( terms.get() );
for( Py_ssize_t i = 0; i < end; ++i )
{
PyObject* item = PyTuple_GET_ITEM( terms.get(), i );
if( !Term::TypeCheck( item ) )
return cppy::type_error( item, "Term" );
}
double constant = 0.0;
if( pyconstant && !convert_to_double( pyconstant, constant ) )
return 0;
PyObject* pyexpr = PyType_GenericNew( type, args, kwargs );
if( !pyexpr )
return 0;
Expression* self = reinterpret_cast<Expression*>( pyexpr );
self->terms = terms.release();
self->constant = constant;
return pyexpr;
}
void
Expression_clear( Expression* self )
{
Py_CLEAR( self->terms );
}
int
Expression_traverse( Expression* self, visitproc visit, void* arg )
{
Py_VISIT( self->terms );
return 0;
}
void
Expression_dealloc( Expression* self )
{
PyObject_GC_UnTrack( self );
Expression_clear( self );
Py_TYPE( self )->tp_free( pyobject_cast( self ) );
}
PyObject*
Expression_repr( Expression* self )
{
std::stringstream stream;
Py_ssize_t end = PyTuple_GET_SIZE( self->terms );
for( Py_ssize_t i = 0; i < end; ++i )
{
PyObject* item = PyTuple_GET_ITEM( self->terms, i );
Term* term = reinterpret_cast<Term*>( item );
stream << term->coefficient << " * ";
stream << reinterpret_cast<Variable*>( term->variable )->variable.name();
stream << " + ";
}
stream << self->constant;
return PyUnicode_FromString( stream.str().c_str() );
}
PyObject*
Expression_terms( Expression* self )
{
return cppy::incref( self->terms );
}
PyObject*
Expression_constant( Expression* self )
{
return PyFloat_FromDouble( self->constant );
}
PyObject*
Expression_value( Expression* self )
{
double result = self->constant;
Py_ssize_t size = PyTuple_GET_SIZE( self->terms );
for( Py_ssize_t i = 0; i < size; ++i )
{
PyObject* item = PyTuple_GET_ITEM( self->terms, i );
Term* term = reinterpret_cast<Term*>( item );
Variable* pyvar = reinterpret_cast<Variable*>( term->variable );
result += term->coefficient * pyvar->variable.value();
}
return PyFloat_FromDouble( result );
}
PyObject*
Expression_add( PyObject* first, PyObject* second )
{
return BinaryInvoke<BinaryAdd, Expression>()( first, second );
}
PyObject*
Expression_sub( PyObject* first, PyObject* second )
{
return BinaryInvoke<BinarySub, Expression>()( first, second );
}
PyObject*
Expression_mul( PyObject* first, PyObject* second )
{
return BinaryInvoke<BinaryMul, Expression>()( first, second );
}
PyObject*
Expression_div( PyObject* first, PyObject* second )
{
return BinaryInvoke<BinaryDiv, Expression>()( first, second );
}
PyObject*
Expression_neg( PyObject* value )
{
return UnaryInvoke<UnaryNeg, Expression>()( value );
}
PyObject*
Expression_richcmp( PyObject* first, PyObject* second, int op )
{
switch( op )
{
case Py_EQ:
return BinaryInvoke<CmpEQ, Expression>()( first, second );
case Py_LE:
return BinaryInvoke<CmpLE, Expression>()( first, second );
case Py_GE:
return BinaryInvoke<CmpGE, Expression>()( first, second );
default:
break;
}
PyErr_Format(
PyExc_TypeError,
"unsupported operand type(s) for %s: "
"'%.100s' and '%.100s'",
pyop_str( op ),
first->ob_type->tp_name,
second->ob_type->tp_name
);
return 0;
}
static PyMethodDef
Expression_methods[] = {
{ "terms", ( PyCFunction )Expression_terms, METH_NOARGS,
"Get the tuple of terms for the expression." },
{ "constant", ( PyCFunction )Expression_constant, METH_NOARGS,
"Get the constant for the expression." },
{ "value", ( PyCFunction )Expression_value, METH_NOARGS,
"Get the value for the expression." },
{ 0 } // sentinel
};
static PyType_Slot Expression_Type_slots[] = {
{ Py_tp_dealloc, void_cast( Expression_dealloc ) }, /* tp_dealloc */
{ Py_tp_traverse, void_cast( Expression_traverse ) }, /* tp_traverse */
{ Py_tp_clear, void_cast( Expression_clear ) }, /* tp_clear */
{ Py_tp_repr, void_cast( Expression_repr ) }, /* tp_repr */
{ Py_tp_richcompare, void_cast( Expression_richcmp ) }, /* tp_richcompare */
{ Py_tp_methods, void_cast( Expression_methods ) }, /* tp_methods */
{ Py_tp_new, void_cast( Expression_new ) }, /* tp_new */
{ Py_tp_alloc, void_cast( PyType_GenericAlloc ) }, /* tp_alloc */
{ Py_tp_free, void_cast( PyObject_GC_Del ) }, /* tp_free */
{ Py_nb_add, void_cast( Expression_add ) }, /* nb_add */
{ Py_nb_subtract, void_cast( Expression_sub ) }, /* nb_sub */
{ Py_nb_multiply, void_cast( Expression_mul ) }, /* nb_mul */
{ Py_nb_negative, void_cast( Expression_neg ) }, /* nb_neg */
{ Py_nb_true_divide, void_cast( Expression_div ) }, /* nb_div */
{ 0, 0 },
};
} // namespace
// Initialize static variables (otherwise the compiler eliminates them)
PyTypeObject* Expression::TypeObject = NULL;
PyType_Spec Expression::TypeObject_Spec = {
"kiwisolver.Expression", /* tp_name */
sizeof( Expression ), /* tp_basicsize */
0, /* tp_itemsize */
Py_TPFLAGS_DEFAULT|
Py_TPFLAGS_HAVE_GC|
Py_TPFLAGS_BASETYPE, /* tp_flags */
Expression_Type_slots /* slots */
};
bool Expression::Ready()
{
// The reference will be handled by the module to which we will add the type
TypeObject = pytype_cast( PyType_FromSpec( &TypeObject_Spec ) );
if( !TypeObject )
{
return false;
}
return true;
}
} // namesapce kiwisolver
|