Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,9 @@ end
"""
strides(A)

Return a tuple of the memory strides in each dimension.
Return a tuple of the memory strides in each dimension, for an `AbstractArray` with a
strided memory layout. For arrays with a non-strided layout (such as sparse arrays), return
`nothing`.

See also: [`stride`](@ref).

Expand All @@ -571,7 +573,7 @@ julia> strides(A)
(1, 3, 12)
```
"""
function strides end
strides(::AbstractArray) = nothing

"""
stride(A, k::Integer)
Expand All @@ -592,15 +594,14 @@ julia> stride(A,3)
```
"""
function stride(A::AbstractArray, k::Integer)
st = strides(A)
k ≤ ndims(A) && return st[k]
ndims(A) == 0 && return 1
sz = size(A)
s = st[1] * sz[1]
for i in 2:ndims(A)
s += st[i] * sz[i]
str = strides(A)
if str === nothing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More compact, if desired:

return str === nothing ? nothing : str[k]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing function allows k>ndims(A), like stride([1,2,3], 99) == 3.

return nothing
else
k ≤ ndims(A) && return st[k]
ndims(A) == 0 && return 1
return sum(st .* size(A))
end
return s
end

@inline size_to_strides(s, d, sz...) = (s, size_to_strides(s * d, sz...)...)
Expand Down
6 changes: 5 additions & 1 deletion base/permuteddimsarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ Base.pointer(A::PermutedDimsArray, i::Integer) = throw(ArgumentError("pointer(A,

function Base.strides(A::PermutedDimsArray{T,N,perm}) where {T,N,perm}
s = strides(parent(A))
ntuple(d->s[perm[d]], Val(N))
if s === nothing
return nothing
else
return ntuple(d->s[perm[d]], Val(N))
end
end
Base.elsize(::Type{<:PermutedDimsArray{<:Any, <:Any, <:Any, <:Any, P}}) where {P} = Base.elsize(P)

Expand Down
19 changes: 19 additions & 0 deletions stdlib/LinearAlgebra/src/adjtrans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,25 @@ parent(A::AdjOrTrans) = A.parent
vec(v::TransposeAbsVec{<:Number}) = parent(v)
vec(v::AdjointAbsVec{<:Real}) = parent(v)

# provide strides, but only for eltypes that are directly stored in memory (i.e. unaffected
# by recursive `adjoint` and `transpose`, being `Real` and `Number` respectively)
function Base.strides(a::Union{Adjoint{<:Real, <:AbstractVector}, Transpose{<:Number, <:AbstractVector}})
str = strides(a.parent)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use A and strides(parent(A))?

if str === nothing
return nothing
else
return (1, str[1])
end
end
function Base.strides(a::Union{Adjoint{<:Real, <:AbstractMatrix}, Transpose{<:Number, <:AbstractMatrix}})
str = strides(a.parent)
if str === nothing
return nothing
else
return (str[2], str[1])
end
end

### concatenation
# preserve Adjoint/Transpose wrapper around vectors
# to retain the associated semantics post-concatenation
Expand Down
Loading