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

Add selectdim #522

Merged
merged 1 commit into from
Mar 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ Currently, the `@compat` macro supports the following syntaxes:
`Compat.median`, `Compat.minimum`, `Compat.prod`, `Compat.reduce`, `Compat.sort`,
`Compat.std`, `Compat.sum`, `Compat.var`, and `Compat.varm` with `dims` keyword argument ([#25989],[#26369]).

* `selectdim` to obtain a view of an array with a specified index for a specified dimension ([#26009]).

## Renaming

Expand Down Expand Up @@ -607,6 +608,7 @@ includes this fix. Find the minimum version from there.
[#25989]: https://github.com/JuliaLang/julia/issues/25989
[#25990]: https://github.com/JuliaLang/julia/issues/25990
[#25998]: https://github.com/JuliaLang/julia/issues/25998
[#26009]: https://github.com/JuliaLang/julia/issues/26009
[#26069]: https://github.com/JuliaLang/julia/issues/26069
[#26089]: https://github.com/JuliaLang/julia/issues/26089
[#26149]: https://github.com/JuliaLang/julia/issues/26149
Expand Down
11 changes: 11 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1778,6 +1778,17 @@ if VERSION < v"0.7.0-DEV.4534"
dims===nothing ? Base.reverse(a) : Base.flipdim(a, dims)
end

if !isdefined(Base, :selectdim) # 0.7.0-DEV.3976
export selectdim
@inline selectdim(A::AbstractArray, d::Integer, i) = _selectdim(A, d, i, Base.setindex(axes(A), i, d))
@noinline function _selectdim(A, d, i, idxs)
d >= 1 || throw(ArgumentError("dimension must be ≥ 1"))
nd = ndims(A)
d > nd && (i == 1 || throw(BoundsError(A, (ntuple(k->Colon(),d-1)..., i))))
return view(A, idxs...)
end
end

include("deprecated.jl")

end # module Compat
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1669,4 +1669,14 @@ end
@test Compat.reverse([1 2; 3 4], dims=1) == [3 4; 1 2]
@test Compat.reverse([1 2; 3 4], dims=2) == [2 1; 4 3]

# 0.7.0-DEV.3976
let A = rand(5,5)
@test selectdim(A, 1, 3) == A[3, :]
@test selectdim(A, 1, 1:3) == A[1:3, :]
@test selectdim(A, 2, 3) == A[:, 3]
@test selectdim(A, 2, 1:3) == A[:, 1:3]
selectdim(A, 1, 3)[3] = 42
@test A[3,3] == 42
end

nothing