Skip to content

New method for sum for SubArray of SparseMatrix #377

@btmit

Description

@btmit

I often find myself needing to operate on a subset of a sparse array, typically a few adjacent columns. Creating a new SparseMatrix is relatively expensive, so being able to operate on view would be a big win.

Here's an example

using LinearAlgebra, SparseArrays, BenchmarkTools
s = sprand(101, 99, 0.01);

sc = s[:, 5:11];
sv = view(s, :, 5:11);

# sum parent array
@btime sum($s, dims=1);  # 400.706 ns (4 allocations: 1.02 KiB)
@btime sum($s, dims=2);  # 575.319 ns (9 allocations: 1.97 KiB)

# sum copy of columns
@btime sum($sc, dims=1);  # 244.282 ns (4 allocations: 256 bytes)
@btime sum($sc, dims=2);  # 454.545 ns (9 allocations: 1.97 KiB)

# sum view of columns
@btime sum($sv, dims=1);  # 1.179 μs (5 allocations: 416 bytes)
@btime sum($sv, dims=2);  # 6.442 μs (9 allocations: 1.88 KiB)

It looks like there is a specialized method for 1D column views. It would be great to extend this to groups of columns.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions