symbolics.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) 2013-2017, Nucleic Development Team.
  3. |
  4. | Distributed under the terms of the Modified BSD License.
  5. |
  6. | The full license is in the file COPYING.txt, distributed with this software.
  7. |----------------------------------------------------------------------------*/
  8. #pragma once
  9. #include <Python.h>
  10. #include "pythonhelpers.h"
  11. #include "types.h"
  12. #include "util.h"
  13. template<typename Op, typename T>
  14. struct UnaryInvoke
  15. {
  16. PyObject* operator()( PyObject* value )
  17. {
  18. return Op()( reinterpret_cast<T*>( value ) );
  19. }
  20. };
  21. template<typename Op, typename T>
  22. struct BinaryInvoke
  23. {
  24. PyObject* operator()( PyObject* first, PyObject* second )
  25. {
  26. if( T::TypeCheck( first ) )
  27. return invoke<Normal>( reinterpret_cast<T*>( first ), second );
  28. return invoke<Reverse>( reinterpret_cast<T*>( second ), first );
  29. }
  30. struct Normal
  31. {
  32. template<typename U>
  33. PyObject* operator()( T* primary, U secondary )
  34. {
  35. return Op()( primary, secondary );
  36. }
  37. };
  38. struct Reverse
  39. {
  40. template<typename U>
  41. PyObject* operator()( T* primary, U secondary )
  42. {
  43. return Op()( secondary, primary );
  44. }
  45. };
  46. template<typename Invk>
  47. PyObject* invoke( T* primary, PyObject* secondary )
  48. {
  49. if( Expression::TypeCheck( secondary ) )
  50. return Invk()( primary, reinterpret_cast<Expression*>( secondary ) );
  51. if( Term::TypeCheck( secondary ) )
  52. return Invk()( primary, reinterpret_cast<Term*>( secondary ) );
  53. if( Variable::TypeCheck( secondary ) )
  54. return Invk()( primary, reinterpret_cast<Variable*>( secondary ) );
  55. if( PyFloat_Check( secondary ) )
  56. return Invk()( primary, PyFloat_AS_DOUBLE( secondary ) );
  57. #if PY_MAJOR_VERSION < 3
  58. if( PyInt_Check( secondary ) )
  59. return Invk()( primary, double( PyInt_AS_LONG( secondary ) ) );
  60. #endif
  61. if( PyLong_Check( secondary ) )
  62. {
  63. double v = PyLong_AsDouble( secondary );
  64. if( v == -1 && PyErr_Occurred() )
  65. return 0;
  66. return Invk()( primary, v );
  67. }
  68. Py_RETURN_NOTIMPLEMENTED;
  69. }
  70. };
  71. struct BinaryMul
  72. {
  73. template<typename T, typename U>
  74. PyObject* operator()( T first, U second )
  75. {
  76. Py_RETURN_NOTIMPLEMENTED;
  77. }
  78. };
  79. template<> inline
  80. PyObject* BinaryMul::operator()( Variable* first, double second )
  81. {
  82. PyObject* pyterm = PyType_GenericNew( &Term_Type, 0, 0 );
  83. if( !pyterm )
  84. return 0;
  85. Term* term = reinterpret_cast<Term*>( pyterm );
  86. term->variable = PythonHelpers::newref( pyobject_cast( first ) );
  87. term->coefficient = second;
  88. return pyterm;
  89. }
  90. template<> inline
  91. PyObject* BinaryMul::operator()( Term* first, double second )
  92. {
  93. PyObject* pyterm = PyType_GenericNew( &Term_Type, 0, 0 );
  94. if( !pyterm )
  95. return 0;
  96. Term* term = reinterpret_cast<Term*>( pyterm );
  97. term->variable = PythonHelpers::newref( first->variable );
  98. term->coefficient = first->coefficient * second;
  99. return pyterm;
  100. }
  101. template<> inline
  102. PyObject* BinaryMul::operator()( Expression* first, double second )
  103. {
  104. using namespace PythonHelpers;
  105. PyObjectPtr pyexpr( PyType_GenericNew( &Expression_Type, 0, 0 ) );
  106. if( !pyexpr )
  107. return 0;
  108. Expression* expr = reinterpret_cast<Expression*>( pyexpr.get() );
  109. PyObjectPtr terms( PyTuple_New( PyTuple_GET_SIZE( first->terms ) ) );
  110. if( !terms )
  111. return 0;
  112. Py_ssize_t end = PyTuple_GET_SIZE( first->terms );
  113. for( Py_ssize_t i = 0; i < end; ++i ) // memset 0 for safe error return
  114. PyTuple_SET_ITEM( terms.get(), i, 0 );
  115. for( Py_ssize_t i = 0; i < end; ++i )
  116. {
  117. PyObject* item = PyTuple_GET_ITEM( first->terms, i );
  118. PyObject* term = BinaryMul()( reinterpret_cast<Term*>( item ), second );
  119. if( !term )
  120. return 0;
  121. PyTuple_SET_ITEM( terms.get(), i, term );
  122. }
  123. expr->terms = terms.release();
  124. expr->constant = first->constant * second;
  125. return pyexpr.release();
  126. }
  127. template<> inline
  128. PyObject* BinaryMul::operator()( double first, Variable* second )
  129. {
  130. return operator()( second, first );
  131. }
  132. template<> inline
  133. PyObject* BinaryMul::operator()( double first, Term* second )
  134. {
  135. return operator()( second, first );
  136. }
  137. template<> inline
  138. PyObject* BinaryMul::operator()( double first, Expression* second )
  139. {
  140. return operator()( second, first );
  141. }
  142. struct BinaryDiv
  143. {
  144. template<typename T, typename U>
  145. PyObject* operator()( T first, U second )
  146. {
  147. Py_RETURN_NOTIMPLEMENTED;
  148. }
  149. };
  150. template<> inline
  151. PyObject* BinaryDiv::operator()( Variable* first, double second )
  152. {
  153. if( second == 0.0 )
  154. {
  155. PyErr_SetString( PyExc_ZeroDivisionError, "float division by zero" );
  156. return 0;
  157. }
  158. return BinaryMul()( first, 1.0 / second );
  159. }
  160. template<> inline
  161. PyObject* BinaryDiv::operator()( Term* first, double second )
  162. {
  163. if( second == 0.0 )
  164. {
  165. PyErr_SetString( PyExc_ZeroDivisionError, "float division by zero" );
  166. return 0;
  167. }
  168. return BinaryMul()( first, 1.0 / second );
  169. }
  170. template<> inline
  171. PyObject* BinaryDiv::operator()( Expression* first, double second )
  172. {
  173. if( second == 0.0 )
  174. {
  175. PyErr_SetString( PyExc_ZeroDivisionError, "float division by zero" );
  176. return 0;
  177. }
  178. return BinaryMul()( first, 1.0 / second );
  179. }
  180. struct UnaryNeg
  181. {
  182. template<typename T>
  183. PyObject* operator()( T value )
  184. {
  185. Py_RETURN_NOTIMPLEMENTED;
  186. }
  187. };
  188. template<> inline
  189. PyObject* UnaryNeg::operator()( Variable* value )
  190. {
  191. return BinaryMul()( value, -1.0 );
  192. }
  193. template<> inline
  194. PyObject* UnaryNeg::operator()( Term* value )
  195. {
  196. return BinaryMul()( value, -1.0 );
  197. }
  198. template<> inline
  199. PyObject* UnaryNeg::operator()( Expression* value )
  200. {
  201. return BinaryMul()( value, -1.0 );
  202. }
  203. struct BinaryAdd
  204. {
  205. template<typename T, typename U>
  206. PyObject* operator()( T first, U second )
  207. {
  208. Py_RETURN_NOTIMPLEMENTED;
  209. }
  210. };
  211. template<> inline
  212. PyObject* BinaryAdd::operator()( Expression* first, Expression* second )
  213. {
  214. PythonHelpers::PyObjectPtr pyexpr( PyType_GenericNew( &Expression_Type, 0, 0 ) );
  215. if( !pyexpr )
  216. return 0;
  217. Expression* expr = reinterpret_cast<Expression*>( pyexpr.get() );
  218. expr->constant = first->constant + second->constant;
  219. expr->terms = PySequence_Concat( first->terms, second->terms );
  220. if( !expr->terms )
  221. return 0;
  222. return pyexpr.release();
  223. }
  224. template<> inline
  225. PyObject* BinaryAdd::operator()( Expression* first, Term* second )
  226. {
  227. using namespace PythonHelpers;
  228. PyObjectPtr pyexpr( PyType_GenericNew( &Expression_Type, 0, 0 ) );
  229. if( !pyexpr )
  230. return 0;
  231. PyObject* terms = PyTuple_New( PyTuple_GET_SIZE( first->terms ) + 1 );
  232. if( !terms )
  233. return 0;
  234. Py_ssize_t end = PyTuple_GET_SIZE( first->terms );
  235. for( Py_ssize_t i = 0; i < end; ++i )
  236. {
  237. PyObject* item = PyTuple_GET_ITEM( first->terms, i );
  238. PyTuple_SET_ITEM( terms, i, newref( item ) );
  239. }
  240. PyTuple_SET_ITEM( terms, end, newref( pyobject_cast( second ) ) );
  241. Expression* expr = reinterpret_cast<Expression*>( pyexpr.get() );
  242. expr->terms = terms;
  243. expr->constant = first->constant;
  244. return pyexpr.release();
  245. }
  246. template<> inline
  247. PyObject* BinaryAdd::operator()( Expression* first, Variable* second )
  248. {
  249. PythonHelpers::PyObjectPtr temp( BinaryMul()( second, 1.0 ) );
  250. if( !temp )
  251. return 0;
  252. return operator()( first, reinterpret_cast<Term*>( temp.get() ) );
  253. }
  254. template<> inline
  255. PyObject* BinaryAdd::operator()( Expression* first, double second )
  256. {
  257. using namespace PythonHelpers;
  258. PyObjectPtr pyexpr( PyType_GenericNew( &Expression_Type, 0, 0 ) );
  259. if( !pyexpr )
  260. return 0;
  261. Expression* expr = reinterpret_cast<Expression*>( pyexpr.get() );
  262. expr->terms = newref( first->terms );
  263. expr->constant = first->constant + second;
  264. return pyexpr.release();
  265. }
  266. template<> inline
  267. PyObject* BinaryAdd::operator()( Term* first, double second )
  268. {
  269. PythonHelpers::PyObjectPtr pyexpr( PyType_GenericNew( &Expression_Type, 0, 0 ) );
  270. if( !pyexpr )
  271. return 0;
  272. Expression* expr = reinterpret_cast<Expression*>( pyexpr.get() );
  273. expr->constant = second;
  274. expr->terms = PyTuple_Pack( 1, first );
  275. if( !expr->terms )
  276. return 0;
  277. return pyexpr.release();
  278. }
  279. template<> inline
  280. PyObject* BinaryAdd::operator()( Term* first, Expression* second )
  281. {
  282. return operator()( second, first );
  283. }
  284. template<> inline
  285. PyObject* BinaryAdd::operator()( Term* first, Term* second )
  286. {
  287. PythonHelpers::PyObjectPtr pyexpr( PyType_GenericNew( &Expression_Type, 0, 0 ) );
  288. if( !pyexpr )
  289. return 0;
  290. Expression* expr = reinterpret_cast<Expression*>( pyexpr.get() );
  291. expr->constant = 0.0;
  292. expr->terms = PyTuple_Pack( 2, first, second );
  293. if( !expr->terms )
  294. return 0;
  295. return pyexpr.release();
  296. }
  297. template<> inline
  298. PyObject* BinaryAdd::operator()( Term* first, Variable* second )
  299. {
  300. PythonHelpers::PyObjectPtr temp( BinaryMul()( second, 1.0 ) );
  301. if( !temp )
  302. return 0;
  303. return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) );
  304. }
  305. template<> inline
  306. PyObject* BinaryAdd::operator()( Variable* first, double second )
  307. {
  308. PythonHelpers::PyObjectPtr temp( BinaryMul()( first, 1.0 ) );
  309. if( !temp )
  310. return 0;
  311. return operator()( reinterpret_cast<Term*>( temp.get() ), second );
  312. }
  313. template<> inline
  314. PyObject* BinaryAdd::operator()( Variable* first, Variable* second )
  315. {
  316. PythonHelpers::PyObjectPtr temp( BinaryMul()( first, 1.0 ) );
  317. if( !temp )
  318. return 0;
  319. return operator()( reinterpret_cast<Term*>( temp.get() ), second );
  320. }
  321. template<> inline
  322. PyObject* BinaryAdd::operator()( Variable* first, Term* second )
  323. {
  324. PythonHelpers::PyObjectPtr temp( BinaryMul()( first, 1.0 ) );
  325. if( !temp )
  326. return 0;
  327. return operator()( reinterpret_cast<Term*>( temp.get() ), second );
  328. }
  329. template<> inline
  330. PyObject* BinaryAdd::operator()( Variable* first, Expression* second )
  331. {
  332. PythonHelpers::PyObjectPtr temp( BinaryMul()( first, 1.0 ) );
  333. if( !temp )
  334. return 0;
  335. return operator()( reinterpret_cast<Term*>( temp.get() ), second );
  336. }
  337. template<> inline
  338. PyObject* BinaryAdd::operator()( double first, Variable* second )
  339. {
  340. return operator()( second, first );
  341. }
  342. template<> inline
  343. PyObject* BinaryAdd::operator()( double first, Term* second )
  344. {
  345. return operator()( second, first );
  346. }
  347. template<> inline
  348. PyObject* BinaryAdd::operator()( double first, Expression* second )
  349. {
  350. return operator()( second, first );
  351. }
  352. struct BinarySub
  353. {
  354. template<typename T, typename U>
  355. PyObject* operator()( T first, U second )
  356. {
  357. Py_RETURN_NOTIMPLEMENTED;
  358. }
  359. };
  360. template<> inline
  361. PyObject* BinarySub::operator()( Variable* first, double second )
  362. {
  363. return BinaryAdd()( first, -second );
  364. }
  365. template<> inline
  366. PyObject* BinarySub::operator()( Variable* first, Variable* second )
  367. {
  368. PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) );
  369. if( !temp )
  370. return 0;
  371. return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) );
  372. }
  373. template<> inline
  374. PyObject* BinarySub::operator()( Variable* first, Term* second )
  375. {
  376. PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) );
  377. if( !temp )
  378. return 0;
  379. return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) );
  380. }
  381. template<> inline
  382. PyObject* BinarySub::operator()( Variable* first, Expression* second )
  383. {
  384. PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) );
  385. if( !temp )
  386. return 0;
  387. return BinaryAdd()( first, reinterpret_cast<Expression*>( temp.get() ) );
  388. }
  389. template<> inline
  390. PyObject* BinarySub::operator()( Term* first, double second )
  391. {
  392. return BinaryAdd()( first, -second );
  393. }
  394. template<> inline
  395. PyObject* BinarySub::operator()( Term* first, Variable* second )
  396. {
  397. PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) );
  398. if( !temp )
  399. return 0;
  400. return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) );
  401. }
  402. template<> inline
  403. PyObject* BinarySub::operator()( Term* first, Term* second )
  404. {
  405. PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) );
  406. if( !temp )
  407. return 0;
  408. return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) );
  409. }
  410. template<> inline
  411. PyObject* BinarySub::operator()( Term* first, Expression* second )
  412. {
  413. PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) );
  414. if( !temp )
  415. return 0;
  416. return BinaryAdd()( first, reinterpret_cast<Expression*>( temp.get() ) );
  417. }
  418. template<> inline
  419. PyObject* BinarySub::operator()( Expression* first, double second )
  420. {
  421. return BinaryAdd()( first, -second );
  422. }
  423. template<> inline
  424. PyObject* BinarySub::operator()( Expression* first, Variable* second )
  425. {
  426. PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) );
  427. if( !temp )
  428. return 0;
  429. return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) );
  430. }
  431. template<> inline
  432. PyObject* BinarySub::operator()( Expression* first, Term* second )
  433. {
  434. PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) );
  435. if( !temp )
  436. return 0;
  437. return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) );
  438. }
  439. template<> inline
  440. PyObject* BinarySub::operator()( Expression* first, Expression* second )
  441. {
  442. PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) );
  443. if( !temp )
  444. return 0;
  445. return BinaryAdd()( first, reinterpret_cast<Expression*>( temp.get() ) );
  446. }
  447. template<> inline
  448. PyObject* BinarySub::operator()( double first, Variable* second )
  449. {
  450. PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) );
  451. if( !temp )
  452. return 0;
  453. return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) );
  454. }
  455. template<> inline
  456. PyObject* BinarySub::operator()( double first, Term* second )
  457. {
  458. PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) );
  459. if( !temp )
  460. return 0;
  461. return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) );
  462. }
  463. template<> inline
  464. PyObject* BinarySub::operator()( double first, Expression* second )
  465. {
  466. PythonHelpers::PyObjectPtr temp( UnaryNeg()( second ) );
  467. if( !temp )
  468. return 0;
  469. return BinaryAdd()( first, reinterpret_cast<Expression*>( temp.get() ) );
  470. }
  471. template<typename T, typename U>
  472. PyObject* makecn( T first, U second, kiwi::RelationalOperator op )
  473. {
  474. PythonHelpers::PyObjectPtr pyexpr( BinarySub()( first, second ) );
  475. if( !pyexpr )
  476. return 0;
  477. PythonHelpers::PyObjectPtr pycn( PyType_GenericNew( &Constraint_Type, 0, 0 ) );
  478. if( !pycn )
  479. return 0;
  480. Constraint* cn = reinterpret_cast<Constraint*>( pycn.get() );
  481. cn->expression = reduce_expression( pyexpr.get() );
  482. if( !cn->expression )
  483. return 0;
  484. kiwi::Expression expr( convert_to_kiwi_expression( cn->expression ) );
  485. new( &cn->constraint ) kiwi::Constraint( expr, op, kiwi::strength::required );
  486. return pycn.release();
  487. }
  488. struct CmpEQ
  489. {
  490. template<typename T, typename U>
  491. PyObject* operator()( T first, U second )
  492. {
  493. return makecn( first, second, kiwi::OP_EQ );
  494. }
  495. };
  496. struct CmpLE
  497. {
  498. template<typename T, typename U>
  499. PyObject* operator()( T first, U second )
  500. {
  501. return makecn( first, second, kiwi::OP_LE );
  502. }
  503. };
  504. struct CmpGE
  505. {
  506. template<typename T, typename U>
  507. PyObject* operator()( T first, U second )
  508. {
  509. return makecn( first, second, kiwi::OP_GE );
  510. }
  511. };