Skip to content

Commit 6a1b630

Browse files
authored
Concise version of "show" (#109)
* Add show * A few docstrings * Refine docstrings * Comment about codecov * Restore module docstring * Bump patch
1 parent 85fc450 commit 6a1b630

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
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.8.13"
3+
version = "0.8.14"
44

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

src/FillArrays.jl

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
""" `FillArrays` module to lazily represent matrices with a single value """
12
module FillArrays
23

34
using LinearAlgebra, SparseArrays
45
import Base: size, getindex, setindex!, IndexStyle, checkbounds, convert,
56
+, -, *, /, \, diff, sum, cumsum, maximum, minimum, sort, sort!,
67
any, all, axes, isone, iterate, unique, allunique, permutedims, inv,
7-
copy, vec, setindex!, count, ==, reshape, _throw_dmrs, map, zero
8+
copy, vec, setindex!, count, ==, reshape, _throw_dmrs, map, zero,
9+
show
810

911
import LinearAlgebra: rank, svdvals!, tril, triu, tril!, triu!, diag, transpose, adjoint, fill!,
1012
norm2, norm1, normInf, normMinusInf, normp, lmul!, rmul!, diagzero, AbstractTriangular
@@ -15,6 +17,11 @@ import Base.Broadcast: broadcasted, DefaultArrayStyle, broadcast_shape
1517

1618
export Zeros, Ones, Fill, Eye
1719

20+
"""
21+
AbstractFill{T, N, Axes} <: AbstractArray{T, N}
22+
23+
Supertype for lazy array types whose entries are all equal to constant.
24+
"""
1825
abstract type AbstractFill{T, N, Axes} <: AbstractArray{T, N} end
1926

2027
==(a::AbstractFill, b::AbstractFill) = axes(a) == axes(b) && getindex_value(a) == getindex_value(b)
@@ -50,6 +57,29 @@ rank(F::AbstractFill) = iszero(getindex_value(F)) ? 0 : 1
5057
IndexStyle(::Type{<:AbstractFill{<:Any,N,<:NTuple{N,Base.OneTo{Int}}}}) where N = IndexLinear()
5158

5259

60+
"""
61+
Fill{T, N, Axes}
62+
where `Axes <: Tuple{Vararg{AbstractUnitRange,N}}`
63+
64+
A lazy representation of an array of dimension `N`
65+
whose entries are all equal to a constant of type `T`,
66+
with axes of type `Axes`.
67+
Typically created by `Fill` or `Zeros` or `Ones`
68+
69+
# Examples
70+
71+
```jldoctest
72+
julia> Fill(7, (2,3))
73+
2×3 Fill{Int64,2,Tuple{Base.OneTo{Int64},Base.OneTo{Int64}}}:
74+
7 7 7
75+
7 7 7
76+
77+
julia> Fill{Float64, 1, Tuple{UnitRange{Int64}}}(7., (1:2,))
78+
2-element Fill{Float64,1,Tuple{UnitRange{Int64}}} with indices 1:2:
79+
7.0
80+
7.0
81+
```
82+
"""
5383
struct Fill{T, N, Axes} <: AbstractFill{T, N, Axes}
5484
value::T
5585
axes::Axes
@@ -73,7 +103,9 @@ Fill{T,0}(x::T, ::Tuple{}) where T = Fill{T,0,Tuple{}}(x, ()) # ambiguity fix
73103

74104
@inline Fill{T}(x, sz::Vararg{<:Integer,N}) where {T, N} = Fill{T, N}(x, sz)
75105
@inline Fill{T}(x, sz::Tuple{Vararg{<:Any,N}}) where {T, N} = Fill{T, N}(x, sz)
106+
""" `Fill(x, dims...)` construct lazy version of `fill(x, dims...)` """
76107
@inline Fill(x::T, sz::Vararg{<:Integer,N}) where {T, N} = Fill{T, N}(x, sz)
108+
""" `Fill(x, dims)` construct lazy version of `fill(x, dims)` """
77109
@inline Fill(x::T, sz::Tuple{Vararg{<:Any,N}}) where {T, N} = Fill{T, N}(x, sz)
78110

79111
# We restrict to when T is specified to avoid ambiguity with a Fill of a Fill
@@ -189,6 +221,7 @@ Base._reshape(parent::AbstractFill{T, 1, Axes}, dims::Tuple{Int}) where {T, Axes
189221

190222
for (Typ, funcs, func) in ((:Zeros, :zeros, :zero), (:Ones, :ones, :one))
191223
@eval begin
224+
""" `$($Typ){T, N, Axes} <: AbstractFill{T, N, Axes}` (lazy `$($funcs)` with axes)"""
192225
struct $Typ{T, N, Axes} <: AbstractFill{T, N, Axes}
193226
axes::Axes
194227
@inline $Typ{T,N,Axes}(sz::Axes) where Axes<:Tuple{Vararg{AbstractUnitRange,N}} where {T, N} =
@@ -202,6 +235,7 @@ for (Typ, funcs, func) in ((:Zeros, :zeros, :zero), (:Ones, :ones, :one))
202235
@inline $Typ{T, 0}(sz::Tuple{}) where {T} = $Typ{T,0,Tuple{}}(sz)
203236
@inline $Typ{T, N}(sz::Tuple{Vararg{<:Integer, N}}) where {T, N} = $Typ{T,N}(Base.OneTo.(sz))
204237
@inline $Typ{T, N}(sz::Vararg{<:Integer, N}) where {T, N} = $Typ{T,N}(sz)
238+
""" `$($Typ){T}(dims...)` construct lazy version of `$($funcs)(dims...)`"""
205239
@inline $Typ{T}(sz::Vararg{Integer,N}) where {T, N} = $Typ{T, N}(sz)
206240
@inline $Typ{T}(sz::SZ) where SZ<:Tuple{Vararg{Any,N}} where {T, N} = $Typ{T, N}(sz)
207241
@inline $Typ(sz::Vararg{Any,N}) where N = $Typ{Float64,N}(sz)
@@ -552,4 +586,13 @@ Base.print_matrix_row(io::IO,
552586
i::Integer, cols::AbstractVector, sep::AbstractString) =
553587
axes_print_matrix_row(axes(X), io, X, A, i, cols, sep)
554588

589+
590+
# Display concise description of a Fill.
591+
Base.show(io::IO, x::AbstractFill) =
592+
print(io, "$(summary(x)) = $(getindex_value(x))")
593+
594+
function Base.show(io::IO, ::MIME"text/plain", x::AbstractFill)
595+
show(io, x)
596+
end
597+
555598
end # module

src/fillalgebra.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ end
228228

229229
function rmul!(z::AbstractFill, x::Number)
230230
λ = getindex_value(z)
231-
# Following check ensures consistency w/ lmul!(x, Array(z))
231+
# Following check ensures consistency w/ lmul!(x, Array(z))
232232
# for, e.g., lmul!(NaN, z)
233233
λ*x == λ || throw(ArgumentError("Cannot scale by $x"))
234234
z
@@ -238,4 +238,4 @@ fillzero(::Type{Fill{T,N,AXIS}}, n, m) where {T,N,AXIS} = Fill{T,N,AXIS}(zero(T)
238238
fillzero(::Type{Zeros{T,N,AXIS}}, n, m) where {T,N,AXIS} = Zeros{T,N,AXIS}((n, m))
239239
fillzero(::Type{F}, n, m) where F = throw(ArgumentError("Cannot create a zero array of type $F"))
240240

241-
diagzero(D::Diagonal{F}, i, j) where F<:AbstractFill = fillzero(F, axes(D.diag[i], 1), axes(D.diag[j], 2))
241+
diagzero(D::Diagonal{F}, i, j) where F<:AbstractFill = fillzero(F, axes(D.diag[i], 1), axes(D.diag[j], 2))

test/runtests.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ import FillArrays: AbstractFill, RectDiagonal, SquareEye
9191
Fill{Float32}(one(Float32),5,5)
9292

9393
@test Fill{T}(F) Fill{T,2}(F) typeof(F)(F) F
94+
95+
show(devnull, MIME("text/plain"), F) # for codecov
9496
end
9597

9698
@test Eye(5) isa Diagonal{Float64}
@@ -964,7 +966,7 @@ end
964966
end
965967

966968
@testset "print" begin
967-
@test stringmime("text/plain", Zeros(3)) == "3-element Zeros{Float64,1,Tuple{Base.OneTo{$Int}}}:\n\n\n"
969+
@test stringmime("text/plain", Zeros(3)) == "3-element Zeros{Float64,1,Tuple{Base.OneTo{$Int}}} = 0.0"
968970
end
969971

970972
@testset "reshape" begin
@@ -1012,4 +1014,4 @@ end
10121014
@test U == UpperTriangular(ones(3,3))
10131015
@test axes(U) == (Base.OneTo(3),Base.OneTo(3))
10141016
end
1015-
end
1017+
end

0 commit comments

Comments
 (0)