From a5214271957e5fa57b64cb335257ae5ed9de693d Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Sat, 27 May 2017 21:21:09 +0200 Subject: [PATCH] tests for conj, real, imag of SparseMatrixCSC and SparseVector fix aliasing to match abstractarray methods --- base/sparse/sparsematrix.jl | 2 +- base/sparse/sparsevector.jl | 5 ++--- test/sparse/sparse.jl | 10 ++++++++-- test/sparse/sparsevector.jl | 4 +++- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index 220c918c132be5..bcc1e5f5923b99 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -1448,7 +1448,7 @@ 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) +imag(A::SparseMatrixCSC{Tv,Ti}) where {Tv<:Real,Ti} = spzeros(Tv, Ti, A.m, A.n) ## Binary arithmetic and boolean operators (+)(A::SparseMatrixCSC, B::SparseMatrixCSC) = map(+, A, B) diff --git a/base/sparse/sparsevector.jl b/base/sparse/sparsevector.jl index 7f915492a2fd31..525b57e8c48d25 100644 --- a/base/sparse/sparsevector.jl +++ b/base/sparse/sparsevector.jl @@ -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))) -(x::SparseVector) = SparseVector(length(x), copy(nonzeroinds(x)), -(nonzeros(x))) # functions f, such that @@ -1019,12 +1018,12 @@ macro unarymap_nz2z_z2z(op, TF) end) end -real(x::AbstractSparseVector{<:Real}) = x +# the rest of real, conj, imag are handled correctly via AbstractArray methods @unarymap_nz2z_z2z real Complex - imag(x::AbstractSparseVector{Tv,Ti}) where {Tv<:Real,Ti<:Integer} = SparseVector(length(x), Ti[], Tv[]) @unarymap_nz2z_z2z imag Complex + for op in [:floor, :ceil, :trunc, :round] @eval @unarymap_nz2z_z2z $(op) Real end diff --git a/test/sparse/sparse.jl b/test/sparse/sparse.jl index bfca29bcac8f8f..e22c0b7fe1d0dc 100644 --- a/test/sparse/sparse.jl +++ b/test/sparse/sparse.jl @@ -561,13 +561,19 @@ end I = rand(T[1:100;], 2, 2) D = R + I*im S = sparse(D) + spR = sparse(R) + @test R == real.(S) == real(S) @test I == imag.(S) == imag(S) @test conj(full(S)) == conj.(S) == conj(S) - @test real.(sparse(R)) == R - @test nnz(imag.(sparse(R))) == 0 + @test real.(spR) == R + @test nnz(imag.(spR)) == nnz(imag(spR)) == 0 @test abs.(S) == abs.(D) @test abs2.(S) == abs2.(D) + + # test aliasing of real and conj of real valued matrix + @test real(spR) === spR + @test conj(spR) === spR end end diff --git a/test/sparse/sparsevector.jl b/test/sparse/sparsevector.jl index 543b7668160fd3..77024341a19ba5 100644 --- a/test/sparse/sparsevector.jl +++ b/test/sparse/sparsevector.jl @@ -637,14 +637,16 @@ let x = spv_x1, x2 = spv_x2 @test exact_equal(complex.(x2, x), SparseVector(8, [1,2,5,6,7], [3.25+0.0im, 4.0+1.25im, -0.75im, -5.5+3.5im, -6.0+0.0im])) - # real & imag + # real, imag and conj @test real(x) === x @test exact_equal(imag(x), spzeros(Float64, length(x))) + @test conj(x) === x xcp = complex.(x, x2) @test exact_equal(real(xcp), x) @test exact_equal(imag(xcp), x2) + @test exact_equal(conj(xcp), complex.(x, -x2)) end ### Zero-preserving math functions: sparse -> sparse