-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement sum #446
Merged
dlfivefifty
merged 14 commits into
JuliaLinearAlgebra:master
from
max-vassili3v:implement-sum
Jul 16, 2024
Merged
Implement sum #446
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
87c311f
add sum without dims
max-vassili3v b6530b9
add sum; dims=1
max-vassili3v bec277e
support for dims = 2 and error handling
max-vassili3v cd92d7f
fix for empty matrices and added unit tests
max-vassili3v 0768d34
style
max-vassili3v cb197f5
make improvements
max-vassili3v d0109be
add test_sum.jl to runtests.jl
max-vassili3v 8da60df
fix method dispatch issue in a way that mimics Base.sum
max-vassili3v de15c53
update unit tests, reduce memory allocation, improve style
max-vassili3v 0e0bc12
update unit tests, add sum!, move to AbstractBandedMatrix.jl
max-vassili3v cfc895e
revert tests to \approx
max-vassili3v dc9a82d
make improvements in AbstractBandedMatrix.jl
max-vassili3v 393bf62
test special cases of sum!
max-vassili3v 5ec4b3c
add some tests and avoid CI failure
dlfivefifty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -336,3 +336,68 @@ | |
copy(A::Adjoint{T,<:AbstractBandedMatrix}) where T = copy(parent(A))' | ||
copy(A::Transpose{T,<:AbstractBandedMatrix}) where T = transpose(copy(parent(A))) | ||
end | ||
|
||
function sum!(ret::AbstractArray, A::AbstractBandedMatrix) | ||
#Behaves similarly to Base.sum! | ||
fill!(ret, zero(eltype(ret))) | ||
n,m = size(A) | ||
s = size(ret) | ||
l = length(s) | ||
#Check for singleton dimension and perform respective sum | ||
if s[1] == 1 && (l == 1 || s[2]==1) | ||
for j = 1:m, i = colrange(A, j) | ||
ret .+= A[i, j] | ||
end | ||
elseif s[1] == n && (l == 1 || s[2]==1) | ||
for i = 1:n, j = rowrange(A, i) | ||
ret[i, 1] += A[i, j] | ||
end | ||
elseif s[1] == 1 && s[2] == m | ||
for j = 1:m, i = colrange(A, j) | ||
ret[1, j] += A[i, j] | ||
end | ||
elseif s[1] == n && s[2] == m | ||
copyto!(ret,A) | ||
else | ||
throw(DimensionMismatch("reduction on matrix of size ($n, $m) with output size $s")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add test using |
||
end | ||
#return the value to mimic Base.sum! | ||
ret | ||
end | ||
|
||
function sum(A::AbstractBandedMatrix; dims=:) | ||
if dims isa Colon | ||
l, u = bandwidths(A) | ||
ret = zero(eltype(A)) | ||
if l + u < 0 | ||
return ret | ||
end | ||
n, m = size(A) | ||
for j = 1:m, i = colrange(A, j) | ||
ret += A[i, j] | ||
end | ||
ret | ||
elseif dims > 2 | ||
A | ||
elseif dims == 2 | ||
l, u = bandwidths(A) | ||
n, m = size(A) | ||
ret = zeros(eltype(A), n, 1) | ||
if l + u < 0 | ||
return ret | ||
end | ||
sum!(ret, A) | ||
ret | ||
elseif dims == 1 | ||
l, u = bandwidths(A) | ||
n, m = size(A) | ||
ret = zeros(eltype(A), 1, m) | ||
if l + u < 0 | ||
return ret | ||
end | ||
sum!(ret, A) | ||
ret | ||
else | ||
throw(ArgumentError("dimension must be ≥ 1, got $dims")) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module TestSum | ||
|
||
using Test, BandedMatrices, Random | ||
|
||
Random.seed!(0) | ||
r = brand(rand(1:10_000),rand(1:10_000),rand(-20:100),rand(-20:100)) | ||
empty_r = brand(rand(1:1_000),rand(1:1_000),rand(1:100),rand(-200:-101)) | ||
n,m = size(empty_r) | ||
matr = Matrix(r) | ||
@testset "sum" begin | ||
@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(n2) | ||
@test sum!(v, r) == sum!(v, Matrix(r)) | ||
V = zeros(1,m2) | ||
@test sum!(V, r) === V ≈ sum!(zeros(1,m2), Matrix(r)) | ||
V = zeros(n2,m2) | ||
@test sum!(V, r) === V == r | ||
@test_throws DimensionMismatch sum!(zeros(Float64, n2 + 1, m2 + 1), r) | ||
end | ||
|
||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add tests fro this special case