Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let spdiagm preserve sparsity #37254

Closed
wants to merge 31 commits into from
Closed

Let spdiagm preserve sparsity #37254

wants to merge 31 commits into from

Conversation

dkarrasch
Copy link
Member

@dkarrasch dkarrasch commented Aug 28, 2020

Closes #37174.

With this PR:

julia> y = sprand(10, 0.2)
10-element SparseVector{Float64,Int64} with 4 stored entries:
  [1 ]  =  0.290569
  [2 ]  =  0.454582
  [6 ]  =  0.578091
  [9 ]  =  0.822982

julia> spdiagm(0 => y)
10×10 SparseMatrixCSC{Float64,Int64} with 4 stored entries:
 0.290569                                                   
          0.454582                                          
                                                           
                                                           
                                                           
                               0.578091                     
                                                           
                                                           
                                                0.822982    
                                                           

@dkarrasch dkarrasch added the sparse Sparse arrays label Aug 28, 2020
@barucden
Copy link
Contributor

Have you considered using dispatching over isa? For example, the methods for determining the number of (non-zero) coefficients

_numel(vect::SparseVector) = nnz(vect)
_numel(vect::AbstractArray) = length(vect)

instead of

p.second isa AbstractSparseVector ? nnz(p.second) : length(p.second)

?

Imho, it is more readable and/or extensible.

Copy link
Contributor

@barucden barucden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AbstractSparseVector (unlike SparseVector) should be available since it is defined in abstractsparse.jl, which is included prior to sparsematrix.jl in SparseArrays.jl. I guess your second point is valid. I just think that dispatching is clearer than isa branching. Perhaps, someone more experienced will join the discussion.

stdlib/SparseArrays/src/sparsematrix.jl Outdated Show resolved Hide resolved
@andreasnoack
Copy link
Member

Which part of #37174 isn't addressed here?

@dkarrasch
Copy link
Member Author

Which part of #37174 isn't addressed here?

The wish for a default spdiagm(v::AbstractVector) = spdiagm(0 => v). It's easy to add, actually, but I'm not sure if there is some hidden reason why that hasn't been done earlier.

@dkarrasch
Copy link
Member Author

I included the convenience constructor. Originally, without much thinking, I was under the impression that this might be somewhat controversial, but the analogy to diagm is straightforward, and I don't think there is any conflict with existing functionality. Is this something we need to announce in NEWS? Seems pretty minor.

@andreasnoack
Copy link
Member

The wish for a default spdiagm(v::AbstractVector) = spdiagm(0 => v).

Thanks. I missed that part. I guess the only reason not to have it would be to keep the code slightly simpler by only having a single interface for spdiagm. However, I suspect most people would intuitively try calling spdiagm(v::AbstractVector) as their first attempt so it might be friendly to include that method.

@ViralBShah
Copy link
Member

Good stuff - Since @andreasnoack has already reviewed, I am happy (without my own review).

Off-topic - I wish we could pick better names (and even move sparse matrices out of stdlib sooner than 2.0)

dkarrasch and others added 13 commits September 20, 2020 20:30
Although we don't really need a logo here.
`dlopen`ing the release version of the library in the debug build is a **REALLY** bad idea.
Deserializing an array needs to examine the element type (tparam0)
layout. Usually we know the layout of a DataType is initialized early
(when present). This also ensures that the path to it is initialized
(for our case where it may be inline allocated with interior pointers).
#37652)

* add in_sysimage to check if a package is in the sysimage

* suggestion to empty _sysimage_modules first

Co-authored-by: Takafumi Arakaki <takafumi.a@gmail.com>

* add tests for in_sysimage

* Fix typo in test

Co-authored-by: Takafumi Arakaki <takafumi.a@gmail.com>
add an error check that the max size is not exceeded
author Daniel Karrasch <Daniel.Karrasch@gmx.de> 1598605142 +0200
committer Daniel Karrasch <Daniel.Karrasch@gmx.de> 1600713298 +0200

let spdiagm preserve sparsity
@dkarrasch
Copy link
Member Author

Something went horribly wrong with rebasing. I'll try again with #37684.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sparse Sparse arrays
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unexpected behaviours of spdiagm
9 participants