Skip to content

Commit

Permalink
Merge pull request #16972 from JuliaLang/sb/sub2view
Browse files Browse the repository at this point in the history
rename sub/slice -> view
  • Loading branch information
JeffBezanson committed Jun 18, 2016
2 parents 9840b23 + 9ab7c8f commit 9d2a40b
Show file tree
Hide file tree
Showing 62 changed files with 280 additions and 362 deletions.
4 changes: 2 additions & 2 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 2 additions & 10 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5383,14 +5383,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
Expand Down Expand Up @@ -7408,14 +7408,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()
Expand Down
3 changes: 1 addition & 2 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,6 @@ export
shuffle,
shuffle!,
size,
slice,
slicedim,
sort!,
sort,
Expand All @@ -601,7 +600,6 @@ export
step,
stride,
strides,
sub,
sub2ind,
sum!,
sum,
Expand All @@ -612,6 +610,7 @@ export
sum_kbn,
vcat,
vec,
view,
zeros,

# linear algebra
Expand Down
2 changes: 1 addition & 1 deletion base/iterator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions base/linalg/arnoldi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,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
Expand Down
6 changes: 3 additions & 3 deletions base/linalg/arpack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions base/linalg/cholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions base/linalg/eigen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion base/linalg/lapack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 19 additions & 19 deletions base/linalg/qr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)))
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion base/linalg/svd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion base/poll.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions base/sharedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1479,7 +1479,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
Expand Down
4 changes: 2 additions & 2 deletions base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,:]
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion base/sparse/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions base/sparse/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3127,7 +3127,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

Expand Down
Loading

0 comments on commit 9d2a40b

Please sign in to comment.