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

Make broadcast over sparse and rowvecs sparse #20959

Merged
merged 1 commit into from
Mar 17, 2017

Conversation

pabloferz
Copy link
Contributor

Part of #20102, fixes #20954.

@tkelman tkelman requested a review from Sacha0 March 9, 2017 15:18
@@ -1061,8 +1062,8 @@ promote_containertype(::Type{Array}, ::Type{AbstractSparseArray}) = PromoteToSpa
# mostly just disambiguates Array from the main containertype promotion mechanism
# AbstractArray serves as a marker to shunt to the generic AbstractArray broadcast code
_spcontainertype(x) = _containertype(x)
_spcontainertype(::Type{<:Vector}) = Vector
_spcontainertype(::Type{<:Matrix}) = Matrix
_spcontainertype(::Type{<:AbstractVector}) = Vector
Copy link
Member

Choose a reason for hiding this comment

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

Loosening these signatures would cause sparse broadcast[!] to capture a much broader array of type combinations than it does now. A conservative approach seems wiser. Instead just add a method for row vectors?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the future I'd love if we could have some kind of out-of-the-box functionality being guaranteed for types that implement some interfaces, e.g. the AbstractArray interface. That is, it would be cool IMO if you could properly define the interface methods and the you automatically you get the same behavior as for other subtypes of AbstractArray when broadcasting with other type of containers, and only have to worry about implementing more methods if you want a specific behavior.

This change and the general idea in #20102 are some proposals in that direction, but of course, that needs more careful design. So I could go with a more conservative approach for now, but I really hope we can come up with a better structure for all of these.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed! :)

@kshyatt kshyatt added sparse Sparse arrays broadcast Applying a function over a collection labels Mar 9, 2017
@inline _sparsifystructured(M::StructuredMatrix) = SparseMatrixCSC(M)
@inline _sparsifystructured(M::Matrix) = SparseMatrixCSC(M)
@inline _sparsifystructured(V::Vector) = SparseVector(V)
@inline _sparsifystructured(M::AbstractMatrix) = SparseMatrixCSC(M)
Copy link
Member

Choose a reason for hiding this comment

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

As I mentioned in #20102, I like these changes: These definitions are safer if we at some point broaden the set of type combinations sparse broadcast[!] captures. On the other hand, absent that broadening, being explicit about the types sparse broadcast[!] handles and what it does with them is also nice. But then being explicit in _spcontainertype should be sufficient, so I favor keeping these changes. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd also prefer having this and if necessary, then being more conservative over _spcontainertype.

@test broadcast!(*, Z, s, V, A, X) == sparse(broadcast(*, s, fV, fA, X))
# Issue #20954 combinations of sparse arrays and RowVectors
Copy link
Member

Choose a reason for hiding this comment

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

X runs over both vectors and matrices in these two tests. If you intended to test only vectors (transposing matrix X not testing anything additional at the moment), move these below the loop? Or did you place these inside the loop in anticipation of lazy transposition of matrices appearing in the next dev cycle?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is intentional. It would work now, so this would make sure it doesn't regress in the future if/when the lazy transposition of matrices land.

@pabloferz
Copy link
Contributor Author

All right, I went with a more conservative approach for _spcontainertype for now. I believe it should be good to merge.

@pabloferz
Copy link
Contributor Author

pabloferz commented Mar 16, 2017

I'll merge this by the end of the day (CST) if there are no objections.

@pabloferz pabloferz merged commit a2e714b into JuliaLang:master Mar 17, 2017
@pabloferz pabloferz deleted the pz/bcsprowvec branch March 17, 2017 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
broadcast Applying a function over a collection sparse Sparse arrays
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sparse .* rowvec is dense
3 participants