Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only Vcats of AbstractRange can be guaranteed to be vcat #357

Merged
merged 2 commits into from
Dec 7, 2024
Merged
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
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 = "2.3"
version = "2.3.1"

[deps]
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
Expand Down
8 changes: 4 additions & 4 deletions ext/LazyArraysBandedMatricesExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@
for op in (:+, :-, :*)
@eval begin
@inline _BandedMatrix(::BroadcastBandedLayout{typeof($op)}, V::AbstractMatrix)::BandedMatrix = broadcast($op, map(_broadcast_BandedMatrix,arguments(V))...)
copyto!_layout(::AbstractBandedLayout, ::BroadcastBandedLayout{typeof($op)}, dest::AbstractMatrix, src::AbstractMatrix) =
broadcast!($op, dest, map(_broadcast_BandedMatrix, arguments(src))...)
copyto!_layout(::AbstractBandedLayout, srclay::BroadcastBandedLayout{typeof($op)}, dest::AbstractMatrix, src::AbstractMatrix) =

Check warning on line 314 in ext/LazyArraysBandedMatricesExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/LazyArraysBandedMatricesExt.jl#L314

Added line #L314 was not covered by tests
broadcast!($op, dest, map(_broadcast_BandedMatrix, arguments(srclay, src))...)
end
end

Expand All @@ -325,8 +325,8 @@
_mulbanded_BandedMatrix(A, ::NTuple{2,OneTo{Int}}) = BandedMatrix(A)
_mulbanded_BandedMatrix(A) = _mulbanded_BandedMatrix(A, axes(A))

copyto!_layout(::AbstractBandedLayout, ::ApplyBandedLayout{typeof(*)}, dest::AbstractMatrix, src::AbstractMatrix) =
_mulbanded_copyto!(dest, map(_mulbanded_BandedMatrix,arguments(src))...)
copyto!_layout(::AbstractBandedLayout, srclay::ApplyBandedLayout{typeof(*)}, dest::AbstractMatrix, src::AbstractMatrix) =

Check warning on line 328 in ext/LazyArraysBandedMatricesExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/LazyArraysBandedMatricesExt.jl#L328

Added line #L328 was not covered by tests
_mulbanded_copyto!(dest, map(_mulbanded_BandedMatrix,arguments(srclay,src))...)

arguments(::BroadcastBandedLayout{F}, V::SubArray) where F = _broadcast_sub_arguments(V)

Expand Down
27 changes: 16 additions & 11 deletions src/lazyconcat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -786,10 +786,10 @@
# subarrays
###

sublayout(::ApplyLayout{typeof(vcat)}, ::Type{<:Tuple{Vararg{Union{AbstractVector{Int},Int}}}}) = ApplyLayout{typeof(vcat)}()
sublayout(::ApplyLayout{typeof(hcat)}, ::Type{<:Tuple{Vararg{Union{AbstractVector{Int},Int}}}}) = ApplyLayout{typeof(hcat)}()
sublayout(::ApplyLayout{typeof(vcat)}, ::Type{<:Tuple{Vararg{Union{AbstractRange{Int},Int}}}}) = ApplyLayout{typeof(vcat)}()
sublayout(::ApplyLayout{typeof(hcat)}, ::Type{<:Tuple{Vararg{Union{AbstractRange{Int},Int}}}}) = ApplyLayout{typeof(hcat)}()

Check warning on line 790 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L789-L790

Added lines #L789 - L790 were not covered by tests
# a row-slice of an Hcat is equivalent to a Vcat
sublayout(::ApplyLayout{typeof(hcat)}, ::Type{<:Tuple{Int,AbstractVector{Int}}}) = ApplyLayout{typeof(vcat)}()
sublayout(::ApplyLayout{typeof(hcat)}, ::Type{<:Tuple{Int,AbstractRange{Int}}}) = ApplyLayout{typeof(vcat)}()

Check warning on line 792 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L792

Added line #L792 was not covered by tests

_vcat_lastinds(sz) = _vcat_cumsum(sz...)
_vcat_firstinds(sz) = (1, (1 .+ Base.front(_vcat_lastinds(sz)))...)
Expand All @@ -800,18 +800,21 @@
_view_vcat(a::Number, kr, jr) = Fill(a,length(kr), length(jr))
_view_vcat(a, kr...) = _viewifmutable(a, kr...)

function _vcat_sub_arguments(::ApplyLayout{typeof(vcat)}, A, V, kr)
sz = size.(arguments(A),1)
_reverse_if_neg_step(args, kr::AbstractUnitRange) = args
_reverse_if_neg_step(args, kr::AbstractRange) = step(kr) ≥ 0 ? args : reverse(args)

Check warning on line 804 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L803-L804

Added lines #L803 - L804 were not covered by tests

function _vcat_sub_arguments(lay::ApplyLayout{typeof(vcat)}, A, V, kr)
sz = size.(arguments(lay, A),1)

Check warning on line 807 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L806-L807

Added lines #L806 - L807 were not covered by tests
skr = intersect.(_argsindices(sz), Ref(kr))
skr2 = broadcast((a,b) -> a .- b .+ 1, skr, _vcat_firstinds(sz))
map(_view_vcat, arguments(A), skr2)
_reverse_if_neg_step(map(_view_vcat, arguments(lay, A), skr2), kr)

Check warning on line 810 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L810

Added line #L810 was not covered by tests
end

function _vcat_sub_arguments(::ApplyLayout{typeof(vcat)}, A, V, kr, jr)
sz = size.(arguments(A),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))
_reverse_if_neg_step(_view_vcat.(arguments(A), skr2, Ref(jr)), kr)

Check warning on line 817 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L817

Added line #L817 was not covered by tests
end

_vcat_sub_arguments(LAY::ApplyLayout{typeof(vcat)}, A, V) = _vcat_sub_arguments(LAY, A, V, parentindices(V)...)
Expand All @@ -824,21 +827,23 @@
sz = size.(args,2)
sjr = intersect.(_argsindices(sz), Ref(jr))
sjr2 = broadcast((a,b) -> a .- b .+ 1, sjr, _vcat_firstinds(sz))
_view_hcat.(args, k, sjr2)
_view_hcat.(_reverse_if_neg_step(args, jr), k, sjr2)

Check warning on line 830 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L830

Added line #L830 was not covered by tests
end

_vcat_sub_arguments(::DualLayout{ML}, A, V) where ML = _vcat_sub_arguments(ML(), 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(L::ApplyLayout{typeof(vcat)}, V::SubArray{<:Any,2})
A = parent(V)
args = arguments(L, A)
kr,jr = parentindices(V)
sz = size.(args,1)
skr = intersect.(_argsindices(sz), Ref(kr))
skr2 = broadcast((a,b) -> a .- b .+ 1, skr, _vcat_firstinds(sz))
_view_vcat.(args, skr2, Ref(jr))
_view_vcat.(_reverse_if_neg_step(args, kr), skr2, Ref(jr))

Check warning on line 846 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L846

Added line #L846 was not covered by tests
end

@inline _view_hcat(a::Number, kr, jr) = Fill(a,length(kr),length(jr))
Expand Down Expand Up @@ -867,11 +872,11 @@
arguments(::ApplyLayout{typeof(hcat)}, V::SubArray{<:Any,2,<:Any,<:Tuple{<:Any,<:Slice}}) =
__view_hcat(arguments(parent(V)), parentindices(V)[1], :)

function sub_materialize(::ApplyLayout{typeof(vcat)}, V::AbstractMatrix, _)
function sub_materialize(lay::ApplyLayout{typeof(vcat)}, V::AbstractMatrix, _)

Check warning on line 875 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L875

Added line #L875 was not covered by tests
ret = similar(V)
n = 0
_,jr = parentindices(V)
for a in arguments(V)
for a in arguments(lay, V)

Check warning on line 879 in src/lazyconcat.jl

View check run for this annotation

Codecov / codecov/patch

src/lazyconcat.jl#L879

Added line #L879 was not covered by tests
m = size(a,1)
copyto!(view(ret,n+1:n+m,:), a)
n += m
Expand Down
5 changes: 5 additions & 0 deletions test/concattests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,11 @@ import LazyArrays: MemoryLayout, DenseColumnMajor, materialize!, call, paddeddat
@test Array(Hcat()) == Array{Any}(undef,0,0)
@test rowsupport(Hcat(Vcat(Zeros(3,1))),1:2) == colsupport(Vcat(Hcat(Zeros(1,3))),1:2)
end

@testset "reverse Vcat" begin
A = Vcat([1 2 3], [4 5 6])
@test A[2:-1:1,1:-1:1] == [4; 1 ;;]
end
end

end # module
Loading