Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Julia equivalent to NumPy's ellipsis slicing syntax (...)?

In NumPy, the ellipsis syntax is for

filling in a number of : until the number of slicing specifiers matches the dimension of the array.

(paraphrasing this answer).

How can I do that in Julia?

like image 723
Lyndon White Avatar asked May 11 '15 04:05

Lyndon White


People also ask

Can you index a NumPy array?

Array indexing is the same as accessing an array element. You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.

What is ellipsis in NumPy?

Ellipsis is used for slicing multidimensional numpy arrays. The ellipsis syntax may be used to indicate selecting in full any remaining unspecified dimensions.

How do you use ellipsis in Python?

In Short: Use the Ellipsis as a Placeholder in Python That means you can use an ellipsis as a placeholder similar to the pass keyword. Using three dots creates minimal visual clutter. So, it can be convenient to replace irrelevant code when you're sharing parts of your code online.


1 Answers

Not yet, but you can help yourself if you want.

    import Base.getindex, Base.setindex!
    const .. = Val{:...}

    setindex!{T}(A::AbstractArray{T,1}, x, ::Type{Val{:...}}, n) = A[n] = x
    setindex!{T}(A::AbstractArray{T,2}, x, ::Type{Val{:...}}, n) = A[ :, n] = x
    setindex!{T}(A::AbstractArray{T,3}, x, ::Type{Val{:...}}, n) = A[ :, :, n] =x

    getindex{T}(A::AbstractArray{T,1}, ::Type{Val{:...}}, n) = A[n]
    getindex{T}(A::AbstractArray{T,2}, ::Type{Val{:...}}, n) = A[ :, n]
    getindex{T}(A::AbstractArray{T,3}, ::Type{Val{:...}}, n) = A[ :, :, n]

Then you can write

    > rand(3,3,3)[.., 1]
    3x3 Array{Float64,2}:
     0.0750793  0.490528  0.273044
     0.470398   0.461376  0.01372 
     0.311559   0.879684  0.531157

If you want more elaborate slicing, you need to generate/expand the definition or use staged functions.

Edit: Nowadays, see https://github.com/ChrisRackauckas/EllipsisNotation.jl

like image 148
mschauer Avatar answered Nov 04 '22 04:11

mschauer