Skip to content

Commit

Permalink
Add SparseMatrixCSC[{Tv,Ti}](s::UniformScaling, n) constructors for c…
Browse files Browse the repository at this point in the history
…onvenience.
  • Loading branch information
Sacha0 committed Oct 30, 2017
1 parent 6eee482 commit f5bdacb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,10 @@ function SparseMatrixCSC{Tv,Ti}(s::UniformScaling, dims::Dims{2}) where {Tv,Ti}
for i in (k + 2):(n + 1) colptr[i] = (k + 1) end
SparseMatrixCSC{Tv,Ti}(dims..., colptr, rowval, nzval)
end
# convenience variations that accept a single integer to specify dims
SparseMatrixCSC{Tv,Ti}(s::UniformScaling, m::Integer) where {Tv,Ti} = SparseMatrixCSC{Tv,Ti}(s, m, m)
SparseMatrixCSC{Tv}(s::UniformScaling, m::Integer) where {Tv} = SparseMatrixCSC{Tv}(s, m, m)
SparseMatrixCSC(s::UniformScaling, m::Integer) = SparseMatrixCSC(s, m, m)


Base.iszero(A::SparseMatrixCSC) = iszero(view(A.nzval, 1:(A.colptr[size(A, 2) + 1] - 1)))
Expand Down
3 changes: 3 additions & 0 deletions test/sparse/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ end
end

@testset "SparseMatrixCSC construction from UniformScaling" begin
@test_throws ArgumentError SparseMatrixCSC(I, -1)
@test_throws ArgumentError SparseMatrixCSC(I, -1, 3)
@test_throws ArgumentError SparseMatrixCSC(I, 3, -1)
@test SparseMatrixCSC(2I, 3)::SparseMatrixCSC{Int,Int} == 2*eye(3)
@test SparseMatrixCSC(2I, 3, 3)::SparseMatrixCSC{Int,Int} == 2*eye(3)
@test SparseMatrixCSC(2I, 3, 4)::SparseMatrixCSC{Int,Int} == 2*eye(3, 4)
@test SparseMatrixCSC(2I, 4, 3)::SparseMatrixCSC{Int,Int} == 2*eye(4, 3)
@test SparseMatrixCSC(2.0I, 3, 3)::SparseMatrixCSC{Float64,Int} == 2*eye(3)
@test SparseMatrixCSC{Real}(2I, 3)::SparseMatrixCSC{Real,Int} == 2*eye(3)
@test SparseMatrixCSC{Real}(2I, 3, 3)::SparseMatrixCSC{Real,Int} == 2*eye(3)
@test SparseMatrixCSC{Float64}(2I, 3, 3)::SparseMatrixCSC{Float64,Int} == 2*eye(3)
@test SparseMatrixCSC{Float64,Int32}(2I, 3, 3)::SparseMatrixCSC{Float64,Int32} == 2*eye(3)
Expand Down

0 comments on commit f5bdacb

Please sign in to comment.