Skip to content

Commit 760afe4

Browse files
authored
size -> axes in broadcast_shape (#113)
* size -> axes in broadcast_shape * Improve printing * Use : to make it look more like Array * Add type info to Fill * Restrict version for tests * Update runtests.jl * Update runtests.jl
1 parent fccf2cf commit 760afe4

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "FillArrays"
22
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
3-
version = "0.9.1"
3+
version = "0.9.2"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/FillArrays.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,18 @@ Base.print_matrix_row(io::IO,
590590

591591
# Display concise description of a Fill.
592592
Base.show(io::IO, x::AbstractFill) =
593-
print(io, "$(summary(x)) = $(getindex_value(x))")
593+
print(io, "$(summary(x)): entries equal to $(getindex_value(x))")
594+
Base.show(io::IO, x::Union{Zeros,Ones}) =
595+
print(io, "$(summary(x))")
596+
597+
if VERSION  v"1.5"
598+
Base.array_summary(io::IO, ::Zeros{T}, inds::Tuple{Vararg{Base.OneTo}}) where T =
599+
print(io, Base.dims2string(length.(inds)), " Zeros{$T}")
600+
Base.array_summary(io::IO, ::Ones{T}, inds::Tuple{Vararg{Base.OneTo}}) where T =
601+
print(io, Base.dims2string(length.(inds)), " Ones{$T}")
602+
Base.array_summary(io::IO, a::Fill{T}, inds::Tuple{Vararg{Base.OneTo}}) where T =
603+
print(io, Base.dims2string(length.(inds)), " Fill{$T}")
604+
end
594605

595606
function Base.show(io::IO, ::MIME"text/plain", x::AbstractFill)
596607
show(io, x)

src/fillbroadcast.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ broadcasted(::DefaultArrayStyle{N}, ::typeof(imag), r::Ones{T,N}) where {T,N} =
2424

2525
function broadcasted(::DefaultArrayStyle, op, a::AbstractFill, b::AbstractFill)
2626
val = op(getindex_value(a), getindex_value(b))
27-
return Fill(val, broadcast_shape(size(a), size(b)))
27+
return Fill(val, broadcast_shape(axes(a), axes(b)))
2828
end
2929

3030
function _broadcasted_zeros(a, b)
31-
return Zeros{promote_type(eltype(a), eltype(b))}(broadcast_shape(size(a), size(b)))
31+
return Zeros{promote_type(eltype(a), eltype(b))}(broadcast_shape(axes(a), axes(b)))
3232
end
3333
function _broadcasted_ones(a, b)
34-
return Ones{promote_type(eltype(a), eltype(b))}(broadcast_shape(size(a), size(b)))
34+
return Ones{promote_type(eltype(a), eltype(b))}(broadcast_shape(axes(a), axes(b)))
3535
end
3636

3737
broadcasted(::DefaultArrayStyle, ::typeof(+), a::Zeros, b::Zeros) = _broadcasted_zeros(a, b)
@@ -77,23 +77,23 @@ _range_convert(::Type{AbstractVector{T}}, a::AbstractUnitRange) where T = conver
7777
_range_convert(::Type{AbstractVector{T}}, a::AbstractRange) where T = convert(T,first(a)):step(a):convert(T,last(a))
7878

7979
function broadcasted(::DefaultArrayStyle{1}, ::typeof(*), a::Ones{T}, b::AbstractRange{V}) where {T,V}
80-
broadcast_shape(size(a), size(b)) # Check sizes are compatible.
80+
broadcast_shape(axes(a), axes(b)) # Check sizes are compatible.
8181
return _range_convert(AbstractVector{promote_type(T,V)}, b)
8282
end
8383

8484
function broadcasted(::DefaultArrayStyle{1}, ::typeof(*), a::AbstractRange{V}, b::Ones{T}) where {T,V}
85-
broadcast_shape(size(a), size(b)) # Check sizes are compatible.
85+
broadcast_shape(axes(a), axes(b)) # Check sizes are compatible.
8686
return _range_convert(AbstractVector{promote_type(T,V)}, a)
8787
end
8888

8989

9090
function broadcasted(::DefaultArrayStyle{1}, ::typeof(*), a::AbstractFill, b::AbstractRange)
91-
broadcast_shape(size(a), size(b)) # Check sizes are compatible.
91+
broadcast_shape(axes(a), axes(b)) # Check sizes are compatible.
9292
return broadcasted(*, getindex_value(a), b)
9393
end
9494

9595
function broadcasted(::DefaultArrayStyle{1}, ::typeof(*), a::AbstractRange, b::AbstractFill)
96-
broadcast_shape(size(a), size(b)) # Check sizes are compatible.
96+
broadcast_shape(axes(a), axes(b)) # Check sizes are compatible.
9797
return broadcasted(*, a, getindex_value(b))
9898
end
9999

test/runtests.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,11 @@ end
561561

562562
@test conj.(Zeros(5)) Zeros(5)
563563
@test conj.(Zeros{ComplexF64}(5)) Zeros{ComplexF64}(5)
564+
565+
@test_throws DimensionMismatch broadcast(*, Ones(3), 1:6)
566+
@test_throws DimensionMismatch broadcast(*, 1:6, Ones(3))
567+
@test_throws DimensionMismatch broadcast(*, Fill(1,3), 1:6)
568+
@test_throws DimensionMismatch broadcast(*, 1:6, Fill(1,3))
564569
end
565570

566571
@testset "support Ref" begin
@@ -976,8 +981,15 @@ end
976981
end
977982
end
978983

979-
@testset "print" begin
980-
@test stringmime("text/plain", Zeros(3)) == "3-element Zeros{Float64,1,Tuple{Base.OneTo{$Int}}} = 0.0"
984+
if VERSION  v"1.5"
985+
@testset "print" begin
986+
@test stringmime("text/plain", Zeros(3)) == "3-element Zeros{Float64}"
987+
@test stringmime("text/plain", Ones(3)) == "3-element Ones{Float64}"
988+
@test stringmime("text/plain", Fill(7,2)) == "2-element Fill{Int64}: entries equal to 7"
989+
@test stringmime("text/plain", Zeros(3,2)) == "3×2 Zeros{Float64}"
990+
@test stringmime("text/plain", Ones(3,2)) == "3×2 Ones{Float64}"
991+
@test stringmime("text/plain", Fill(7,2,3)) == "2×3 Fill{Int64}: entries equal to 7"
992+
end
981993
end
982994

983995
@testset "reshape" begin

0 commit comments

Comments
 (0)