Skip to content

Commit

Permalink
tests for conj, real, imag of SparseMatrixCSC and SparseVector
Browse files Browse the repository at this point in the history
fix aliasing to match abstractarray methods
  • Loading branch information
fredrikekre committed May 27, 2017
1 parent fe64bdc commit a521427
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions base/sparse/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions test/sparse/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion test/sparse/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a521427

Please sign in to comment.