Skip to content

Commit 7927829

Browse files
committed
add a test suite to SparseVector
1 parent 45ffc24 commit 7927829

File tree

8 files changed

+202
-25
lines changed

8 files changed

+202
-25
lines changed

base/sparse/linalg.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ function spmatmul{Tv,Ti}(A::SparseMatrixCSC{Tv,Ti}, B::SparseMatrixCSC{Tv,Ti};
220220

221221
# The Gustavson algorithm does not guarantee the product to have sorted row indices.
222222
Cunsorted = SparseMatrixCSC(mA, nB, colptrC, rowvalC, nzvalC)
223-
C = Base.SparseMatrix.sortSparseMatrixCSC!(Cunsorted, sortindices=sortindices)
223+
C = Base.Sparse.sortSparseMatrixCSC!(Cunsorted, sortindices=sortindices)
224224
return C
225225
end
226226

base/sparse/sparsevector.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ Base.abs2(x::SparseVector) = SparseVector(x.n, copy(x.nzind), abs2(x.nzval))
208208

209209
# zero-preserved: f(0, 0) -> 0
210210
function zero_preserve_map{Tx,Ty}(f, x::SparseVector{Tx}, y::SparseVector{Ty})
211-
R = typeof(_eval(op, zero(Tx), zero(Ty)))
211+
R = typeof(f(zero(Tx), zero(Ty)))
212212
n = length(x)
213213
length(y) == n || throw(DimensionMismatch())
214214

@@ -239,7 +239,7 @@ function zero_preserve_map{Tx,Ty}(f, x::SparseVector{Tx}, y::SparseVector{Ty})
239239
ix += 1
240240
iy += 1
241241
elseif jx < jy
242-
v = f(xnzval[i], zero(Ty))
242+
v = f(xnzval[ix], zero(Ty))
243243
if v != zero(v)
244244
push!(rind, jx)
245245
push!(rval, v)
@@ -277,7 +277,7 @@ function zero_preserve_map{Tx,Ty}(f, x::SparseVector{Tx}, y::SparseVector{Ty})
277277
end
278278

279279
function map{Tx,Ty}(f, x::StridedVector{Tx}, y::SparseVector{Ty})
280-
R = typeof(_eval(op, zero(Tx), zero(Ty)))
280+
R = typeof(f(zero(Tx), zero(Ty)))
281281
n = length(x)
282282
length(y) == n || throw(DimensionMismatch())
283283

@@ -306,7 +306,7 @@ function map{Tx,Ty}(f, x::StridedVector{Tx}, y::SparseVector{Ty})
306306
end
307307

308308
function map{Tx,Ty}(f, x::SparseVector{Tx}, y::StridedVector{Ty})
309-
R = typeof(_eval(op, zero(Tx), zero(Ty)))
309+
R = typeof(f(zero(Tx), zero(Ty)))
310310
n = length(x)
311311
length(y) == n || throw(DimensionMismatch())
312312

test/sparse.jl

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

3+
include("sparsedir/sparsevec.jl")
34
include("sparsedir/sparse.jl")
45
if Base.USE_GPL_LIBS
56
include("sparsedir/umfpack.jl")

test/sparsedir/cholmod.jl

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

33
using Base.Test
44

5-
using Base.SparseMatrix.CHOLMOD
5+
using Base.Sparse.CHOLMOD
66

77
# based on deps/SuiteSparse-4.0.2/CHOLMOD/Demo/
88

@@ -179,10 +179,10 @@ end
179179

180180
# test Sparse constructor Symmetric and Hermitian input (and issym and ishermitian)
181181
ACSC = sprandn(10, 10, 0.3) + I
182-
@test issym(Sparse(Symmetric(ACSC, :L)))
183-
@test issym(Sparse(Symmetric(ACSC, :U)))
184-
@test ishermitian(Sparse(Hermitian(complex(ACSC), :L)))
185-
@test ishermitian(Sparse(Hermitian(complex(ACSC), :U)))
182+
@test issym(CHOLMOD.Sparse(Symmetric(ACSC, :L)))
183+
@test issym(CHOLMOD.Sparse(Symmetric(ACSC, :U)))
184+
@test ishermitian(CHOLMOD.Sparse(Hermitian(complex(ACSC), :L)))
185+
@test ishermitian(CHOLMOD.Sparse(Hermitian(complex(ACSC), :U)))
186186

187187
# test Sparse constructor for c_Sparse{Tv,Ti} input( and Sparse*Sparse)
188188
B = CHOLMOD.Sparse(SparseMatrixCSC{Float64,Int32}(sprandn(48, 48, 0.1))) # A has Int32 indices
@@ -324,8 +324,8 @@ for elty in (Float64, Complex{Float64})
324324
A1pdSparse = CHOLMOD.Sparse(
325325
A1pd.m,
326326
A1pd.n,
327-
Base.SparseMatrix.decrement(A1pd.colptr),
328-
Base.SparseMatrix.decrement(A1pd.rowval),
327+
Base.Sparse.decrement(A1pd.colptr),
328+
Base.Sparse.decrement(A1pd.rowval),
329329
A1pd.nzval)
330330

331331
## High level interface
@@ -393,14 +393,14 @@ for elty in (Float64, Complex{Float64})
393393
@test !isposdef(A1)
394394
@test !isposdef(A1 + A1' |> t -> t - 2eigmax(full(t))*I)
395395
if elty <: Real
396-
@test CHOLMOD.issym(Sparse(A1pd, 0))
396+
@test CHOLMOD.issym(CHOLMOD.Sparse(A1pd, 0))
397397
@test CHOLMOD.Sparse(cholfact(Symmetric(A1pd, :L))) == CHOLMOD.Sparse(cholfact(A1pd))
398398
@test CHOLMOD.Sparse(cholfact(Symmetric(A1pd, :L), shift=2)) == CHOLMOD.Sparse(cholfact(A1pd, shift=2))
399399
@test CHOLMOD.Sparse(ldltfact(Symmetric(A1pd, :L))) == CHOLMOD.Sparse(ldltfact(A1pd))
400400
@test CHOLMOD.Sparse(ldltfact(Symmetric(A1pd, :L), shift=2)) == CHOLMOD.Sparse(ldltfact(A1pd, shift=2))
401401
else
402-
@test !CHOLMOD.issym(Sparse(A1pd, 0))
403-
@test CHOLMOD.ishermitian(Sparse(A1pd, 0))
402+
@test !CHOLMOD.issym(CHOLMOD.Sparse(A1pd, 0))
403+
@test CHOLMOD.ishermitian(CHOLMOD.Sparse(A1pd, 0))
404404
@test CHOLMOD.Sparse(cholfact(Hermitian(A1pd, :L))) == CHOLMOD.Sparse(cholfact(A1pd))
405405
@test CHOLMOD.Sparse(cholfact(Hermitian(A1pd, :L), shift=2)) == CHOLMOD.Sparse(cholfact(A1pd, shift=2))
406406
@test CHOLMOD.Sparse(ldltfact(Hermitian(A1pd, :L))) == CHOLMOD.Sparse(ldltfact(A1pd))

test/sparsedir/sparse.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ for i = 1:5
177177
a = sprand(10, 5, 0.7)
178178
b = sprand(5, 15, 0.3)
179179
@test maximum(abs(a*b - full(a)*full(b))) < 100*eps()
180-
@test maximum(abs(Base.SparseMatrix.spmatmul(a,b,sortindices=:sortcols) - full(a)*full(b))) < 100*eps()
181-
@test maximum(abs(Base.SparseMatrix.spmatmul(a,b,sortindices=:doubletranspose) - full(a)*full(b))) < 100*eps()
180+
@test maximum(abs(Base.Sparse.spmatmul(a,b,sortindices=:sortcols) - full(a)*full(b))) < 100*eps()
181+
@test maximum(abs(Base.Sparse.spmatmul(a,b,sortindices=:doubletranspose) - full(a)*full(b))) < 100*eps()
182182
@test full(kron(a,b)) == kron(full(a), full(b))
183183
@test full(kron(full(a),b)) == kron(full(a), full(b))
184184
@test full(kron(a,full(b))) == kron(full(a), full(b))
@@ -624,9 +624,9 @@ function test_getindex_algs{Tv,Ti}(A::SparseMatrixCSC{Tv,Ti}, I::AbstractVector,
624624
((minj < 1) || (maxj > n)) && BoundsError()
625625
end
626626

627-
(alg == 0) ? Base.SparseMatrix.getindex_I_sorted_bsearch_A(A, I, J) :
628-
(alg == 1) ? Base.SparseMatrix.getindex_I_sorted_bsearch_I(A, I, J) :
629-
Base.SparseMatrix.getindex_I_sorted_linear(A, I, J)
627+
(alg == 0) ? Base.Sparse.getindex_I_sorted_bsearch_A(A, I, J) :
628+
(alg == 1) ? Base.Sparse.getindex_I_sorted_bsearch_I(A, I, J) :
629+
Base.Sparse.getindex_I_sorted_linear(A, I, J)
630630
end
631631

632632
let M=2^14, N=2^4

test/sparsedir/sparsevec.jl

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
# helpers
2+
3+
function exact_equal(x::SparseVector, y::SparseVector)
4+
x.n == y.n && x.nzind == y.nzind && x.nzval == y.nzval
5+
end
6+
7+
# empty sparse vectors
8+
9+
x0 = SparseVector(8)
10+
@test isa(x0, SparseVector{Float64,Int})
11+
@test length(x0) == 8
12+
@test nnz(x0) == 0
13+
14+
x0 = SparseVector(Float32, 8)
15+
@test isa(x0, SparseVector{Float32,Int})
16+
@test length(x0) == 8
17+
@test nnz(x0) == 0
18+
19+
x0 = SparseVector(Float32, Int32, 8)
20+
@test isa(x0, SparseVector{Float32, Int32})
21+
@test length(x0) == 8
22+
@test nnz(x0) == 0
23+
24+
25+
# construction
26+
27+
x = SparseVector(8, [2, 5, 6], [1.25, -0.75, 3.5])
28+
x2 = SparseVector(8, [1, 2, 6, 7], [3.25, 4.0, -5.5, -6.0])
29+
30+
@test eltype(x) == Float64
31+
@test ndims(x) == 1
32+
@test length(x) == 8
33+
@test size(x) == (8,)
34+
@test size(x,1) == 8
35+
@test size(x,2) == 1
36+
@test !isempty(x)
37+
38+
@test countnz(x) == 3
39+
@test nnz(x) == 3
40+
@test nonzeros(x) == [1.25, -0.75, 3.5]
41+
42+
dct = Dict{Int,Float64}()
43+
dct[2] = 1.25
44+
dct[5] = -0.75
45+
dct[6] = 3.5
46+
xc = SparseVector(8, dct)
47+
@test isa(xc, SparseVector{Float64,Int})
48+
@test exact_equal(x, xc)
49+
50+
# full
51+
52+
xf = zeros(8)
53+
xf[2] = 1.25
54+
xf[5] = -0.75
55+
xf[6] = 3.5
56+
@test isa(full(x), Vector{Float64})
57+
@test full(x) == xf
58+
59+
xf2 = zeros(8)
60+
xf2[1] = 3.25
61+
xf2[2] = 4.0
62+
xf2[6] = -5.5
63+
xf2[7] = -6.0
64+
@test isa(full(x2), Vector{Float64})
65+
@test full(x2) == xf2
66+
67+
68+
# conversion
69+
70+
xc = convert(SparseVector, xf)
71+
@test isa(xc, SparseVector{Float64,Int})
72+
@test exact_equal(x, xc)
73+
74+
xc = convert(SparseVector{Float32,Int}, x)
75+
@test isa(xc, SparseVector{Float32,Int})
76+
@test exact_equal(x, xc)
77+
78+
xc = convert(SparseVector{Float32}, x)
79+
@test isa(xc, SparseVector{Float32,Int})
80+
@test exact_equal(x, xc)
81+
82+
# copy
83+
84+
xc = copy(x)
85+
@test isa(xc, SparseVector{Float64,Int})
86+
@test !is(x.nzind, xc.nzval)
87+
@test !is(x.nzval, xc.nzval)
88+
@test exact_equal(x, xc)
89+
90+
# getindex
91+
92+
for i = 1:length(x)
93+
@test x[i] == xf[i]
94+
end
95+
96+
# setindex
97+
98+
xc = SparseVector(8)
99+
xc[3] = 2.0
100+
@test exact_equal(xc, SparseVector(8, [3], [2.0]))
101+
102+
xc = copy(x)
103+
xc[5] = 2.0
104+
@test exact_equal(xc, SparseVector(8, [2, 5, 6], [1.25, 2.0, 3.5]))
105+
106+
xc = copy(x)
107+
xc[3] = 4.0
108+
@test exact_equal(xc, SparseVector(8, [2, 3, 5, 6], [1.25, 4.0, -0.75, 3.5]))
109+
110+
xc[1] = 6.0
111+
@test exact_equal(xc, SparseVector(8, [1, 2, 3, 5, 6], [6.0, 1.25, 4.0, -0.75, 3.5]))
112+
113+
xc[8] = -1.5
114+
@test exact_equal(xc, SparseVector(8, [1, 2, 3, 5, 6, 8], [6.0, 1.25, 4.0, -0.75, 3.5, -1.5]))
115+
116+
xc = copy(x)
117+
xc[5] = 0.0
118+
@test exact_equal(xc, SparseVector(8, [2, 6], [1.25, 3.5]))
119+
120+
xc[6] = 0.0
121+
@test exact_equal(xc, SparseVector(8, [2], [1.25]))
122+
123+
xc[2] = 0.0
124+
@test exact_equal(xc, SparseVector(8, Int[], Float64[]))
125+
126+
127+
# sprand
128+
129+
xr = sprand(1000, 0.3)
130+
@test isa(xr, SparseVector{Float64,Int})
131+
@test length(xr) == 1000
132+
@test all(nonzeros(xr) .>= 0.0)
133+
134+
xr = sprand(1000, 0.3, Float32)
135+
@test isa(xr, SparseVector{Float32,Int})
136+
@test length(xr) == 1000
137+
@test all(nonzeros(xr) .>= 0.0)
138+
139+
xr = sprandn(1000, 0.3)
140+
@test isa(xr, SparseVector{Float64,Int})
141+
@test length(xr) == 1000
142+
@test any(nonzeros(xr) .> 0.0) && any(nonzeros(xr) .< 0.0)
143+
144+
# abs and abs2
145+
146+
@test exact_equal(abs(x), SparseVector(8, [2, 5, 6], abs([1.25, -0.75, 3.5])))
147+
@test exact_equal(abs2(x), SparseVector(8, [2, 5, 6], abs2([1.25, -0.75, 3.5])))
148+
149+
# plus and minus
150+
151+
xa = SparseVector(8, [1,2,5,6,7], [3.25,5.25,-0.75,-2.0,-6.0])
152+
153+
@test exact_equal(x + x, SparseVector(8, [2,5,6], [2.5,-1.5,7.0]))
154+
@test exact_equal(x + x2, xa)
155+
156+
xb = SparseVector(8, [1,2,5,6,7], [-3.25,-2.75,-0.75,9.0,6.0])
157+
158+
@test exact_equal(x - x, SparseVector(8, Int[], Float64[]))
159+
@test exact_equal(x - x2, xb)
160+
161+
@test full(x) + x2 == full(xa)
162+
@test full(x) - x2 == full(xb)
163+
@test x + full(x2) == full(xa)
164+
@test x - full(x2) == full(xb)
165+
166+
167+
# reduction
168+
169+
@test sum(x) == 4.0
170+
@test sumabs(x) == 5.5
171+
@test sumabs2(x) == 14.375
172+
173+
@test vecnorm(x) == sqrt(14.375)
174+
@test vecnorm(x, 1) == 5.5
175+
@test vecnorm(x, 2) == sqrt(14.375)
176+
@test vecnorm(x, Inf) == 3.5

test/sparsedir/spqr.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
using Base.Test
44

5-
using Base.SparseMatrix.SPQR
6-
using Base.SparseMatrix.CHOLMOD
5+
using Base.Sparse.SPQR
6+
using Base.Sparse.CHOLMOD
77

88
m, n = 100, 10
99
nn = 100

test/sparsedir/umfpack.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ do33 = ones(3)
66

77
# based on deps/Suitesparse-4.0.2/UMFPACK/Demo/umfpack_di_demo.c
88

9-
using Base.SparseMatrix.UMFPACK.increment!
9+
using Base.Sparse.UMFPACK.increment!
1010

1111
A0 = sparse(increment!([0,4,1,1,2,2,0,1,2,3,4,4]),
1212
increment!([0,4,0,2,1,2,1,4,3,2,1,2]),
1313
[2.,1.,3.,4.,-1.,-3.,3.,6.,2.,1.,4.,2.], 5, 5)
1414

1515
for Tv in (Float64, Complex128)
16-
for Ti in Base.SparseMatrix.UMFPACK.UMFITypes.types
16+
for Ti in Base.Sparse.UMFPACK.UMFITypes.types
1717
A = convert(SparseMatrixCSC{Tv,Ti}, A0)
1818
lua = lufact(A)
1919
@test nnz(lua) == 18
@@ -37,7 +37,7 @@ for Tv in (Float64, Complex128)
3737
end
3838

3939
Ac0 = complex(A0,A0)
40-
for Ti in Base.SparseMatrix.UMFPACK.UMFITypes.types
40+
for Ti in Base.Sparse.UMFPACK.UMFITypes.types
4141
Ac = convert(SparseMatrixCSC{Complex128,Ti}, Ac0)
4242
lua = lufact(Ac)
4343
L,U,p,q,Rs = lua[:(:)]

0 commit comments

Comments
 (0)