|
45 | 45 | import math |
46 | 46 | from typing import TYPE_CHECKING |
47 | 47 |
|
48 | | -from pyscipopt.scip cimport Variable, Solution |
49 | 48 | from cpython.dict cimport PyDict_Next |
| 49 | +from cpython.object cimport Py_TYPE |
50 | 50 | from cpython.ref cimport PyObject |
| 51 | +from pyscipopt.scip cimport Variable, Solution |
51 | 52 |
|
52 | 53 | import numpy as np |
53 | 54 |
|
@@ -636,6 +637,20 @@ cdef class GenExpr: |
636 | 637 | '''returns operator of GenExpr''' |
637 | 638 | return self._op |
638 | 639 |
|
| 640 | + cdef GenExpr copy(self, bool copy = True): |
| 641 | + cdef object cls = <type>Py_TYPE(self) |
| 642 | + cdef GenExpr res = cls.__new__(cls) |
| 643 | + res._op = self._op |
| 644 | + res.children = self.children.copy() if copy else self.children |
| 645 | + if cls is SumExpr: |
| 646 | + (<SumExpr>res).constant = (<SumExpr>self).constant |
| 647 | + (<SumExpr>res).coefs = (<SumExpr>self).coefs.copy() if copy else (<SumExpr>self).coefs |
| 648 | + if cls is ProdExpr: |
| 649 | + (<ProdExpr>res).constant = (<ProdExpr>self).constant |
| 650 | + elif cls is PowExpr: |
| 651 | + (<PowExpr>res).expo = (<PowExpr>self).expo |
| 652 | + return res |
| 653 | + |
639 | 654 |
|
640 | 655 | # Sum Expressions |
641 | 656 | cdef class SumExpr(GenExpr): |
@@ -725,6 +740,11 @@ cdef class UnaryExpr(GenExpr): |
725 | 740 | self.children.append(expr) |
726 | 741 | self._op = op |
727 | 742 |
|
| 743 | + def __abs__(self) -> UnaryExpr: |
| 744 | + if self._op == "abs": |
| 745 | + return <UnaryExpr>self.copy() |
| 746 | + return UnaryExpr(Operator.fabs, self) |
| 747 | + |
728 | 748 | def __repr__(self): |
729 | 749 | return self._op + "(" + self.children[0].__repr__() + ")" |
730 | 750 |
|
|
0 commit comments