Skip to content

Commit

Permalink
Rewrite spzeros and speye in terms of zeros and eye, respectively
Browse files Browse the repository at this point in the history
  • Loading branch information
martinholters committed Jun 13, 2016
1 parent defd6c9 commit 2529e7b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
40 changes: 20 additions & 20 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions base/sparse/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down

0 comments on commit 2529e7b

Please sign in to comment.