From 8bfb69767efaf00208081286fcd5037b3cd01a08 Mon Sep 17 00:00:00 2001 From: timholy Date: Sun, 11 Aug 2013 14:46:45 -0500 Subject: [PATCH] Add abstractions for working with Array view types (like SubArray) --- base/exports.jl | 2 + base/subarray.jl | 12 +++++ doc/helpdb.jl | 109 ++++++++++++++++++++++++++++++++++++++------ doc/stdlib/base.rst | 8 ++++ test/arrayops.jl | 13 +++++- test/sparse.jl | 4 +- 6 files changed, 131 insertions(+), 17 deletions(-) diff --git a/base/exports.jl b/base/exports.jl index f333504f9cc0a..99a956b9b4029 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -520,6 +520,8 @@ export nthperm!, nthperm, ones, + parent, + parentindexes, partitions, pascal, permute!, diff --git a/base/subarray.jl b/base/subarray.jl index 9cc546b84f8a4..ad9f7cc4e4a47 100644 --- a/base/subarray.jl +++ b/base/subarray.jl @@ -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 @@ -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))) @@ -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) diff --git a/doc/helpdb.jl b/doc/helpdb.jl index d4cf5397663eb..f71912d03b402 100644 --- a/doc/helpdb.jl +++ b/doc/helpdb.jl @@ -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 @@ -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]\" @@ -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 @@ -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- @@ -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. "), @@ -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 @@ -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). @@ -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. @@ -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\" @@ -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. "), @@ -7861,9 +7931,6 @@ popdisplay(d::Display) "), -("Punctuation","","punctuation","punctuation - -"), ("Sorting and Related Functions","Base","sort!","sort!(v, [dim,] [alg=,] [by=,] [lt=,] [rev=false]) @@ -7940,6 +8007,22 @@ popdisplay(d::Display) "), +("Sorting and Related Functions","Base","searchsortedfirst","searchsortedfirst(a, x, [by=,] [lt=,] [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=,] [lt=,] [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\". + +"), + ("Sorting and Related Functions","Base","select!","select!(v, k, [by=,] [lt=,] [rev=false]) Partially sort the vector \"v\" in place, according to the order diff --git a/doc/stdlib/base.rst b/doc/stdlib/base.rst index e06bce8520c85..314eb2e7983f2 100644 --- a/doc/stdlib/base.rst +++ b/doc/stdlib/base.rst @@ -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``. diff --git a/test/arrayops.jl b/test/arrayops.jl index 039869b776c36..4cd70d51debe7 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -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] @@ -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) @@ -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) diff --git a/test/sparse.jl b/test/sparse.jl index 411609fbde7f1..512e6e814f6a4 100644 --- a/test/sparse.jl +++ b/test/sparse.jl @@ -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])