Skip to content

Commit

Permalink
Specialize last for BroadcastArray (#291)
Browse files Browse the repository at this point in the history
* Specialize last for BroadcastArray

* Fix definition

* Bump version to v1.9.0

* Add tests for mismatched sizes

* Add test
  • Loading branch information
jishnub authored Apr 2, 2024
1 parent 35ea5e5 commit 01894b1
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 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 = "1.8.3"
version = "1.9.0"

[deps]
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
Expand Down
1 change: 1 addition & 0 deletions src/lazybroadcasting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Broadcasted(A::SubArray{<:Any,N,<:BroadcastArray}) where N = broadcasted(A)::Bro
axes(A::BroadcastArray) = axes(broadcasted(A))
size(A::BroadcastArray) = map(length, axes(A))

last(A::BroadcastArray) = A.f(last.(A.args)...)

@propagate_inbounds getindex(A::BroadcastArray{T,N}, kj::Vararg{Int,N}) where {T,N} = convert(T,broadcasted(A)[kj...])::T

Expand Down
27 changes: 27 additions & 0 deletions test/broadcasttests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ import Base: broadcasted
@test Matrix(B) == exp.(A)
@test B[1] == exp(A[1,1])
@test B[7] == exp(A[1,2])
@test last(B) == exp(last(A))

C = BroadcastArray(+, A, 2)
@test C == A .+ 2
D = BroadcastArray(+, A, C)
@test D == A + C
@test last(C) == last(A) + 2

@test sum(B) sum(exp, A)
@test sum(C) sum(A .+ 2)
Expand Down Expand Up @@ -79,6 +81,31 @@ import Base: broadcasted
@test eltype(B) == Float64
@test B[1] 3.0
end

@testset "last" begin
B = BroadcastArray(*, [1 2; 3 4], [5,6]')
@test last(B) == B[end] == 4*6
B = BroadcastArray(*, [1 2; 3 4], [5,6])
@test last(B) == B[end] == 4*6
g(x,y) = x^2 + y^3
B = BroadcastArray(g, [1 2; 3 4], [1 2; 3 4;;;])
@test last(B) == B[end] == g(4,4)
B = BroadcastArray(+, [1,2], [5,6]')
@test last(B) == B[end] == 2+6
end

@testset "infinite" begin
struct Wrapper{T,A<:AbstractVector{T}} <:AbstractVector{T}
r :: A
end
Base.size(w::Wrapper) = size(w.r)
Base.getindex(w::Wrapper, i::Int) = getindex(w.r, i)
Base.last(w::Wrapper) = last(w.r)
r = InfiniteArrays.OneToInf()
B = BroadcastArray(-, Wrapper(r), 2)
@test first(B) == -1
@test last(B) == last(r)
end
end

@testset "vector*matrix broadcasting #27" begin
Expand Down
4 changes: 0 additions & 4 deletions test/cachetests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ using LazyArrays, FillArrays, LinearAlgebra, ArrayLayouts, StaticArrays, SparseA
import LazyArrays: CachedArray, CachedMatrix, CachedVector, PaddedLayout, CachedLayout, resizedata!, zero!,
CachedAbstractArray, CachedAbstractVector, CachedAbstractMatrix, AbstractCachedArray, AbstractCachedMatrix

include("infinitearrays.jl")
using .InfiniteArrays
using Infinities

@testset "Cache" begin
@testset "basics" begin
A = 1:10
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ end
end
end

include("infinitearrays.jl")
using .InfiniteArrays
using Infinities

include("applytests.jl")
include("multests.jl")
include("ldivtests.jl")
Expand Down

0 comments on commit 01894b1

Please sign in to comment.