Skip to content

Commit

Permalink
Add abstractions for working with Array view types (like SubArray)
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Aug 11, 2013
1 parent 489d0f5 commit 8bfb697
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 17 deletions.
2 changes: 2 additions & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,8 @@ export
nthperm!,
nthperm,
ones,
parent,
parentindexes,
partitions,
pascal,
permute!,
Expand Down
12 changes: 12 additions & 0 deletions base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function sub(A::SubArray, i::RangeIndex...)
SubArray{eltype(A),L,typeof(A.parent),typeof(ni)}(A.parent, ni)
end


function slice{T,N}(A::AbstractArray{T,N}, i::NTuple{N,RangeIndex})
n = 0
for j = i; if !isa(j, Int); n += 1; end; end
Expand All @@ -101,6 +102,11 @@ function slice(A::SubArray, i::RangeIndex...)
slice(A.parent, tuple(newindexes...))
end

# Generic fallback for Colon translation
sub(A::AbstractArray, I...) = sub(A, ntuple(length(I), i-> isa(I[i], Colon) ? (1:size(A,i)) : I[i])...)
slice(A::AbstractArray, I...) = slice(A, ntuple(length(I), i-> isa(I[i], Colon) ? (1:size(A,i)) : I[i])...)


### rename the old slice function ###
##squeeze all dimensions of length 1
#slice{T,N}(a::AbstractArray{T,N}) = sub(a, map(i-> i == 1 ? 1 : (1:i), size(a)))
Expand Down Expand Up @@ -148,6 +154,12 @@ end
size(s::SubArray) = s.dims
ndims{T,N}(s::SubArray{T,N}) = N

parent(s::SubArray) = s.parent
parentindexes(s::SubArray) = s.indexes

parent(a::AbstractArray) = a
parentindexes(a::AbstractArray) = ntuple(ndims(a), i->1:size(a,i))

copy(s::SubArray) = copy!(similar(s.parent, size(s)), s)
similar(s::SubArray, T, dims::Dims) = similar(s.parent, T, dims)

Expand Down
109 changes: 96 additions & 13 deletions doc/helpdb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@
"),

("Getting Around","Base","versioninfo","versioninfo([verbose::Bool])
Print information about the version of Julia in use. If the
\"verbose\" argument is true, detailed system information is shown
as well.
"),

("All Objects","Base","is","is(x, y)
Determine whether \"x\" and \"y\" are identical, in the sense that
Expand Down Expand Up @@ -743,7 +751,7 @@

("Iterable Collections","Base","map","map(f, c) -> collection
Transform collection or iterator \"c\" by applying \"f\" to each element.
Transform collection \"c\" by applying \"f\" to each element.
**Example**: \"map((x) -> x * 2, [1, 2, 3]) = [2, 4, 6]\"
Expand Down Expand Up @@ -789,6 +797,13 @@
"),

("Iterable Collections","Base","issubset","issubset(a, b)
Determine whether every element of \"a\" is also in \"b\", using
the \"contains\" function.
"),

("Indexable Collections","Base","getindex","getindex(collection, key...)
Retrieve the value(s) stored at the given key or index within a
Expand Down Expand Up @@ -1609,6 +1624,18 @@
"),

("I/O","Base","isreadonly","isreadonly(stream)
Determine whether a stream is read-only.
"),

("I/O","Base","isopen","isopen(stream)
Determine whether a stream is open (i.e. has not been closed yet).
"),

("I/O","Base","ntoh","ntoh(x)
Converts the endianness of a value from Network byte order (big-
Expand Down Expand Up @@ -1712,7 +1739,19 @@

("Text I/O","Base","println","println(x)
Print (using \"print()\") \"x\" followed by a newline
Print (using \"print()\") \"x\" followed by a newline.
"),

("Text I/O","Base","info","info(msg)
Display an informational message.
"),

("Text I/O","Base","warn","warn(msg)
Display a warning.
"),

Expand All @@ -1730,6 +1769,14 @@
"),

("Text I/O","Base","sprint","sprint(f::Function, args...)
Call the given function with an I/O stream and the supplied extra
arguments. Everything written to this I/O stream is returned as a
string.
"),

("Text I/O","Base","showall","showall(x)
Show x, printing all elements of arrays
Expand Down Expand Up @@ -1864,10 +1911,10 @@ display(d::Display, mime, x)
display objects of this type.
There are also two variants with a \"mime\" argument (a MIME type
string, such as \"\"image/png\"\") attempt to display \"x\" using
the requesed MIME type *only*, throwing a \"MethodError\" if this
type is not supported by either the display(s) or by \"x\". With
these variants, one can also supply the \"raw\" data in the
string, such as \"\"image/png\"\"), which attempt to display \"x\"
using the requesed MIME type *only*, throwing a \"MethodError\" if
this type is not supported by either the display(s) or by \"x\".
With these variants, one can also supply the \"raw\" data in the
requested MIME type by passing \"x::String\" (for MIME types with
text-based storage, such as text/html or application/postscript) or
\"x::Vector{Uint8}\" (for binary MIME types).
Expand Down Expand Up @@ -3389,6 +3436,15 @@ popdisplay(d::Display)
"),

("Data Formats","Base","digits","digits(n[, base][, pad])
Returns an array of the digits of \"n\" in the given base,
optionally padded with zeros to a specified size. More significant
digits are at higher indexes, such that \"n ==
sum([digits[k]*base^(k-1) for k=1:length(digits)])\".
"),

("Data Formats","Base","bits","bits(n)
A string giving the literal bit representation of a number.
Expand Down Expand Up @@ -4167,6 +4223,20 @@ popdisplay(d::Display)
"),

("Arrays","Base","parent","parent(A)
Returns the \"parent array\" of an array view type (e.g.,
SubArray), or the array itself if it is not a view
"),

("Arrays","Base","parentindexes","parentindexes(A)
From an array view \"A\", returns the corresponding indexes in the
parent
"),

("Arrays","Base","slicedim","slicedim(A, d, i)
Return all the data of \"A\" where the index for dimension \"d\"
Expand Down Expand Up @@ -5144,10 +5214,10 @@ popdisplay(d::Display)

("Parallel Computing","Base","pmap","pmap(f, c)
Transform collection or iterator \"c\" by applying \"f\" to each
element in parallel. If \"nprocs() > 1\", the calling process
will be dedicated to assigning tasks. All other available
processes will be used as parallel workers.
Transform collection \"c\" by applying \"f\" to each element in
parallel. If \"nprocs() > 1\", the calling process will be
dedicated to assigning tasks. All other available processes will be
used as parallel workers.
"),

Expand Down Expand Up @@ -7861,9 +7931,6 @@ popdisplay(d::Display)
"),

("Punctuation","","punctuation","punctuation
"),

("Sorting and Related Functions","Base","sort!","sort!(v, [dim,] [alg=<algorithm>,] [by=<transform>,] [lt=<comparison>,] [rev=false])
Expand Down Expand Up @@ -7940,6 +8007,22 @@ popdisplay(d::Display)
"),

("Sorting and Related Functions","Base","searchsortedfirst","searchsortedfirst(a, x, [by=<transform>,] [lt=<comparison>,] [rev=false])
Returns the index of the first value in \"a\" greater than or equal
to \"x\", according to the specified order. Returns \"length(a)+1\"
if \"x\" is greater than all values in \"a\".
"),

("Sorting and Related Functions","Base","searchsortedlast","searchsortedlast(a, x, [by=<transform>,] [lt=<comparison>,] [rev=false])
Returns the index of the last value in \"a\" less than or equal to
\"x\", according to the specified order. Returns \"0\" if \"x\" is
less than all values in \"a\".
"),

This comment has been minimized.

Copy link
@kmsquire

kmsquire Aug 11, 2013

Member

I realize these are on the exported, undocumented list, but @StefanKarpinski explicitly removed the documentation for them in b8b7bef. I believe his desire is that they will go away, or at least not be exported, but since they're still being used...

This comment has been minimized.

Copy link
@kmsquire

kmsquire Aug 11, 2013

Member

Whoops, you can ignore this--I didn't realize that this was just helpdb.jl. They must have been added back somewhere else...

This comment has been minimized.

Copy link
@timholy

timholy Aug 11, 2013

Author Member

Hmm, hydra-like documentation?

("Sorting and Related Functions","Base","select!","select!(v, k, [by=<transform>,] [lt=<comparison>,] [rev=false])
Partially sort the vector \"v\" in place, according to the order
Expand Down
8 changes: 8 additions & 0 deletions doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2875,6 +2875,14 @@ Indexing, Assignment, and Concatenation

Returns a SubArray, which stores the input ``A`` and ``inds`` rather than computing the result immediately. Calling ``getindex`` on a SubArray computes the indices on the fly.

.. function:: parent(A)

Returns the "parent array" of an array view type (e.g., SubArray), or the array itself if it is not a view

.. function:: parentindexes(A)

From an array view ``A``, returns the corresponding indexes in the parent

.. function:: slicedim(A, d, i)

Return all the data of ``A`` where the index for dimension ``d`` equals ``i``. Equivalent to ``A[:,:,...,i,:,:,...]`` where ``i`` is in position ``d``.
Expand Down
13 changes: 11 additions & 2 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ ind = findin(a, b)

# sub
A = reshape(1:120, 3, 5, 8)
sA = sub(A, 2, 1:5, 1:8)
sA = sub(A, 2, 1:5, :)
@test parent(sA) == A
@test parentindexes(sA) == (2:2, 1:5, 1:8)
@test Base.parentdims(sA) == 1:3
@test size(sA) == (1, 5, 8)
@test_throws sA[2, 1:8]
Expand Down Expand Up @@ -120,7 +122,9 @@ sA = sub(A, 1:2:3, 1:3:5, 1:2:8)

# slice
A = reshape(1:120, 3, 5, 8)
sA = slice(A, 2, 1:5, 1:8)
sA = slice(A, 2, :, 1:8)
@test parent(sA) == A
@test parentindexes(sA) == (2, 1:5, 1:8)
@test Base.parentdims(sA) == 2:3
@test size(sA) == (5, 8)
@test strides(sA) == (3,15)
Expand All @@ -140,6 +144,11 @@ sA = slice(A, 1:2:3, 3, 1:2:8)
@test strides(sA) == (2,30)
@test sA[:] == A[sA.indexes...][:]

a = [5:8]
@test parent(a) == a
@test parentindexes(a) == (1:4,)


# get
let
A = reshape(1:24, 3, 8)
Expand Down
4 changes: 2 additions & 2 deletions test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ end
rowval = int32([1,2,2,3,4,5,1,4,6,1,7,2,5,8,6,9,3,4,6,8,10,3,5,7,8,10,11])
colval = int32([1,2,3,3,4,5,6,6,6,7,7,8,8,8,9,9,10,10,10,10,10,11,11,11,11,11,11])
A = sparse(rowval, colval, ones(length(rowval)))
parent,post = Base.LinAlg.etree(A, true)
@assert parent == int32([6,3,8,6,8,7,9,10,10,11,0])
P,post = Base.LinAlg.etree(A, true)
@assert P == int32([6,3,8,6,8,7,9,10,10,11,0])
@assert post == int32([2,3,5,8,1,4,6,7,9,10,11])

0 comments on commit 8bfb697

Please sign in to comment.