Skip to content

Commit

Permalink
Fix sub_materialize for Padded (#83)
Browse files Browse the repository at this point in the history
* Fix sub_materialize for Padded

* Update Project.toml
  • Loading branch information
dlfivefifty authored Nov 24, 2019
1 parent 803400b commit 3c87082
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "LazyArrays"
uuid = "5078a376-72f3-5289-bfd5-ec5146d43c02"
version = "0.14.8"
version = "0.14.9"


[deps]
Expand Down
43 changes: 34 additions & 9 deletions src/lazyconcat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,10 @@ arguments(::ApplyLayout{typeof(vcat)}, V::SubArray{<:Any,2,<:Any,<:Tuple{<:Slice
arguments(::ApplyLayout{typeof(hcat)}, V::SubArray{<:Any,2,<:Any,<:Tuple{<:Any,<:Slice}}) =
view.(arguments(parent(V)), Ref(parentindices(V)[1]), Ref(:))

copyto!(dest::AbstractArray{T,N}, src::SubArray{T,N,<:Vcat{T,N}}) where {T,N} = vcat_copyto!(dest, arguments(src)...)
copyto!(dest::AbstractMatrix{T}, src::SubArray{T,2,<:Hcat{T}}) where T = hcat_copyto!(dest, arguments(src)...)
copyto!(dest::AbstractArray{T,N}, src::SubArray{T,N,<:Vcat{T,N}}) where {T,N} =
vcat_copyto!(dest, arguments(ApplyLayout{typeof(vcat)}(), src)...)
copyto!(dest::AbstractMatrix{T}, src::SubArray{T,2,<:Hcat{T}}) where T =
hcat_copyto!(dest, arguments(ApplyLayout{typeof(hcat)}(), src)...)


_vcat_lastinds(sz) = _vcat_cumsum(sz...)
Expand All @@ -597,32 +599,35 @@ function _vcat_sub_arguments(::ApplyLayout{typeof(vcat)}, A, V)
_view_vcat.(arguments(A), skr2)
end
_vcat_sub_arguments(::ApplyLayout{typeof(hcat)}, A, V) = arguments(ApplyLayout{typeof(hcat)}(), V)
_vcat_sub_arguments(::PaddedLayout, A, V) = _vcat_sub_arguments(ApplyLayout{typeof(vcat)}(), A, V)

_vcat_sub_arguments(A, V) = _vcat_sub_arguments(MemoryLayout(typeof(A)), A, V)
arguments(::ApplyLayout{typeof(vcat)}, V::SubArray{<:Any,1}) = _vcat_sub_arguments(parent(V), V)

function arguments(::ApplyLayout{typeof(vcat)}, V::SubArray{<:Any,2})
function arguments(L::ApplyLayout{typeof(vcat)}, V::SubArray{<:Any,2})
A = parent(V)
args = arguments(L, A)
kr,jr = parentindices(V)
sz = size.(arguments(A),1)
sz = size.(args,1)
skr = intersect.(_argsindices(sz), Ref(kr))
skr2 = broadcast((a,b) -> a .- b .+ 1, skr, _vcat_firstinds(sz))
_view_vcat.(arguments(A), skr2, Ref(jr))
_view_vcat.(args, skr2, Ref(jr))
end

_view_hcat(a::Number, kr, jr) = Fill(a,length(kr),length(jr))
_view_hcat(a, kr, jr) = view(a, kr, jr)

function arguments(::ApplyLayout{typeof(hcat)}, V::SubArray)
function arguments(L::ApplyLayout{typeof(hcat)}, V::SubArray)
A = parent(V)
args = arguments(L, A)
kr,jr = parentindices(V)
sz = size.(arguments(A),2)
sz = size.(args,2)
sjr = intersect.(_argsindices(sz), Ref(jr))
sjr2 = broadcast((a,b) -> a .- b .+ 1, sjr, _vcat_firstinds(sz))
_view_hcat.(arguments(A), Ref(kr), sjr2)
_view_hcat.(args, Ref(kr), sjr2)
end

function sub_materialize(::ApplyLayout{typeof(vcat)}, V)
function sub_materialize(::ApplyLayout{typeof(vcat)}, V::AbstractMatrix)
ret = similar(V)
n = 0
_,jr = parentindices(V)
Expand Down Expand Up @@ -652,6 +657,26 @@ materialize!(M::MatMulMatAdd{<:AbstractColumnMajor,<:ApplyLayout{typeof(vcat)}})
materialize!(M::MatMulVecAdd{<:AbstractColumnMajor,<:ApplyLayout{typeof(vcat)}}) =
materialize!(MulAdd(M.α,M.A,Array(M.B),M.β,M.C))

sublayout(::PaddedLayout{L}, ::Type{I}) where {L,I<:Tuple{AbstractUnitRange}} =
PaddedLayout{typeof(sublayout(L(), I))}()
sublayout(::PaddedLayout{L}, ::Type{I}) where {L,I<:Tuple{AbstractUnitRange,AbstractUnitRange}} =
PaddedLayout{typeof(sublayout(L(), I))}()

function sub_materialize(::PaddedLayout, v::AbstractVector{T}) where T
A = parent(v)
dat = paddeddata(A)
(kr,) = parentindices(v)
kr2 = kr axes(dat,1)
Vcat(lazy_getindex(dat, kr2), Zeros{T}(length(kr) - length(kr2)))
end

function sub_materialize(::PaddedLayout, v::AbstractMatrix{T}) where T
A = parent(v)
dat = paddeddata(A)
kr,jr = parentindices(v)
kr2 = kr axes(dat,1)
Vcat(lazy_getindex(dat, kr2, jr), Zeros{T}(length(kr) - length(kr2), length(jr)))
end

## print

Expand Down
17 changes: 17 additions & 0 deletions test/concattests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,21 @@ import LazyArrays: MemoryLayout, DenseColumnMajor, PaddedLayout, materialize!,
@test arguments(v) == ([2], zeros(4))
@test A[2,1:5] == copy(v) == sub_materialize(v)
end

@testset "Padded subarrays" begin
a = Vcat([1,2,3],[4,5,6])
@test sub_materialize(view(a,2:6)) == a[2:6]
a = Vcat([1,2,3], Zeros(10))
c = cache(Zeros(10)); c[1:3] = 1:3;
v = view(a,2:4)
w = view(c,2:4);
@test MemoryLayout(typeof(a)) isa PaddedLayout{DenseColumnMajor}
@test MemoryLayout(typeof(v)) isa PaddedLayout{DenseColumnMajor}
@test sub_materialize(v) == a[2:4] == sub_materialize(w)
@test sub_materialize(v) isa Vcat
@test sub_materialize(w) isa Vcat
A = Vcat(Eye(2), Zeros(10,2))
V = view(A, 1:5, 1:2)
@test sub_materialize(V) == A[1:5,1:2]
end
end

0 comments on commit 3c87082

Please sign in to comment.