Debian Codegraph @

py/src/symbolics.h

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 #pragma once
9 #include <cppy/cppy.h>
10 #include "types.h"
11 #include "util.h"
12
13
14 namespace kiwisolver
15 {
16
17 template<typename Op, typename T>
18 struct UnaryInvoke
19 {
20 PyObject* operator()( PyObject* value )
21 {
22 return Op()( reinterpret_cast<T*>( value ) );
23 }
24 };
25
26
27 template<typename Op, typename T>
28 struct BinaryInvoke
29 {
30 PyObject* operator()( PyObject* first, PyObject* second )
31 {
32 if( T::TypeCheck( first ) )
33 return invoke<Normal>( reinterpret_cast<T*>( first ), second );
34 return invoke<Reverse>( reinterpret_cast<T*>( second ), first );
35 }
36
37 struct Normal
38 {
39 template<typename U>
40 PyObject* operator()( T* primary, U secondary )
41 {
42 return Op()( primary, secondary );
43 }
44 };
45
46 struct Reverse
47 {
48 template<typename U>
49 PyObject* operator()( T* primary, U secondary )
50 {
51 return Op()( secondary, primary );
52 }
53 };
54
55 template<typename Invk>
56 PyObject* invoke( T* primary, PyObject* secondary )
57 {
58 if( Expression::TypeCheck( secondary ) )
59 return Invk()( primary, reinterpret_cast<Expression*>( secondary ) );
60 if( Term::TypeCheck( secondary ) )
61 return Invk()( primary, reinterpret_cast<Term*>( secondary ) );
62 if( Variable::TypeCheck( secondary ) )
63 return Invk()( primary, reinterpret_cast<Variable*>( secondary ) );
64 if( PyFloat_Check( secondary ) )
65 return Invk()( primary, PyFloat_AS_DOUBLE( secondary ) );
66 if( PyLong_Check( secondary ) )
67 {
68 double v = PyLong_AsDouble( secondary );
69 if( v == -1 && PyErr_Occurred() )
70 return 0;
71 return Invk()( primary, v );
72 }
73 Py_RETURN_NOTIMPLEMENTED;
74 }
75 };
76
77
78 struct BinaryMul
79 {
80 template<typename T, typename U>
81 PyObject* operator()( T first, U second )
82 {
83 Py_RETURN_NOTIMPLEMENTED;
84 }
85 };
86
87
88 template<> inline
89 PyObject* BinaryMul::operator()( Variable* first, double second )
90 {
91 PyObject* pyterm = PyType_GenericNew( Term::TypeObject, 0, 0 );
92 if( !pyterm )
93 return 0;
94 Term* term = reinterpret_cast<Term*>( pyterm );
95 term->variable = cppy::incref( pyobject_cast( first ) );
96 term->coefficient = second;
97 return pyterm;
98 }
99
100
101 template<> inline
102 PyObject* BinaryMul::operator()( Term* first, double second )
103 {
104 PyObject* pyterm = PyType_GenericNew( Term::TypeObject, 0, 0 );
105 if( !pyterm )
106 return 0;
107 Term* term = reinterpret_cast<Term*>( pyterm );
108 term->variable = cppy::incref( first->variable );
109 term->coefficient = first->coefficient * second;
110 return pyterm;
111 }
112
113
114 template<> inline
115 PyObject* BinaryMul::operator()( Expression* first, double second )
116 {
117 cppy::ptr pyexpr( PyType_GenericNew( Expression::TypeObject, 0, 0 ) );
118 if( !pyexpr )
119 return 0;
120 Expression* expr = reinterpret_cast<Expression*>( pyexpr.get() );
121 cppy::ptr terms( PyTuple_New( PyTuple_GET_SIZE( first->terms ) ) );
122 if( !terms )
123 return 0;
124 Py_ssize_t end = PyTuple_GET_SIZE( first->terms );
125 for( Py_ssize_t i = 0; i < end; ++i ) // memset 0 for safe error return
126 PyTuple_SET_ITEM( terms.get(), i, 0 );
127 for( Py_ssize_t i = 0; i < end; ++i )
128 {
129 PyObject* item = PyTuple_GET_ITEM( first->terms, i );
130 PyObject* term = BinaryMul()( reinterpret_cast<Term*>( item ), second );
131 if( !term )
132 return 0;
133 PyTuple_SET_ITEM( terms.get(), i, term );
134 }
135 expr->terms = terms.release();
136 expr->constant = first->constant * second;
137 return pyexpr.release();
138 }
139
140
141 template<> inline
142 PyObject* BinaryMul::operator()( double first, Variable* second )
143 {
144 return operator()( second, first );
145 }
146
147
148 template<> inline
149 PyObject* BinaryMul::operator()( double first, Term* second )
150 {
151 return operator()( second, first );
152 }
153
154
155 template<> inline
156 PyObject* BinaryMul::operator()( double first, Expression* second )
157 {
158 return operator()( second, first );
159 }
160
161
162 struct BinaryDiv
163 {
164 template<typename T, typename U>
165 PyObject* operator()( T first, U second )
166 {
167 Py_RETURN_NOTIMPLEMENTED;
168 }
169 };
170
171
172 template<> inline
173 PyObject* BinaryDiv::operator()( Variable* first, double second )
174 {
175 if( second == 0.0 )
176 {
177 PyErr_SetString( PyExc_ZeroDivisionError, "float division by zero" );
178 return 0;
179 }
180 return BinaryMul()( first, 1.0 / second );
181 }
182
183
184 template<> inline
185 PyObject* BinaryDiv::operator()( Term* first, double second )
186 {
187 if( second == 0.0 )
188 {
189 PyErr_SetString( PyExc_ZeroDivisionError, "float division by zero" );
190 return 0;
191 }
192 return BinaryMul()( first, 1.0 / second );
193 }
194
195
196 template<> inline
197 PyObject* BinaryDiv::operator()( Expression* first, double second )
198 {
199 if( second == 0.0 )
200 {
201 PyErr_SetString( PyExc_ZeroDivisionError, "float division by zero" );
202 return 0;
203 }
204 return BinaryMul()( first, 1.0 / second );
205 }
206
207
208 struct UnaryNeg
209 {
210 template<typename T>
211 PyObject* operator()( T value )
212 {
213 Py_RETURN_NOTIMPLEMENTED;
214 }
215 };
216
217
218 template<> inline
219 PyObject* UnaryNeg::operator()( Variable* value )
220 {
221 return BinaryMul()( value, -1.0 );
222 }
223
224
225 template<> inline
226 PyObject* UnaryNeg::operator()( Term* value )
227 {
228 return BinaryMul()( value, -1.0 );
229 }
230
231
232 template<> inline
233 PyObject* UnaryNeg::operator()( Expression* value )
234 {
235 return BinaryMul()( value, -1.0 );
236 }
237
238
239 struct BinaryAdd
240 {
241 template<typename T, typename U>
242 PyObject* operator()( T first, U second )
243 {
244 Py_RETURN_NOTIMPLEMENTED;
245 }
246 };
247
248
249 template<> inline
250 PyObject* BinaryAdd::operator()( Expression* first, Expression* second )
251 {
252 cppy::ptr pyexpr( PyType_GenericNew( Expression::TypeObject, 0, 0 ) );
253 if( !pyexpr )
254 return 0;
255 Expression* expr = reinterpret_cast<Expression*>( pyexpr.get() );
256 expr->constant = first->constant + second->constant;
257 expr->terms = PySequence_Concat( first->terms, second->terms );
258 if( !expr->terms )
259 return 0;
260 return pyexpr.release();
261 }
262
263
264 template<> inline
265 PyObject* BinaryAdd::operator()( Expression* first, Term* second )
266 {
267 cppy::ptr pyexpr( PyType_GenericNew( Expression::TypeObject, 0, 0 ) );
268 if( !pyexpr )
269 return 0;
270 PyObject* terms = PyTuple_New( PyTuple_GET_SIZE( first->terms ) + 1 );
271 if( !terms )
272 return 0;
273 Py_ssize_t end = PyTuple_GET_SIZE( first->terms );
274 for( Py_ssize_t i = 0; i < end; ++i )
275 {
276 PyObject* item = PyTuple_GET_ITEM( first->terms, i );
277 PyTuple_SET_ITEM( terms, i, cppy::incref( item ) );
278 }
279 PyTuple_SET_ITEM( terms, end, cppy::incref( pyobject_cast( second ) ) );
280 Expression* expr = reinterpret_cast<Expression*>( pyexpr.get() );
281 expr->terms = terms;
282 expr->constant = first->constant;
283 return pyexpr.release();
284 }
285
286
287 template<> inline
288 PyObject* BinaryAdd::operator()( Expression* first, Variable* second )
289 {
290 cppy::ptr temp( BinaryMul()( second, 1.0 ) );
291 if( !temp )
292 return 0;
293 return operator()( first, reinterpret_cast<Term*>( temp.get() ) );
294 }
295
296
297 template<> inline
298 PyObject* BinaryAdd::operator()( Expression* first, double second )
299 {
300 cppy::ptr pyexpr( PyType_GenericNew( Expression::TypeObject, 0, 0 ) );
301 if( !pyexpr )
302 return 0;
303 Expression* expr = reinterpret_cast<Expression*>( pyexpr.get() );
304 expr->terms = cppy::incref( first->terms );
305 expr->constant = first->constant + second;
306 return pyexpr.release();
307 }
308
309
310 template<> inline
311 PyObject* BinaryAdd::operator()( Term* first, double second )
312 {
313 cppy::ptr pyexpr( PyType_GenericNew( Expression::TypeObject, 0, 0 ) );
314 if( !pyexpr )
315 return 0;
316 Expression* expr = reinterpret_cast<Expression*>( pyexpr.get() );
317 expr->constant = second;
318 expr->terms = PyTuple_Pack( 1, first );
319 if( !expr->terms )
320 return 0;
321 return pyexpr.release();
322 }
323
324
325 template<> inline
326 PyObject* BinaryAdd::operator()( Term* first, Expression* second )
327 {
328 return operator()( second, first );
329 }
330
331
332 template<> inline
333 PyObject* BinaryAdd::operator()( Term* first, Term* second )
334 {
335 cppy::ptr pyexpr( PyType_GenericNew( Expression::TypeObject, 0, 0 ) );
336 if( !pyexpr )
337 return 0;
338 Expression* expr = reinterpret_cast<Expression*>( pyexpr.get() );
339 expr->constant = 0.0;
340 expr->terms = PyTuple_Pack( 2, first, second );
341 if( !expr->terms )
342 return 0;
343 return pyexpr.release();
344 }
345
346
347 template<> inline
348 PyObject* BinaryAdd::operator()( Term* first, Variable* second )
349 {
350 cppy::ptr temp( BinaryMul()( second, 1.0 ) );
351 if( !temp )
352 return 0;
353 return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) );
354 }
355
356
357 template<> inline
358 PyObject* BinaryAdd::operator()( Variable* first, double second )
359 {
360 cppy::ptr temp( BinaryMul()( first, 1.0 ) );
361 if( !temp )
362 return 0;
363 return operator()( reinterpret_cast<Term*>( temp.get() ), second );
364 }
365
366
367 template<> inline
368 PyObject* BinaryAdd::operator()( Variable* first, Variable* second )
369 {
370 cppy::ptr temp( BinaryMul()( first, 1.0 ) );
371 if( !temp )
372 return 0;
373 return operator()( reinterpret_cast<Term*>( temp.get() ), second );
374 }
375
376
377 template<> inline
378 PyObject* BinaryAdd::operator()( Variable* first, Term* second )
379 {
380 cppy::ptr temp( BinaryMul()( first, 1.0 ) );
381 if( !temp )
382 return 0;
383 return operator()( reinterpret_cast<Term*>( temp.get() ), second );
384 }
385
386
387 template<> inline
388 PyObject* BinaryAdd::operator()( Variable* first, Expression* second )
389 {
390 cppy::ptr temp( BinaryMul()( first, 1.0 ) );
391 if( !temp )
392 return 0;
393 return operator()( reinterpret_cast<Term*>( temp.get() ), second );
394 }
395
396
397 template<> inline
398 PyObject* BinaryAdd::operator()( double first, Variable* second )
399 {
400 return operator()( second, first );
401 }
402
403
404 template<> inline
405 PyObject* BinaryAdd::operator()( double first, Term* second )
406 {
407 return operator()( second, first );
408 }
409
410
411 template<> inline
412 PyObject* BinaryAdd::operator()( double first, Expression* second )
413 {
414 return operator()( second, first );
415 }
416
417
418 struct BinarySub
419 {
420 template<typename T, typename U>
421 PyObject* operator()( T first, U second )
422 {
423 Py_RETURN_NOTIMPLEMENTED;
424 }
425 };
426
427
428 template<> inline
429 PyObject* BinarySub::operator()( Variable* first, double second )
430 {
431 return BinaryAdd()( first, -second );
432 }
433
434
435 template<> inline
436 PyObject* BinarySub::operator()( Variable* first, Variable* second )
437 {
438 cppy::ptr temp( UnaryNeg()( second ) );
439 if( !temp )
440 return 0;
441 return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) );
442 }
443
444
445 template<> inline
446 PyObject* BinarySub::operator()( Variable* first, Term* second )
447 {
448 cppy::ptr temp( UnaryNeg()( second ) );
449 if( !temp )
450 return 0;
451 return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) );
452 }
453
454
455 template<> inline
456 PyObject* BinarySub::operator()( Variable* first, Expression* second )
457 {
458 cppy::ptr temp( UnaryNeg()( second ) );
459 if( !temp )
460 return 0;
461 return BinaryAdd()( first, reinterpret_cast<Expression*>( temp.get() ) );
462 }
463
464
465 template<> inline
466 PyObject* BinarySub::operator()( Term* first, double second )
467 {
468 return BinaryAdd()( first, -second );
469 }
470
471
472 template<> inline
473 PyObject* BinarySub::operator()( Term* first, Variable* second )
474 {
475 cppy::ptr temp( UnaryNeg()( second ) );
476 if( !temp )
477 return 0;
478 return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) );
479 }
480
481
482 template<> inline
483 PyObject* BinarySub::operator()( Term* first, Term* second )
484 {
485 cppy::ptr temp( UnaryNeg()( second ) );
486 if( !temp )
487 return 0;
488 return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) );
489 }
490
491
492 template<> inline
493 PyObject* BinarySub::operator()( Term* first, Expression* second )
494 {
495 cppy::ptr temp( UnaryNeg()( second ) );
496 if( !temp )
497 return 0;
498 return BinaryAdd()( first, reinterpret_cast<Expression*>( temp.get() ) );
499 }
500
501
502 template<> inline
503 PyObject* BinarySub::operator()( Expression* first, double second )
504 {
505 return BinaryAdd()( first, -second );
506 }
507
508
509 template<> inline
510 PyObject* BinarySub::operator()( Expression* first, Variable* second )
511 {
512 cppy::ptr temp( UnaryNeg()( second ) );
513 if( !temp )
514 return 0;
515 return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) );
516 }
517
518
519 template<> inline
520 PyObject* BinarySub::operator()( Expression* first, Term* second )
521 {
522 cppy::ptr temp( UnaryNeg()( second ) );
523 if( !temp )
524 return 0;
525 return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) );
526 }
527
528
529 template<> inline
530 PyObject* BinarySub::operator()( Expression* first, Expression* second )
531 {
532 cppy::ptr temp( UnaryNeg()( second ) );
533 if( !temp )
534 return 0;
535 return BinaryAdd()( first, reinterpret_cast<Expression*>( temp.get() ) );
536 }
537
538
539 template<> inline
540 PyObject* BinarySub::operator()( double first, Variable* second )
541 {
542 cppy::ptr temp( UnaryNeg()( second ) );
543 if( !temp )
544 return 0;
545 return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) );
546 }
547
548
549 template<> inline
550 PyObject* BinarySub::operator()( double first, Term* second )
551 {
552 cppy::ptr temp( UnaryNeg()( second ) );
553 if( !temp )
554 return 0;
555 return BinaryAdd()( first, reinterpret_cast<Term*>( temp.get() ) );
556 }
557
558
559 template<> inline
560 PyObject* BinarySub::operator()( double first, Expression* second )
561 {
562 cppy::ptr temp( UnaryNeg()( second ) );
563 if( !temp )
564 return 0;
565 return BinaryAdd()( first, reinterpret_cast<Expression*>( temp.get() ) );
566 }
567
568
569 template<typename T, typename U>
570 PyObject* makecn( T first, U second, kiwi::RelationalOperator op )
571 {
572 cppy::ptr pyexpr( BinarySub()( first, second ) );
573 if( !pyexpr )
574 return 0;
575 cppy::ptr pycn( PyType_GenericNew( Constraint::TypeObject, 0, 0 ) );
576 if( !pycn )
577 return 0;
578 Constraint* cn = reinterpret_cast<Constraint*>( pycn.get() );
579 cn->expression = reduce_expression( pyexpr.get() );
580 if( !cn->expression )
581 return 0;
582 ACQUIRE_GLOBAL_LOCK();
583 kiwi::Expression expr( convert_to_kiwi_expression( cn->expression ) );
584 new( &cn->constraint ) kiwi::Constraint( expr, op, kiwi::strength::required );
585 RELEASE_GLOBAL_LOCK();
586 return pycn.release();
587 }
588
589
590 struct CmpEQ
591 {
592 template<typename T, typename U>
593 PyObject* operator()( T first, U second )
594 {
595 return makecn( first, second, kiwi::OP_EQ );
596 }
597 };
598
599
600 struct CmpLE
601 {
602 template<typename T, typename U>
603 PyObject* operator()( T first, U second )
604 {
605 return makecn( first, second, kiwi::OP_LE );
606 }
607 };
608
609
610 struct CmpGE
611 {
612 template<typename T, typename U>
613 PyObject* operator()( T first, U second )
614 {
615 return makecn( first, second, kiwi::OP_GE );
616 }
617 };
618
619
620 } // namespace kiwisolver