Skip to content

Commit

Permalink
support addition & subtraction
Browse files Browse the repository at this point in the history
é
  • Loading branch information
guimarqu committed Sep 3, 2022
1 parent 66117f4 commit a1779d1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/vector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,22 @@ end

SparseArrays.nnz(v::DynamicSparseVector) = nnz(v.pma)
Base.copy(::DynamicSparseVector) = error("copy of a dynamic sparse vector not implemented.")

# to use linear algebra:
function SparseArrays.nonzeroinds(v::DynamicSparseVector{K,T}) where {K,T}
return reduce(v.pma.array; init = K[]) do collection, e
if !isnothing(e)
push!(collection, first(e))
end
return collection
end
end

function SparseArrays.nonzeros(v::DynamicSparseVector{K,T}) where {K,T}
return reduce(v.pma.array; init = T[]) do collection, e
if !isnothing(e)
push!(collection, last(e))
end
return collection
end
end
4 changes: 3 additions & 1 deletion test/functional/functionaltests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ function functional_tests()
dynsparsematrix_fill_mode()
end

@testset "spMv" begin
@testset "lin alg" begin
spMv1()
addition()
subtraction()
end
return
end
42 changes: 42 additions & 0 deletions test/functional/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,45 @@ function spMv1()
@test i == j == d
end

function addition()
vec_rows1 = rand(rng, 1:100, 25)
vec_values1 = rand(rng, 1:10, 25)

dyn_vec1 = dynamicsparsevec(vec_rows1, vec_values1, 100)
vec1 = sparsevec(vec_rows1, vec_values1, 100)

vec_rows2 = rand(rng, 1:100, 25)
vec_values2 = rand(rng, 1:10, 25)

dyn_vec2 = dynamicsparsevec(vec_rows2, vec_values2, 100)
vec2 = sparsevec(vec_rows2, vec_values2, 100)

classic_sum = vec1 + vec2

@test dyn_vec1 + dyn_vec2 == classic_sum
@test vec1 + dyn_vec2 == classic_sum
@test dyn_vec1 + vec2 == classic_sum
end

function subtraction()
vec_rows1 = rand(rng, 1:100, 25)
vec_values1 = rand(rng, 1:10, 25)

dyn_vec1 = dynamicsparsevec(vec_rows1, vec_values1, 100)
vec1 = sparsevec(vec_rows1, vec_values1, 100)

vec_rows2 = rand(rng, 1:100, 25)
vec_values2 = rand(rng, 1:10, 25)

dyn_vec2 = dynamicsparsevec(vec_rows2, vec_values2, 100)
vec2 = sparsevec(vec_rows2, vec_values2, 100)

classic_sub = vec1 - vec2

@test dyn_vec1 - dyn_vec2 == classic_sub
@test vec1 - dyn_vec2 == classic_sub
@test dyn_vec1 - vec2 == classic_sub
@test -dyn_vec1 == -vec1
@test -dyn_vec2 == -vec2
end

0 comments on commit a1779d1

Please sign in to comment.