Skip to content

Fix \ for sparse matrices with LayoutArray right-hand sides - #288

Merged
dlfivefifty merged 1 commit into
masterfrom
sparse-layout-ldiv
Oct 13, 2026
Merged

dlfivefifty merged 1 commit into
masterfrom
sparse-layout-ldiv

Conversation

@dlfivefifty

Copy link
Copy Markdown
Member

A \ B fails when A is a sparse matrix and B is a LayoutArray. Found via BlockArrays.jl, where sparse_matrix \ BlockedVector fails.

Ambiguity. For A a sparse matrix, its Adjoint or its Transpose, A \ B is ambiguous between SparseArrays' \(::AbstractSparseMatrixCSC / AdjOrTrans{sparse}, ::AbstractVecOrMat) and the \(::AbstractMatrix, ::Typ) methods from @layoutldiv. B here is a LayoutVector, a LayoutMatrix, a Diagonal{<:LayoutVector}, or a triangular wrapper of a LayoutMatrix (or of its view, adjoint or transpose).

julia> sparse([4.0 1; 1 4]) \ BlockedArray([1.0, 2], [1, 1])
ERROR: MethodError: \(::SparseMatrixCSC{Float64, Int64}, ::BlockedVector{...}) is ambiguous.

CHOLMOD. For A a Symmetric/Hermitian sparse matrix, A \ B goes through ArrayLayouts.ldiv. That ends in ldiv!(::CHOLMOD.Factor, ...), which CHOLMOD does not support. It fails whenever copy(B) is not a strided array, e.g. BlockedVector, or the MyVector/MyMatrix test types:

MethodError: no method matching ldiv!(::SparseArrays.CHOLMOD.Factor{Float64, Int64}, ::Vector{Float64})

Fix

In ArrayLayoutsSparseArraysExt, define \ for each of these sparse matrix types against each layout right-hand side type. Each method converts B to an Array and calls SparseArrays' solver, which requires strided arrays anyway. A Diagonal{<:LayoutVector} keeps its Diagonal wrapper, with only the diagonal converted.

This removes all 57 \ ambiguities between ArrayLayouts and SparseArrays that have a sparse matrix on the left. 48 ambiguities in the other direction remain: a triangular layout matrix \ a sparse vector or matrix. They're not touched here.

Tests

  • Added a sparse/ldiv testset in test_layoutarray.jl covering every combination. It errors without the fix.
  • The full test suite passes.
  • With this branch, the 20 previously failing BlockArrays.jl combinations work, with no BlockArrays changes:
    • sparse, Adjoint, Transpose, Symmetric and Hermitian matrices
    • against BlockedVector, BlockVector, BlockedMatrix and BlockMatrix right-hand sides.

Version bumped to 1.15.1.

🤖 Generated with Claude Code

`\` with a sparse matrix (or its Adjoint/Transpose) and a LayoutVector,
LayoutMatrix, Diagonal{<:LayoutVector} or triangular LayoutMatrix right-hand side
was ambiguous between SparseArrays and the @layoutldiv overloads.
Symmetric/Hermitian sparse matrices errored for LayoutArrays whose copy is not
strided, since CHOLMOD does not support ldiv! with such arrays.

Resolve both by converting the right-hand side to an Array and calling
SparseArrays' solver.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.57%. Comparing base (0d4c00b) to head (29765de).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #288      +/-   ##
==========================================
+ Coverage   90.33%   90.57%   +0.24%     
==========================================
  Files          12       12              
  Lines        1987     2038      +51     
==========================================
+ Hits         1795     1846      +51     
  Misses        192      192              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dlfivefifty
dlfivefifty merged commit 395126e into master Oct 13, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant