You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Keep CSR backing for elementwise ops with non-scalar constants #965
Keep the CSR backing for elementwise arithmetic with non-scalar constants. Child of #756.
Today the sparse fast path accepts only a plain scalar. LinearExpression.__mul__ gates on int | float | np.number (linopy/expressions.py:2500) and calls CSRLinearExpression.scaled. A DataArray, Series or ndarray factor falls through to _multiply_by_constant and densifies the whole rectangle, even though only the coefficients change.
Adding or subtracting a constant is worse: _add_constant (expressions.py:1153-1178) expands everything although only const changes and the CSR matrix is untouched.
Coefficient vectors over a grid dimension are the normal case in real models, for example efficiencies per generator or weights per snapshot, so this gate excludes most realistic arithmetic.
Implementation ideas
Two new methods on CSRLinearExpression, both cheap on the stored arrays:
scaled_by(vector, dim) — broadcast the factor along the grid and scale each row's data slice. Reuse Grid.indexer/strides to map cells to row ranges.
shifted(const) — add to the stored const only, leaving indptr/indices/data alone.
Then wire __mul__, __rmul__, __truediv__, div, _add_constant and _sub_constant to them. Alignment of the operand against the grid should reuse _matmul_operand_to_matrix in linopy/alignment.py (#961), which already enforces the v1 NaN, label-mismatch and aux-coord rules identically to *.
Note
The following content was generated by AI.
Describe the feature you'd like to see
Keep the CSR backing for elementwise arithmetic with non-scalar constants. Child of #756.
Today the sparse fast path accepts only a plain scalar.
LinearExpression.__mul__gates onint | float | np.number(linopy/expressions.py:2500) and callsCSRLinearExpression.scaled. ADataArray,Seriesorndarrayfactor falls through to_multiply_by_constantand densifies the whole rectangle, even though only the coefficients change.Adding or subtracting a constant is worse:
_add_constant(expressions.py:1153-1178) expands everything although onlyconstchanges and the CSR matrix is untouched.Coefficient vectors over a grid dimension are the normal case in real models, for example efficiencies per generator or weights per snapshot, so this gate excludes most realistic arithmetic.
Implementation ideas
Two new methods on
CSRLinearExpression, both cheap on the stored arrays:scaled_by(vector, dim)— broadcast the factor along the grid and scale each row'sdataslice. ReuseGrid.indexer/stridesto map cells to row ranges.shifted(const)— add to the storedconstonly, leavingindptr/indices/dataalone.Then wire
__mul__,__rmul__,__truediv__,div,_add_constantand_sub_constantto them. Alignment of the operand against the grid should reuse_matmul_operand_to_matrixinlinopy/alignment.py(#961), which already enforces the v1 NaN, label-mismatch and aux-coord rules identically to*.