aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/kiwisolver/py3/py/term.cpp
blob: 4be64a1eeafba258bea2ec4dc4b8710ceca82a3e (plain) (blame)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*-----------------------------------------------------------------------------
| Copyright (c) 2013-2017, Nucleic Development Team.
|
| Distributed under the terms of the Modified BSD License.
|
| The full license is in the file COPYING.txt, distributed with this software.
|----------------------------------------------------------------------------*/
#include <sstream>
#include <Python.h>
#include "pythonhelpers.h"
#include "symbolics.h"
#include "types.h"
#include "util.h"


using namespace PythonHelpers;


static PyObject*
Term_new( PyTypeObject* type, PyObject* args, PyObject* kwargs )
{
	static const char *kwlist[] = { "variable", "coefficient", 0 };
	PyObject* pyvar;
	PyObject* pycoeff = 0;
	if( !PyArg_ParseTupleAndKeywords(
		args, kwargs, "O|O:__new__", const_cast<char**>( kwlist ),
		&pyvar, &pycoeff ) )
		return 0;
	if( !Variable::TypeCheck( pyvar ) )
		return py_expected_type_fail( pyvar, "Variable" );
	double coefficient = 1.0;
	if( pycoeff && !convert_to_double( pycoeff, coefficient ) )
		return 0;
	PyObject* pyterm = PyType_GenericNew( type, args, kwargs );
	if( !pyterm )
		return 0;
	Term* self = reinterpret_cast<Term*>( pyterm );
	self->variable = newref( pyvar );
	self->coefficient = coefficient;
	return pyterm;
}


static void
Term_clear( Term* self )
{
	Py_CLEAR( self->variable );
}


static int
Term_traverse( Term* self, visitproc visit, void* arg )
{
	Py_VISIT( self->variable );
	return 0;
}


static void
Term_dealloc( Term* self )
{
	PyObject_GC_UnTrack( self );
	Term_clear( self );
	Py_TYPE( self )->tp_free( pyobject_cast( self ) );
}


static PyObject*
Term_repr( Term* self )
{
	std::stringstream stream;
	stream << self->coefficient << " * ";
	stream << reinterpret_cast<Variable*>( self->variable )->variable.name();
	return FROM_STRING( stream.str().c_str() );
}


static PyObject*
Term_variable( Term* self )
{
	return newref( self->variable );
}


static PyObject*
Term_coefficient( Term* self )
{
	return PyFloat_FromDouble( self->coefficient );
}


static PyObject*
Term_value( Term* self )
{
	Variable* pyvar = reinterpret_cast<Variable*>( self->variable );
	return PyFloat_FromDouble( self->coefficient * pyvar->variable.value() );
}


static PyObject*
Term_add( PyObject* first, PyObject* second )
{
	return BinaryInvoke<BinaryAdd, Term>()( first, second );
}


static PyObject*
Term_sub( PyObject* first, PyObject* second )
{
	return BinaryInvoke<BinarySub, Term>()( first, second );
}


static PyObject*
Term_mul( PyObject* first, PyObject* second )
{
	return BinaryInvoke<BinaryMul, Term>()( first, second );
}


static PyObject*
Term_div( PyObject* first, PyObject* second )
{
	return BinaryInvoke<BinaryDiv, Term>()( first, second );
}


static PyObject*
Term_neg( PyObject* value )
{
	return UnaryInvoke<UnaryNeg, Term>()( value );
}


static PyObject*
Term_richcmp( PyObject* first, PyObject* second, int op )
{
	switch( op )
	{
		case Py_EQ:
			return BinaryInvoke<CmpEQ, Term>()( first, second );
		case Py_LE:
			return BinaryInvoke<CmpLE, Term>()( first, second );
		case Py_GE:
			return BinaryInvoke<CmpGE, Term>()( 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
Term_methods[] = {
	{ "variable", ( PyCFunction )Term_variable, METH_NOARGS,
	  "Get the variable for the term." },
	{ "coefficient", ( PyCFunction )Term_coefficient, METH_NOARGS,
	  "Get the coefficient for the term." },
	{ "value", ( PyCFunction )Term_value, METH_NOARGS,
	  "Get the value for the term." },
	{ 0 } // sentinel
};


static PyNumberMethods
Term_as_number = {
	(binaryfunc)Term_add,       /* nb_add */
	(binaryfunc)Term_sub,       /* nb_subtract */
	(binaryfunc)Term_mul,       /* nb_multiply */
#if PY_MAJOR_VERSION < 3
	(binaryfunc)Term_div,       /* nb_divide */
#endif
	0,                          /* nb_remainder */
	0,                          /* nb_divmod */
	0,                          /* nb_power */
	(unaryfunc)Term_neg,        /* nb_negative */
	0,                          /* nb_positive */
	0,                          /* nb_absolute */
#if PY_MAJOR_VERSION >= 3
	0,                          /* nb_bool */
#else
	0,                          /* nb_nonzero */
#endif
	0,                          /* nb_invert */
	0,                          /* nb_lshift */
	0,                          /* nb_rshift */
	0,                          /* nb_and */
	0,                          /* nb_xor */
	(binaryfunc)0,              /* nb_or */
#if PY_MAJOR_VERSION < 3
	0,                          /* nb_coerce */
#endif
	0,                          /* nb_int */
	0,                          /* nb_long */
	0,                          /* nb_float */
#if PY_MAJOR_VERSION < 3
	0,                          /* nb_oct */
	0,                          /* nb_hex */
#endif
	0,                          /* nb_inplace_add */
	0,                          /* nb_inplace_subtract */
	0,                          /* nb_inplace_multiply */
#if PY_MAJOR_VERSION < 3
	0,                          /* nb_inplace_divide */
#endif
	0,                          /* nb_inplace_remainder */
	0,                          /* nb_inplace_power */
	0,                          /* nb_inplace_lshift */
	0,                          /* nb_inplace_rshift */
	0,                          /* nb_inplace_and */
	0,                          /* nb_inplace_xor */
	0,                          /* nb_inplace_or */
	(binaryfunc)0,              /* nb_floor_divide */
	(binaryfunc)Term_div,       /* nb_true_divide */
	0,                          /* nb_inplace_floor_divide */
	0,                          /* nb_inplace_true_divide */
#if PY_VERSION_HEX >= 0x02050000
	(unaryfunc)0,               /* nb_index */
#endif
#if PY_VERSION_HEX >= 0x03050000
	(binaryfunc)0,              /* nb_matrix_multiply */
	(binaryfunc)0,              /* nb_inplace_matrix_multiply */
#endif
};


PyTypeObject Term_Type = {
	PyVarObject_HEAD_INIT( &PyType_Type, 0 )
	"kiwisolver.Term",                      /* tp_name */
	sizeof( Term ),                         /* tp_basicsize */
	0,                                      /* tp_itemsize */
	(destructor)Term_dealloc,               /* tp_dealloc */
	(printfunc)0,                           /* tp_print */
	(getattrfunc)0,                         /* tp_getattr */
	(setattrfunc)0,                         /* tp_setattr */
#if PY_VERSION_HEX >= 0x03050000
	( PyAsyncMethods* )0,                   /* tp_as_async */
#elif PY_VERSION_HEX >= 0x03000000
	( void* ) 0,                            /* tp_reserved */
#else
	( cmpfunc )0,                           /* tp_compare */
#endif
	(reprfunc)Term_repr,                    /* tp_repr */
	(PyNumberMethods*)&Term_as_number,      /* tp_as_number */
	(PySequenceMethods*)0,                  /* tp_as_sequence */
	(PyMappingMethods*)0,                   /* tp_as_mapping */
	(hashfunc)0,                            /* tp_hash */
	(ternaryfunc)0,                         /* tp_call */
	(reprfunc)0,                            /* tp_str */
	(getattrofunc)0,                        /* tp_getattro */
	(setattrofunc)0,                        /* tp_setattro */
	(PyBufferProcs*)0,                      /* tp_as_buffer */
#if PY_MAJOR_VERSION >= 3
	Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC|Py_TPFLAGS_BASETYPE, /* tp_flags */
#else
	Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_CHECKTYPES, /* tp_flags */
#endif
	0,                                      /* Documentation string */
	(traverseproc)Term_traverse,            /* tp_traverse */
	(inquiry)Term_clear,                    /* tp_clear */
	(richcmpfunc)Term_richcmp,              /* tp_richcompare */
	0,                                      /* tp_weaklistoffset */
	(getiterfunc)0,                         /* tp_iter */
	(iternextfunc)0,                        /* tp_iternext */
	(struct PyMethodDef*)Term_methods,      /* tp_methods */
	(struct PyMemberDef*)0,                 /* tp_members */
	0,                                      /* tp_getset */
	0,                                      /* tp_base */
	0,                                      /* tp_dict */
	(descrgetfunc)0,                        /* tp_descr_get */
	(descrsetfunc)0,                        /* tp_descr_set */
	0,                                      /* tp_dictoffset */
	(initproc)0,                            /* tp_init */
	(allocfunc)PyType_GenericAlloc,         /* tp_alloc */
	(newfunc)Term_new,                      /* tp_new */
	(freefunc)PyObject_GC_Del,              /* tp_free */
	(inquiry)0,                             /* tp_is_gc */
	0,                                      /* tp_bases */
	0,                                      /* tp_mro */
	0,                                      /* tp_cache */
	0,                                      /* tp_subclasses */
	0,                                      /* tp_weaklist */
	(destructor)0                           /* tp_del */
};


int import_term()
{
	return PyType_Ready( &Term_Type );
}