Skip to content

Commit 2f2c7ac

Browse files
authored
Improve sublayout for DualLayout (#39)
* Improve support for subarrays with DualLayout * v0.4.6 * Update ArrayLayouts.jl * Increase coverage * Update runtests.jl
1 parent 1cde449 commit 2f2c7ac

File tree

5 files changed

+37
-22
lines changed

5 files changed

+37
-22
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ArrayLayouts"
22
uuid = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
33
authors = ["Sheehan Olver <[email protected]>"]
4-
version = "0.4.5"
4+
version = "0.4.6"
55

66
[deps]
77
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"

src/ArrayLayouts.jl

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -163,29 +163,25 @@ _copyto!(_, _, dest::AbstractArray{T,N}, src::AbstractArray{V,N}) where {T,V,N}
163163
Base.invoke(copyto!, Tuple{AbstractArray{T,N},AbstractArray{V,N}}, dest, src)
164164

165165

166-
copyto!(dest::LayoutArray{<:Any,N}, src::LayoutArray{<:Any,N}) where N =
167-
_copyto!(MemoryLayout(dest), MemoryLayout(src), dest, src)
168-
copyto!(dest::AbstractArray{<:Any,N}, src::LayoutArray{<:Any,N}) where N =
169-
_copyto!(MemoryLayout(dest), MemoryLayout(src), dest, src)
170-
copyto!(dest::LayoutArray{<:Any,N}, src::AbstractArray{<:Any,N}) where N =
171-
_copyto!(MemoryLayout(dest), MemoryLayout(src), dest, src)
172-
173-
copyto!(dest::SubArray{<:Any,N,<:LayoutArray}, src::SubArray{<:Any,N,<:LayoutArray}) where N =
174-
_copyto!(MemoryLayout(dest), MemoryLayout(src), dest, src)
175-
copyto!(dest::SubArray{<:Any,N,<:LayoutArray}, src::LayoutArray{<:Any,N}) where N =
176-
_copyto!(MemoryLayout(dest), MemoryLayout(src), dest, src)
177-
copyto!(dest::LayoutArray{<:Any,N}, src::SubArray{<:Any,N,<:LayoutArray}) where N =
178-
_copyto!(MemoryLayout(dest), MemoryLayout(src), dest, src)
179-
copyto!(dest::SubArray{<:Any,N,<:LayoutArray}, src::AbstractArray{<:Any,N}) where N =
180-
_copyto!(MemoryLayout(dest), MemoryLayout(src), dest, src)
181-
copyto!(dest::AbstractArray{<:Any,N}, src::SubArray{<:Any,N,<:LayoutArray}) where N =
182-
_copyto!(MemoryLayout(dest), MemoryLayout(src), dest, src)
166+
_copyto!(dest, src) = _copyto!(MemoryLayout(dest), MemoryLayout(src), dest, src)
167+
copyto!(dest::LayoutArray{<:Any,N}, src::LayoutArray{<:Any,N}) where N = _copyto!(dest, src)
168+
copyto!(dest::AbstractArray{<:Any,N}, src::LayoutArray{<:Any,N}) where N = _copyto!(dest, src)
169+
copyto!(dest::LayoutArray{<:Any,N}, src::AbstractArray{<:Any,N}) where N = _copyto!(dest, src)
170+
171+
copyto!(dest::SubArray{<:Any,N,<:LayoutArray}, src::SubArray{<:Any,N,<:LayoutArray}) where N = _copyto!(dest, src)
172+
copyto!(dest::SubArray{<:Any,N,<:LayoutArray}, src::LayoutArray{<:Any,N}) where N = _copyto!(dest, src)
173+
copyto!(dest::LayoutArray{<:Any,N}, src::SubArray{<:Any,N,<:LayoutArray}) where N = _copyto!(dest, src)
174+
copyto!(dest::SubArray{<:Any,N,<:LayoutArray}, src::AbstractArray{<:Any,N}) where N = _copyto!(dest, src)
175+
copyto!(dest::AbstractArray{<:Any,N}, src::SubArray{<:Any,N,<:LayoutArray}) where N = _copyto!(dest, src)
176+
177+
copyto!(dest::LayoutMatrix, src::AdjOrTrans{<:Any,<:LayoutArray}) = _copyto!(dest, src)
178+
copyto!(dest::LayoutMatrix, src::SubArray{<:Any,2,<:AdjOrTrans{<:Any,<:LayoutArray}}) = _copyto!(dest, src)
179+
copyto!(dest::AbstractMatrix, src::AdjOrTrans{<:Any,<:LayoutArray}) = _copyto!(dest, src)
180+
copyto!(dest::AbstractMatrix, src::SubArray{<:Any,2,<:AdjOrTrans{<:Any,<:LayoutArray}}) = _copyto!(dest, src)
183181
# ambiguity from sparsematrix.jl
184182
if VERSION v"1.5"
185-
copyto!(dest::LayoutMatrix, src::SparseArrays.AbstractSparseMatrixCSC) =
186-
_copyto!(MemoryLayout(dest), MemoryLayout(src), dest, src)
187-
copyto!(dest::SubArray{<:Any,2,<:LayoutMatrix}, src::SparseArrays.AbstractSparseMatrixCSC) =
188-
_copyto!(MemoryLayout(dest), MemoryLayout(src), dest, src)
183+
copyto!(dest::LayoutMatrix, src::SparseArrays.AbstractSparseMatrixCSC) = _copyto!(dest, src)
184+
copyto!(dest::SubArray{<:Any,2,<:LayoutMatrix}, src::SparseArrays.AbstractSparseMatrixCSC) = _copyto!(dest, src)
189185
end
190186

191187
zero!(A::AbstractArray{T}) where T = fill!(A,zero(T))

src/memorylayout.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,14 @@ transposelayout(::RowMajor) = ColumnMajor()
291291
transposelayout(::DenseColumnMajor) = DenseRowMajor()
292292
transposelayout(::DenseRowMajor) = DenseColumnMajor()
293293
transposelayout(::ConjLayout{ML}) where ML = ConjLayout{typeof(transposelayout(ML()))}()
294+
adjointlayout(::Type{T}, ::DualLayout{ML}) where {T,ML} = adjointlayout(T, ML())
294295
adjointlayout(::Type{T}, M::MemoryLayout) where T = transposelayout(conjlayout(T, M))
296+
sublayout(::DualLayout{ML}, ::Type{<:Tuple{KR,JR}}) where {ML,KR<:Slice,JR} = DualLayout{typeof(sublayout(ML(),Tuple{KR,JR}))}()
297+
sublayout(::DualLayout{ML}, INDS::Type) where ML = sublayout(ML(), INDS)
298+
sub_materialize(::DualLayout{ML}, A) where ML = sub_materialize(adjointlayout(eltype(A), ML()), A')'
295299

300+
_copyto!(dlay, ::DualLayout{ML}, dest::AbstractArray{T,N}, src::AbstractArray{V,N}) where {T,V,N,ML} =
301+
_copyto!(dlay, ML(), dest, src)
296302

297303
# Layouts of PermutedDimsArrays
298304
"""

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ MemoryLayout(::Type{MyVector}) = DenseColumnMajor()
6969
@test copyto!(MyMatrix(Array{Float64}(undef,3,3)), view(A,1:3,1:3)) == A[1:3,1:3]
7070
@test copyto!(view(MyMatrix(Array{Float64}(undef,5,5)),:,:), A.A) == A
7171
@test copyto!(Array{Float64}(undef,3,3), view(A,1:3,1:3)) == A[1:3,1:3]
72+
@test copyto!(MyMatrix(Array{Float64}(undef,5,5)), A') == A'
73+
@test copyto!(MyMatrix(Array{Float64}(undef,5,5)), view(A',:,:)) == A'
74+
@test copyto!(Array{Float64}(undef,5,5), A') == A'
75+
@test copyto!(Array{Float64}(undef,5,5), view(A',:,:)) == A'
7276

7377
@test qr(A).factors qr(A.A).factors
7478
@test qr(A,Val(true)).factors qr(A.A,Val(true)).factors

test/test_layouts.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ struct FooNumber <: Number end
8383
a = randn(5)
8484
@test MemoryLayout(a') isa DualLayout{DenseRowMajor}
8585
@test MemoryLayout(transpose(a)) isa DualLayout{DenseRowMajor}
86+
@test MemoryLayout(view(a',:,1:3)) isa DualLayout{RowMajor}
87+
@test MemoryLayout(view(a',1,1:3)) isa DenseColumnMajor
88+
@test MemoryLayout(view(a',1:1,1:3)) isa RowMajor
89+
@test (a')[:,1:3] == layout_getindex(a',:,1:3)
90+
@test (a')[1:1,1:3] == layout_getindex(a',1:1,1:3)
91+
@test layout_getindex(a',:,1:3) isa Adjoint
92+
@test layout_getindex(a',1:1,1:3) isa Array
93+
94+
@test ArrayLayouts._copyto!(similar(a'), a') == a'
8695
end
8796
end
8897

0 commit comments

Comments
 (0)