Skip to content
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

Speed up mapslices #40996

Merged
merged 18 commits into from
May 31, 2022
Prev Previous commit
Next Next commit
explicit error for dims > ndims
  • Loading branch information
mcabbott committed Apr 28, 2022
commit 07d7eb0ca3df5b8e0a22e63e471f886d6930998b
7 changes: 7 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2838,6 +2838,13 @@ true
function mapslices(f, A::AbstractArray; dims)
isempty(dims) && return map(f, A)

for d in dims
# Indexing a matrix M[:,1,:] produces a matrix, but dims=(1,3) here would
# otherwise ignore 3. Previously this gave error:
# BoundsError: attempt to access 2-element Vector{Any} at index [3]
d > ndims(A) && throw(ArgumentError("mapslices does not accept dims > ndims(A)"))
end

# Apply the function to the first slice in order to determine the next steps
idx1 = ntuple(d -> d in dims ? (:) : firstindex(A,d), ndims(A))
Aslice = A[idx1...]
Expand Down
6 changes: 6 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,12 @@ end
m = mapslices(x->tuple(x), [1 2; 3 4], dims=1)
@test m[1,1] == ([1,3],)
@test m[1,2] == ([2,4],)

# issue #21123
@test mapslices(nnz, sparse(1.0I, 3, 3), dims=1) == [1 1 1]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests like this must move to https://github.com/JuliaSparse/SparseArrays.jl now, right? So I will remove them here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that I don't lose them, they are:

    # issue #21123
    @test mapslices(nnz, sparse(1.0I, 3, 3), dims=1) == [1 1 1]

    r = rand(Int8, 4,5,2)
    @test vec(mapslices(repr, r, dims=(2,1))) == map(repr, eachslice(r, dims=3))
    @test mapslices(prod, sparse(r[:,:,1]), dims=1) == prod(r[:,:,1], dims=1)
    @test mapslices(cumsum, sparse(r[:,:,1]), dims=1) == cumsum(r[:,:,1], dims=1)


# re-write, #40996
@test_throws ArgumentError mapslices(identity, rand(2,3), dims=(1,3)) # previously BoundsError
end

@testset "single multidimensional index" begin
Expand Down