arithmetic
Arithmetic operations for symbolic expressions.
This module provides fundamental arithmetic operations that form the building blocks of symbolic expressions in openscvx. These operations are created automatically through operator overloading on Expr objects.
Arithmetic Operations:
- Binary operations:
Add,Sub,Mul,Div,MatMul,Power- Standard arithmetic - Unary operations:
Neg- Negation (unary minus)
All arithmetic operations support: - Automatic canonicalization (constant folding, identity elimination, flattening) - Broadcasting following NumPy rules (except MatMul which follows linear algebra rules) - Shape checking and validation
Example
Arithmetic operations are created via operator overloading::
import openscvx as ox
x = ox.State("x", shape=(3,))
y = ox.State("y", shape=(3,))
# Element-wise operations
z = x + y # Creates Add(x, y)
w = x * 2 # Creates Mul(x, Constant(2))
neg_x = -x # Creates Neg(x)
# Matrix multiplication
A = ox.State("A", shape=(3, 3))
b = A @ x # Creates MatMul(A, x)
Add
¶
Bases: Expr
Addition operation for symbolic expressions.
Represents element-wise addition of two or more expressions. Supports broadcasting following NumPy rules. Can be created using the + operator on Expr objects.
Attributes:
| Name | Type | Description |
|---|---|---|
terms |
List of expression operands to add together |
Example
Define an Add expression:
x = ox.State("x", shape=(3,))
y = ox.State("y", shape=(3,))
z = x + y + 5 # Creates Add(x, y, Constant(5))
Source code in openscvx/symbolic/expr/arithmetic.py
canonicalize() -> Expr
¶
Canonicalize addition: flatten, fold constants, and eliminate zeros.
Returns:
| Name | Type | Description |
|---|---|---|
Expr |
Expr
|
Canonical form of the addition expression |
Source code in openscvx/symbolic/expr/arithmetic.py
check_shape() -> Tuple[int, ...]
¶
Check shape compatibility and compute broadcasted result shape like NumPy.
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Tuple[int, ...]
|
The broadcasted shape of all operands |
Raises:
| Type | Description |
|---|---|
ValueError
|
If operand shapes are not broadcastable |
Source code in openscvx/symbolic/expr/arithmetic.py
Div
¶
Bases: Expr
Element-wise division operation for symbolic expressions.
Represents element-wise division (left / right). Supports broadcasting following NumPy rules. Can be created using the / operator on Expr objects.
Attributes:
| Name | Type | Description |
|---|---|---|
left |
Numerator expression |
|
right |
Denominator expression |
Example
Define a Div expression
x = ox.State("x", shape=(3,))
y = ox.State("y", shape=(3,))
z = x / y # Creates Div(x, y)
Source code in openscvx/symbolic/expr/arithmetic.py
canonicalize() -> Expr
¶
Canonicalize division: fold constants if both sides are constants.
Returns:
| Name | Type | Description |
|---|---|---|
Expr |
Expr
|
Canonical form of the division expression |
Source code in openscvx/symbolic/expr/arithmetic.py
check_shape() -> Tuple[int, ...]
¶
Check shape compatibility and compute broadcasted result shape like NumPy.
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Tuple[int, ...]
|
The broadcasted shape of both operands |
Raises:
| Type | Description |
|---|---|
ValueError
|
If operand shapes are not broadcastable |
Source code in openscvx/symbolic/expr/arithmetic.py
MatMul
¶
Bases: Expr
Matrix multiplication operation for symbolic expressions.
Represents matrix multiplication following standard linear algebra rules. Can be created using the @ operator on Expr objects. Handles: - Matrix @ Matrix: (m,n) @ (n,k) -> (m,k) - Matrix @ Vector: (m,n) @ (n,) -> (m,) - Vector @ Matrix: (m,) @ (m,n) -> (n,) - Vector @ Vector: (m,) @ (m,) -> scalar
Attributes:
| Name | Type | Description |
|---|---|---|
left |
Left-hand side expression |
|
right |
Right-hand side expression |
Example
Define a MatMul expression:
A = ox.State("A", shape=(3, 4))
x = ox.State("x", shape=(4,))
y = A @ x # Creates MatMul(A, x), result shape (3,)
Source code in openscvx/symbolic/expr/arithmetic.py
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 | |
check_shape() -> Tuple[int, ...]
¶
Check matrix multiplication shape compatibility and return result shape.
Source code in openscvx/symbolic/expr/arithmetic.py
Mul
¶
Bases: Expr
Element-wise multiplication operation for symbolic expressions.
Represents element-wise (Hadamard) multiplication of two or more expressions. Supports broadcasting following NumPy rules. Can be created using the * operator on Expr objects. For matrix multiplication, use MatMul or the @ operator.
Attributes:
| Name | Type | Description |
|---|---|---|
factors |
List of expression operands to multiply together |
Example
Define a Mul expression:
x = ox.State("x", shape=(3,))
y = ox.State("y", shape=(3,))
z = x * y * 2 # Creates Mul(x, y, Constant(2))
Source code in openscvx/symbolic/expr/arithmetic.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | |
canonicalize() -> Expr
¶
Canonicalize multiplication: flatten, fold constants, and eliminating ones.
Returns:
| Name | Type | Description |
|---|---|---|
Expr |
Expr
|
Canonical form of the multiplication expression |
Source code in openscvx/symbolic/expr/arithmetic.py
check_shape() -> Tuple[int, ...]
¶
Check shape compatibility and compute broadcasted result shape like NumPy.
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Tuple[int, ...]
|
The broadcasted shape of all operands |
Raises:
| Type | Description |
|---|---|
ValueError
|
If operand shapes are not broadcastable |
Source code in openscvx/symbolic/expr/arithmetic.py
Neg
¶
Bases: Expr
Negation operation for symbolic expressions.
Represents element-wise negation (unary minus). Can be created using the unary - operator on Expr objects.
Attributes:
| Name | Type | Description |
|---|---|---|
operand |
Expression to negate |
Example
Define a Neg expression:
x = ox.State("x", shape=(3,))
y = -x # Creates Neg(x)
Source code in openscvx/symbolic/expr/arithmetic.py
Power
¶
Bases: Expr
Element-wise power operation for symbolic expressions.
Represents element-wise exponentiation (base ** exponent). Supports broadcasting following NumPy rules. Can be created using the ** operator on Expr objects.
Attributes:
| Name | Type | Description |
|---|---|---|
base |
Base expression |
|
exponent |
Exponent expression |
Example
Define a Power expression:
x = ox.State("x", shape=(3,))
y = x ** 2 # Creates Power(x, Constant(2))
Source code in openscvx/symbolic/expr/arithmetic.py
Sub
¶
Bases: Expr
Subtraction operation for symbolic expressions.
Represents element-wise subtraction (left - right). Supports broadcasting following NumPy rules. Can be created using the - operator on Expr objects.
Attributes:
| Name | Type | Description |
|---|---|---|
left |
Left-hand side expression (minuend) |
|
right |
Right-hand side expression (subtrahend) |
Example
Define a Sub expression:
x = ox.State("x", shape=(3,))
y = ox.State("y", shape=(3,))
z = x - y # Creates Sub(x, y)
Source code in openscvx/symbolic/expr/arithmetic.py
canonicalize() -> Expr
¶
Canonicalize subtraction: fold constants if both sides are constants.
Returns:
| Name | Type | Description |
|---|---|---|
Expr |
Expr
|
Canonical form of the subtraction expression |
Source code in openscvx/symbolic/expr/arithmetic.py
check_shape() -> Tuple[int, ...]
¶
Check shape compatibility and compute broadcasted result shape like NumPy.
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Tuple[int, ...]
|
The broadcasted shape of all operands |
Raises:
| Type | Description |
|---|---|
ValueError
|
If operand shapes are not broadcastable |