Skip to content

Fix Variable arithmetic with Eigen matrices and NumPy arrays - #1458

Merged
calcmogul merged 1 commit into
SleipnirGroup:mainfrom
calcmogul:fix-variable-arithmetic-with-eigen-matrices-and-numpy-arrays
Nov 8, 2026
Merged

calcmogul merged 1 commit into
SleipnirGroup:mainfrom
calcmogul:fix-variable-arithmetic-with-eigen-matrices-and-numpy-arrays

Conversation

@calcmogul

Copy link
Copy Markdown
Member

The Eigen * Variable, Variable * Eigen, and Eigen / Variable operators were hidden friends of VariableMatrix, so argument-dependent lookup never found them since neither argument is a VariableMatrix. They're now namespace-scope templates. They only accept Eigen matrices of Scalar, so Eigen's own operators still handle Eigen matrices of Variable (e.g., in solve()).

In Python, multiplying or dividing a 2D NumPy array by a Variable now returns a VariableMatrix. Previously, NumPy applied the operation elementwise and returned an object array of Variables. Variable now defines array_ufunc, so other operations between a Variable and a NumPy array raise TypeError instead, like they already do for VariableMatrix. NumPy scalars (e.g., np.float64) still work with Variable's arithmetic and comparison operators.

@calcmogul
calcmogul force-pushed the fix-variable-arithmetic-with-eigen-matrices-and-numpy-arrays branch 4 times, most recently from c5e91d2 to 1413e66 Compare November 8, 2026 19:48
The Eigen * Variable, Variable * Eigen, and Eigen / Variable operators
were hidden friends of VariableMatrix, so argument-dependent lookup
never found them since neither argument is a VariableMatrix. They're now
namespace-scope templates. They only accept Eigen matrices of Scalar, so
Eigen's own operators still handle Eigen matrices of Variable (e.g., in
solve()).

In Python, multiplying or dividing a 2D NumPy array by a Variable now
returns a VariableMatrix. Previously, NumPy applied the operation
elementwise and returned an object array of Variables. Variable now
defines __array_ufunc__, so other operations between a Variable and a
NumPy array raise TypeError instead, like they already do for
VariableMatrix. NumPy scalars (e.g., np.float64) still work with
Variable's arithmetic and comparison operators.
@calcmogul
calcmogul force-pushed the fix-variable-arithmetic-with-eigen-matrices-and-numpy-arrays branch from 1413e66 to 564c2ab Compare November 8, 2026 19:58
@calcmogul
calcmogul merged commit d3229d1 into SleipnirGroup:main Nov 8, 2026
39 checks passed
@calcmogul
calcmogul deleted the fix-variable-arithmetic-with-eigen-matrices-and-numpy-arrays branch November 8, 2026 20:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant