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

Undeprecate real and imag for sparse matrices #22090

Merged
merged 1 commit into from
May 28, 2017

Conversation

fredrikekre
Copy link
Member

fix #22065

ref #18566, #18495 (comment)

conj got the same treatment in #19242

cc @Sacha0

@fredrikekre
Copy link
Member Author

Hmm... This does not quite restore the 0.5 behaviour. On 0.5 real gives a reference (

real{T<:Real}(x::AbstractArray{T}) = x
) for A::AbstractArray{<:Real} but a copy (
real{Tv<:Number,Ti}(A::SparseMatrixCSC{Tv,Ti}) = copy(A)
) for A::SparseMatrix{<:Real}. That seems like a bug?

@tkelman
Copy link
Contributor

tkelman commented May 26, 2017

where is the fallback for this? should test that at least for complex arrays where this should return a new array, the index arrays aren't aliasing, for SparseMatrixCSC or SparseVector

@fredrikekre
Copy link
Member Author

Well, that issue is automatically fixed by this PR currently since sparse matrix will dispatch to

real(x::AbstractArray{<:Real}) = x

On the other hand, imag(A::SparseMatrix{<:Real}) on 0.5 results in an empty sparse matrix, while with this pr currently we will dispatch here and obtain a matrix with explicitly stored zeroes.

@kshyatt kshyatt added kind:deprecation This change introduces or involves a deprecation domain:arrays:sparse Sparse arrays labels May 27, 2017
@tkelman
Copy link
Contributor

tkelman commented May 27, 2017

that issue is automatically fixed by this PR currently since sparse matrix will dispatch to

not for complex elements

@fredrikekre
Copy link
Member Author

I think the behaviour is correct now, will add tests.

@@ -1447,6 +1447,8 @@ sparse(S::UniformScaling, m::Integer, n::Integer=m) = speye_scaled(S.λ, m, n)
conj!(A::SparseMatrixCSC) = (@inbounds broadcast!(conj, A.nzval, A.nzval); A)
(-)(A::SparseMatrixCSC) = SparseMatrixCSC(A.m, A.n, copy(A.colptr), copy(A.rowval), map(-, A.nzval))

# the rest of real, conj, imag are handled correctly via AbstractArray methods
imag(A::SparseMatrixCSC{Tv,Ti}) where {Tv<:Number,Ti} = spzeros(Tv, Ti, A.m, A.n)
Copy link
Contributor

Choose a reason for hiding this comment

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

wouldn't SparseVector need the same?

Copy link
Member Author

Choose a reason for hiding this comment

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

imag(x::AbstractSparseVector{Tv,Ti}) where {Tv<:Real,Ti<:Integer} = SparseVector(length(x), Ti[], Tv[])

Seems like there has been some inconsistency here for AbstractArray/SparseMatrixCSC/SparseVector.

Copy link
Member

@Sacha0 Sacha0 left a comment

Choose a reason for hiding this comment

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

lgtm from a rapid read --- thanks @fredrikekre! :)

@@ -985,7 +985,6 @@ hvcat(rows::Tuple{Vararg{Int}}, xs::_TypedDenseConcatGroup{T}...) where {T} = Ba
### Unary Map

# zero-preserving functions (z->z, nz->nz)
conj(x::SparseVector) = SparseVector(length(x), copy(nonzeroinds(x)), conj(nonzeros(x)))
Copy link
Contributor

Choose a reason for hiding this comment

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

this may have been kept as a separate method if it was faster than broadcast

Copy link
Member

Choose a reason for hiding this comment

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

Should be faster, if marginally. Worth the extra complexity though?

Copy link
Contributor

Choose a reason for hiding this comment

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

it's one line, so if it makes more than say 10-15% difference, imo yes

Copy link
Member Author

Choose a reason for hiding this comment

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

Didn't think about that, I will add it back

Copy link
Member

Choose a reason for hiding this comment

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

(Checked performance difference, slightly under a factor of two in a simple test case.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Might as well add such a shortcut for SparseMatrixCSC also?

Copy link
Contributor

Choose a reason for hiding this comment

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

if it doesn't have one, might be (and worth tracking along with other sparse broadcast perf in case it gets better someday and this stops being as much of a difference)

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

make sure aliasing is the same as for AbstracArrays for SparseArray{<:Real}
add tests for this
add shortcut for conj(SparseArray{<:Complex} which is faster than broadcasting
@KristofferC KristofferC merged commit 6b0d54e into JuliaLang:master May 28, 2017
@fredrikekre fredrikekre deleted the fe/real-imag-sparse branch May 28, 2017 13:41
tkelman pushed a commit that referenced this pull request Jun 3, 2017
make sure aliasing is the same as for AbstracArrays for SparseArray{<:Real}
add tests for this
add shortcut for conj(SparseArray{<:Complex} which is faster than broadcasting
(cherry picked from commit 6b0d54e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:arrays:sparse Sparse arrays kind:deprecation This change introduces or involves a deprecation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

real(::SparseMatrixCSC) should not be deprecated
5 participants