Support adding AbstractPDMats backed by static arrays#239
Merged
Conversation
`+` and `pdadd` of a `PDMat`/`PDiagMat`/`ScalMat` failed when the underlying storage was a static array (e.g. `SMatrix`/`SVector`, issue #167), because `_adddiag`/`_adddiag!` were only defined for `Union{Matrix, SparseMatrixCSC}` and mutated in place — which is impossible for immutable arrays. Replace the non-mutating `_adddiag` wrappers with generic `AbstractMatrix` methods that construct the result (`a + Diagonal(v)`, `a + v*I`, `a .+ c.*Diagonal(v)`); these match the previous single-allocation cost for dense matrices, stay sparse for sparse inputs, and preserve the static container otherwise. Add `_scaleadddiag` for the matrix-scaled `pdadd` paths (`c*a + diagonal`), keeping an in-place fast path for mutable storage and falling back to `muladd` elsewhere. The in-place `_adddiag!` is retained for the truly in-place `pdadd!`. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #239 +/- ##
==========================================
+ Coverage 94.78% 94.84% +0.05%
==========================================
Files 11 11
Lines 805 814 +9
==========================================
+ Hits 763 772 +9
Misses 42 42 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Member
Author
|
This is required for #238 |
andreasnoack
approved these changes
Jun 25, 2026
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.
Fixes #167.
+andpdaddof aPDMat/PDiagMat/ScalMaterrored when the underlying storage was a static array, e.g.Root cause
_adddiag/_adddiag!were only defined forUnion{Matrix, SparseMatrixCSC}and mutated in place — which is impossible for immutable arrays (andcopy(::SMatrix)is still immutable). Subtraction already worked because it routes through Base's broadcasting fallback.Changes
_adddiagwrappers with genericAbstractMatrixmethods that build the result by construction (a + Diagonal(v),a + v*I,a .+ c.*Diagonal(v)). Allocation-measured to match the previous copy-and-mutate path for dense matrices (1 allocation), stay sparse for sparse inputs, and preserve the static container otherwise._scaleadddiagfor the matrix-scaledpdaddpaths (c*a + diagonal), keeping an in-place fast path for mutable storage (avoids a second allocation) and falling back tomuladdfor other matrix types._adddiag!— it is still used by the zero-allocationpdadd!and the denseMatrix(b)fallbacks.@check_argdimsto guard against silent broadcast singleton-expansion.Tests
Added coverage in
test/specialarrays.jlfor every+/pdaddcombination ofPDMat{SMatrix}/PDiagMat{SVector}/ScalMat, asserting both numerical correctness and static-type preservation.🤖 Generated with Claude Code