Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- Speed up MatrixExpr.add.reduce via quicksum
- Speed up np.ndarray(..., dtype=np.float64) @ MatrixExpr
- MatrixExpr and MatrixExprCons use `__array_ufunc__` protocol to control all numpy.ufunc inputs and outputs
- Return itself for abs to UnaryExpr(Operator.fabs)
### Removed

## 6.0.0 - 2025.xx.yy
Expand Down
22 changes: 21 additions & 1 deletion src/pyscipopt/expr.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@
import math
from typing import TYPE_CHECKING

from pyscipopt.scip cimport Variable, Solution
from cpython.dict cimport PyDict_Next
from cpython.object cimport Py_TYPE
from cpython.ref cimport PyObject
from pyscipopt.scip cimport Variable, Solution

import numpy as np

Expand Down Expand Up @@ -647,6 +648,20 @@ cdef class GenExpr:
'''returns operator of GenExpr'''
return self._op

cdef GenExpr copy(self, bool copy = True):
cdef object cls = <type>Py_TYPE(self)
cdef GenExpr res = cls.__new__(cls)
res._op = self._op
res.children = self.children.copy() if copy else self.children
if cls is SumExpr:
(<SumExpr>res).constant = (<SumExpr>self).constant
(<SumExpr>res).coefs = (<SumExpr>self).coefs.copy() if copy else (<SumExpr>self).coefs
if cls is ProdExpr:
(<ProdExpr>res).constant = (<ProdExpr>self).constant
elif cls is PowExpr:
(<PowExpr>res).expo = (<PowExpr>self).expo
return res


# Sum Expressions
cdef class SumExpr(GenExpr):
Expand Down Expand Up @@ -736,6 +751,11 @@ cdef class UnaryExpr(GenExpr):
self.children.append(expr)
self._op = op

def __abs__(self) -> UnaryExpr:
if self._op == "abs":
return <UnaryExpr>self.copy()
return UnaryExpr(Operator.fabs, self)

def __repr__(self):
return self._op + "(" + self.children[0].__repr__() + ")"

Expand Down
4 changes: 2 additions & 2 deletions src/pyscipopt/scip.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ class GenExpr:
def __init__(self) -> None: ...
def degree(self) -> Incomplete: ...
def getOp(self) -> Incomplete: ...
def __abs__(self) -> Incomplete: ...
def __abs__(self) -> GenExpr: ...
def __add__(self, other: Incomplete) -> Incomplete: ...
def __eq__(self, other: object) -> bool: ...
def __ge__(self, other: object) -> bool: ...
Expand Down Expand Up @@ -2161,9 +2161,9 @@ class Term:
def __lt__(self, other: object) -> bool: ...
def __ne__(self, other: object) -> bool: ...

@disjoint_base
class UnaryExpr(GenExpr):
def __init__(self, *args: Incomplete, **kwargs: Incomplete) -> None: ...
def __abs__(self) -> GenExpr: ...

@disjoint_base
class VarExpr(GenExpr):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,11 @@ def test_getVal_with_GenExpr():

with pytest.raises(ZeroDivisionError):
m.getVal(1 / z)


def test_abs_abs_expr():
m = Model()
x = m.addVar(name="x")

# should print abs(x) not abs(abs(x))
assert str(abs(abs(x))) == str(abs(x))
Loading