A Matrix capability release. The dense matrix type gains NumPy-style fancy
indexing, comparison masks and lexicographic comparison operators, a
where selector, a single-rounding fma with vector broadcasting, and
sqrt. Two older method names move to their NumPy spellings: select
becomes take and clip adopts min / max keyword bounds.
New Features
- Fancy indexing —
m[[r0, r1]]/m[[r0, r1], :]gather rows and
m[:, [c0, c1]]gathers columns, returning a :class:Matrix; the
matching assignment forms scatter into rows or columns with last-write-wins
duplicates and all-or-nothing validation. New :meth:Matrix.takeand
:meth:Matrix.putexpose the same gather/scatter as methods,putwith
anaccumulate=Truemode that folds duplicate indices. - Comparison masks — :meth:
Matrix.less,less_equal,greater,
greater_equal,equal, andnot_equalreturn a1.0/0.0
mask matrix, accepting a same-shape matrix, a scalar (includingbool), a
1x1matrix, a broadcasting row/column vector, or a list/tuple of
numbers. Distinct from the comparison operators, which return a single
bool. - Lexicographic comparison operators —
<<=>>===
!=compare element by element in row-major order and return a single
:class:bool.==/!=are total: a shape mismatch or an
uncoercible list/tuple yieldsFalse/Truerather than raising, so
matrix in some_listworks. ANaNnever decides the comparison, so an
all-NaNmatrix compares==equal to itself. Defining value equality
makes :class:Matrixunhashable. Matrix.where(mask, a, b)— a NumPy-style selector taking a where the
mask is non-zero (NaNcounts as non-zero) and b elsewhere; a and b
may each be a scalar, a same-shape matrix, or a list/tuple of numbers.Matrix.fma(b, c)— fused multiply-add computing single-rounding
self * b + c; b and c may be a same-shape matrix, a1x1matrix,
a scalar, or a row / column vector that broadcasts againstself. The
contraction kernel is preserved so hardware FMA still applies. Use it as an
accuracy primitive — compare results with :meth:Matrix.allclose, never
==.Matrix.sqrt()— element-wise square root (negative inputs map to
NaN), with anin_place=Trueform.
Breaking Changes
Matrix.selectrenamed toMatrix.take. The gather method is now
spelled :meth:Matrix.taketo match NumPy and pair with the new
:meth:Matrix.put. Replacem.select(indices, axis)with
m.take(indices, axis); the signature and semantics are otherwise
unchanged.Matrix.clipbounds are nowmin/maxkeywords. The signature
changes fromclip(min_or_maxval, maxval=None)to
clip(min=None, max=None), matching :func:numpy.clip. Either bound may
be omitted to leave that side unbounded:m.clip(min=0.0)clamps only
below,m.clip(max=255.0)only above.
Documentation
- Expanded the :doc:
apimatrix surface for the new indexing, masking,
comparison,where,fma, andsqrtmethods via the
__init__.pyistub docstrings, including the totality,NaN, and
broadcasting rules.
Tests
- Extensive
test_matrix.pyadditions covering fancy-index gather/scatter,
take/put(including accumulate and all-or-nothing validation),
the comparison masks, lexicographic operators (totality,NaN,
reflected-scalar, and list/tuple/bool coercion edge cases),where
selection and value propagation, andfmarow/column broadcasting.
Internal
- New
bench_fmaandbench_takemicro-benchmarks in
scripts/bench_matrix.py. Theexamples/boids.pydemo migrates from
selecttotake.