Skip to content

Commit

Permalink
test special cases of sum!
Browse files Browse the repository at this point in the history
  • Loading branch information
max-vassili3v committed Jul 16, 2024
1 parent dc9a82d commit 393bf62
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/generic/AbstractBandedMatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ function sum!(ret::AbstractArray, A::AbstractBandedMatrix)
else
throw(DimensionMismatch("reduction on matrix of size ($n, $m) with output size $s"))
end
#return the value to mimic Base.sum!
ret
end

function sum(A::AbstractBandedMatrix; dims=:)
Expand Down
10 changes: 10 additions & 0 deletions test/test_sum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module TestSum

using Test, BandedMatrices, Random

Random.seed!(0)
r = brand(Float64,rand(1:10_000),rand(1:10_000),rand(-20:100),rand(-20:100))
empty_r = brand(Float64,rand(1:1_000),rand(1:1_000),rand(1:100),rand(-200:-101))
n,m = size(empty_r)
Expand All @@ -10,11 +11,20 @@ matr = Matrix(r)
@test sum(empty_r) == 0
@test sum(empty_r; dims = 2) == zeros(n,1)
@test sum(empty_r; dims = 1) == zeros(1,m)

@test sum(r) sum(matr) rtol = 1e-10
@test sum(r; dims=2) sum(matr; dims=2) rtol = 1e-10
@test sum(r; dims=1) sum(matr; dims=1) rtol = 1e-10
@test sum(r; dims=3) == r
@test_throws ArgumentError sum(r; dims=0)

v = [1.0]
sum!(v, r)
@test v == sum!(v, Matrix(r))
n2, m2 = size(r)
v = ones(Float64, n2)
@test sum!(v, r) == sum!(v, Matrix(r))
@test_throws DimensionMismatch sum!(zeros(Float64, n2 + 1, m2 + 1), r)
end

end

0 comments on commit 393bf62

Please sign in to comment.