-
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
Implement sum #446
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #446 +/- ##
==========================================
+ Coverage 89.61% 89.75% +0.14%
==========================================
Files 25 25
Lines 3571 3622 +51
==========================================
+ Hits 3200 3251 +51
Misses 371 371 ☔ View full report in Codecov by Sentry. |
Can you make sure your unit tests are being run? I think just add |
added changes |
The test failure is very surprising. It's possible its calling We should make sure we are consistent with the order. Can you add some tests that |
I noticed earlier with == that there is some floating point error and so the test fails. |
I thought it was to be expected but this could be the problem |
Right, this floating point error is because you are computing the sums in a different order. This is unnecessary so we can change the implementation to make sure we do things in the right order. Eg: julia> A = randn(5,5)
5×5 Matrix{Float64}:
-0.574303 -0.909723 0.589035 0.125461 -0.85839
2.36645 -2.01842 0.305596 0.739664 0.281112
-0.449434 1.65078 0.293241 -0.12409 0.535829
0.388728 1.3232 -1.61161 -0.54598 -0.237829
-0.570773 -0.0989053 -0.515742 0.116799 -2.14109
julia> sum(A) == sum(vec(A)) # sum should traverse column-by-coumn
true
julia> sum(A) ≠ sum(vec(A')) # it doesn't match row-by-row
true |
It is expected when the order of the operations change. But all things being equal we should avoid it. Also, traversing column-by-column will be much faster than row-by-row since it accesses memory in order. |
I've changed the traversal order but I still get floating point error on the tests without dims and dims = 1. It's a different type, but I've also noticed that sum(vec(A)) == sum(Matrix(A)) for BandedMatrix A returns false |
Can you push your changes? Note I made a suggestion that fixes the order for the no-dims case |
Wouldn't a better check for this probably be that This is a similar problem in e.g. SparseArrays where |
Can you explain the difference? I take it foldl forces a specific order. Do you know why sum might choose a different order? |
As far as I can tell, the difference is that Another way to test would be to check |
I think in this case just use ≈ since we don't care that much about preserving order |
src/generic/AbstractBandedMatrix.jl
Outdated
|
||
function sum!(ret::AbstractArray, A::AbstractBandedMatrix) | ||
#Behaves similarly to Base.sum! | ||
ret .= 0 |
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.
ret .= 0 | |
fill!(ret, zero(eltype(ret))) |
if s[1] == 1 && (l == 1 || s[2]==1) | ||
for j = 1:m, i = colrange(A, j) | ||
ret .+= A[i, j] | ||
end |
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
src/generic/AbstractBandedMatrix.jl
Outdated
ret[1, j] += A[i, j] | ||
end | ||
elseif s[1] == n && s[2] == m | ||
ret = A |
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.
This is not changing ret
!
ret = A | |
copyto!(ret, A) |
elseif s[1] == n && s[2] == m | ||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Add test using @test_throws
seems to be the same issue as before with the floating point error |
That error is unrelated I believe, we have seen it other places. |
I struggled to find an elegant solution involving selecting relevant elements from B.data in the case where B is created by brand() with certain parameters (e.g very non square matrices, more bands than those that fit in the matrix). I decided to go with this solution that involves populating a data matrix only using relevant information accessed by B[band(i)]. Please let me know any improvements.