matrix-out-methods#44
Merged
Merged
Conversation
A `Matrix` ergonomics release. The dense matrix type gains named method forms of the arithmetic operators, a numpy-style ``out=`` target on every element-wise method, and a two-rounding ``scaled_add`` companion to ``fma``. The shared ``out=`` machinery is hardened against Python-sequence operands so no method form can be tricked into returning a mistyped object. **New Features** - **`Matrix.add` / `subtract` / `multiply` / `divide`** — named method forms of ``+`` ``-`` ``*`` ``/`` (``m.add(other, out=...)``), bit-for-bit identical to the operators and sharing their broadcasting: *other* may be a same-shape matrix, a ``1x1`` matrix, a ``1xN`` / ``Mx1`` vector that broadcasts, or a scalar. - **`out=` targets on every element-wise method** — ``add`` / ``subtract`` / ``multiply`` / ``divide`` and the unary ufuncs ``ceil`` / ``floor`` / ``round`` / ``negate`` / ``abs`` / ``sqrt`` accept a keyword-only ``out=`` :class:`Matrix` to write into allocation-free, returning that matrix in place of a fresh one. For the unary ufuncs ``out`` is mutually exclusive with ``in_place``; the binary forms may alias an input operand. - **`Matrix.scaled_add(s, x)`** — the two-rounding sibling of :meth:`Matrix.fma`, computing ``self + s * x`` bit-for-bit identical to the plain expression (the product and the sum are each rounded). *s* may be a same-shape matrix, a ``1x1`` matrix, a broadcasting row / column vector, or a scalar; *x* is a same-shape matrix. Has an ``in_place=True`` form. Use it when you need the plain ``self + s * x`` rounding and :meth:`Matrix.fma` for the single-rounding fused form. **Documentation** - Expanded the :doc:`api` matrix surface with the new ``scaled_add``, ``add`` / ``subtract`` / ``multiply`` / ``divide``, and ``out=`` parameters via the ``__init__.pyi`` stub docstrings, including the aliasing and ``in_place`` mutual-exclusivity rules. **Tests** - Added operator-parity, scalar, ``out=`` round-trip, aliasing, shape-mismatch, wrong-type, keyword-only, and unacquired-cown coverage for the named binary methods and the unary ``out=`` targets, plus a sequence-coercion regression suite that exercises the hardened wrap path across the binary operators and ``@``. **Internal** - Hardened the shared ``out=`` / sequence-coercion path so a coerced ``list`` / ``tuple`` operand can no longer cause an element-wise method or ``@`` to allocate a mistyped result object; the wrap type is now always the canonical per-interpreter ``Matrix`` type and result shapes are read from the unwrapped buffer. - Refreshed the audit and docs dependency constraints (``ci/constraints-audit.txt``, ``ci/constraints-docs.txt``) and bumped the pinned GitHub Actions in the wheel-build, nightly-audit, and PR-gate workflows. Signed-off-by: Matthew A Johnson <matthew@matthewajohnson.org>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A
Matrixergonomics release. The dense matrix type gains named method forms of the arithmetic operators, a numpy-styleout=target on every element-wise method, and a two-roundingscaled_addcompanion tofma. The sharedout=machinery is hardened against Python-sequence operands so no method form can be tricked into returning a mistyped object.New Features
Matrix.add/subtract/multiply/divide— named method forms of+-*/(m.add(other, out=...)), bit-for-bit identical to the operators and sharing their broadcasting: other may be a same-shape matrix, a1x1matrix, a1xN/Mx1vector that broadcasts, or a scalar.out=targets on every element-wise method —add/subtract/multiply/divideand the unary ufuncsceil/floor/round/negate/abs/sqrtaccept a keyword-onlyout=:class:Matrixto write into allocation-free, returning that matrix in place of a fresh one. For the unary ufuncsoutis mutually exclusive within_place; the binary forms may alias an input operand.Matrix.scaled_add(s, x)— the two-rounding sibling of :meth:Matrix.fma, computingself + s * xbit-for-bit identical to the plain expression (the product and the sum are each rounded). s may be a same-shape matrix, a1x1matrix, a broadcasting row / column vector, or a scalar; x is a same-shape matrix. Has anin_place=Trueform. Use it when you need the plainself + s * xrounding andMatrix.fmafor the single-rounding fused form.Documentation
apimatrix surface with the newscaled_add,add/subtract/multiply/divide, andout=parameters via the__init__.pyistub docstrings, including the aliasing andin_placemutual-exclusivity rules.Tests
out=round-trip, aliasing, shape-mismatch, wrong-type, keyword-only, and unacquired-cown coverage for the named binary methods and the unaryout=targets, plus a sequence-coercion regression suite that exercises the hardened wrap path across the binary operators and@.Internal
out=/ sequence-coercion path so a coercedlist/tupleoperand can no longer cause an element-wise method or@to allocate a mistyped result object; the wrap type is now always the canonical per-interpreterMatrixtype and result shapes are read from the unwrapped buffer.ci/constraints-audit.txt,ci/constraints-docs.txt) and bumped the pinned GitHub Actions in the wheel-build, nightly-audit, and PR-gate workflows.