Skip to content

Commit

Permalink
add a test suite to SparseVector
Browse files Browse the repository at this point in the history
  • Loading branch information
lindahua authored and tkelman committed Jul 7, 2015
1 parent 81b7961 commit 73cfb40
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 25 deletions.
2 changes: 1 addition & 1 deletion base/sparse/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ function spmatmul{Tv,Ti}(A::SparseMatrixCSC{Tv,Ti}, B::SparseMatrixCSC{Tv,Ti};

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

Expand Down
8 changes: 4 additions & 4 deletions base/sparse/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ Base.abs2(x::SparseVector) = SparseVector(x.n, copy(x.nzind), abs2(x.nzval))

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

Expand Down Expand Up @@ -239,7 +239,7 @@ function zero_preserve_map{Tx,Ty}(f, x::SparseVector{Tx}, y::SparseVector{Ty})
ix += 1
iy += 1
elseif jx < jy
v = f(xnzval[i], zero(Ty))
v = f(xnzval[ix], zero(Ty))
if v != zero(v)
push!(rind, jx)
push!(rval, v)
Expand Down Expand Up @@ -277,7 +277,7 @@ function zero_preserve_map{Tx,Ty}(f, x::SparseVector{Tx}, y::SparseVector{Ty})
end

function map{Tx,Ty}(f, x::StridedVector{Tx}, y::SparseVector{Ty})
R = typeof(_eval(op, zero(Tx), zero(Ty)))
R = typeof(f(zero(Tx), zero(Ty)))
n = length(x)
length(y) == n || throw(DimensionMismatch())

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

function map{Tx,Ty}(f, x::SparseVector{Tx}, y::StridedVector{Ty})
R = typeof(_eval(op, zero(Tx), zero(Ty)))
R = typeof(f(zero(Tx), zero(Ty)))
n = length(x)
length(y) == n || throw(DimensionMismatch())

Expand Down
1 change: 1 addition & 0 deletions test/sparse.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# This file is a part of Julia. License is MIT: http://julialang.org/license

include("sparsedir/sparsevec.jl")
include("sparsedir/sparse.jl")
if Base.USE_GPL_LIBS
include("sparsedir/umfpack.jl")
Expand Down
20 changes: 10 additions & 10 deletions test/sparsedir/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
srand(123)
using Base.Test

using Base.SparseMatrix.CHOLMOD
using Base.Sparse.CHOLMOD

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

Expand Down Expand Up @@ -185,10 +185,10 @@ end

# test Sparse constructor Symmetric and Hermitian input (and issym and ishermitian)
ACSC = sprandn(10, 10, 0.3) + I
@test issym(Sparse(Symmetric(ACSC, :L)))
@test issym(Sparse(Symmetric(ACSC, :U)))
@test ishermitian(Sparse(Hermitian(complex(ACSC), :L)))
@test ishermitian(Sparse(Hermitian(complex(ACSC), :U)))
@test issym(CHOLMOD.Sparse(Symmetric(ACSC, :L)))
@test issym(CHOLMOD.Sparse(Symmetric(ACSC, :U)))
@test ishermitian(CHOLMOD.Sparse(Hermitian(complex(ACSC), :L)))
@test ishermitian(CHOLMOD.Sparse(Hermitian(complex(ACSC), :U)))

# test Sparse constructor for c_SparseVoid (and read_sparse)
let testfile = joinpath(tempdir(), "tmp.mtx")
Expand Down Expand Up @@ -318,8 +318,8 @@ for elty in (Float64, Complex{Float64})
A1pdSparse = CHOLMOD.Sparse(
A1pd.m,
A1pd.n,
Base.SparseMatrix.decrement(A1pd.colptr),
Base.SparseMatrix.decrement(A1pd.rowval),
Base.Sparse.decrement(A1pd.colptr),
Base.Sparse.decrement(A1pd.rowval),
A1pd.nzval)

## High level interface
Expand Down Expand Up @@ -388,7 +388,7 @@ for elty in (Float64, Complex{Float64})
@test !isposdef(A1 + A1' |> t -> t - 2eigmax(full(t))*I)

if elty <: Real
@test CHOLMOD.issym(Sparse(A1pd, 0))
@test CHOLMOD.issym(CHOLMOD.Sparse(A1pd, 0))
@test CHOLMOD.Sparse(cholfact(Symmetric(A1pd, :L))) == CHOLMOD.Sparse(cholfact(A1pd))
F1 = CHOLMOD.Sparse(cholfact(Symmetric(A1pd, :L), shift=2))
F2 = CHOLMOD.Sparse(cholfact(A1pd, shift=2))
Expand All @@ -398,8 +398,8 @@ for elty in (Float64, Complex{Float64})
F2 = CHOLMOD.Sparse(ldltfact(A1pd, shift=2))
@test F1 == F2
else
@test !CHOLMOD.issym(Sparse(A1pd, 0))
@test CHOLMOD.ishermitian(Sparse(A1pd, 0))
@test !CHOLMOD.issym(CHOLMOD.Sparse(A1pd, 0))
@test CHOLMOD.ishermitian(CHOLMOD.Sparse(A1pd, 0))
@test CHOLMOD.Sparse(cholfact(Hermitian(A1pd, :L))) == CHOLMOD.Sparse(cholfact(A1pd))
F1 = CHOLMOD.Sparse(cholfact(Hermitian(A1pd, :L), shift=2))
F2 = CHOLMOD.Sparse(cholfact(A1pd, shift=2))
Expand Down
10 changes: 5 additions & 5 deletions test/sparsedir/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ for i = 1:5
a = sprand(10, 5, 0.7)
b = sprand(5, 15, 0.3)
@test maximum(abs(a*b - full(a)*full(b))) < 100*eps()
@test maximum(abs(Base.SparseMatrix.spmatmul(a,b,sortindices=:sortcols) - full(a)*full(b))) < 100*eps()
@test maximum(abs(Base.SparseMatrix.spmatmul(a,b,sortindices=:doubletranspose) - full(a)*full(b))) < 100*eps()
@test maximum(abs(Base.Sparse.spmatmul(a,b,sortindices=:sortcols) - full(a)*full(b))) < 100*eps()
@test maximum(abs(Base.Sparse.spmatmul(a,b,sortindices=:doubletranspose) - full(a)*full(b))) < 100*eps()
@test full(kron(a,b)) == kron(full(a), full(b))
@test full(kron(full(a),b)) == kron(full(a), full(b))
@test full(kron(a,full(b))) == kron(full(a), full(b))
Expand Down Expand Up @@ -635,9 +635,9 @@ function test_getindex_algs{Tv,Ti}(A::SparseMatrixCSC{Tv,Ti}, I::AbstractVector,
((minj < 1) || (maxj > n)) && BoundsError()
end

(alg == 0) ? Base.SparseMatrix.getindex_I_sorted_bsearch_A(A, I, J) :
(alg == 1) ? Base.SparseMatrix.getindex_I_sorted_bsearch_I(A, I, J) :
Base.SparseMatrix.getindex_I_sorted_linear(A, I, J)
(alg == 0) ? Base.Sparse.getindex_I_sorted_bsearch_A(A, I, J) :
(alg == 1) ? Base.Sparse.getindex_I_sorted_bsearch_I(A, I, J) :
Base.Sparse.getindex_I_sorted_linear(A, I, J)
end

let M=2^14, N=2^4
Expand Down
176 changes: 176 additions & 0 deletions test/sparsedir/sparsevec.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# helpers

function exact_equal(x::SparseVector, y::SparseVector)
x.n == y.n && x.nzind == y.nzind && x.nzval == y.nzval
end

# empty sparse vectors

x0 = SparseVector(8)
@test isa(x0, SparseVector{Float64,Int})
@test length(x0) == 8
@test nnz(x0) == 0

x0 = SparseVector(Float32, 8)
@test isa(x0, SparseVector{Float32,Int})
@test length(x0) == 8
@test nnz(x0) == 0

x0 = SparseVector(Float32, Int32, 8)
@test isa(x0, SparseVector{Float32, Int32})
@test length(x0) == 8
@test nnz(x0) == 0


# construction

x = SparseVector(8, [2, 5, 6], [1.25, -0.75, 3.5])
x2 = SparseVector(8, [1, 2, 6, 7], [3.25, 4.0, -5.5, -6.0])

@test eltype(x) == Float64
@test ndims(x) == 1
@test length(x) == 8
@test size(x) == (8,)
@test size(x,1) == 8
@test size(x,2) == 1
@test !isempty(x)

@test countnz(x) == 3
@test nnz(x) == 3
@test nonzeros(x) == [1.25, -0.75, 3.5]

dct = Dict{Int,Float64}()
dct[2] = 1.25
dct[5] = -0.75
dct[6] = 3.5
xc = SparseVector(8, dct)
@test isa(xc, SparseVector{Float64,Int})
@test exact_equal(x, xc)

# full

xf = zeros(8)
xf[2] = 1.25
xf[5] = -0.75
xf[6] = 3.5
@test isa(full(x), Vector{Float64})
@test full(x) == xf

xf2 = zeros(8)
xf2[1] = 3.25
xf2[2] = 4.0
xf2[6] = -5.5
xf2[7] = -6.0
@test isa(full(x2), Vector{Float64})
@test full(x2) == xf2


# conversion

xc = convert(SparseVector, xf)
@test isa(xc, SparseVector{Float64,Int})
@test exact_equal(x, xc)

xc = convert(SparseVector{Float32,Int}, x)
@test isa(xc, SparseVector{Float32,Int})
@test exact_equal(x, xc)

xc = convert(SparseVector{Float32}, x)
@test isa(xc, SparseVector{Float32,Int})
@test exact_equal(x, xc)

# copy

xc = copy(x)
@test isa(xc, SparseVector{Float64,Int})
@test !is(x.nzind, xc.nzval)
@test !is(x.nzval, xc.nzval)
@test exact_equal(x, xc)

# getindex

for i = 1:length(x)
@test x[i] == xf[i]
end

# setindex

xc = SparseVector(8)
xc[3] = 2.0
@test exact_equal(xc, SparseVector(8, [3], [2.0]))

xc = copy(x)
xc[5] = 2.0
@test exact_equal(xc, SparseVector(8, [2, 5, 6], [1.25, 2.0, 3.5]))

xc = copy(x)
xc[3] = 4.0
@test exact_equal(xc, SparseVector(8, [2, 3, 5, 6], [1.25, 4.0, -0.75, 3.5]))

xc[1] = 6.0
@test exact_equal(xc, SparseVector(8, [1, 2, 3, 5, 6], [6.0, 1.25, 4.0, -0.75, 3.5]))

xc[8] = -1.5
@test exact_equal(xc, SparseVector(8, [1, 2, 3, 5, 6, 8], [6.0, 1.25, 4.0, -0.75, 3.5, -1.5]))

xc = copy(x)
xc[5] = 0.0
@test exact_equal(xc, SparseVector(8, [2, 6], [1.25, 3.5]))

xc[6] = 0.0
@test exact_equal(xc, SparseVector(8, [2], [1.25]))

xc[2] = 0.0
@test exact_equal(xc, SparseVector(8, Int[], Float64[]))


# sprand

xr = sprand(1000, 0.3)
@test isa(xr, SparseVector{Float64,Int})
@test length(xr) == 1000
@test all(nonzeros(xr) .>= 0.0)

xr = sprand(1000, 0.3, Float32)
@test isa(xr, SparseVector{Float32,Int})
@test length(xr) == 1000
@test all(nonzeros(xr) .>= 0.0)

xr = sprandn(1000, 0.3)
@test isa(xr, SparseVector{Float64,Int})
@test length(xr) == 1000
@test any(nonzeros(xr) .> 0.0) && any(nonzeros(xr) .< 0.0)

# abs and abs2

@test exact_equal(abs(x), SparseVector(8, [2, 5, 6], abs([1.25, -0.75, 3.5])))
@test exact_equal(abs2(x), SparseVector(8, [2, 5, 6], abs2([1.25, -0.75, 3.5])))

# plus and minus

xa = SparseVector(8, [1,2,5,6,7], [3.25,5.25,-0.75,-2.0,-6.0])

@test exact_equal(x + x, SparseVector(8, [2,5,6], [2.5,-1.5,7.0]))
@test exact_equal(x + x2, xa)

xb = SparseVector(8, [1,2,5,6,7], [-3.25,-2.75,-0.75,9.0,6.0])

@test exact_equal(x - x, SparseVector(8, Int[], Float64[]))
@test exact_equal(x - x2, xb)

@test full(x) + x2 == full(xa)
@test full(x) - x2 == full(xb)
@test x + full(x2) == full(xa)
@test x - full(x2) == full(xb)


# reduction

@test sum(x) == 4.0
@test sumabs(x) == 5.5
@test sumabs2(x) == 14.375

@test vecnorm(x) == sqrt(14.375)
@test vecnorm(x, 1) == 5.5
@test vecnorm(x, 2) == sqrt(14.375)
@test vecnorm(x, Inf) == 3.5
4 changes: 2 additions & 2 deletions test/sparsedir/spqr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

using Base.Test

using Base.SparseMatrix.SPQR
using Base.SparseMatrix.CHOLMOD
using Base.Sparse.SPQR
using Base.Sparse.CHOLMOD

m, n = 100, 10
nn = 100
Expand Down
6 changes: 3 additions & 3 deletions test/sparsedir/umfpack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ do33 = ones(3)

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

using Base.SparseMatrix.UMFPACK.increment!
using Base.Sparse.UMFPACK.increment!

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

for Tv in (Float64, Complex128)
for Ti in Base.SparseMatrix.UMFPACK.UMFITypes.types
for Ti in Base.Sparse.UMFPACK.UMFITypes.types
A = convert(SparseMatrixCSC{Tv,Ti}, A0)
lua = lufact(A)
@test nnz(lua) == 18
Expand All @@ -37,7 +37,7 @@ for Tv in (Float64, Complex128)
end

Ac0 = complex(A0,A0)
for Ti in Base.SparseMatrix.UMFPACK.UMFITypes.types
for Ti in Base.Sparse.UMFPACK.UMFITypes.types
Ac = convert(SparseMatrixCSC{Complex128,Ti}, Ac0)
lua = lufact(Ac)
L,U,p,q,Rs = lua[:(:)]
Expand Down

0 comments on commit 73cfb40

Please sign in to comment.