Skip to content

Commit

Permalink
Only Vcats of AbstractRange can be guaranteed to be vcat (#357)
Browse files Browse the repository at this point in the history
* Only Vcats of AbstractUnitRange can be guaranteed to be ranges.

* Update lazyconcat.jl
  • Loading branch information
dlfivefifty authored Dec 7, 2024
1 parent 9c908d6 commit 97361a4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 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 = "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 @@ _broadcast_BandedMatrix(a) = a
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) =
broadcast!($op, dest, map(_broadcast_BandedMatrix, arguments(srclay, src))...)
end
end

Expand All @@ -325,8 +325,8 @@ _mulbanded_BandedMatrix(A, _) = A
_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) =
_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 @@ end
# 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)}()
# 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)}()

_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) = Fill(a,length(kr))
_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)

function _vcat_sub_arguments(lay::ApplyLayout{typeof(vcat)}, A, V, kr)
sz = size.(arguments(lay, A),1)
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)
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)
end

_vcat_sub_arguments(LAY::ApplyLayout{typeof(vcat)}, A, V) = _vcat_sub_arguments(LAY, A, V, parentindices(V)...)
Expand All @@ -824,21 +827,23 @@ function _vcat_sub_arguments(L::ApplyLayout{typeof(hcat)}, A, V)
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)
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))
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(vcat)}, V::SubArray{<:Any,2,<:Any,<:Tuple{<:Slice
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, _)
ret = similar(V)
n = 0
_,jr = parentindices(V)
for a in arguments(V)
for a in arguments(lay, V)
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

2 comments on commit 97361a4

@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/120883

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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 v2.3.1 -m "<description of version>" 97361a45af6184eb5ea491898e8118efbc673338
git push origin v2.3.1

Please sign in to comment.