From 96efd0db5ad3498ba1d9b654df80fe19c111591e Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Thu, 16 Jun 2016 21:44:57 +0100 Subject: [PATCH] rename sub/slice -> view --- base/abstractarray.jl | 4 +- base/deprecated.jl | 3 ++ base/docs/helpdb/Base.jl | 12 +---- base/exports.jl | 3 +- base/iterator.jl | 2 +- base/linalg/arnoldi.jl | 4 +- base/linalg/arpack.jl | 6 +-- base/linalg/cholesky.jl | 4 +- base/linalg/eigen.jl | 4 +- base/linalg/lapack.jl | 2 +- base/linalg/qr.jl | 38 +++++++------- base/linalg/svd.jl | 2 +- base/loading.jl | 2 +- base/poll.jl | 2 +- base/sharedarray.jl | 4 +- base/show.jl | 2 +- base/sort.jl | 4 +- base/sparse/cholmod.jl | 2 +- base/sparse/linalg.jl | 4 +- base/sparse/sparsematrix.jl | 2 +- base/sparse/sparsevector.jl | 8 +-- base/strings/io.jl | 2 +- base/subarray.jl | 48 +++--------------- test/arrayops.jl | 34 ++++++------- test/blas.jl | 2 +- test/broadcast.jl | 6 +-- test/core.jl | 2 +- test/dsp.jl | 6 +-- test/fft.jl | 6 +-- test/hashing.jl | 2 +- test/linalg/arnoldi.jl | 2 +- test/linalg/bidiag.jl | 4 +- test/linalg/bunchkaufman.jl | 14 +++--- test/linalg/cholesky.jl | 2 +- test/linalg/dense.jl | 16 +++--- test/linalg/diagonal.jl | 8 +-- test/linalg/eigen.jl | 16 +++--- test/linalg/generic.jl | 18 +++---- test/linalg/givens.jl | 4 +- test/linalg/lq.jl | 2 +- test/linalg/lu.jl | 8 +-- test/linalg/matmul.jl | 82 +++++++++++++++--------------- test/linalg/qr.jl | 4 +- test/linalg/schur.jl | 10 ++-- test/linalg/svd.jl | 4 +- test/linalg/triangular.jl | 10 ++-- test/linalg/tridiag.jl | 6 +-- test/linalg/uniformscaling.jl | 12 ++--- test/numbers.jl | 2 +- test/offsetarray.jl | 12 ++--- test/perf/array/indexing.jl | 2 +- test/perf/kernel/laplace.jl | 2 +- test/reduce.jl | 4 +- test/serialize.jl | 6 +-- test/sorting.jl | 10 ++-- test/sparsedir/cholmod.jl | 4 +- test/sparsedir/sparse.jl | 2 +- test/staged.jl | 2 +- test/subarray.jl | 94 ++++++++++++++++------------------- test/unicode/utf32.jl | 2 +- 60 files changed, 269 insertions(+), 317 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index c500a68586ef0..e937e9ca4208a 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -1416,7 +1416,7 @@ function mapslices(f, A::AbstractArray, dims::AbstractVector) idx[d] = Colon() end - r1 = f(slice(A, idx...)) + r1 = f(view(A, idx...)) # determine result size and allocate Rsize = copy(dimsA) @@ -1448,7 +1448,7 @@ function mapslices(f, A::AbstractArray, dims::AbstractVector) for i in 1:nidx idx[otherdims[i]] = ridx[otherdims[i]] = I.I[i] end - R[ridx...] = f(slice(A, idx...)) + R[ridx...] = f(view(A, idx...)) end end diff --git a/base/deprecated.jl b/base/deprecated.jl index 03026cbc47a16..d938921f4b65f 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -756,6 +756,9 @@ function first(::Colon) 1 end +@deprecate slice view +@deprecate sub view + # During the 0.5 development cycle, do not add any deprecations below this line # To be deprecated in 0.6 diff --git a/base/docs/helpdb/Base.jl b/base/docs/helpdb/Base.jl index d73f689a60a17..d65da22b5ea2b 100644 --- a/base/docs/helpdb/Base.jl +++ b/base/docs/helpdb/Base.jl @@ -5402,14 +5402,14 @@ is raised as an exception in the woken tasks. notify """ - sub(A, inds...) + view(A, inds...) Like [`getindex`](:func:`getindex`), but returns a view into the parent array `A` with the given indices instead of making a copy. Calling [`getindex`](:func:`getindex`) or [`setindex!`](:func:`setindex!`) on the returned [`SubArray`](:obj:`SubArray`) computes the indices to the parent array on the fly without checking bounds. """ -sub +view """ expanduser(path::AbstractString) -> AbstractString @@ -7443,14 +7443,6 @@ julia> "Hello " * "world" """ Base.:(*)(s::AbstractString, t::AbstractString) -""" - slice(A, inds...) - -Returns a view of array `A` with the given indices like [`sub`](:func:`sub`), but drops all -dimensions indexed with scalars. -""" -slice - """ time() diff --git a/base/exports.jl b/base/exports.jl index a08da4b723892..77a05d6d73069 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -587,7 +587,6 @@ export shuffle, shuffle!, size, - slice, slicedim, sort!, sort, @@ -601,7 +600,6 @@ export step, stride, strides, - sub, sub2ind, sum!, sum, @@ -612,6 +610,7 @@ export sum_kbn, vcat, vec, + view, zeros, # linear algebra diff --git a/base/iterator.jl b/base/iterator.jl index c3479f8fab74b..05359a4a14093 100644 --- a/base/iterator.jl +++ b/base/iterator.jl @@ -547,7 +547,7 @@ done(itr::PartitionIterator, state) = done(itr.c, state) function next{T<:Vector}(itr::PartitionIterator{T}, state) l = state r = min(state + itr.n-1, length(itr.c)) - return sub(itr.c, l:r), r + 1 + return view(itr.c, l:r), r + 1 end function next(itr::PartitionIterator, state) diff --git a/base/linalg/arnoldi.jl b/base/linalg/arnoldi.jl index 155ef14be7686..b7b2b4d93d3fa 100644 --- a/base/linalg/arnoldi.jl +++ b/base/linalg/arnoldi.jl @@ -296,8 +296,8 @@ end function A_mul_B!{T,S}(u::StridedVector{T}, s::SVDOperator{T,S}, v::StridedVector{T}) a, b = s.m, length(v) - A_mul_B!(sub(u,1:a), s.X, sub(v,a+1:b)) # left singular vector - Ac_mul_B!(sub(u,a+1:b), s.X, sub(v,1:a)) # right singular vector + A_mul_B!(view(u,1:a), s.X, view(v,a+1:b)) # left singular vector + Ac_mul_B!(view(u,a+1:b), s.X, view(v,1:a)) # right singular vector u end size(s::SVDOperator) = s.m + s.n, s.m + s.n diff --git a/base/linalg/arpack.jl b/base/linalg/arpack.jl index a812ef3a3fb28..9b27aa3bc019b 100644 --- a/base/linalg/arpack.jl +++ b/base/linalg/arpack.jl @@ -53,8 +53,8 @@ function aupd_wrapper(T, matvecA!::Function, matvecB::Function, solveSI::Functio throw(ARPACKException(info[1])) end - x = sub(workd, ipntr[1]+zernm1) - y = sub(workd, ipntr[2]+zernm1) + x = view(workd, ipntr[1]+zernm1) + y = view(workd, ipntr[2]+zernm1) if mode == 1 # corresponds to dsdrv1, dndrv1 or zndrv1 if ido[1] == 1 matvecA!(y, x) @@ -89,7 +89,7 @@ function aupd_wrapper(T, matvecA!::Function, matvecB::Function, solveSI::Functio if ido[1] == -1 y[:] = solveSI(matvecB(x)) elseif ido[1] == 1 - y[:] = solveSI(sub(workd,ipntr[3]+zernm1)) + y[:] = solveSI(view(workd,ipntr[3]+zernm1)) elseif ido[1] == 2 y[:] = matvecB(x) elseif ido[1] == 99 diff --git a/base/linalg/cholesky.jl b/base/linalg/cholesky.jl index 611df8b838004..8265d694c160a 100644 --- a/base/linalg/cholesky.jl +++ b/base/linalg/cholesky.jl @@ -379,11 +379,11 @@ function A_ldiv_B!{T<:BlasFloat}(C::CholeskyPivoted{T}, B::StridedMatrix{T}) chkfullrank(C) n = size(C, 1) for i=1:size(B, 2) - permute!(sub(B, 1:n, i), C.piv) + permute!(view(B, 1:n, i), C.piv) end LAPACK.potrs!(C.uplo, C.factors, B) for i=1:size(B, 2) - ipermute!(sub(B, 1:n, i), C.piv) + ipermute!(view(B, 1:n, i), C.piv) end B end diff --git a/base/linalg/eigen.jl b/base/linalg/eigen.jl index 13fdf3b0b6423..e1edaabe3ffa8 100644 --- a/base/linalg/eigen.jl +++ b/base/linalg/eigen.jl @@ -35,7 +35,7 @@ function eigfact!{T<:BlasReal}(A::StridedMatrix{T}; permute::Bool=true, scale::B j = 1 while j <= n if WI[j] == 0 - evec[:,j] = slice(VR, :, j) + evec[:,j] = view(VR, :, j) else for i = 1:n evec[i,j] = VR[i,j] + im*VR[i,j+1] @@ -131,7 +131,7 @@ function eigfact!{T<:BlasReal}(A::StridedMatrix{T}, B::StridedMatrix{T}) j = 1 while j <= n if alphai[j] == 0 - vecs[:,j] = slice(vr, :, j) + vecs[:,j] = view(vr, :, j) else for i = 1:n vecs[i,j ] = vr[i,j] + im*vr[i,j+1] diff --git a/base/linalg/lapack.jl b/base/linalg/lapack.jl index 7a40910e07f73..62a8e1a8fba6c 100644 --- a/base/linalg/lapack.jl +++ b/base/linalg/lapack.jl @@ -1437,7 +1437,7 @@ for (gglse, elty) in ((:dgglse_, :Float64), work = Array{$elty}(lwork) end end - X, dot(sub(c, n - p + 1:m), sub(c, n - p + 1:m)) + X, dot(view(c, n - p + 1:m), view(c, n - p + 1:m)) end end end diff --git a/base/linalg/qr.jl b/base/linalg/qr.jl index 0a4459ba8d39c..42f4f77fd5056 100644 --- a/base/linalg/qr.jl +++ b/base/linalg/qr.jl @@ -28,20 +28,20 @@ function qrfactUnblocked!{T}(A::AbstractMatrix{T}) m, n = size(A) τ = zeros(T, min(m,n)) for k = 1:min(m - 1 + !(T<:Real), n) - x = slice(A, k:m, k) + x = view(A, k:m, k) τk = reflector!(x) τ[k] = τk - reflectorApply!(x, τk, slice(A, k:m, k + 1:n)) + reflectorApply!(x, τk, view(A, k:m, k + 1:n)) end QR(A, τ) end # Find index for columns with largest two norm function indmaxcolumn(A::StridedMatrix) - mm = norm(slice(A, :, 1)) + mm = norm(view(A, :, 1)) ii = 1 for i = 2:size(A, 2) - mi = norm(slice(A, :, i)) + mi = norm(view(A, :, i)) if abs(mi) > mm mm = mi ii = i @@ -57,7 +57,7 @@ function qrfactPivotedUnblocked!(A::StridedMatrix) for j = 1:min(m,n) # Find column with maximum norm in trailing submatrix - jm = indmaxcolumn(slice(A, j:m, j:n)) + j - 1 + jm = indmaxcolumn(view(A, j:m, j:n)) + j - 1 if jm != j # Flip elements in pivoting vector @@ -74,12 +74,12 @@ function qrfactPivotedUnblocked!(A::StridedMatrix) end # Compute reflector of columns j - x = slice(A, j:m, j) + x = view(A, j:m, j) τj = LinAlg.reflector!(x) τ[j] = τj # Update trailing submatrix with reflector - LinAlg.reflectorApply!(x, τj, sub(A, j:m, j+1:n)) + LinAlg.reflectorApply!(x, τj, view(A, j:m, j+1:n)) end return LinAlg.QRPivoted{eltype(A), typeof(A)}(A, τ, piv) end @@ -451,8 +451,8 @@ for (f1, f2) in ((:Ac_mul_B, :A_mul_B!), end end -A_ldiv_B!{T<:BlasFloat}(A::QRCompactWY{T}, b::StridedVector{T}) = (A_ldiv_B!(UpperTriangular(A[:R]), sub(Ac_mul_B!(A[:Q], b), 1:size(A, 2))); b) -A_ldiv_B!{T<:BlasFloat}(A::QRCompactWY{T}, B::StridedMatrix{T}) = (A_ldiv_B!(UpperTriangular(A[:R]), sub(Ac_mul_B!(A[:Q], B), 1:size(A, 2), 1:size(B, 2))); B) +A_ldiv_B!{T<:BlasFloat}(A::QRCompactWY{T}, b::StridedVector{T}) = (A_ldiv_B!(UpperTriangular(A[:R]), view(Ac_mul_B!(A[:Q], b), 1:size(A, 2))); b) +A_ldiv_B!{T<:BlasFloat}(A::QRCompactWY{T}, B::StridedMatrix{T}) = (A_ldiv_B!(UpperTriangular(A[:R]), view(Ac_mul_B!(A[:Q], B), 1:size(A, 2), 1:size(B, 2))); B) # Julia implementation similarly to xgelsy function A_ldiv_B!{T<:BlasFloat}(A::QRPivoted{T}, B::StridedMatrix{T}, rcond::Real) @@ -467,8 +467,8 @@ function A_ldiv_B!{T<:BlasFloat}(A::QRPivoted{T}, B::StridedMatrix{T}, rcond::Re xmax = ones(T, 1) tmin = tmax = ar while rnk < nr - tmin, smin, cmin = LAPACK.laic1!(2, xmin, tmin, sub(A.factors, 1:rnk, rnk + 1), A.factors[rnk + 1, rnk + 1]) - tmax, smax, cmax = LAPACK.laic1!(1, xmax, tmax, sub(A.factors, 1:rnk, rnk + 1), A.factors[rnk + 1, rnk + 1]) + tmin, smin, cmin = LAPACK.laic1!(2, xmin, tmin, view(A.factors, 1:rnk, rnk + 1), A.factors[rnk + 1, rnk + 1]) + tmax, smax, cmax = LAPACK.laic1!(1, xmax, tmax, view(A.factors, 1:rnk, rnk + 1), A.factors[rnk + 1, rnk + 1]) tmax*rcond > tmin && break push!(xmin, cmin) push!(xmax, cmax) @@ -479,10 +479,10 @@ function A_ldiv_B!{T<:BlasFloat}(A::QRPivoted{T}, B::StridedMatrix{T}, rcond::Re rnk += 1 end C, τ = LAPACK.tzrzf!(A.factors[1:rnk,:]) - A_ldiv_B!(UpperTriangular(C[1:rnk,1:rnk]),sub(Ac_mul_B!(getq(A),sub(B, 1:mA, 1:nrhs)),1:rnk,1:nrhs)) + A_ldiv_B!(UpperTriangular(C[1:rnk,1:rnk]),view(Ac_mul_B!(getq(A),view(B, 1:mA, 1:nrhs)),1:rnk,1:nrhs)) B[rnk+1:end,:] = zero(T) - LAPACK.ormrz!('L', eltype(B)<:Complex ? 'C' : 'T', C, τ, sub(B,1:nA,1:nrhs)) - B[1:nA,:] = sub(B, 1:nA, :)[invperm(A[:p]::Vector{BlasInt}),:] + LAPACK.ormrz!('L', eltype(B)<:Complex ? 'C' : 'T', C, τ, view(B,1:nA,1:nrhs)) + B[1:nA,:] = view(B, 1:nA, :)[invperm(A[:p]::Vector{BlasInt}),:] return B, rnk end A_ldiv_B!{T<:BlasFloat}(A::QRPivoted{T}, B::StridedVector{T}) = vec(A_ldiv_B!(A,reshape(B,length(B),1))) @@ -491,13 +491,13 @@ function A_ldiv_B!{T}(A::QR{T}, B::StridedMatrix{T}) m, n = size(A) minmn = min(m,n) mB, nB = size(B) - Ac_mul_B!(A[:Q], sub(B, 1:m, :)) + Ac_mul_B!(A[:Q], view(B, 1:m, :)) R = A[:R] @inbounds begin if n > m # minimum norm solution τ = zeros(T,m) for k = m:-1:1 # Trapezoid to triangular by elementary operation - x = slice(R, k, [k; m + 1:n]) + x = view(R, k, [k; m + 1:n]) τk = reflector!(x) τ[k] = τk' for i = 1:k - 1 @@ -513,7 +513,7 @@ function A_ldiv_B!{T}(A::QR{T}, B::StridedMatrix{T}) end end end - Base.A_ldiv_B!(UpperTriangular(sub(R, :, 1:minmn)), sub(B, 1:minmn, :)) + Base.A_ldiv_B!(UpperTriangular(view(R, :, 1:minmn)), view(B, 1:minmn, :)) if n > m # Apply elementary transformation to solution B[m + 1:mB,1:nB] = zero(T) for j = 1:nB @@ -536,12 +536,12 @@ end A_ldiv_B!(A::QR, B::StridedVector) = A_ldiv_B!(A, reshape(B, length(B), 1))[:] function A_ldiv_B!(A::QRPivoted, b::StridedVector) A_ldiv_B!(QR(A.factors,A.τ), b) - b[1:size(A.factors, 2)] = sub(b, 1:size(A.factors, 2))[invperm(A.jpvt)] + b[1:size(A.factors, 2)] = view(b, 1:size(A.factors, 2))[invperm(A.jpvt)] b end function A_ldiv_B!(A::QRPivoted, B::StridedMatrix) A_ldiv_B!(QR(A.factors, A.τ), B) - B[1:size(A.factors, 2),:] = sub(B, 1:size(A.factors, 2), :)[invperm(A.jpvt),:] + B[1:size(A.factors, 2),:] = view(B, 1:size(A.factors, 2), :)[invperm(A.jpvt),:] B end diff --git a/base/linalg/svd.jl b/base/linalg/svd.jl index a3f80b67aa465..896364c69d242 100644 --- a/base/linalg/svd.jl +++ b/base/linalg/svd.jl @@ -95,7 +95,7 @@ svdvals{T, Tr}(S::SVD{T, Tr}) = (S[:S])::Vector{Tr} # SVD least squares function A_ldiv_B!{Ta,Tb}(A::SVD{Ta}, B::StridedVecOrMat{Tb}) k = searchsortedlast(A.S, eps(real(Ta))*A.S[1], rev=true) - sub(A.Vt,1:k,:)' * (sub(A.S,1:k) .\ (sub(A.U,:,1:k)' * B)) + view(A.Vt,1:k,:)' * (view(A.S,1:k) .\ (view(A.U,:,1:k)' * B)) end # Generalized svd diff --git a/base/loading.jl b/base/loading.jl index 04406d0b4c8b1..99c049faf5593 100644 --- a/base/loading.jl +++ b/base/loading.jl @@ -60,7 +60,7 @@ elseif is_apple() continue end casepreserved_basename = - sub(buf, (header_size+1):(header_size+filename_length-1)) + view(buf, (header_size+1):(header_size+filename_length-1)) break end # Hack to compensate for inability to create a string from a subarray with no allocations. diff --git a/base/poll.jl b/base/poll.jl index f9a6036d1e31f..1425b6ee59be4 100644 --- a/base/poll.jl +++ b/base/poll.jl @@ -102,7 +102,7 @@ type _FDWatcher if fd.fd+1 > length(FDWatchers) old_len = length(FDWatchers) resize!(FDWatchers, fd.fd+1) - fill!(sub(FDWatchers, old_len+1:fd.fd+1), nothing) + fill!(view(FDWatchers, old_len+1:fd.fd+1), nothing) elseif FDWatchers[fd.fd + 1] !== nothing this = FDWatchers[fd.fd + 1]::_FDWatcher this.refcount = (this.refcount[1] + Int(readable), this.refcount[2] + Int(writable)) diff --git a/base/sharedarray.jl b/base/sharedarray.jl index ab46bd146f4cc..fa9d42412c966 100644 --- a/base/sharedarray.jl +++ b/base/sharedarray.jl @@ -327,7 +327,7 @@ function range_1dim(S::SharedArray, pidx) end end -sub_1dim(S::SharedArray, pidx) = sub(S.s, range_1dim(S, pidx)) +sub_1dim(S::SharedArray, pidx) = view(S.s, range_1dim(S, pidx)) function init_loc_flds{T,N}(S::SharedArray{T,N}) if myid() in S.pids @@ -342,7 +342,7 @@ function init_loc_flds{T,N}(S::SharedArray{T,N}) S.loc_subarr_1d = sub_1dim(S, S.pidx) else S.pidx = 0 - S.loc_subarr_1d = sub(Array{T}(ntuple(d->0,N)), 1:0) + S.loc_subarr_1d = view(Array{T}(ntuple(d->0,N)), 1:0) end end diff --git a/base/show.jl b/base/show.jl index 492dda654f4b4..b3ad846153220 100644 --- a/base/show.jl +++ b/base/show.jl @@ -1438,7 +1438,7 @@ function show_nd(io::IO, a::AbstractArray, print_matrix, label_slices) for i = 1:(nd-1); print(io, "$(idxs[i]), "); end println(io, idxs[end], "] =") end - slice = sub(a, indices(a,1), indices(a,2), idxs...) + slice = view(a, indices(a,1), indices(a,2), idxs...) print_matrix(io, slice) print(io, idxs == map(last,tail) ? "" : "\n\n") @label skip diff --git a/base/sort.jl b/base/sort.jl index 1abe23f6fdbac..9b2bd7ea4e724 100644 --- a/base/sort.jl +++ b/base/sort.jl @@ -509,7 +509,7 @@ function sortrows(A::AbstractMatrix; kws...) T = slicetypeof(A, inds, :) rows = allocate_for(Vector{T}, A, shape(A, 1)) for i in inds - rows[i] = slice(A, i, :) + rows[i] = view(A, i, :) end p = sortperm(rows; kws..., order=Lexicographic) A[p,:] @@ -520,7 +520,7 @@ function sortcols(A::AbstractMatrix; kws...) T = slicetypeof(A, :, inds) cols = allocate_for(Vector{T}, A, shape(A, 2)) for i in inds - cols[i] = slice(A, :, i) + cols[i] = view(A, :, i) end p = sortperm(cols; kws..., order=Lexicographic) A[:,p] diff --git a/base/sparse/cholmod.jl b/base/sparse/cholmod.jl index ca90b2aa521b5..f596b26d40ae0 100644 --- a/base/sparse/cholmod.jl +++ b/base/sparse/cholmod.jl @@ -830,7 +830,7 @@ function (::Type{Sparse}){Tv<:VTypes}(m::Integer, n::Integer, # check if columns are sorted iss = true for i = 2:length(colptr) - if !issorted(sub(rowval, colptr[i - 1] + 1:colptr[i])) + if !issorted(view(rowval, colptr[i - 1] + 1:colptr[i])) iss = false break end diff --git a/base/sparse/linalg.jl b/base/sparse/linalg.jl index 1b24452007c97..3d92314eee891 100644 --- a/base/sparse/linalg.jl +++ b/base/sparse/linalg.jl @@ -588,7 +588,7 @@ function normestinv{T}(A::SparseMatrixCSC{T}, t::Integer = min(2,maximum(size(A) X[1:n,1] = 1 for j = 2:t while true - _rand_pm1!(slice(X,1:n,j)) + _rand_pm1!(view(X,1:n,j)) yaux = X[1:n,j]' * X[1:n,1:j-1] if !_any_abs_eq(yaux,n) break @@ -649,7 +649,7 @@ function normestinv{T}(A::SparseMatrixCSC{T}, t::Integer = min(2,maximum(size(A) end end if repeated - _rand_pm1!(slice(S,1:n,j)) + _rand_pm1!(view(S,1:n,j)) else break end diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index 83d209c3cdc37..0b11bfbafa734 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -3110,7 +3110,7 @@ function spdiagm_internal(B, d) range = 1+i:numel+i I[range] = row+1:row+numel J[range] = col+1:col+numel - copy!(sub(V, range), vec) + copy!(view(V, range), vec) i += numel end diff --git a/base/sparse/sparsevector.jl b/base/sparse/sparsevector.jl index f7987a2b898da..72e42f1531c57 100644 --- a/base/sparse/sparsevector.jl +++ b/base/sparse/sparsevector.jl @@ -1550,8 +1550,8 @@ for isunittri in (true, false), islowertri in (true, false) nzrange = $( (islowertri && !istrans) || (!islowertri && istrans) ? :(b.nzind[1]:b.n) : :(1:b.nzind[end]) ) - nzrangeviewr = sub(r, nzrange) - nzrangeviewA = $tritype(sub(A.data, nzrange, nzrange)) + nzrangeviewr = view(r, nzrange) + nzrangeviewA = $tritype(view(A.data, nzrange, nzrange)) ($ipfunc)(convert(AbstractArray{TAb}, nzrangeviewA), nzrangeviewr) end r @@ -1586,8 +1586,8 @@ for isunittri in (true, false), islowertri in (true, false) nzrange = $( (islowertri && !istrans) || (!islowertri && istrans) ? :(b.nzind[1]:b.n) : :(1:b.nzind[end]) ) - nzrangeviewbnz = sub(b.nzval, nzrange - b.nzind[1] + 1) - nzrangeviewA = $tritype(sub(A.data, nzrange, nzrange)) + nzrangeviewbnz = view(b.nzval, nzrange - b.nzind[1] + 1) + nzrangeviewA = $tritype(view(A.data, nzrange, nzrange)) ($func)(nzrangeviewA, nzrangeviewbnz) # could strip any miraculous zeros here perhaps end diff --git a/base/strings/io.jl b/base/strings/io.jl index 89a8c84b02ea0..199fe0eefc785 100644 --- a/base/strings/io.jl +++ b/base/strings/io.jl @@ -90,7 +90,7 @@ end # IOBuffer views of a (byte)string: IOBuffer(str::String) = IOBuffer(str.data) -IOBuffer(s::SubString{String}) = IOBuffer(sub(s.string.data, s.offset + 1 : s.offset + sizeof(s))) +IOBuffer(s::SubString{String}) = IOBuffer(view(s.string.data, s.offset + 1 : s.offset + sizeof(s))) # join is implemented using IO function join(io::IO, strings, delim, last) diff --git a/base/subarray.jl b/base/subarray.jl index aea6dcf80bfdd..ff155d3a49675 100644 --- a/base/subarray.jl +++ b/base/subarray.jl @@ -85,53 +85,27 @@ parentindexes(a::AbstractArray) = ntuple(i->1:size(a,i), ndims(a)) ## SubArray creation # Drops singleton dimensions (those indexed with a scalar) -function slice{T,N}(A::AbstractArray{T,N}, I::Vararg{ViewIndex,N}) +function view{T,N}(A::AbstractArray{T,N}, I::Vararg{ViewIndex,N}) @_inline_meta @boundscheck checkbounds(A, I...) - unsafe_slice(A, I...) + unsafe_view(A, I...) end -function slice(A::AbstractArray, i::ViewIndex) +function view(A::AbstractArray, i::ViewIndex) @_inline_meta @boundscheck checkbounds(A, i) - unsafe_slice(reshape(A, Val{1}), i) + unsafe_view(reshape(A, Val{1}), i) end -function slice{N}(A::AbstractArray, I::Vararg{ViewIndex,N}) # TODO: DEPRECATE FOR #14770 +function view{N}(A::AbstractArray, I::Vararg{ViewIndex,N}) # TODO: DEPRECATE FOR #14770 @_inline_meta @boundscheck checkbounds(A, I...) - unsafe_slice(reshape(A, Val{N}), I...) + unsafe_view(reshape(A, Val{N}), I...) end -function unsafe_slice{T,N}(A::AbstractArray{T,N}, I::Vararg{ViewIndex,N}) +function unsafe_view{T,N}(A::AbstractArray{T,N}, I::Vararg{ViewIndex,N}) @_inline_meta J = to_indexes(I...) SubArray(A, J, map(dimlength, index_shape(A, J...))) end -keep_leading_scalars(T::Tuple{}) = T -keep_leading_scalars(T::Tuple{Real, Vararg{Real}}) = T -keep_leading_scalars(T::Tuple{Real, Vararg{Any}}) = (@_inline_meta; (NoSlice(T[1]), keep_leading_scalars(tail(T))...)) -keep_leading_scalars(T::Tuple{Any, Vararg{Any}}) = (@_inline_meta; (T[1], keep_leading_scalars(tail(T))...)) - -function sub{T,N}(A::AbstractArray{T,N}, I::Vararg{ViewIndex,N}) - @_inline_meta - @boundscheck checkbounds(A, I...) - unsafe_sub(A, I...) -end -function sub(A::AbstractArray, i::ViewIndex) - @_inline_meta - @boundscheck checkbounds(A, i) - unsafe_sub(reshape(A, Val{1}), i) -end -function sub{N}(A::AbstractArray, I::Vararg{ViewIndex,N}) # TODO: DEPRECATE FOR #14770 - @_inline_meta - @boundscheck checkbounds(A, I...) - unsafe_sub(reshape(A, Val{N}), I...) -end -function unsafe_sub{T,N}(A::AbstractArray{T,N}, I::Vararg{ViewIndex,N}) - @_inline_meta - J = keep_leading_scalars(to_indexes(I...)) - SubArray(A, J, index_shape(A, J...)) -end - # Re-indexing is the heart of a view, transforming A[i, j][x, y] to A[i[x], j[y]] # # Recursively look through the heads of the parent- and sub-indexes, considering @@ -216,18 +190,12 @@ function setindex!(V::FastContiguousSubArray, x, i::Real) V end -function unsafe_slice{T,N}(V::SubArray{T,N}, I::Vararg{ViewIndex,N}) +function unsafe_view{T,N}(V::SubArray{T,N}, I::Vararg{ViewIndex,N}) @_inline_meta idxs = reindex(V, V.indexes, to_indexes(I...)) SubArray(V.parent, idxs, index_shape(V.parent, idxs...)) end -function unsafe_sub{T,N}(V::SubArray{T,N}, I::Vararg{ViewIndex,N}) - @_inline_meta - idxs = reindex(V, V.indexes, keep_leading_scalars(to_indexes(I...))) - SubArray(V.parent, idxs, index_shape(V.parent, idxs...)) -end - linearindexing{T<:FastSubArray}(::Type{T}) = LinearFast() linearindexing{T<:SubArray}(::Type{T}) = LinearSlow() diff --git a/test/arrayops.jl b/test/arrayops.jl index a556260ad942a..6a061d8011cc1 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -90,7 +90,7 @@ a = reshape(b, (2, 2, 2, 2, 2)) a = collect(reshape(1:5, 1, 5)) # reshaping linearfast SubArrays -s = sub(a, :, 2:4) +s = view(a, :, 2:4) r = reshape(s, (length(s),)) @test length(r) == 3 @test r[1] == 2 @@ -105,7 +105,7 @@ r = reshape(s, (length(s),)) @test Base.unsafe_convert(Ptr{Int}, r) == Base.unsafe_convert(Ptr{Int}, s) # reshaping linearslow SubArrays -s = sub(a, :, [2,3,5]) +s = view(a, :, [2,3,5]) r = reshape(s, length(s)) @test length(r) == 3 @test r[1] == 2 @@ -121,7 +121,7 @@ r = reshape(s, length(s)) r[2] = -1 @test a[3] == -1 a = zeros(0, 5) # an empty linearslow array -s = sub(a, :, [2,3,5]) +s = view(a, :, [2,3,5]) @test length(reshape(s, length(s))) == 0 @test reshape(1:5, (5,)) === 1:5 @@ -435,14 +435,14 @@ end # of a subarray a = rand(5,5) -s = sub(a,2:3,2:3) +s = view(a,2:3,2:3) p = permutedims(s, [2,1]) @test p[1,1]==a[2,2] && p[1,2]==a[3,2] @test p[2,1]==a[2,3] && p[2,2]==a[3,3] # of a non-strided subarray a = reshape(1:60, 3, 4, 5) -s = sub(a,:,[1,2,4],[1,5]) +s = view(a,:,[1,2,4],[1,5]) c = convert(Array, s) for p in ([1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]) @test permutedims(s, p) == permutedims(c, p) @@ -700,7 +700,7 @@ let @test R == [1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2] A = rand(4,4) - for s in Any[A[1:2:4, 1:2:4], sub(A, 1:2:4, 1:2:4)] + for s in Any[A[1:2:4, 1:2:4], view(A, 1:2:4, 1:2:4)] c = cumsum(s, 1) @test c[1,1] == A[1,1] @test c[2,1] == A[1,1]+A[3,1] @@ -932,9 +932,9 @@ end # fill @test fill!(Array{Float64}(1),-0.0)[1] === -0.0 A = ones(3,3) -S = sub(A, 2, 1:3) +S = view(A, 2, 1:3) fill!(S, 2) -S = sub(A, 1:2, 3) +S = view(A, 1:2, 3) fill!(S, 3) @test A == [1 1 3; 2 2 3; 1 1 1] rt = Base.return_types(fill!, Tuple{Array{Int32, 3}, UInt8}) @@ -1166,7 +1166,7 @@ end a = [1:5;] @test isa(Base.linearindexing(a), Base.LinearFast) -b = sub(a, :) +b = view(a, :) @test isa(Base.linearindexing(b), Base.LinearFast) @test isa(Base.linearindexing(trues(2)), Base.LinearFast) @test isa(Base.linearindexing(BitArray{2}), Base.LinearFast) @@ -1178,7 +1178,7 @@ for i = 1:10 @test mdsum(A) == 15 @test mdsum2(A) == 15 AA = reshape(aa, tuple(2, shp...)) - B = sub(AA, 1:1, ntuple(i->Colon(), i)...) + B = view(AA, 1:1, ntuple(i->Colon(), i)...) @test isa(Base.linearindexing(B), Base.IteratorsMD.LinearSlow) @test mdsum(B) == 15 @test mdsum2(B) == 15 @@ -1191,7 +1191,7 @@ for i = 2:10 A = reshape(a, tuple(shp...)) @test mdsum(A) == 55 @test mdsum2(A) == 55 - B = sub(A, ntuple(i->Colon(), i)...) + B = view(A, ntuple(i->Colon(), i)...) @test mdsum(B) == 55 @test mdsum2(B) == 55 insert!(shp, 2, 1) @@ -1202,10 +1202,10 @@ a = reshape([2]) @test mdsum2(a) == 2 a = ones(0,5) -b = sub(a, :, :) +b = view(a, :, :) @test mdsum(b) == 0 a = ones(5,0) -b = sub(a, :, :) +b = view(a, :, :) @test mdsum(b) == 0 a = copy(reshape(1:60, 3, 4, 5)) @@ -1219,7 +1219,7 @@ a[1,CartesianIndex{2}(3,4)] = -2 a[CartesianIndex{1}(2),3,CartesianIndex{1}(3)] = -3 @test a[CartesianIndex{1}(2),3,CartesianIndex{1}(3)] == -3 -a = sub(zeros(3, 4, 5), :, :, :) +a = view(zeros(3, 4, 5), :, :, :) a[CartesianIndex{3}(2,3,3)] = -1 @test a[CartesianIndex{3}(2,3,3)] == -1 a[1,CartesianIndex{2}(3,4)] = -2 @@ -1264,7 +1264,7 @@ a = spzeros(2,3) @test CartesianRange(size(a)) == eachindex(a) a[CartesianIndex{2}(2,3)] = 5 @test a[2,3] == 5 -b = sub(a, 1:2, 2:3) +b = view(a, 1:2, 2:3) b[CartesianIndex{2}(1,1)] = 7 @test a[1,2] == 7 @test 2*CartesianIndex{3}(1,2,3) == CartesianIndex{3}(2,4,6) @@ -1314,7 +1314,7 @@ R = CartesianRange((3,0)) @test @inferred(eachindex(Base.LinearSlow(),zeros(3),zeros(2,2),zeros(2,2,2),zeros(2,2))) == CartesianRange((3,2,2)) @test @inferred(eachindex(Base.LinearFast(),zeros(3),zeros(2,2),zeros(2,2,2),zeros(2,2))) == 1:8 -@test @inferred(eachindex(zeros(3),sub(zeros(3,3),1:2,1:2),zeros(2,2,2),zeros(2,2))) == CartesianRange((3,2,2)) +@test @inferred(eachindex(zeros(3),view(zeros(3,3),1:2,1:2),zeros(2,2,2),zeros(2,2))) == CartesianRange((3,2,2)) @test @inferred(eachindex(zeros(3),zeros(2,2),zeros(2,2,2),zeros(2,2))) == 1:8 @@ -1461,7 +1461,7 @@ Base.setindex!(A::LinSlowMatrix, v, i::Integer, j::Integer) = A.data[i,j] = v A = rand(3,5) B = LinSlowMatrix(A) -S = sub(A, :, :) +S = view(A, :, :) @test A == B @test B == A diff --git a/test/blas.jl b/test/blas.jl index b870ae59b12a7..f44a5ae900b51 100644 --- a/test/blas.jl +++ b/test/blas.jl @@ -100,7 +100,7 @@ for elty in [Float32, Float64, Complex64, Complex128] # nrm2, iamax, and asum for StridedVectors a = rand(elty,n) - b = slice(a,2:2:n,1) + b = view(a,2:2:n,1) @test BLAS.nrm2(b) ≈ norm(b) if elty <: Real @test BLAS.asum(b) ≈ sum(abs(b)) diff --git a/test/broadcast.jl b/test/broadcast.jl index 357711fbd585d..2484a43d29701 100644 --- a/test/broadcast.jl +++ b/test/broadcast.jl @@ -55,13 +55,13 @@ end function as_sub(x::AbstractVector) y = similar(x, eltype(x), tuple(([size(x)...]*2)...)) - y = sub(y, 2:2:length(y)) + y = view(y, 2:2:length(y)) y[:] = x[:] y end function as_sub(x::AbstractMatrix) y = similar(x, eltype(x), tuple(([size(x)...]*2)...)) - y = sub(y, 2:2:size(y,1), 2:2:size(y,2)) + y = view(y, 2:2:size(y,1), 2:2:size(y,2)) for j=1:size(x,2) for i=1:size(x,1) y[i,j] = x[i,j] @@ -71,7 +71,7 @@ function as_sub(x::AbstractMatrix) end function as_sub{T}(x::AbstractArray{T,3}) y = similar(x, eltype(x), tuple(([size(x)...]*2)...)) - y = sub(y, 2:2:size(y,1), 2:2:size(y,2), 2:2:size(y,3)) + y = view(y, 2:2:size(y,1), 2:2:size(y,2), 2:2:size(y,3)) for k=1:size(x,3) for j=1:size(x,2) for i=1:size(x,1) diff --git a/test/core.jl b/test/core.jl index 11c038ae1b0b6..0fb29bf35ca10 100644 --- a/test/core.jl +++ b/test/core.jl @@ -2061,7 +2061,7 @@ function issue7897!(data, arr) end a = ones(UInt8, 10) -sa = sub(a,4:6) +sa = view(a,4:6) # This can throw an error, but shouldn't segfault try issue7897!(sa, zeros(10)) diff --git a/test/dsp.jl b/test/dsp.jl index 34aa579c14c12..a55ad5faa2a1c 100644 --- a/test/dsp.jl +++ b/test/dsp.jl @@ -48,7 +48,7 @@ if Base.fftw_vendor() != :mkl X = reshape([1,2,7,2,1,5,9,-1,3,4,6,9],3,4) Y = rand(17,14) Y[3:5,9:12] = X - sX = slice(Y,3:5,9:12) + sX = view(Y,3:5,9:12) true_Xdct = [ 13.856406460551018 -3.863239728836245 2.886751345948129 -0.274551994240164; -2.828427124746190 -2.184015211898548 -4.949747468305834 3.966116180118245; 4.898979485566356 -0.194137576915510 -2.857738033247041 2.731723009609389 ] @@ -87,8 +87,8 @@ if Base.fftw_vendor() != :mkl sXdct = dct(sX) psXdct = plan_dct(sX)*(sX) - sYdct! = copy(Y); sXdct! = slice(sYdct!,3:5,9:12); dct!(sXdct!) - psYdct! = copy(Y); psXdct! = slice(psYdct!,3:5,9:12); plan_dct!(psXdct!)*(psXdct!) + sYdct! = copy(Y); sXdct! = view(sYdct!,3:5,9:12); dct!(sXdct!) + psYdct! = copy(Y); psXdct! = view(psYdct!,3:5,9:12); plan_dct!(psXdct!)*(psXdct!) for i = 1:length(X) @test_approx_eq Xdct[i] true_Xdct[i] diff --git a/test/fft.jl b/test/fft.jl index a15f0ab474262..9ebc93c353dfc 100644 --- a/test/fft.jl +++ b/test/fft.jl @@ -29,7 +29,7 @@ true_fftd2_m4 = [ b = rand(17,14) b[3:6,9:12] = m4 -sm4 = slice(b,3:6,9:12) +sm4 = view(b,3:6,9:12) m3d = map(Float32,copy(reshape(1:5*3*2, 5, 3, 2))) true_fftd3_m3d = Array{Float32}(5, 3, 2) @@ -78,9 +78,9 @@ for (f,fi,pf,pfi) in ((fft,ifft,plan_fft,plan_ifft), sfftn_m4 = f(sm4) psfftn_m4 = pf(sm4)*sm4 sfft!n_b = map(Complex128,b) - sfft!n_m4 = slice(sfft!n_b,3:6,9:12); fft!(sfft!n_m4) + sfft!n_m4 = view(sfft!n_b,3:6,9:12); fft!(sfft!n_m4) psfft!n_b = map(Complex128,b) - psfft!n_m4 = slice(psfft!n_b,3:6,9:12); plan_fft!(psfft!n_m4)*psfft!n_m4 + psfft!n_m4 = view(psfft!n_b,3:6,9:12); plan_fft!(psfft!n_m4)*psfft!n_m4 for i = 1:length(m4) @test_approx_eq fft_m4[i] true_fft_m4[i] diff --git a/test/hashing.jl b/test/hashing.jl index 9ee196c119e1e..1919226f41d2a 100644 --- a/test/hashing.jl +++ b/test/hashing.jl @@ -83,7 +83,7 @@ end @test hash(:(X.x)) == hash(:(X.x)) @test hash(:(X.x)) != hash(:(X.y)) -@test hash([1,2]) == hash(sub([1,2,3,4],1:2)) +@test hash([1,2]) == hash(view([1,2,3,4],1:2)) # test explicit zeros in SparseMatrixCSC x = sprand(10, 10, 0.5) diff --git a/test/linalg/arnoldi.jl b/test/linalg/arnoldi.jl index 35eaad7107862..d9bc48fc00106 100644 --- a/test/linalg/arnoldi.jl +++ b/test/linalg/arnoldi.jl @@ -115,7 +115,7 @@ function A_mul_B!{T<:Base.LinAlg.BlasFloat}(rho2::StridedVector{T},Phi::CPM{T},r rho=reshape(rho,(size(Phi.kraus,3),size(Phi.kraus,3))) rho1=zeros(T,(size(Phi.kraus,1),size(Phi.kraus,1))) for s=1:size(Phi.kraus,2) - As=slice(Phi.kraus,:,s,:) + As=view(Phi.kraus,:,s,:) rho1+=As*rho*As' end return copy!(rho2,rho1) diff --git a/test/linalg/bidiag.jl b/test/linalg/bidiag.jl index 168790a2dddd1..6ea351a3c2a65 100644 --- a/test/linalg/bidiag.jl +++ b/test/linalg/bidiag.jl @@ -139,8 +139,8 @@ for relty in (Int, Float32, Float64, BigFloat), elty in (relty, Complex{relty}) b = bb c = cc else - b = sub(bb, 1:n) - c = sub(cc, 1:n, 1:2) + b = view(bb, 1:n) + c = view(cc, 1:n, 1:2) end end debug && println("Linear solver") diff --git a/test/linalg/bunchkaufman.jl b/test/linalg/bunchkaufman.jl index cf2dda7cc3fdd..7e3cbeef29e30 100644 --- a/test/linalg/bunchkaufman.jl +++ b/test/linalg/bunchkaufman.jl @@ -30,10 +30,10 @@ for eltya in (Float32, Float64, Complex64, Complex128, Int) a = a a2 = a2 else - a = sub(a, 1:n, 1:n) - a2 = sub(a2, 1:n, 1:n) - asym = sub(asym, 1:n, 1:n) - apd = sub(apd, 1:n, 1:n) + a = view(a, 1:n, 1:n) + a2 = view(a2, 1:n, 1:n) + asym = view(asym, 1:n, 1:n) + apd = view(apd, 1:n, 1:n) end ε = εa = eps(abs(float(one(eltya)))) @@ -43,7 +43,7 @@ for eltya in (Float32, Float64, Complex64, Complex128, Int) if btype == "Array" b = b else - b = sub(b, 1:n, 1:2) + b = view(b, 1:n, 1:2) end εb = eps(abs(float(one(eltyb)))) @@ -92,7 +92,7 @@ let if Astype == "Array" As = As else - As = sub(As, 1:n, 1:n) + As = view(As, 1:n, 1:n) end for rook in (false, true) @@ -110,6 +110,6 @@ let A = rand(6,5); A = complex(A'*A) # to avoid calling the real-lhs-complex-rhs method F = cholfact(A); v6 = rand(Complex128, 6) - v5 = sub(v6, 1:5) + v5 = view(v6, 1:5) @test F\v5 == F\v6[1:5] end diff --git a/test/linalg/cholesky.jl b/test/linalg/cholesky.jl index 0f8faf8e82963..f42a00e865c0c 100644 --- a/test/linalg/cholesky.jl +++ b/test/linalg/cholesky.jl @@ -116,7 +116,7 @@ debug && println("\ntype of a: ", eltya, " type of b: ", eltyb, "\n") if atype == "Array" b = Bs else - b = sub(Bs, 1:n, 1) + b = view(Bs, 1:n, 1) end # Test error bound on linear solver: LAWNS 14, Theorem 2.1 diff --git a/test/linalg/dense.jl b/test/linalg/dense.jl index eb69d862392ec..393d58ab64ba7 100644 --- a/test/linalg/dense.jl +++ b/test/linalg/dense.jl @@ -159,7 +159,7 @@ for elty in (Float32, Float64, BigFloat, Complex{Float32}, Complex{Float64}, Com ## Vector x = ones(elty,10) - xs = sub(x,1:2:10) + xs = view(x,1:2:10) @test_approx_eq norm(x, -Inf) 1 @test_approx_eq norm(x, -1) 1/10 @test_approx_eq norm(x, 0) 10 @@ -199,11 +199,11 @@ for elty in (Float32, Float64, BigFloat, Complex{Float32}, Complex{Float64}, Com x = elty <: Integer ? convert(Vector{elty}, rand(1:10, nnorm)) : elty <: Complex ? convert(Vector{elty}, complex(randn(nnorm), randn(nnorm))) : convert(Vector{elty}, randn(nnorm)) - xs = sub(x,1:2:nnorm) + xs = view(x,1:2:nnorm) y = elty <: Integer ? convert(Vector{elty}, rand(1:10, nnorm)) : elty <: Complex ? convert(Vector{elty}, complex(randn(nnorm), randn(nnorm))) : convert(Vector{elty}, randn(nnorm)) - ys = sub(y,1:2:nnorm) + ys = view(y,1:2:nnorm) α = elty <: Integer ? randn() : elty <: Complex ? convert(elty, complex(randn(),randn())) : convert(elty, randn()) @@ -244,7 +244,7 @@ for elty in (Float32, Float64, BigFloat, Complex{Float32}, Complex{Float64}, Com end ## Matrix (Operator) A = ones(elty,10,10) - As = sub(A,1:5,1:5) + As = view(A,1:5,1:5) @test_approx_eq norm(A, 1) 10 elty <: Union{BigFloat,Complex{BigFloat},BigInt} || @test_approx_eq norm(A, 2) 10 @test_approx_eq norm(A, Inf) 10 @@ -256,11 +256,11 @@ for elty in (Float32, Float64, BigFloat, Complex{Float32}, Complex{Float64}, Com A = elty <: Integer ? convert(Matrix{elty}, rand(1:10, mmat, nmat)) : elty <: Complex ? convert(Matrix{elty}, complex(randn(mmat, nmat), randn(mmat, nmat))) : convert(Matrix{elty}, randn(mmat, nmat)) - As = sub(A,1:nmat,1:nmat) + As = view(A,1:nmat,1:nmat) B = elty <: Integer ? convert(Matrix{elty}, rand(1:10, mmat, nmat)) : elty <: Complex ? convert(Matrix{elty}, complex(randn(mmat, nmat), randn(mmat, nmat))) : convert(Matrix{elty}, randn(mmat, nmat)) - Bs = sub(B,1:nmat,1:nmat) + Bs = view(B,1:nmat,1:nmat) α = elty <: Integer ? randn() : elty <: Complex ? convert(elty, complex(randn(),randn())) : convert(elty, randn()) @@ -323,7 +323,7 @@ let A = [1 2 0 0; 0 1 0 0; 0 0 0 0; 0 0 0 0] Asq = sqrtm(A) @test_approx_eq Asq*Asq A - A2 = sub(A, 1:2, 1:2) + A2 = view(A, 1:2, 1:2) A2sq = sqrtm(A2) @test_approx_eq A2sq*A2sq A2 end @@ -510,5 +510,5 @@ end # stride1 a = rand(10) -b = slice(a,2:2:10) +b = view(a,2:2:10) @test Base.LinAlg.stride1(b) == 2 diff --git a/test/linalg/diagonal.jl b/test/linalg/diagonal.jl index 3ed0deed3a23d..be856257cef9d 100644 --- a/test/linalg/diagonal.jl +++ b/test/linalg/diagonal.jl @@ -62,8 +62,8 @@ for relty in (Float32, Float64, BigFloat), elty in (relty, Complex{relty}) v = vv U = UU else - v = sub(vv, 1:n) - U = sub(UU, 1:n, 1:2) + v = view(vv, 1:n) + U = view(UU, 1:n, 1:2) end debug && println("Linear solve") @@ -81,7 +81,7 @@ for relty in (Float32, Float64, BigFloat), elty in (relty, Complex{relty}) b = sparse(b) @test A_ldiv_B!(D,copy(b)) ≈ full(D)\full(b) @test_throws SingularException A_ldiv_B!(Diagonal(zeros(elty,n)),copy(b)) - b = sub(rand(elty,n),collect(1:n)) + b = view(rand(elty,n),collect(1:n)) b2 = copy(b) c = A_ldiv_B!(D,b) d = full(D)\b2 @@ -92,7 +92,7 @@ for relty in (Float32, Float64, BigFloat), elty in (relty, Complex{relty}) b = rand(elty,n+1,n+1) b = sparse(b) @test_throws DimensionMismatch A_ldiv_B!(D,copy(b)) - b = sub(rand(elty,n+1),collect(1:n+1)) + b = view(rand(elty,n+1),collect(1:n+1)) @test_throws DimensionMismatch A_ldiv_B!(D,b) end diff --git a/test/linalg/eigen.jl b/test/linalg/eigen.jl index ab6aed4185a0c..89089f0f422fc 100644 --- a/test/linalg/eigen.jl +++ b/test/linalg/eigen.jl @@ -24,9 +24,9 @@ for eltya in (Float32, Float64, Complex64, Complex128, Int) if atype == "Array" a = aa else - a = sub(aa, 1:n, 1:n) - asym = sub(asym, 1:n, 1:n) - apd = sub(apd, 1:n, 1:n) + a = view(aa, 1:n, 1:n) + asym = view(asym, 1:n, 1:n) + apd = view(apd, 1:n, 1:n) end ε = εa = eps(abs(float(one(eltya)))) @@ -59,8 +59,8 @@ for eltya in (Float32, Float64, Complex64, Complex128, Int) asym_sg = asym[1:n1, 1:n1] a_sg = a[:,n1+1:n2] else - asym_sg = sub(asym, 1:n1, 1:n1) - a_sg = sub(a, 1:n, n1+1:n2) + asym_sg = view(asym, 1:n1, 1:n1) + a_sg = view(a, 1:n, n1+1:n2) end f = eigfact(asym_sg, a_sg'a_sg) @test_approx_eq asym_sg*f[:vectors] (a_sg'a_sg*f[:vectors]) * Diagonal(f[:values]) @@ -80,8 +80,8 @@ for eltya in (Float32, Float64, Complex64, Complex128, Int) a1_nsg = a[1:n1, 1:n1] a2_nsg = a[n1+1:n2, n1+1:n2] else - a1_nsg = sub(a, 1:n1, 1:n1) - a2_nsg = sub(a, n1+1:n2, n1+1:n2) + a1_nsg = view(a, 1:n1, 1:n1) + a2_nsg = view(a, n1+1:n2, n1+1:n2) end f = eigfact(a1_nsg, a2_nsg) @test_approx_eq a1_nsg*f[:vectors] (a2_nsg*f[:vectors]) * Diagonal(f[:values]) @@ -101,7 +101,7 @@ let aa = rand(200, 200) if atype == "Array" a = aa else - a = sub(aa, 1:n, 1:n) + a = view(aa, 1:n, 1:n) end f = eigfact(a) @test a ≈ f[:vectors] * Diagonal(f[:values]) / f[:vectors] diff --git a/test/linalg/generic.jl b/test/linalg/generic.jl index 70f0a2f8c3a91..1ce2337b0d043 100644 --- a/test/linalg/generic.jl +++ b/test/linalg/generic.jl @@ -57,10 +57,10 @@ let X = [3 9 5; 2 1 10] @test diff(X,1) == [4 -5 -3; -5 -3 8] @test diff(X,2) == [6 -4; -3 -2; -1 9] - @test diff(sub(X, 1:2, 1:2),1) == [4 -5] - @test diff(sub(X, 1:2, 1:2),2) == reshape([6; -3], (2,1)) - @test diff(sub(X, 2:3, 2:3),1) == [-3 8] - @test diff(sub(X, 2:3, 2:3),2) == reshape([-2; 9], (2,1)) + @test diff(view(X, 1:2, 1:2),1) == [4 -5] + @test diff(view(X, 1:2, 1:2),2) == reshape([6; -3], (2,1)) + @test diff(view(X, 2:3, 2:3),1) == [-3 8] + @test diff(view(X, 2:3, 2:3),2) == reshape([-2; 9], (2,1)) @test_throws ArgumentError diff(X,3) @test_throws ArgumentError diff(X,-1) end @@ -78,7 +78,7 @@ y = [3; 7; 10] x = 1:12 y = [5.5; 6.3; 7.6; 8.8; 10.9; 11.79; 13.48; 15.02; 17.77; 20.81; 22.0; 22.99] @test_approx_eq [linreg(x,y)...] [2.5559090909090867, 1.6960139860139862] -@test_approx_eq [linreg(sub(x,1:6),sub(y,1:6))...] [3.8366666666666642,1.3271428571428574] +@test_approx_eq [linreg(view(x,1:6),view(y,1:6))...] [3.8366666666666642,1.3271428571428574] # check (LinSpace, UnitRange) x = linspace(1.0, 12.0, 100) @@ -114,8 +114,8 @@ y4 = [6.58; 5.76; 7.71; 8.84; 8.47; 7.04; 5.25; 12.50; 5.56; 7.91; 6.89] # test diag let A = eye(4) @test diag(A) == ones(4) - @test diag(sub(A, 1:3, 1:3)) == ones(3) - @test diag(sub(A, 1:2, 1:2)) == ones(2) + @test diag(view(A, 1:3, 1:3)) == ones(3) + @test diag(view(A, 1:2, 1:2)) == ones(2) end # test generic axpy @@ -142,14 +142,14 @@ let aa = reshape([1.:6;], (2,3)) if atype == "Array" a = aa else - a = sub(aa, 1:2, 1:2) + a = view(aa, 1:2, 1:2) end # 2-argument version of scale! @test scale!(copy(a), 5.) == a*5 @test scale!(5., copy(a)) == a*5 b = randn(Base.LinAlg.SCAL_CUTOFF) # make sure we try BLAS path - subB = sub(b, :, :) + subB = view(b, :, :) @test scale!(copy(b), 5.) == b*5 @test scale!(copy(subB), 5.) == subB*5 @test scale!([1.; 2.], copy(a)) == a.*[1; 2] diff --git a/test/linalg/givens.jl b/test/linalg/givens.jl index 5d5a5dfd6c7f0..1078de8b839de 100644 --- a/test/linalg/givens.jl +++ b/test/linalg/givens.jl @@ -19,7 +19,7 @@ for elty in (Float32, Float64, Complex64, Complex128) if Atype == "Array" A = A else - A = sub(A, 1:10, 1:10) + A = view(A, 1:10, 1:10) end Ac = copy(A) R = Base.LinAlg.Rotation(Base.LinAlg.Givens{elty}[]) @@ -56,7 +56,7 @@ for elty in (Float32, Float64, Complex64, Complex128) if Atype == "Array" x = A[:, 1] else - x = sub(A, 1:10, 1) + x = view(A, 1:10, 1) end G, r = givens(x[2], x[4], 2, 4) @test (G*x)[2] ≈ r diff --git a/test/linalg/lq.jl b/test/linalg/lq.jl index c56692f68e1c7..e898dbcfdaed8 100644 --- a/test/linalg/lq.jl +++ b/test/linalg/lq.jl @@ -43,7 +43,7 @@ for eltya in (Float32, Float64, Complex64, Complex128) debug && println("\ntype of a: ", eltya, " type of b: ", eltyb, "\n") debug && println("LQ decomposition") for i = 1:2 - let a = i == 1 ? a : sub(a, 1:n - 1, 1:n - 1), b = i == 1 ? b : sub(b, 1:n - 1), n = i == 1 ? n : n - 1 + let a = i == 1 ? a : view(a, 1:n - 1, 1:n - 1), b = i == 1 ? b : view(b, 1:n - 1), n = i == 1 ? n : n - 1 lqa = lqfact(a) l,q = lqa[:L], lqa[:Q] qra = qrfact(a) diff --git a/test/linalg/lu.jl b/test/linalg/lu.jl index 1f1dc86bfd04b..5891fe63a7ea2 100644 --- a/test/linalg/lu.jl +++ b/test/linalg/lu.jl @@ -60,8 +60,8 @@ debug && println("(Automatic) Square LU decomposition") b = Bs c = Cs else - b = sub(Bs, 1:n, 1) - c = sub(Cs, 1:n) + b = view(Bs, 1:n, 1) + c = view(Cs, 1:n) end @test norm(a*(lua\b) - b, 1) < ε*κ*n*2 # Two because the right hand side has two columns @test norm(a'*(lua'\b) - b, 1) < ε*κ*n*2 # Two because the right hand side has two columns @@ -97,7 +97,7 @@ debug && println("Tridiagonal LU") if atype == "Array" b = Bs else - b = sub(Bs, 1:n, 1) + b = view(Bs, 1:n, 1) end @test norm(d*(lud\b) - b, 1) < ε*κd*n*2 # Two because the right hand side has two columns @@ -157,7 +157,7 @@ let Bs = b if atype == "Array" b = Bs else - b = sub(Bs, 1:n, 1) + b = view(Bs, 1:n, 1) end @test a*(lua\b) ≈ b end diff --git a/test/linalg/matmul.jl b/test/linalg/matmul.jl index 803d44b1ff654..53d1368506e95 100644 --- a/test/linalg/matmul.jl +++ b/test/linalg/matmul.jl @@ -24,15 +24,15 @@ let AAi = AA+(0.5*im).*BB BBi = BB+(2.5*im).*AA[[2,1],[2,1]] for Atype = ["Array", "SubArray"], Btype = ["Array", "SubArray"] - A = Atype == "Array" ? AA : sub(AA, 1:2, 1:2) - B = Btype == "Array" ? BB : sub(BB, 1:2, 1:2) + A = Atype == "Array" ? AA : view(AA, 1:2, 1:2) + B = Btype == "Array" ? BB : view(BB, 1:2, 1:2) @test A*B == [19 22; 43 50] @test At_mul_B(A, B) == [26 30; 38 44] @test A_mul_Bt(A, B) == [17 23; 39 53] @test At_mul_Bt(A, B) == [23 31; 34 46] - Ai = Atype == "Array" ? AAi : sub(AAi, 1:2, 1:2) - Bi = Btype == "Array" ? BBi : sub(BBi, 1:2, 1:2) + Ai = Atype == "Array" ? AAi : view(AAi, 1:2, 1:2) + Bi = Btype == "Array" ? BBi : view(BBi, 1:2, 1:2) @test Ai*Bi == [-21+53.5im -4.25+51.5im; -12+95.5im 13.75+85.5im] @test Ac_mul_B(Ai, Bi) == [68.5-12im 57.5-28im; 88-3im 76.5-25im] @test A_mul_Bc(Ai, Bi) == [64.5+5.5im 43+31.5im; 104-18.5im 80.5+31.5im] @@ -47,15 +47,15 @@ let AAi = AA+(0.5*im).*BB BBi = BB+(2.5*im).*AA[[2,1,3],[2,3,1]] for Atype = ["Array", "SubArray"], Btype = ["Array", "SubArray"] - A = Atype == "Array" ? AA : sub(AA, 1:3, 1:3) - B = Btype == "Array" ? BB : sub(BB, 1:3, 1:3) + A = Atype == "Array" ? AA : view(AA, 1:3, 1:3) + B = Btype == "Array" ? BB : view(BB, 1:3, 1:3) @test A*B == [-26 38 -27; 1 -4 -6; 28 -46 15] @test Ac_mul_B(A, B) == [-6 2 -25; 3 -12 -18; 12 -26 -11] @test A_mul_Bc(A, B) == [-14 0 6; 4 -3 -3; 22 -6 -12] @test Ac_mul_Bc(A, B) == [6 -8 -6; 12 -9 -9; 18 -10 -12] - Ai = Atype == "Array" ? AAi : sub(AAi, 1:3, 1:3) - Bi = Btype == "Array" ? BBi : sub(BBi, 1:3, 1:3) + Ai = Atype == "Array" ? AAi : view(AAi, 1:3, 1:3) + Bi = Btype == "Array" ? BBi : view(BBi, 1:3, 1:3) @test Ai*Bi == [-44.75+13im 11.75-25im -38.25+30im; -47.75-16.5im -51.5+51.5im -56+6im; 16.75-4.5im -53.5+52im -15.5im] @test Ac_mul_B(Ai, Bi) == [-21+2im -1.75+49im -51.25+19.5im; 25.5+56.5im -7-35.5im 22+35.5im; -3+12im -32.25+43im -34.75-2.5im] @test A_mul_Bc(Ai, Bi) == [-20.25+15.5im -28.75-54.5im 22.25+68.5im; -12.25+13im -15.5+75im -23+27im; 18.25+im 1.5+94.5im -27-54.5im] @@ -83,25 +83,25 @@ let AA = [1 2 3; 4 5 6] .- 3 BB = [2 -2; 3 -5; -4 7] for Atype = ["Array", "SubArray"], Btype = ["Array", "SubArray"] - A = Atype == "Array" ? AA : sub(AA, 1:2, 1:3) - B = Btype == "Array" ? BB : sub(BB, 1:3, 1:2) + A = Atype == "Array" ? AA : view(AA, 1:2, 1:3) + B = Btype == "Array" ? BB : view(BB, 1:3, 1:2) @test A*B == [-7 9; -4 9] @test At_mul_Bt(A, B) == [-6 -11 15; -6 -13 18; -6 -15 21] end AA = ones(Int, 2, 100) BB = ones(Int, 100, 3) for Atype = ["Array", "SubArray"], Btype = ["Array", "SubArray"] - A = Atype == "Array" ? AA : sub(AA, 1:2, 1:100) - B = Btype == "Array" ? BB : sub(BB, 1:100, 1:3) + A = Atype == "Array" ? AA : view(AA, 1:2, 1:100) + B = Btype == "Array" ? BB : view(BB, 1:100, 1:3) @test A*B == [100 100 100; 100 100 100] end AA = rand(1:20, 5, 5) .- 10 BB = rand(1:20, 5, 5) .- 10 CC = Array{Int}(size(AA, 1), size(BB, 2)) for Atype = ["Array", "SubArray"], Btype = ["Array", "SubArray"], Ctype = ["Array", "SubArray"] - A = Atype == "Array" ? AA : sub(AA, 1:5, 1:5) - B = Btype == "Array" ? BB : sub(BB, 1:5, 1:5) - C = Btype == "Array" ? CC : sub(CC, 1:5, 1:5) + A = Atype == "Array" ? AA : view(AA, 1:5, 1:5) + B = Btype == "Array" ? BB : view(BB, 1:5, 1:5) + C = Btype == "Array" ? CC : view(CC, 1:5, 1:5) @test At_mul_B(A, B) == A'*B @test A_mul_Bt(A, B) == A*B' # Preallocated @@ -118,8 +118,8 @@ let vv = [1,2] CC = Array{Int}(2, 2) for vtype = ["Array", "SubArray"], Ctype = ["Array", "SubArray"] - v = vtype == "Array" ? vv : sub(vv, 1:2) - C = Ctype == "Array" ? CC : sub(CC, 1:2, 1:2) + v = vtype == "Array" ? vv : view(vv, 1:2) + C = Ctype == "Array" ? CC : view(CC, 1:2, 1:2) @test @inferred(A_mul_Bc!(C, v, v)) == [1 2; 2 4] end end @@ -129,23 +129,23 @@ let AA = rand(5,5) BB = rand(5) for Atype = ["Array", "SubArray"], Btype = ["Array", "SubArray"] - A = Atype == "Array" ? AA : sub(AA, 1:5, 1:5) - B = Btype == "Array" ? BB : sub(BB, 1:5) + A = Atype == "Array" ? AA : view(AA, 1:5, 1:5) + B = Btype == "Array" ? BB : view(BB, 1:5) @test_throws DimensionMismatch Base.LinAlg.generic_matvecmul!(zeros(6),'N',A,B) @test_throws DimensionMismatch Base.LinAlg.generic_matvecmul!(B,'N',A,zeros(6)) end vv = [1,2,3] CC = Array{Int}(3, 3) for vtype = ["Array", "SubArray"], Ctype = ["Array", "SubArray"] - v = vtype == "Array" ? vv : sub(vv, 1:3) - C = Ctype == "Array" ? CC : sub(CC, 1:3, 1:3) + v = vtype == "Array" ? vv : view(vv, 1:3) + C = Ctype == "Array" ? CC : view(CC, 1:3, 1:3) @test A_mul_Bt!(C, v, v) == v*v' end vvf = map(Float64,vv) CC = Array{Float64}(3, 3) for vtype = ["Array", "SubArray"], Ctype = ["Array", "SubArray"] - vf = vtype == "Array" ? vvf : sub(vvf, 1:3) - C = Ctype == "Array" ? CC : sub(CC, 1:3, 1:3) + vf = vtype == "Array" ? vvf : view(vvf, 1:3) + C = Ctype == "Array" ? CC : view(CC, 1:3, 1:3) @test A_mul_Bt!(C, vf, vf) == vf*vf' end end @@ -156,9 +156,9 @@ let BB = rand(Float64,6,6) CC = zeros(Float64,6,6) for Atype = ["Array", "SubArray"], Btype = ["Array", "SubArray"], Ctype = ["Array", "SubArray"] - A = Atype == "Array" ? AA : sub(AA, 1:6, 1:6) - B = Btype == "Array" ? BB : sub(BB, 1:6, 1:6) - C = Ctype == "Array" ? CC : sub(CC, 1:6, 1:6) + A = Atype == "Array" ? AA : view(AA, 1:6, 1:6) + B = Btype == "Array" ? BB : view(BB, 1:6, 1:6) + C = Ctype == "Array" ? CC : view(CC, 1:6, 1:6) @test Base.LinAlg.At_mul_Bt!(C,A,B) == A.'*B.' @test Base.LinAlg.A_mul_Bc!(C,A,B) == A*B.' @test Base.LinAlg.Ac_mul_B!(C,A,B) == A.'*B @@ -169,26 +169,26 @@ end let A = reshape(map(Float64,1:20),5,4) Aref = A[1:2:end,1:2:end] - Asub = sub(A, 1:2:5, 1:2:4) + Asub = view(A, 1:2:5, 1:2:4) b = [1.2,-2.5] @test (Aref*b) == (Asub*b) @test At_mul_B(Asub, Asub) == At_mul_B(Aref, Aref) @test A_mul_Bt(Asub, Asub) == A_mul_Bt(Aref, Aref) Ai = A .+ im Aref = Ai[1:2:end,1:2:end] - Asub = sub(Ai, 1:2:5, 1:2:4) + Asub = view(Ai, 1:2:5, 1:2:4) @test Ac_mul_B(Asub, Asub) == Ac_mul_B(Aref, Aref) @test A_mul_Bc(Asub, Asub) == A_mul_Bc(Aref, Aref) end # issue #15286 -let A = reshape(map(Float64, 1:20), 5, 4), C = zeros(8, 8), sC = sub(C, 1:2:8, 1:2:8), B = reshape(map(Float64,-9:10),5,4) +let A = reshape(map(Float64, 1:20), 5, 4), C = zeros(8, 8), sC = view(C, 1:2:8, 1:2:8), B = reshape(map(Float64,-9:10),5,4) @test At_mul_B!(sC, A, A) == A'*A @test At_mul_B!(sC, A, B) == A'*B Aim = A .- im C = zeros(Complex128,8,8) - sC = sub(C, 1:2:8, 1:2:8) + sC = view(C, 1:2:8, 1:2:8) B = reshape(map(Float64,-9:10),5,4) .+ im @test Ac_mul_B!(sC, Aim, Aim) == Aim'*Aim @test Ac_mul_B!(sC, Aim, B) == Aim'*B @@ -199,17 +199,17 @@ let AA = reshape(1:1503, 501, 3).-750.0 res = Float64[135228751 9979252 -115270247; 9979252 10481254 10983256; -115270247 10983256 137236759] for Atype = ["Array", "SubArray"] - A = Atype == "Array" ? AA : sub(AA, 1:501, 1:3) + A = Atype == "Array" ? AA : view(AA, 1:501, 1:3) @test At_mul_B(A, A) == res @test A_mul_Bt(A',A') == res end cutoff = 501 A = reshape(1:6*cutoff,2*cutoff,3).-(6*cutoff)/2 - Asub = sub(A, 1:2:2*cutoff, 1:3) + Asub = view(A, 1:2:2*cutoff, 1:3) Aref = A[1:2:2*cutoff, 1:3] @test At_mul_B(Asub, Asub) == At_mul_B(Aref, Aref) Ai = A .- im - Asub = sub(Ai, 1:2:2*cutoff, 1:3) + Asub = view(Ai, 1:2:2*cutoff, 1:3) Aref = Ai[1:2:2*cutoff, 1:3] @test Ac_mul_B(Asub, Asub) == Ac_mul_B(Aref, Aref) @@ -221,7 +221,7 @@ end let AA = fill(complex(1,1), 10, 10) for Atype = ["Array", "SubArray"] - A = Atype == "Array" ? AA : sub(AA, 1:10, 1:10) + A = Atype == "Array" ? AA : view(AA, 1:10, 1:10) A2 = A^2 @test A2[1,1] == 20im end @@ -233,9 +233,9 @@ let CC = rand(5, 6) for Atype = ["Array", "SubArray"], Btype = ["Array", "SubArray"] for Ctype = ["Array", "SubArray"] - A = Atype == "Array" ? AA : sub(AA, 1:5, 1:5) - B = Btype == "Array" ? BB : sub(BB, 1:5) - C = Ctype == "Array" ? CC : sub(CC, 1:5, 1:6) + A = Atype == "Array" ? AA : view(AA, 1:5, 1:5) + B = Btype == "Array" ? BB : view(BB, 1:5) + C = Ctype == "Array" ? CC : view(CC, 1:5, 1:6) @test_throws DimensionMismatch scale!(A, B, C) end @@ -258,8 +258,8 @@ end vecdot_(x,y) = invoke(vecdot, (Any,Any), x,y) # generic vecdot let AA = [1+2im 3+4im; 5+6im 7+8im], BB = [2+7im 4+1im; 3+8im 6+5im] for Atype = ["Array", "SubArray"], Btype = ["Array", "SubArray"] - A = Atype == "Array" ? AA : sub(AA, 1:2, 1:2) - B = Btype == "Array" ? BB : sub(BB, 1:2, 1:2) + A = Atype == "Array" ? AA : view(AA, 1:2, 1:2) + B = Btype == "Array" ? BB : view(BB, 1:2, 1:2) @test vecdot(A,B) == dot(vec(A),vec(B)) == vecdot_(A,B) == vecdot(float(A),float(B)) @test vecdot(Int[], Int[]) == 0 == vecdot_(Int[], Int[]) @test_throws MethodError vecdot(Any[], Any[]) @@ -308,8 +308,8 @@ let aa = rand(3,3) bb = rand(3,3) for atype = ["Array", "SubArray"], btype = ["Array", "SubArray"] - a = atype == "Array" ? aa : sub(aa, 1:3, 1:3) - b = btype == "Array" ? bb : sub(bb, 1:3, 1:3) + a = atype == "Array" ? aa : view(aa, 1:3, 1:3) + b = btype == "Array" ? bb : view(bb, 1:3, 1:3) @test_throws ArgumentError A_mul_B!(a, a, b) @test_throws ArgumentError A_mul_B!(a, b, a) @test_throws ArgumentError A_mul_B!(a, a, a) diff --git a/test/linalg/qr.jl b/test/linalg/qr.jl index b024351d9d1b9..39829db9fdfc6 100644 --- a/test/linalg/qr.jl +++ b/test/linalg/qr.jl @@ -41,7 +41,7 @@ for eltya in (Float32, Float64, Complex64, Complex128, BigFloat, Int) debug && println("\ntype of a: ", eltya, " type of b: ", eltyb, "\n") debug && println("QR decomposition (without pivoting)") for i = 1:2 - let a = i == 1 ? a : sub(a, 1:n - 1, 1:n - 1), b = i == 1 ? b : sub(b, 1:n - 1), n = i == 1 ? n : n - 1 + let a = i == 1 ? a : view(a, 1:n - 1, 1:n - 1), b = i == 1 ? b : view(b, 1:n - 1), n = i == 1 ? n : n - 1 qra = @inferred qrfact(a) @inferred qr(a) q, r = qra[:Q], qra[:R] @@ -58,7 +58,7 @@ debug && println("QR decomposition (without pivoting)") if eltya != Int @test eye(eltyb,n)*q ≈ convert(AbstractMatrix{tab},q) ac = copy(a) - @test qrfact!(a[:, 1:5])\b == qrfact!(sub(ac, :, 1:5))\b + @test qrfact!(a[:, 1:5])\b == qrfact!(view(ac, :, 1:5))\b end debug && println("Thin QR decomposition (without pivoting)") diff --git a/test/linalg/schur.jl b/test/linalg/schur.jl index 0ff240832d99d..d3dc91567ed2a 100644 --- a/test/linalg/schur.jl +++ b/test/linalg/schur.jl @@ -24,9 +24,9 @@ for eltya in (Float32, Float64, Complex64, Complex128, Int) if atype == "Array" a = a else - a = sub(a, 1:n, 1:n) - asym = sub(asym, 1:n, 1:n) - apd = sub(apd, 1:n, 1:n) + a = view(a, 1:n, 1:n) + asym = view(asym, 1:n, 1:n) + apd = view(apd, 1:n, 1:n) end ε = εa = eps(abs(float(one(eltya)))) @@ -61,8 +61,8 @@ for eltya in (Float32, Float64, Complex64, Complex128, Int) a1_sf = a[1:n1, 1:n1] a2_sf = a[n1+1:n2, n1+1:n2] else - a1_sf = sub(a, 1:n1, 1:n1) - a2_sf = sub(a, n1+1:n2, n1+1:n2) + a1_sf = view(a, 1:n1, 1:n1) + a2_sf = view(a, n1+1:n2, n1+1:n2) end f = schurfact(a1_sf, a2_sf) @test_approx_eq f[:Q]*f[:S]*f[:Z]' a1_sf diff --git a/test/linalg/svd.jl b/test/linalg/svd.jl index 7e44e83e6e566..d98a63c1d7625 100644 --- a/test/linalg/svd.jl +++ b/test/linalg/svd.jl @@ -28,8 +28,8 @@ for eltya in (Float32, Float64, Complex64, Complex128, Int) a = aa a2 = aa2 else - a = sub(aa, 1:n, 1:n) - a2 = sub(aa2, 1:n, 1:n) + a = view(aa, 1:n, 1:n) + a2 = view(aa2, 1:n, 1:n) end ε = εa = eps(abs(float(one(eltya)))) diff --git a/test/linalg/triangular.jl b/test/linalg/triangular.jl index 6f01fb1eff2d6..76a177ceb6f9c 100644 --- a/test/linalg/triangular.jl +++ b/test/linalg/triangular.jl @@ -123,7 +123,7 @@ for elty1 in (Float32, Float64, BigFloat, Complex64, Complex128, Complex{BigFloa @test factorize(A1) == A1 # [c]transpose[!] (test views as well, see issue #14317) - let vrange = 1:n-1, viewA1 = t1(sub(A1.data, vrange, vrange)) + let vrange = 1:n-1, viewA1 = t1(view(A1.data, vrange, vrange)) # transpose @test full(A1.') == full(A1).' @test full(viewA1.') == full(viewA1).' @@ -132,10 +132,10 @@ for elty1 in (Float32, Float64, BigFloat, Complex64, Complex128, Complex{BigFloa @test full(viewA1') == full(viewA1)' # transpose! @test transpose!(copy(A1)) == A1.' - @test transpose!(t1(sub(copy(A1).data, vrange, vrange))) == viewA1.' + @test transpose!(t1(view(copy(A1).data, vrange, vrange))) == viewA1.' # ctranspose! @test ctranspose!(copy(A1)) == A1' - @test ctranspose!(t1(sub(copy(A1).data, vrange, vrange))) == viewA1' + @test ctranspose!(t1(view(copy(A1).data, vrange, vrange))) == viewA1' end # diag @@ -150,7 +150,7 @@ for elty1 in (Float32, Float64, BigFloat, Complex64, Complex128, Complex{BigFloa @test full(-A1) == -full(A1) # copy and copy! (test views as well, see issue #14317) - let vrange = 1:n-1, viewA1 = t1(sub(A1.data, vrange, vrange)) + let vrange = 1:n-1, viewA1 = t1(view(A1.data, vrange, vrange)) # copy @test copy(A1) == copy(full(A1)) @test copy(viewA1) == copy(full(viewA1)) @@ -493,4 +493,4 @@ end @test_throws ArgumentError UpperTriangular(LowerTriangular(randn(3,3))) # Issue 16196 -@test UpperTriangular(eye(3)) \ sub(ones(3), [1,2,3]) == ones(3) +@test UpperTriangular(eye(3)) \ view(ones(3), [1,2,3]) == ones(3) diff --git a/test/linalg/tridiag.jl b/test/linalg/tridiag.jl index 6b8881eaf1723..258569cb1fa9a 100644 --- a/test/linalg/tridiag.jl +++ b/test/linalg/tridiag.jl @@ -76,8 +76,8 @@ for elty in (Float32, Float64, Complex64, Complex128, Int) BB = Bs vv = vs else - BB = sub(Bs, 1:n, 1) - vv = sub(vs, 1:n) + BB = view(Bs, 1:n, 1) + vv = view(vs, 1:n) end end @@ -113,7 +113,7 @@ for elty in (Float32, Float64, Complex64, Complex128, Int) if atype == "Array" vv = vs else - vv = sub(vs, 1:n) + vv = view(vs, 1:n) end end invFsv = Fs\vv diff --git a/test/linalg/uniformscaling.jl b/test/linalg/uniformscaling.jl index b2ba92201c1e5..4d1c3e20d0ad8 100644 --- a/test/linalg/uniformscaling.jl +++ b/test/linalg/uniformscaling.jl @@ -43,8 +43,8 @@ let AA = randn(2, 2) A = AA S = SS else - A = sub(AA, 1:2, 1:2) - S = sub(SS, 1:3, 1:3) + A = view(AA, 1:2, 1:2) + S = view(SS, 1:3, 1:3) end @test A + I == A + eye(A) @@ -74,7 +74,7 @@ let AA = randn(2, 2) if atype == "Array" T = LowerTriangular(randn(3,3)) else - T = LowerTriangular(sub(randn(3,3), 1:3, 1:3)) + T = LowerTriangular(view(randn(3,3), 1:3, 1:3)) end @test T + J == full(T) + J @test J + T == J + full(T) @@ -85,7 +85,7 @@ let AA = randn(2, 2) if atype == "Array" T = LinAlg.UnitLowerTriangular(randn(3,3)) else - T = LinAlg.UnitLowerTriangular(sub(randn(3,3), 1:3, 1:3)) + T = LinAlg.UnitLowerTriangular(view(randn(3,3), 1:3, 1:3)) end @test T + J == full(T) + J @test J + T == J + full(T) @@ -96,7 +96,7 @@ let AA = randn(2, 2) if atype == "Array" T = UpperTriangular(randn(3,3)) else - T = UpperTriangular(sub(randn(3,3), 1:3, 1:3)) + T = UpperTriangular(view(randn(3,3), 1:3, 1:3)) end @test T + J == full(T) + J @test J + T == J + full(T) @@ -107,7 +107,7 @@ let AA = randn(2, 2) if atype == "Array" T = LinAlg.UnitUpperTriangular(randn(3,3)) else - T = LinAlg.UnitUpperTriangular(sub(randn(3,3), 1:3, 1:3)) + T = LinAlg.UnitUpperTriangular(view(randn(3,3), 1:3, 1:3)) end @test T + J == full(T) + J @test J + T == J + full(T) diff --git a/test/numbers.jl b/test/numbers.jl index 8e220dcaecf7a..217faa977d90a 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -2424,7 +2424,7 @@ end # Fill a pre allocated 2x4 matrix let a = zeros(Int,(2,4)) for i in 0:3 - digits!(sub(a,:,i+1),i,2) + digits!(view(a,:,i+1),i,2) end @test a == [0 1 0 1; 0 0 1 1] diff --git a/test/offsetarray.jl b/test/offsetarray.jl index d7bee902c22ac..caf19f96b77f6 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -105,7 +105,7 @@ using OAs # Basics A0 = [1 3; 2 4] A = OffsetArray(A0, (-1,2)) # LinearFast -S = OffsetArray(slice(A0, 1:2, 1:2), (-1,2)) # LinearSlow +S = OffsetArray(view(A0, 1:2, 1:2), (-1,2)) # LinearSlow @test indices(A) == indices(S) == (0:1, 3:4) @test A[0,3] == A[1] == S[0,3] == S[1] == 1 @test A[1,3] == A[2] == S[1,3] == S[2] == 2 @@ -137,26 +137,26 @@ S = OffsetArray(slice(A0, 1:2, 1:2), (-1,2)) # LinearSlow @test eachindex(S) == CartesianRange((0:1,3:4)) # slice -S = slice(A, :, 3) +S = view(A, :, 3) @test S == OffsetArray([1,2], (A.offsets[1],)) @test S[0] == 1 @test S[1] == 2 @test_throws BoundsError S[2] -S = slice(A, 0, :) +S = view(A, 0, :) @test S == OffsetArray([1,3], (A.offsets[2],)) @test S[3] == 1 @test S[4] == 3 @test_throws BoundsError S[1] -S = slice(A, 0:0, 4) +S = view(A, 0:0, 4) @test S == [3] @test S[1] == 3 @test_throws BoundsError S[0] -S = slice(A, 1, 3:4) +S = view(A, 1, 3:4) @test S == [2,4] @test S[1] == 2 @test S[2] == 4 @test_throws BoundsError S[3] -S = slice(A, :, :) +S = view(A, :, :) @test S == A @test S[0,3] == S[1] == 1 @test S[1,3] == S[2] == 2 diff --git a/test/perf/array/indexing.jl b/test/perf/array/indexing.jl index 948e7800269d0..7f6a559f3a33b 100644 --- a/test/perf/array/indexing.jl +++ b/test/perf/array/indexing.jl @@ -167,7 +167,7 @@ function makearrays{T}(::Type{T}, sz) Astrd1 = ArrayStrides1(A) outersz = (sz[1]+1,sz[2]+2) B = reshape(convert(Vector{T}, [1:prod(outersz);]), outersz) - Asub = sub(B, 1:sz[1], 2:sz[2]+1) + Asub = view(B, 1:sz[1], 2:sz[2]+1) Bit = trues(sz) (A, AF, AS, ASS, Asub, Bit,) end diff --git a/test/perf/kernel/laplace.jl b/test/perf/kernel/laplace.jl index 35d5fe783905d..73e3c4ed66915 100644 --- a/test/perf/kernel/laplace.jl +++ b/test/perf/kernel/laplace.jl @@ -40,7 +40,7 @@ end function laplace_iter_vec_sub(u, dx2, dy2, Niter, N) for i = 1:Niter - u[2:N-1, 2:N-1] = ((sub(u, 1:N-2, 2:N-1) + sub(u,3:N, 2:N-1))*dy2 + (sub(u,2:N-1,1:N-2) + sub(u,2:N-1, 3:N))*dx2) * (1./ (2*(dx2+dy2))) + u[2:N-1, 2:N-1] = ((view(u, 1:N-2, 2:N-1) + view(u,3:N, 2:N-1))*dy2 + (view(u,2:N-1,1:N-2) + view(u,2:N-1, 3:N))*dx2) * (1./ (2*(dx2+dy2))) end return u end diff --git a/test/reduce.jl b/test/reduce.jl index 723a85c39037d..8db11f0a4014a 100644 --- a/test/reduce.jl +++ b/test/reduce.jl @@ -277,11 +277,11 @@ end A = reshape(map(UInt8, 101:109), (3,3)) @test @inferred(sum(A)) == 945 -@test @inferred(sum(sub(A, 1:3, 1:3))) == 945 +@test @inferred(sum(view(A, 1:3, 1:3))) == 945 A = reshape(map(UInt8, 1:100), (10,10)) @test @inferred(sum(A)) == 5050 -@test @inferred(sum(sub(A, 1:10, 1:10))) == 5050 +@test @inferred(sum(view(A, 1:10, 1:10))) == 5050 # issue #11618 @test sum([-0.0]) === -0.0 diff --git a/test/serialize.jl b/test/serialize.jl index d7a1fd996e3b8..c7c9e7e3d3c34 100644 --- a/test/serialize.jl +++ b/test/serialize.jl @@ -227,9 +227,9 @@ end # SubArray create_serialization_stream() do s # slices - slc1 = slice(ones(UInt8, 4), 2:3) + slc1 = view(ones(UInt8, 4), 2:3) serialize(s, slc1) - slc2 = slice(ones(UInt8, 4, 4) .+ [0x00, 0x01, 0x02, 0x03], 1, 2:4) + slc2 = view(ones(UInt8, 4, 4) .+ [0x00, 0x01, 0x02, 0x03], 1, 2:4) serialize(s, slc2) seek(s, 0) @@ -251,7 +251,7 @@ Base.getindex(A::ArrayWrapper, i::Real...) = getindex(A.data, i...) end let A = rand(3,4) - for B in (sub(A, :, 2:4), slice(A, 2, 1:3)) + for B in (view(A, :, 2:4), view(A, 2, 1:3)) C = ArrayWrappers.ArrayWrapper(B) io = IOBuffer() serialize(io, C) diff --git a/test/sorting.jl b/test/sorting.jl index 2ec0d37095512..1a40b9223ab51 100644 --- a/test/sorting.jl +++ b/test/sorting.jl @@ -8,12 +8,12 @@ using Base.Order: Forward @test sort(['a':'z';], rev=true) == ['z':-1:'a';] @test sortperm([2,3,1]) == [3,1,2] @test sortperm!([1,2,3], [2,3,1]) == [3,1,2] -let s = sub([1,2,3,4], 1:3) +let s = view([1,2,3,4], 1:3) r = sortperm!(s, [2,3,1]) @test r == [3,1,2] @test r === s end -@test_throws ArgumentError sortperm!(sub([1,2,3,4], 1:4), [2,3,1]) +@test_throws ArgumentError sortperm!(view([1,2,3,4], 1:4), [2,3,1]) @test !issorted([2,3,1]) @test issorted([1,2,3]) @test reverse([2,3,1]) == [1,3,2] @@ -125,7 +125,7 @@ for alg in [InsertionSort, MergeSort] @test issorted(b) @test a[ix] == b - sortperm!(sub(ix, 1:100), sub(a, 1:100), alg=alg) + sortperm!(view(ix, 1:100), view(a, 1:100), alg=alg) b = a[ix][1:100] @test issorted(b) @@ -141,7 +141,7 @@ for alg in [InsertionSort, MergeSort] @test issorted(b, rev=true) @test a[ix] == b - sortperm!(sub(ix, 1:100), sub(a, 1:100), alg=alg, rev=true) + sortperm!(view(ix, 1:100), view(a, 1:100), alg=alg, rev=true) b = a[ix][1:100] @test issorted(b, rev=true) @@ -157,7 +157,7 @@ for alg in [InsertionSort, MergeSort] @test issorted(b, by=x->1/x) @test a[ix] == b - sortperm!(sub(ix, 1:100), sub(a, 1:100), by=x->1/x) + sortperm!(view(ix, 1:100), view(a, 1:100), by=x->1/x) b = a[ix][1:100] @test issorted(b, by=x->1/x) diff --git a/test/sparsedir/cholmod.jl b/test/sparsedir/cholmod.jl index 268f979cfeccb..6f4ca625c1f05 100644 --- a/test/sparsedir/cholmod.jl +++ b/test/sparsedir/cholmod.jl @@ -636,8 +636,8 @@ Fnew = deserialize(b) # test \ for Factor and StridedVecOrMat let x = rand(5) A = cholfact(sparse(diagm(x.\1))) - @test_approx_eq A\sub(ones(10),1:2:10) x - @test_approx_eq A\slice(eye(5,5),:,:) diagm(x) + @test_approx_eq A\view(ones(10),1:2:10) x + @test_approx_eq A\view(eye(5,5),:,:) diagm(x) end # Real factorization and complex rhs diff --git a/test/sparsedir/sparse.jl b/test/sparsedir/sparse.jl index 5f4ae28caae14..ce44b657f14dd 100644 --- a/test/sparsedir/sparse.jl +++ b/test/sparsedir/sparse.jl @@ -289,7 +289,7 @@ cA = sprandn(5,5,0.2) + im*sprandn(5,5,0.2) @test full(conj(cA)) == conj(full(cA)) # transpose of SubArrays -A = sub(sprandn(10, 10, 0.3), 1:4, 1:4) +A = view(sprandn(10, 10, 0.3), 1:4, 1:4) @test transpose(full(A)) == full(transpose(A)) @test ctranspose(full(A)) == full(ctranspose(A)) diff --git a/test/staged.jl b/test/staged.jl index 0a635ee1a1ba4..8f064b56b5e3a 100644 --- a/test/staged.jl +++ b/test/staged.jl @@ -66,7 +66,7 @@ splat3(A, 1:2, 1:2, 1) splat3(A, 1:2, 1, 1:2) @test takebuf_string(stagediobuf) == "(UnitRange{$intstr},$intstr,UnitRange{$intstr})" -B = slice(A, 1:3, 2, 1:3); +B = view(A, 1:3, 2, 1:3); @generated function mygetindex(S::SubArray, indexes::Real...) T, N, A, I = S.parameters if N != length(indexes) diff --git a/test/subarray.jl b/test/subarray.jl index 9df96e9ccce8b..f52578efdc1fd 100644 --- a/test/subarray.jl +++ b/test/subarray.jl @@ -173,19 +173,13 @@ function runsubarraytests(A::Array, I...) # Direct test of linear indexing inference C = Agen_nodrop(A, I...) ld = min(single_stride_dim(C), dim_break_linindex(I)) - # sub - S = sub(A, I...) + S = view(A, I...) if Base.iscontiguous(S) @test S.stride1 == 1 end test_linear(S, C) test_cartesian(S, C) test_mixed(S, C) - # slice - S = slice(A, I...) - test_linear(S, C) - test_cartesian(S, C) - test_mixed(S, C) end function runsubarraytests(A::ANY, I...) @@ -216,7 +210,7 @@ function runsubarraytests(A::ANY, I...) # sub local S try - S = sub(A, I...) + S = view(A, I...) catch err @show typeof(A) @show A.indexes @@ -228,7 +222,7 @@ function runsubarraytests(A::ANY, I...) test_mixed(S, C) # slice try - S = slice(A, I...) + S = view(A, I...) catch err @show typeof(A) @show A.indexes @@ -276,9 +270,9 @@ runviews{T}(SB::AbstractArray{T,0}, indexN, indexNN, indexNNN) = nothing testfull = Bool(parse(Int,(get(ENV, "JULIA_TESTFULL", "0")))) ### Views from Arrays ### -index5 = (1, :, 2:5, [4,1,5], reshape([2]), sub(1:5,[2 3 4 1])) # all work with at least size 5 -index25 = (3, :, 2:11, [19,9,7], reshape([10]), sub(1:25,[19 15; 4 24])) -index125 = (113, :, 85:121, [99,14,103], reshape([72]), sub(1:125,reshape([25,4,102,67], 1, 2, 2))) +index5 = (1, :, 2:5, [4,1,5], reshape([2]), view(1:5,[2 3 4 1])) # all work with at least size 5 +index25 = (3, :, 2:11, [19,9,7], reshape([10]), view(1:25,[19 15; 4 24])) +index125 = (113, :, 85:121, [99,14,103], reshape([72]), view(1:125,reshape([25,4,102,67], 1, 2, 2))) if testfull let A = copy(reshape(1:5*7*11, 11, 7, 5)) @@ -295,10 +289,8 @@ oindex = (:, 6, 3:7, reshape([12]), [8,4,6,12,5,7], [3:7 1:5 2:6 4:8 5:9]) if testfull let B = copy(reshape(1:13^3, 13, 13, 13)) for o3 in oindex, o2 in oindex, o1 in oindex - sliceB = slice(B, o1, o2, o3) - runviews(sliceB, index5, index25, index125) - subB = sub(B, o1, o2, o3) - runviews(subB, index5, index25, index125) + viewB = view(B, o1, o2, o3) + runviews(viewB, index5, index25, index125) end end end @@ -317,30 +309,28 @@ if !testfull (13:-2:1,:,:), ([8,4,6,12,5,7],:,3:7), (6,6,[8,4,6,12,5,7]), - (1,:,sub(1:13,[9,12,4,13,1])), - (sub(1:13,[9,12,4,13,1]),2:6,4), + (1,:,view(1:13,[9,12,4,13,1])), + (view(1:13,[9,12,4,13,1]),2:6,4), ([1:5 2:6 3:7 4:8 5:9], :, 3), (:, [46:-1:42 88:-1:84 22:-1:18 49:-1:45 8:-1:4])) runsubarraytests(B, oind...) - sliceB = slice(B, oind...) - runviews(sliceB, index5, index25, index125) - subB = sub(B, oind...) - runviews(subB, index5, index25, index125) + viewB = view(B, oind...) + runviews(viewB, index5, index25, index125) end end end # issue #11289 x11289 = randn(5,5) -@test isempty(sub(x11289, Int[], :)) -@test isempty(sub(x11289, [2,5], Int[])) -@test isempty(sub(x11289, Int[], 2)) +@test isempty(view(x11289, Int[], :)) +@test isempty(view(x11289, [2,5], Int[])) +@test isempty(view(x11289, Int[], 2)) ####### "Classical" tests ####### # sub A = copy(reshape(1:120, 3, 5, 8)) -sA = sub(A, 2, 1:5, :) +sA = view(A, 2:2, 1:5, :) @test strides(sA) == (1, 3, 15) @test parent(sA) == A @test parentindexes(sA) == (Base.NoSlice(2), 1:5, :) @@ -354,7 +344,7 @@ sA[2:5:end] = -1 @test stride(sA,3) == 15 @test stride(sA,4) == 120 test_bounds(sA) -sA = sub(A, 1:3, 1:5, 5) +sA = view(A, 1:3, 1:5, 5) @test Base.parentdims(sA) == [1:2;] sA[1:3,1:5] = -2 @test all(A[:,:,5] .== -2) @@ -362,34 +352,34 @@ sA[:] = -3 @test all(A[:,:,5] .== -3) @test strides(sA) == (1,3) test_bounds(sA) -sA = sub(A, 1:3, 3, 2:5) +sA = view(A, 1:3, 3:3, 2:5) @test Base.parentdims(sA) == [1:3;] @test size(sA) == (3,1,4) @test sA == A[1:3,3:3,2:5] @test sA[:] == A[1:3,3,2:5][:] test_bounds(sA) -sA = sub(A, 1:2:3, 1:3:5, 1:2:8) +sA = view(A, 1:2:3, 1:3:5, 1:2:8) @test Base.parentdims(sA) == [1:3;] @test strides(sA) == (2,9,30) @test sA[:] == A[1:2:3, 1:3:5, 1:2:8][:] # issue #8807 -@test sub(sub([1:5;], 1:5), 1:5) == [1:5;] +@test view(view([1:5;], 1:5), 1:5) == [1:5;] # Test with mixed types @test sA[:, Int16[1,2], big(2)] == [31 40; 33 42] test_bounds(sA) # sub logical indexing #4763 -A = sub([1:10;], 5:8) +A = view([1:10;], 5:8) @test A[A.<7] == [5, 6] @test Base.unsafe_getindex(A, A.<7) == [5, 6] B = reshape(1:16, 4, 4) -sB = sub(B, 2:3, 2:3) +sB = view(B, 2:3, 2:3) @test sB[sB.>8] == [10, 11] @test Base.unsafe_getindex(sB, sB.>8) == [10, 11] # slice A = copy(reshape(1:120, 3, 5, 8)) -sA = slice(A, 2, :, 1:8) +sA = view(A, 2, :, 1:8) @test parent(sA) == A @test parentindexes(sA) == (2, :, 1:8) @test Base.parentdims(sA) == [2:3;] @@ -402,12 +392,12 @@ sA[2:5:end] = -1 @test all(sA[2:5:end] .== -1) @test all(A[5:15:120] .== -1) test_bounds(sA) -sA = slice(A, 1:3, 1:5, 5) +sA = view(A, 1:3, 1:5, 5) @test Base.parentdims(sA) == [1:2;] @test size(sA) == (3,5) @test strides(sA) == (1,3) test_bounds(sA) -sA = slice(A, 1:2:3, 3, 1:2:8) +sA = view(A, 1:2:3, 3, 1:2:8) @test Base.parentdims(sA) == [1,3] @test size(sA) == (2,4) @test strides(sA) == (2,30) @@ -422,56 +412,56 @@ a = [5:8;] A = rand(2, 2, 3) msk = ones(Bool, 2, 2) msk[2,1] = false -sA = sub(A, :, :, 1) +sA = view(A, :, :, 1) sA[msk] = 1.0 @test sA[msk] == ones(countnz(msk)) # bounds checking upon construction; see #4044, #10296 -@test_throws BoundsError sub(1:10, 8:11) +@test_throws BoundsError view(1:10, 8:11) A = reshape(1:20, 5, 4) -sA = sub(A, 1:2, 1:3) -@test_throws BoundsError sub(sA, 1:3, 1:3) -@test_throws BoundsError sub(sA, 1:2, 1:4) -sub(sA, 1:2, 1:2) -@test_throws BoundsError sub(A, 17:23) -sub(A, 17:20) +sA = view(A, 1:2, 1:3) +@test_throws BoundsError view(sA, 1:3, 1:3) +@test_throws BoundsError view(sA, 1:2, 1:4) +view(sA, 1:2, 1:2) +@test_throws BoundsError view(A, 17:23) +view(A, 17:20) # Linear indexing by one multidimensional array: A = reshape(1:120, 3, 5, 8) -sA = sub(A, :, :, :) +sA = view(A, :, :, :) @test sA[[72 17; 107 117]] == [72 17; 107 117] @test sA[[99 38 119 14 76 81]] == [99 38 119 14 76 81] @test sA[[ones(Int, 2, 2, 2); 2ones(Int, 2, 2, 2)]] == [ones(Int, 2, 2, 2); 2ones(Int, 2, 2, 2)] -sA = sub(A, 1:2, 2:3, 3:4) +sA = view(A, 1:2, 2:3, 3:4) @test sA[(1:8)'] == [34 35 37 38 49 50 52 53] @test sA[[1 2 4 4; 6 1 1 4]] == [34 35 38 38; 50 34 34 38] # issue #11871 let a = ones(Float64, (2,2)), - b = sub(a, 1:2, 1:2) + b = view(a, 1:2, 1:2) b[2] = 2 @test b[2] === 2.0 end # issue #15138 let a = [1,2,3], - b = sub(a, UInt(1):UInt(2)) - @test b == slice(a, UInt(1):UInt(2)) == slice(slice(a, :), UInt(1):UInt(2)) == [1,2] + b = view(a, UInt(1):UInt(2)) + @test b == view(a, UInt(1):UInt(2)) == view(view(a, :), UInt(1):UInt(2)) == [1,2] end let A = reshape(1:4, 2, 2) - B = sub(A, :, :) + B = view(A, :, :) @test parent(B) === A - @test parent(sub(B, 0x1, :)) === parent(slice(B, 0x1, :)) === A + @test parent(view(B, 0x1, :)) === parent(view(B, 0x1, :)) === A end # issue #15168 -let A = rand(10), sA = sub(copy(A), :) +let A = rand(10), sA = view(copy(A), :) @test sA[Int16(1)] === sA[Int32(1)] === sA[Int64(1)] === A[1] permute!(sA, collect(Int16, 1:10)) @test A == sA end # the following segfaults with LLVM 3.8 on Windows, ref #15417 -@test collect(sub(sub(reshape(1:13^3, 13, 13, 13), 3:7, 6, :), 1:2:5, :, 1:2:5)) == +@test collect(view(view(reshape(1:13^3, 13, 13, 13), 3:7, 6, :), 1:2:5, :, 1:2:5)) == cat(3,[68,70,72],[406,408,410],[744,746,748]) diff --git a/test/unicode/utf32.jl b/test/unicode/utf32.jl index 875c22330749d..d1ea8b4a690db 100644 --- a/test/unicode/utf32.jl +++ b/test/unicode/utf32.jl @@ -198,7 +198,7 @@ for (fun, S, T) in ((utf16, UInt16, UTF16String), (utf32, UInt32, UTF32String)) @test_throws UnicodeError map(islower, x) @test_throws ArgumentError map(islower, tst) # SubArray conversion - subarr = sub(cmp, 1:6) + subarr = view(cmp, 1:6) @test convert(T, subarr) == str[4:end] end