term.cpp 5.4 KB

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