diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index 5fbe4ddbefbf5..1902508d7ab3f 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -332,7 +332,7 @@ function sparse_IJ_sorted!{Ti<:Integer}(I::AbstractVector{Ti}, J::AbstractVector m = m < 0 ? 0 : m n = n < 0 ? 0 : n - if isempty(V); return spzeros(eltype(V),Ti,m,n); end + if isempty(V); return SparseMatrixCSC{eltype(V),Ti}(m,n); end cols = zeros(Ti, n+1) cols[1] = 1 # For cumsum purposes @@ -970,25 +970,32 @@ sparse array will not contain any nonzero values. No storage will be allocated for nonzero values during construction. The type defaults to `Float64` if not specified. """ -spzeros(m::Integer, n::Integer) = spzeros(Float64, m, n) -spzeros(Tv::Type, m::Integer, n::Integer) = spzeros(Tv, Int, m, n) -function spzeros(Tv::Type, Ti::Type, m::Integer, n::Integer) - ((m < 0) || (n < 0)) && throw(ArgumentError("invalid Array dimensions")) - SparseMatrixCSC(m, n, ones(Ti, n+1), Array{Ti}(0), Array{Tv}(0)) -end +spzeros(m::Integer, n::Integer) = zeros(SparseMatrixCSC, m, n) +spzeros(Tv::Type, m::Integer, n::Integer) = zeros(SparseMatrixCSC{Tv}, m, n) +spzeros(Tv::Type, Ti::Type, m::Integer, n::Integer) = zeros(SparseMatrixCSC{Tv,Ti}, m, n) +function eye{Tv,Ti}(::Type{SparseMatrixCSC{Tv,Ti}}, m::Integer, n::Integer) + check_array_size(m, n) + x = min(m,n) + rowval = collect(Ti, 1:x) + colptr = Ti[rowval; fill(Ti(x+1), n+1-x)] + nzval = ones(Tv, x) + return SparseMatrixCSC(m, n, colptr, rowval, nzval) +end +eye{Tv}(::Type{SparseMatrixCSC{Tv}}, m::Integer, n::Integer) = eye(SparseMatrixCSC{Tv,Int}, m, n) +eye(::Type{SparseMatrixCSC}, m::Integer, n::Integer) = eye(SparseMatrixCSC{Float64,Int}, m, n) -speye(n::Integer) = speye(Float64, n) -speye(T::Type, n::Integer) = speye(T, n, n) -speye(m::Integer, n::Integer) = speye(Float64, m, n) +speye(n::Integer) = eye(SparseMatrixCSC, n) +speye(T::Type, n::Integer) = eye(SparseMatrixCSC{T}, n, n) +speye(m::Integer, n::Integer) = eye(SparseMatrixCSC, m, n) """ speye(S) Create a sparse identity matrix with the same structure as that of `S`. """ -speye{T}(S::SparseMatrixCSC{T}) = speye(T, size(S, 1), size(S, 2)) -eye(S::SparseMatrixCSC) = speye(S) +speye{T}(S::SparseMatrixCSC{T}) = eye(SparseMatrixCSC{T}, size(S, 1), size(S, 2)) +eye{T<:SparseMatrixCSC}(S::T) = eye(T, S) """ speye([type,]m[,n]) @@ -997,14 +1004,7 @@ Create a sparse identity matrix of size `m x m`. When `n` is supplied, create a sparse identity matrix of size `m x n`. The type defaults to `Float64` if not specified. """ -function speye(T::Type, m::Integer, n::Integer) - ((m < 0) || (n < 0)) && throw(ArgumentError("invalid Array dimensions")) - x = min(m,n) - rowval = [1:x;] - colptr = [rowval; fill(Int(x+1), n+1-x)] - nzval = ones(T, x) - return SparseMatrixCSC(m, n, colptr, rowval, nzval) -end +speye(T::Type, m::Integer, n::Integer) = eye(SparseMatrixCSC{T}, m, n) function one{T}(S::SparseMatrixCSC{T}) m,n = size(S) diff --git a/base/sparse/sparsevector.jl b/base/sparse/sparsevector.jl index 03477a5765a87..c5854635870a1 100644 --- a/base/sparse/sparsevector.jl +++ b/base/sparse/sparsevector.jl @@ -40,8 +40,8 @@ similar{T}(x::SparseVector, ::Type{T}, D::Dims) = spzeros(T, D...) ### Construct empty sparse vector -spzeros(len::Integer) = spzeros(Float64, len) -spzeros{T}(::Type{T}, len::Integer) = SparseVector(len, Int[], T[]) +spzeros(len::Integer) = zeros(SparseVector, len) +spzeros{T}(::Type{T}, len::Integer) = zeros(SparseVector{T}, len) # Construction of same structure, but with all ones spones{T}(x::SparseVector{T}) = SparseVector(x.n, copy(x.nzind), ones(T, length(x.nzval)))