Skip to content

Commit 4c7bc3f

Browse files
committed
Add Type(dims) constructors for SparseMatrixCSC and SparseVector
Support the following: SparseMatrixCSC(::Dims{2}) SparseMatrixCSC(::Integer, ::Integer) SparseMatrixCSC{Tv}(::Dims{2}) SparseMatrixCSC{Tv}(::Integer, ::Integer) SparseMatrixCSC{Tv,Ti}(::Dims{2}) SparseMatrixCSC{Tv,Ti}(::Integer, ::Integer) SparseVector(::Dims{1}) SparseVector(::Integer) SparseVector{Tv}(::Dims{1}) SparseVector{Tv}(::Integer) SparseVector{Tv,Ti}(::Dims{1}) SparseVector{Tv,Ti}(::Integer)
1 parent b6a71ba commit 4c7bc3f

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed

base/sparse/sparse.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module SparseArrays
44

5-
using Base: ReshapedArray, promote_op, setindex_shape_check, to_shape
5+
using Base: ReshapedArray, promote_op, setindex_shape_check, to_shape, check_array_size
66
using Base.Sort: Forward
77
using Base.LinAlg: AbstractTriangular, PosDefException
88

base/sparse/sparsematrix.jl

+8-2
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,22 @@ immutable SparseMatrixCSC{Tv,Ti<:Integer} <: AbstractSparseMatrix{Tv,Ti}
1313
nzval::Vector{Tv} # Nonzero values
1414

1515
function SparseMatrixCSC(m::Integer, n::Integer, colptr::Vector{Ti}, rowval::Vector{Ti}, nzval::Vector{Tv})
16-
m < 0 && throw(ArgumentError("number of rows (m) must be ≥ 0, got $m"))
17-
n < 0 && throw(ArgumentError("number of columns (n) must be ≥ 0, got $n"))
16+
check_array_size(m, n)
1817
new(Int(m), Int(n), colptr, rowval, nzval)
1918
end
19+
function SparseMatrixCSC(m::Integer, n::Integer)
20+
check_array_size(m, n)
21+
SparseMatrixCSC{Tv,Ti}(m, n, ones(Ti, n+1), Array{Ti}(0), Array{Tv}(0))
22+
end
2023
end
2124
function SparseMatrixCSC(m::Integer, n::Integer, colptr::Vector, rowval::Vector, nzval::Vector)
2225
Tv = eltype(nzval)
2326
Ti = promote_type(eltype(colptr), eltype(rowval))
2427
SparseMatrixCSC{Tv,Ti}(m, n, colptr, rowval, nzval)
2528
end
29+
SparseMatrixCSC(m::Integer, n::Integer) = SparseMatrixCSC{Float64}(m, n)
30+
(::Type{SparseMatrixCSC{Tv}}){Tv}(m::Integer, n::Integer) = SparseMatrixCSC{Tv,Int}(m, n)
31+
(::Type{T}){T<:SparseMatrixCSC}(dims::Dims{2}) = T(dims[1], dims[2])
2632

2733
size(S::SparseMatrixCSC) = (S.m, S.n)
2834

base/sparse/sparsevector.jl

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ immutable SparseVector{Tv,Ti<:Integer} <: AbstractSparseVector{Tv,Ti}
1313
nzind::Vector{Ti} # the indices of nonzeros
1414
nzval::Vector{Tv} # the values of nonzeros
1515

16-
function SparseVector(n::Integer, nzind::Vector{Ti}, nzval::Vector{Tv})
17-
n >= 0 || throw(ArgumentError("The number of elements must be non-negative."))
16+
function SparseVector(n::Integer, nzind::Vector{Ti}=Ti[], nzval::Vector{Tv}=Tv[])
17+
check_array_size(n)
1818
length(nzind) == length(nzval) ||
1919
throw(ArgumentError("index and value vectors must be the same length"))
2020
new(convert(Int, n), nzind, nzval)
@@ -23,6 +23,9 @@ end
2323

2424
SparseVector{Tv,Ti}(n::Integer, nzind::Vector{Ti}, nzval::Vector{Tv}) =
2525
SparseVector{Tv,Ti}(n, nzind, nzval)
26+
SparseVector(n::Integer) = SparseVector{Float64}(n)
27+
(::Type{SparseVector{Tv}}){Tv}(n::Integer) = SparseVector{Tv,Int}(n)
28+
(::Type{T}){T<:SparseVector}(dims::Dims{1}) = T(dims[1])
2629

2730
### Basic properties
2831

test/sparse/sparse.jl

+20
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@
55
@test Base.SparseArrays.indtype(sparse(ones(Int8,2),ones(Int8,2),rand(2))) == Int8
66

77
# check sparse matrix construction
8+
let sz = (3, 4)
9+
X = SparseMatrixCSC(sz)
10+
@test isa(X, SparseMatrixCSC{Float64,Int})
11+
@test size(X) == sz
12+
X = SparseMatrixCSC(sz...)
13+
@test isa(X, SparseMatrixCSC{Float64,Int})
14+
@test size(X) == sz
15+
X = SparseMatrixCSC{Int16}(sz)
16+
@test isa(X, SparseMatrixCSC{Int16,Int})
17+
@test size(X) == sz
18+
X = SparseMatrixCSC{Int16}(sz...)
19+
@test isa(X, SparseMatrixCSC{Int16,Int})
20+
@test size(X) == sz
21+
X = SparseMatrixCSC{Int16,Int8}(sz)
22+
@test isa(X, SparseMatrixCSC{Int16,Int8})
23+
@test size(X) == sz
24+
X = SparseMatrixCSC{Int16,Int8}(sz...)
25+
@test isa(X, SparseMatrixCSC{Int16,Int8})
26+
@test size(X) == sz
27+
end
828
@test isequal(Array(sparse(complex(ones(5,5),ones(5,5)))), complex(ones(5,5),ones(5,5)))
929
@test_throws ArgumentError sparse([1,2,3], [1,2], [1,2,3], 3, 3)
1030
@test_throws ArgumentError sparse([1,2,3], [1,2,3], [1,2], 3, 3)

test/sparse/sparsevector.jl

+22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
# This file is a part of Julia. License is MIT: http://julialang.org/license
22

3+
### Non-initializing contrsutor
4+
let sz = (5,)
5+
X = SparseVector(sz)
6+
@test isa(X, SparseVector{Float64,Int})
7+
@test size(X) == sz
8+
X = SparseVector(sz...)
9+
@test isa(X, SparseVector{Float64,Int})
10+
@test size(X) == sz
11+
X = SparseVector{Int16}(sz)
12+
@test isa(X, SparseVector{Int16,Int})
13+
@test size(X) == sz
14+
X = SparseVector{Int16}(sz...)
15+
@test isa(X, SparseVector{Int16,Int})
16+
@test size(X) == sz
17+
X = SparseVector{Int16,Int8}(sz)
18+
@test isa(X, SparseVector{Int16,Int8})
19+
@test size(X) == sz
20+
X = SparseVector{Int16,Int8}(sz...)
21+
@test isa(X, SparseVector{Int16,Int8})
22+
@test size(X) == sz
23+
end
24+
325
### Data
426

527
spv_x1 = SparseVector(8, [2, 5, 6], [1.25, -0.75, 3.5])

0 commit comments

Comments
 (0)