Repository navigation
Conversation
Hand-merged conflicts in CHANGELOG.md, expr.pxi, scip.pxd, scip.pyi (no conflict markers in working tree). Key changes from upstream f5a761d: - scipopt#1267: readonly fields on SumExpr/ProdExpr - scipopt#1256: dual fix - scipopt#1273: addConsLinear - scipopt#1277: remove VarExpr.var - New: stubs/baseline.sh, requirements/types.txt, pylock.types.toml, tests/@types/expr.mypy.out, scripts/generate_expr_type_tests.py M4 core: Variable(ExprLike), ExprLike base with as_expr/expr_view, __init__ throws NotImplementedError on Variable. Bug fix: Expr.__add__/__mul__ now delegate to GenExpr when other is GenExpr, avoiding PyNumber_Add recursion that caused stack overflow when computing Expr + GenExpr (e.g., 2*x + exp(x)).
__add__/__mul__/__truediv__ now call Expr methods directly via __Pyx_PyObject_FastCallMethod instead of going through PyNumber_Add/ PyNumber_Mul/PyNumber_TrueDivide slot lookup. __getitem__/__iter__ inlined to direct dict access (v.terms.get / iter(v.terms)), equivalent to Expr originals. __pow__ unchanged (internal res *= self still dispatches; full inline not worth the complexity). Perf: x+y regression reduced from +48% to +16%; x*y regression eliminated (-6%); degree still +15% (as_expr indirection). 97 tests pass, mypy 0 errors.
- Forward __add__, __mul__, and __truediv__ through operator syntax instead of unbound Expr method calls - Annotate expression-compatible APIs with ExprLike - Remove duplicated Expr.__getitem__/__iter__ in favor of ExprLike - Assert ExprCons.expr is a plain Expr after normalize() - Rename Variable.expr_view to Variable._expr - Raise clearer errors when indexing or iterating a GenExpr
ExprLike.__neg__ never returns a Variable, so Expr | GenExpr is more accurate. Add a Variable.__neg__ override returning Expr, and apply ruff formatting to the stub.
Move the degree computation from Expr to ExprLike as a cpdef double method with "except? -1", so Expr, Variable and GenExpr share one implementation; GenExpr overrides it to return INFINITY. Expr.degree now returns float instead of int, and the stub is updated accordingly. Replace the generator-based max() with an explicit C loop (generators are not allowed inside cpdef functions). Benchmark (Python call, min of 5 runs): 1000 terms: 71.6 us -> 9.3 us (7.7x) 2 terms: 679 ns -> 151 ns (4.5x) Also update test_Variable for the single-argument Term constructor.
- __getitem__: reject None explicitly and raise a clear TypeError for invalid index types; annotate the return type - __iter__: simplify and annotate as Iterator[Term] - __contains__: add O(1) membership test for Term and Variable - GenExpr: override __contains__ to raise TypeError like __getitem__/__iter__ - Stub: drop the duplicate Expr.__getitem__/__iter__ and update signatures
Reviewer checklistTo be checked by the reviewer. Code
Documentation
Testing
Does this PR introduce an API change?
|
| cpdef double degree(self) except? -1: | ||
| cdef Expr expr = self.as_expr() | ||
| cdef double deg = 0, res = 0 | ||
| cdef Term t | ||
| for t in expr.terms: | ||
| deg = len(t) | ||
| if deg > res: | ||
| res = deg | ||
| return res |
There was a problem hiding this comment.
Benchmark (Python call, min of 5 runs):
- 1000 terms: 71.6 us -> 9.3 us (7.7x)
- 2 terms: 679 ns -> 151 ns (4.5x)
|
The developer can't check/edit Reviewer checklist. This list should be put in |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1299 +/- ##
==========================================
- Coverage 58.15% 57.58% -0.57%
==========================================
Files 27 27
Lines 6012 6102 +90
==========================================
+ Hits 3496 3514 +18
- Misses 2516 2588 +72 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This is on purpose, it's for the reviewer. |
|
/benchmark |
|
✅ |
|
(I'm running the benchmark just for fun, I really like it :) ) |
|
Results for Benchmark results (base → PR)
Instructions from callgrind, time is the best of several runs, memory is Python-side (tracemalloc). Flagged above 1% instructions, 20% time, 5% peak memory, 5% retained memory. Against v6.3.0v6.3.0 → PR
Instructions from callgrind, time is the best of several runs, memory is Python-side (tracemalloc). Flagged above 1% instructions, 20% time, 5% peak memory, 5% retained memory. Base |
| def __init__(self, *_): | ||
| raise NotImplementedError( | ||
| "Direct instantiation of 'Variable' is not supported. " | ||
| "Please use Model to create variables." | ||
| ) |
There was a problem hiding this comment.
See #1149, this PR is a part of that series. Variable requires a SCIP variable, so it can't be called by users directly. This prevents creating a Variable instance via Variable()
Why
Variableused to inherit fromExpr. The relationship betweenTerm,Variable, andExpris quite odd. Variable is the subclass of Expr, but Variable also could be a member of a Term.To fix this problem and follow the math (Variable -> Term -> Expr), we have a duck class called
ExprLike(NOTBaseExpr), it defines the basic behavior (like add, sub and others).Performance
I also ran a performance benchmark (in ns). All results are within noise range except for
expr_degree.