JuliaPhysics / JuliaPhysics/Unitful.jl

Untiful sparse arrays in matrix equation solvers

Open
#515 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
Julia
Stars
675
Forks
124
Avg merge
3h 38m
Merged PRs (30d)
1

Description

Hello :-)

Does anyone know a non-allocating way to create a unit-stripped view into a `SparseMatrixCSC`? Let's assume I have the following sparse array:

```julia
A = (sprand(3, 3, 0.5))u"1/s"
```

Now if I want to use `ustrip` directly on that matrix, it'll default to `ustrip.(A)`, which will allocate memory (which we want to avoid). A workaround could be using a reinterpretation of `A`, as it is done in the `ustrip()` implementation for the `Matrix` type:

```julia
reinterpret(Unitful.numtype(eltype(A)), A)
ERROR: `reinterpret` on sparse arrays is discontinued.
Try reinterpreting the value itself instead.
```
:-(

Now let's assume we have the following idea:

```julia
nzval_ustripped = ustrip(A.nzval)
A_new = SparseMatrixCSC(A.m, A.n, A.colptr, A.rowval, nzval_ustripped)
ERROR: MethodError: no method matching SparseMatrixCSC(::Int64, ::Int64, ::Vector{Int64}, ::Vector{Int64}, ::Base.ReinterpretArray{Float64, 1, Quantity{Float64, 𝐓 ^-1, Unitful.FreeUnits{(s^-1,), 𝐓 ^-1, nothing}}, Vector{Quantity{Float64, 𝐓 ^-1, Unitful.FreeUnits{(s^-1,), 𝐓 ^-1, nothing}}}, false})
```

The problem here is that `Base.ReinterpretArray` is not a `Vector`, which is required by the `SparseMatrixCSC` type. And now I'm out of ideas ;-)

Does anyone else have an idea how we can have unit-stripped views into sparse matrices which can then be used for efficient linear algebra?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by examining Unitful's ustrip implementation for Matrix and the SparseMatrixCSC constructor behavior described in the issue. Investigate how A.nzval and reinterpretation interact with sparse arrays. Done means providing a non-allocating unit-stripped view that can be passed to efficient linear algebra routines.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.