Skip to content

Commit

Permalink
100 % sparse
Browse files Browse the repository at this point in the history
  • Loading branch information
ahojukka5 committed Nov 17, 2017
1 parent 244621e commit f4be770
Showing 1 changed file with 54 additions and 2 deletions.
56 changes: 54 additions & 2 deletions test/test_sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
# License is MIT: see https://github.com/JuliaFEM/FEMBase.jl/blob/master/LICENSE

using FEMBase
using FEMBase: SparseMatrixCOO, SparseVectorCOO, optimize!,
resize_sparse, resize_sparsevec
using FEMBase: SparseMatrixCOO, SparseVectorCOO, optimize!
using FEMBase: resize_sparse, resize_sparsevec
using FEMBase: get_nonzero_rows, get_nonzero_columns
using Base.Test

@testset "Add to SparseMatrixCOO" begin
Expand Down Expand Up @@ -47,3 +48,54 @@ end
@test size(b) == (4, )
end

@testset "type conversions" begin
A = reshape(collect(1:9), 3, 3)
A2 = convert(SparseMatrixCOO, A)
A3 = convert(SparseMatrixCOO, sparse(A))
@test isapprox(A, full(A2))
@test isapprox(A, full(A3))
b = sparse(collect(1:3))
b2 = convert(SparseVectorCOO, b)
@test isapprox(b, full(b2))
end

@testset "construct sparse matrix and empty it" begin
A = SparseMatrixCOO()
push!(A, 1, 1, 1.0)
A_expected = sparse([1], [1], [1.0], 2, 2)
A2 = sparse(A, 2, 2)
@test isapprox(A2, A_expected)
empty!(A)
@test length(A.I) == length(A.J) == length(A.V) == 0
end

@testset "construct sparse matrix with special function" begin
A = SparseMatrixCOO()
push!(A, 1, 1, 1.0)
push!(A, 1, 1, 2.0)
B = sparse(A, 2, 2, +)
@test isapprox(B[1,1], 3.0)
end

@testset "normal addition to sparse vector" begin
A = SparseMatrixCOO()
add!(A, [1, 2], [1.0 2.0])
end

@testset "get nonzero rows, columns, size" begin
A = SparseMatrixCOO()
add!(A, [1, 3], [1, 4], [1.0 2.0; 3.0 4.0])
B = sparse(A)
@test get_nonzero_rows(B) == [1, 3]
@test get_nonzero_columns(B) == [1, 4]
@test size(B) == (3, 4)
@test size(B, 1) == 3
end

@testset "isapprox" begin
A = SparseMatrixCOO()
B = SparseMatrixCOO()
add!(A, [1, 3], [1, 4], [1.0 2.0; 3.0 4.0])
add!(B, [1, 3], [1, 4], [1.0 2.0; 3.0 4.0])
@test isapprox(A, B)
end

0 comments on commit f4be770

Please sign in to comment.