Matrix Ops¶
matrix_ops
¶
Matrix operations and linear solvers for multiphysics modeling.
This module provides small, performance-oriented numerical utilities used by multiphysics engines:
- Construction of common 1D linear operators (e.g., upwind advection, Laplacian).
- Cached implicit solves for repeated linear systems with fixed operators.
- High-throughput aggregation utilities for large numbers of subpopulations.
- Optional Kronecker composition utilities for separable multi-axis operators.
Design notes
- Dense implicit solves use the input state's Array-API
linalg.solve. - Sparse acceleration is selected structurally from a registry containing SciPy and, when installed, CuPy adapters.
- Cache semantics: sparse factorizations are keyed by (id(left_op), id(right_op)) within each ecosystem and retain those exact operator objects to prevent stale hits after Python ID recycling. For caching to be effective, operator objects must be constructed once and reused.
Stage operator factories (IMEX/TR-BDF2 support): TR-BDF2 and similar IMEX methods can require stage-specific implicit operators that depend on: - dt (time step) - scale (method stage scalar) - t (stage time) - y (stage state) - stage (a label, e.g. "tr" or "bdf2")
This module supports dynamic base operators via a builder:
base_builder(t, y, stage) -> Operator
Then the stage-operator factory produces (L, R) to solve:
L @ y_next = R @ y_in
where (L, R) follow schemes like implicit Euler or trapezoidal.
DiffusionConfig(coeff, dtype=np.float64, bc='neumann')
dataclass
¶
Configuration for diffusion-like linear operators.
Attributes:
| Name | Type | Description |
|---|---|---|
coeff |
float
|
Physical diffusion coefficient D (units length^2 / time). |
dtype |
DTypeLike
|
Floating dtype (e.g. np.float64). |
bc |
str
|
Boundary condition; either "neumann" or "absorbing". |
__post_init__()
¶
Validate context-free diffusion parameters.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the coefficient or boundary condition is invalid. |
Source code in src/op_engine/matrix_ops.py
149 150 151 152 153 154 155 156 157 158 159 160 | |
GridGeometry(n, dx)
dataclass
¶
Geometry of a 1D spatial grid.
Attributes:
| Name | Type | Description |
|---|---|---|
n |
int
|
Number of grid points. |
dx |
float
|
Grid spacing. |
__post_init__()
¶
Validate context-free grid geometry.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the grid size or spacing is invalid. |
Source code in src/op_engine/matrix_ops.py
121 122 123 124 125 126 127 128 129 130 131 132 | |
SparseAdapter(ecosystem_id, issparse, factorize, solve, cache_key)
dataclass
¶
Function bundle for one sparse-array ecosystem.
Sparse acceleration is optional: dense operators always fall back to the
input array's Array-API linalg.solve implementation. Adapters are
selected structurally with their own issparse predicate.
StageOperatorContext(t, y, stage=None, extra=None)
dataclass
¶
Context passed to time/state-dependent operator builders.
Attributes:
| Name | Type | Description |
|---|---|---|
t |
float
|
Stage time. |
y |
Array
|
Stage state in its runtime Array-API namespace. |
stage |
StageName
|
Optional stage label (e.g. "tr", "bdf2"). |
extra |
Any | None
|
Optional extra payload for future use (kept generic). |
build_advection_matrix(n, dx, velocity, *, bc='absorbing', reference=None)
¶
Build a dense first-order upwind finite-volume operator.
The returned matrix A acts on a column state as dy = A @ y.
Positive velocity transports values toward increasing coordinate indices;
negative velocity transports toward decreasing indices. The velocity may be
an Array-API scalar, so its value stays dynamic under transformations such
as JAX jit and grad.
Boundary modes are:
absorbing: zero inflow at the upstream boundary and free outflow at the downstream boundary;reflecting: zero flux through the downstream boundary;periodic: downstream outflow wraps to the upstream boundary.
The namespace comes from reference when provided, then from velocity;
plain numeric velocities use NumPy. Grid geometry and the boundary mode are
static structural inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of uniformly spaced finite-volume cells. |
required |
dx
|
float
|
Positive cell width. |
required |
velocity
|
float | Array
|
Signed scalar transport velocity. |
required |
bc
|
str
|
Boundary mode: |
'absorbing'
|
reference
|
Array | None
|
Optional array whose namespace and floating dtype control the result. |
None
|
Returns:
| Type | Description |
|---|---|
Array
|
Dense |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the grid, velocity shape/value, or boundary mode is invalid. |
Source code in src/op_engine/matrix_ops.py
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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | |
build_crank_nicolson_operator(geom, cfg, dt)
¶
Build Crank-Nicolson operators with dense/sparse autodispatch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
geom
|
GridGeometry
|
Grid geometry. |
required |
cfg
|
DiffusionConfig
|
Diffusion configuration. |
required |
dt
|
float
|
Time step. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Operator, Operator]
|
Tuple of (L, R) operators for Crank-Nicolson scheme. |
Source code in src/op_engine/matrix_ops.py
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 | |
build_diffusion_matrix(n, dx, coefficient, *, grid=None, bc='neumann', reference=None)
¶
Build a dense second-order 1D finite-volume diffusion operator.
The returned matrix A acts on a column state as dy = A @ y.
Supply either a uniform cell width with dx or strictly increasing
cell-center coordinates with grid. On a non-uniform grid, interior
flux differences are divided by the local Voronoi-cell width. Boundary
faces are inferred one half-spacing beyond the first and last centers.
The coefficient may be an Array-API scalar, so it stays dynamic under
transformations such as JAX jit and grad. Static geometry is
assembled with NumPy and transferred once to the namespace selected by
reference or coefficient.
Boundary modes are:
neumannorreflecting: zero flux at both boundaries;absorbing: zero-valued exterior ghost cells;periodic: the two grid ends are adjacent. A grid passed explicitly must be uniform because its coordinates do not determine wrap spacing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of finite-volume cells. |
required |
dx
|
float | None
|
Positive uniform cell width, or |
required |
coefficient
|
float | Array
|
Non-negative scalar diffusion coefficient. |
required |
grid
|
ArrayLike | None
|
Optional strictly increasing cell-center coordinates of shape
|
None
|
bc
|
str
|
Boundary mode: neumann, reflecting, absorbing, or periodic. |
'neumann'
|
reference
|
Array | None
|
Optional array whose namespace and floating dtype control the result. |
None
|
Returns:
| Type | Description |
|---|---|
Array
|
Dense |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the geometry, coefficient, or boundary mode is invalid. |
Source code in src/op_engine/matrix_ops.py
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 | |
build_identity_operator(n, *, dtype=np.float64, prefer_sparse=None)
¶
Build an identity operator with dense/sparse autodispatch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Size of the identity operator (n x n). |
required |
dtype
|
DTypeLike
|
Floating dtype (e.g. np.float64). |
float64
|
prefer_sparse
|
bool | None
|
If True, always return a sparse operator; if False, always return a dense operator; if None, autodispatch based on n. |
None
|
Returns:
| Type | Description |
|---|---|
Operator
|
Identity operator of shape (n, n) as either a dense ndarray or CSR matrix. |
Source code in src/op_engine/matrix_ops.py
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 | |
build_implicit_euler_operators(base_op, dt_scale)
¶
Build implicit Euler operators for a time-scaled linear operator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_op
|
Operator
|
Base linear operator A. |
required |
dt_scale
|
float
|
Time-step scaling factor (dt * scale). |
required |
Returns:
| Type | Description |
|---|---|
tuple[Operator, Operator]
|
Tuple of (L, R) operators for implicit Euler scheme. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If dt_scale is not finite. |
Source code in src/op_engine/matrix_ops.py
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 | |
build_laplacian_tridiag(n, dx, coeff, dtype=np.float64, bc='neumann', *, grid=None)
¶
Build a sparse 1D finite-volume Laplacian.
The resulting operator corresponds to coeff * Δ_h. Supply either a
uniform cell width with dx or strictly increasing cell-center
coordinates with grid. No time-step scaling is applied here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of finite-volume cells. |
required |
dx
|
float | None
|
Positive uniform cell width, or |
required |
coeff
|
float
|
Physical diffusion coefficient D (units length^2 / time). |
required |
dtype
|
DTypeLike
|
Floating dtype (e.g. np.float64). |
float64
|
bc
|
str
|
Boundary condition; either |
'neumann'
|
grid
|
ArrayLike | None
|
Optional strictly increasing cell-center coordinates of shape
|
None
|
Returns:
| Type | Description |
|---|---|
csr_matrix
|
Sparse CSR matrix representing the Laplacian operator. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the geometry or boundary condition is invalid. |
Source code in src/op_engine/matrix_ops.py
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 | |
build_predictor_corrector(base_matrix)
¶
Build predictor-corrector matrices with dense/sparse autodispatch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_matrix
|
DenseOperator | csr_matrix
|
Base linear operator A. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Operator, Operator, Operator]
|
Tuple of (predictor, L, R) operators for predictor-corrector scheme. |
Source code in src/op_engine/matrix_ops.py
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 | |
build_trapezoidal_operators(base_op, dt_scale)
¶
Build trapezoidal operators for a time-scaled linear operator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_op
|
Operator
|
Base linear operator A. |
required |
dt_scale
|
float
|
Time-step scaling factor (dt * scale). |
required |
Returns:
| Type | Description |
|---|---|
tuple[Operator, Operator]
|
Tuple of (L, R) operators for trapezoidal scheme. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If dt_scale is not finite. |
Source code in src/op_engine/matrix_ops.py
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 | |
clear_implicit_solver_cache()
¶
Clear the internal implicit solver cache.
Source code in src/op_engine/matrix_ops.py
998 999 1000 | |
encode_groups(group_ids, n_groups, *, prefer_sparse=None, dtype=np.float64)
¶
Encode group IDs into a one-hot group membership matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_ids
|
NDArray[integer]
|
1D array of integer group IDs for each item. |
required |
n_groups
|
int
|
Total number of groups. |
required |
prefer_sparse
|
bool | None
|
If True, always return a sparse matrix; if False, always return a dense array; if None, autodispatch based on n_groups. |
None
|
dtype
|
DTypeLike
|
Data type for the output matrix. |
float64
|
Returns:
| Type | Description |
|---|---|
csr_matrix | DenseOperator
|
A (n_groups, n_items) one-hot encoded group membership matrix. |
Source code in src/op_engine/matrix_ops.py
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 | |
grouped_count_ids(group_ids, n_groups)
¶
Perform grouped count using group IDs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_ids
|
NDArray[integer]
|
1D array of integer group IDs. |
required |
n_groups
|
int
|
Total number of groups. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
A 1D array of length n_groups where each element contains the count of |
NDArray[floating]
|
occurrences of the corresponding group ID. |
Source code in src/op_engine/matrix_ops.py
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 | |
grouped_sum_ids(values, group_ids, n_groups)
¶
Perform grouped sum over 1D values array using group IDs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
NDArray[floating]
|
1D array of values. |
required |
group_ids
|
NDArray[integer]
|
1D array of integer group IDs. |
required |
n_groups
|
int
|
Total number of groups. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
A 1D array of length n_groups where each element contains the sum of values |
NDArray[floating]
|
for the corresponding group ID. |
Source code in src/op_engine/matrix_ops.py
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 | |
grouped_sum_ids_2d(values, group_ids, n_groups)
¶
Perform grouped sum over 2D values array using group IDs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
NDArray[floating]
|
2D (N, K) array where N is num of items and K is num of features. |
required |
group_ids
|
NDArray[integer]
|
1D array of integer group IDs of length N. |
required |
n_groups
|
int
|
Total number of groups. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
A 2D array of shape (n_groups, K) where each row contains the sum of values |
NDArray[floating]
|
for the corresponding group ID. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If values is not 2D or if group_ids length does not match the number of items in values. |
Source code in src/op_engine/matrix_ops.py
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 | |
implicit_solve(left_op, right_op, x)
¶
Perform an implicit solve with dense fallback and cached sparse dispatch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left_op
|
object
|
Left operator L in the equation L @ y = R @ x. |
required |
right_op
|
object
|
Right operator R in the equation L @ y = R @ x. |
required |
x
|
Array
|
1D or 2D array representing the input vector(s). |
required |
Returns:
| Type | Description |
|---|---|
Array
|
A 1D or 2D array containing the solution vector(s) y. |
Source code in src/op_engine/matrix_ops.py
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 | |
kron_prod(a, b)
¶
Compute the Kronecker product of two operators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Operator
|
First operator. |
required |
b
|
Operator
|
Second operator. |
required |
Returns:
| Type | Description |
|---|---|
Operator
|
The Kronecker product operator. |
Source code in src/op_engine/matrix_ops.py
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 | |
kron_sum(ops)
¶
Compute a Kronecker sum of square operators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ops
|
list[Operator]
|
List of 2D square operators. |
required |
Returns:
| Type | Description |
|---|---|
Operator
|
The Kronecker sum operator. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If ops is empty or if operators are not square or have incompatible shapes. |
Source code in src/op_engine/matrix_ops.py
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 | |
make_constant_base_builder(operator)
¶
Convenience: wrap a constant operator as a BaseOperatorBuilder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operator
|
Operator
|
Constant operator to wrap. |
required |
Returns:
| Type | Description |
|---|---|
BaseOperatorBuilder
|
A BaseOperatorBuilder that always returns the given operator. |
Source code in src/op_engine/matrix_ops.py
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 | |
make_stage_operator_factory(base_builder, *, scheme='implicit-euler')
¶
Create a stage operator factory supporting time/state dependent base ops.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_builder
|
BaseOperatorBuilder
|
Function that builds a base operator given stage context. |
required |
scheme
|
str
|
Implicit scheme; either "implicit-euler" or "trapezoidal". |
'implicit-euler'
|
Returns:
| Type | Description |
|---|---|
StageOperatorFactory
|
A StageOperatorFactory that builds (L, R) operators for the given scheme. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an unknown scheme is provided. |
Source code in src/op_engine/matrix_ops.py
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 | |
matrix_grouped_count(group_matrix)
¶
Perform grouped count using a group matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_matrix
|
csr_matrix | DenseOperator
|
2D group matrix (csr_matrix or dense ndarray). |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
A 1D array containing the counts for each group. |
Source code in src/op_engine/matrix_ops.py
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 | |
matrix_grouped_sum(group_matrix, values)
¶
Perform grouped sum using a group matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_matrix
|
csr_matrix | DenseOperator
|
2D group matrix (csr_matrix or dense ndarray). |
required |
values
|
NDArray[floating]
|
1D array of values to be summed. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
A 1D array containing the grouped sums. |
Source code in src/op_engine/matrix_ops.py
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 | |
matrix_masked_sum(mask_matrix, data)
¶
Perform masked sum using a mask matrix and data array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask_matrix
|
csr_matrix | DenseOperator
|
2D mask matrix (csr_matrix or dense ndarray). |
required |
data
|
NDArray[floating]
|
1D or 2D data array to be masked and summed. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
A 1D or 2D array containing the masked sums. |
Source code in src/op_engine/matrix_ops.py
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 | |
smooth(x, alpha=0.02, out=None)
¶
Apply simple smoothing along the last axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
NDArray[floating]
|
Input array to smooth. |
required |
alpha
|
float
|
Smoothing factor between 0 and 1. |
0.02
|
out
|
NDArray[floating] | None
|
Optional output array to store the result. |
None
|
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Smoothed array with the same shape as x. |
Source code in src/op_engine/matrix_ops.py
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 | |