Skip to content

Commit

Permalink
Maintain Transpose/Adjoint types and unroll subarrays when materialis… (
Browse files Browse the repository at this point in the history
#136)

* Maintain Transpose/Adjoint types and unroll subarrays when materialising DualLayout

* add tests

* Update ci.yml

* Increase coverage
  • Loading branch information
dlfivefifty authored Jun 8, 2023
1 parent 35a135b commit 51b7289
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ jobs:
version:
- '1.6'
- '1'
- '^1.9.0-0'
os:
- ubuntu-latest
- macOS-latest
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ArrayLayouts"
uuid = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
authors = ["Sheehan Olver <solver@mac.com>"]
version = "1.0.4"
version = "1.0.5"

[deps]
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
Expand Down
9 changes: 8 additions & 1 deletion src/memorylayout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,14 @@ adjointlayout(::Type{T}, ::DualLayout{ML}) where {T,ML} = adjointlayout(T, ML())
adjointlayout(::Type{T}, M::MemoryLayout) where T = transposelayout(conjlayout(T, M))
sublayout(::DualLayout{ML}, ::Type{<:Tuple{KR,JR}}) where {ML,KR<:Slice,JR} = DualLayout{typeof(sublayout(ML(),Tuple{KR,JR}))}()
sublayout(::DualLayout{ML}, INDS::Type) where ML = sublayout(ML(), INDS)
sub_materialize(::DualLayout{ML}, A) where ML = sub_materialize(adjointlayout(eltype(A), ML()), A')'

# try to maintain "type" of parent ie adjoint/transpose when materialising, even though layouts are equivalent
# to preserve special overloads
_dual_adjoint(a::SubArray{<:Any, 2, <:Any, <:Tuple{Slice,Any}}) = view(parent(a)', parentindices(a)[2])
_dual_transpose(a::SubArray{<:Any, 2, <:Any, <:Tuple{Slice,Any}}) = view(transpose(parent(a)), parentindices(a)[2])
sub_materialize(::DualLayout{ML}, A::AbstractMatrix{<:Real}) where ML = sub_materialize(adjointlayout(eltype(A), ML()), _dual_adjoint(A))'
sub_materialize(::DualLayout{ML}, A::AbstractMatrix) where ML<:ConjLayout = sub_materialize(adjointlayout(eltype(A), ML()), _dual_adjoint(A))'
sub_materialize(::DualLayout{ML}, A::AbstractMatrix) where ML = transpose(sub_materialize(transposelayout(ML()), _dual_transpose(A)))

_copyto!(dlay, ::DualLayout{ML}, dest::AbstractArray{T,N}, src::AbstractArray{V,N}) where {T,V,N,ML} =
_copyto!(dlay, ML(), dest, src)
Expand Down
13 changes: 12 additions & 1 deletion test/test_layouts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,14 @@ struct FooNumber <: Number end
@test (a')[:,1:3] == layout_getindex(a',:,1:3)
@test (a')[1:1,1:3] == layout_getindex(a',1:1,1:3)
@test layout_getindex(a',:,1:3) isa Adjoint
@test layout_getindex(transpose(a),:,1:3) isa Adjoint
@test layout_getindex((a .+ im)',:,1:3) isa Adjoint
@test layout_getindex(transpose(a .+ im),:,1:3) isa Transpose
@test layout_getindex(a',1:1,1:3) isa Array

@test layout_getindex((a .+ im)',:,1:3) == (a .+ im)[1:3]'
@test layout_getindex(transpose(a .+ im),:,1:3) == transpose((a .+ im)[1:3])

@test ArrayLayouts._copyto!(similar(a'), a') == a'
end
end
Expand Down Expand Up @@ -181,7 +187,9 @@ struct FooNumber <: Number end
@test colsupport(Symmetric(A),2) colsupport(Symmetric(A),1:2)
rowsupport(Symmetric(A),2) rowsupport(Symmetric(A),1:2) 1:2
@test colsupport(Hermitian(A),2) colsupport(Hermitian(A),1:2)
rowsupport(Hermitian(A),2) rowsupport(Hermitian(A),1:2) 1:2
rowsupport(Hermitian(A),2) rowsupport(Hermitian(A),1:2)
colsupport(Symmetric(A,:L),2) colsupport(Hermitian(A,:L),2)
rowsupport(Symmetric(A,:L),2) rowsupport(Hermitian(A,:L),2) 1:2

B = [1.0+im 2; 3 4]
@test MemoryLayout(Symmetric(B)) == SymmetricLayout{DenseColumnMajor}()
Expand Down Expand Up @@ -299,6 +307,9 @@ struct FooNumber <: Number end
@test layout_getindex(Ones{Int}(1,10), 1, 1:3) Ones{Int}(3)
@test layout_getindex(Zeros{Int}(5,10,12), 1, 1:3,4:6) Zeros{Int}(3,3)

@test isempty(colsupport(Zeros(5,10), 2))
@test isempty(rowsupport(Zeros(5,10), 2))

# views of Fill no longer create Sub Arrays, but are supported
# as there was no strong need to delete their support
v = SubArray(Fill(1,10),(1:3,))
Expand Down

2 comments on commit 51b7289

@dlfivefifty
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/85142

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.5 -m "<description of version>" 51b72892c20d4218bd3b557265aff942c52f3e6b
git push origin v1.0.5

Please sign in to comment.