Skip to content

Conversation

@nathanielpritchard
Copy link
Contributor

Description

Changes the kaczmarz solver to work for sparse matrices and vectors when Sampling compressor is used.
Fixes #135

Motivation and Context

Currently, using Kaczmarz with a sparse matrix and Sampling compressor it returns an error about being unable to convert a SparseArray to an Array. This is because in the initial allocations, only dense arrays were allocated. To fix this a few changes needed to be made.

  1. I needed to add a condition in the complete_solver function that performed preallocations of sparse arrays if a Sampling compressor is used with a sparse matrix.
  2. I needed to add a check to ensure that if the row or block of rows was all zeros no update would be run.
  3. I needed to change the * implementation for the Sampling compressor to preallocate SparseArrays when being applied to sparse objects.
  4. I have added tests for the Sampling and Kaczmarz structures that check the code on sparse matrices.

How has this been tested

The following code should now work.

using RLinearAlgebra
using SparseArrays
using LinearAlgebra
using Random

Random.seed!(42)

# Create a sparse linear system
m, n = 100, 50
A = sprand(m, n, 0.1)  # 10% density sparse matrix
b = rand(m)
x = zeros(n)

# Configure Kaczmarz solver with Sampling compressor
compressor = Sampling(
    cardinality=Left(),
    compression_dim=5,
    distribution=Uniform()
)

solver = Kaczmarz(
    compressor=compressor,
    log=BasicLogger(max_it=10),
    error=FullResidual(),
    sub_solver=LQSolver()
)

# This fails with sparse matrix
x_sol, recipe = rsolve!(solver, x, A, b)

Additionally, the new tests now incorporate sparse matrices in the completion of the solver and in the block update steps, which should be sufficient for ensuring that everything works as expected for sparse matrices in the future.

Types of changes

  • CI
  • Docs
  • Feature
  • Fix
  • Performance
  • Refactor
  • Style
  • Test
  • Other (use sparingly):

Checklists:

Code and Comments
If this PR includes modification to the code base, please select all that apply.

  • My code follows the code style of this project.
  • I have updated all package dependencies (if any).
  • I have included all relevant files to realize the functionality of the PR.
  • I have exported relevant functionality (if any).

API Documentation

  • For every exported function (if any), I have included a detailed docstring.
  • I have checked the spelling and grammar of all docstring updates through an external tool.
  • I have checked that the docstring's function signature is correctly formatted and has all arguments.
  • I have checked that the docstring's list of arguments, fields, or return values match the function.
  • I have compiled the docs locally and read through all docstring updates to check for errors.

Manual Documentation

  • I have checked the spelling and grammar of all manual updates through an external tool.
  • Any code included in the docstring is tested using doc tests to ensure consistency.
  • I have compiled the docs locally and read through all manual updates to check for errors.

Testing

  • I have added unit tests to cover my changes. (For Macros, be sure to check
    @code_lowered and
    @code_typed)
  • All new and existing tests passed.
  • I have achieved sufficient code coverage.

@nathanielpritchard nathanielpritchard self-assigned this Nov 19, 2025
@nathanielpritchard nathanielpritchard added the bug Something isn't working label Nov 19, 2025
@codecov
Copy link

codecov bot commented Nov 19, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Kaczmarz solver fails with sparse matrices and Sampling compressor

2 participants