1
+ """ `FillArrays` module to lazily represent matrices with a single value """
1
2
module FillArrays
2
3
3
4
using LinearAlgebra, SparseArrays
4
5
import Base: size, getindex, setindex!, IndexStyle, checkbounds, convert,
5
6
+ , - , * , / , \ , diff, sum, cumsum, maximum, minimum, sort, sort!,
6
7
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
8
10
9
11
import LinearAlgebra: rank, svdvals!, tril, triu, tril!, triu!, diag, transpose, adjoint, fill!,
10
12
norm2, norm1, normInf, normMinusInf, normp, lmul!, rmul!, diagzero, AbstractTriangular
@@ -15,6 +17,11 @@ import Base.Broadcast: broadcasted, DefaultArrayStyle, broadcast_shape
15
17
16
18
export Zeros, Ones, Fill, Eye
17
19
20
+ """
21
+ AbstractFill{T, N, Axes} <: AbstractArray{T, N}
22
+
23
+ Supertype for lazy array types whose entries are all equal to constant.
24
+ """
18
25
abstract type AbstractFill{T, N, Axes} <: AbstractArray{T, N} end
19
26
20
27
== (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
50
57
IndexStyle (:: Type {<: AbstractFill{<:Any,N,<:NTuple{N,Base.OneTo{Int}}} }) where N = IndexLinear ()
51
58
52
59
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
+ """
53
83
struct Fill{T, N, Axes} <: AbstractFill{T, N, Axes}
54
84
value:: T
55
85
axes:: Axes
@@ -73,7 +103,9 @@ Fill{T,0}(x::T, ::Tuple{}) where T = Fill{T,0,Tuple{}}(x, ()) # ambiguity fix
73
103
74
104
@inline Fill {T} (x, sz:: Vararg{<:Integer,N} ) where {T, N} = Fill {T, N} (x, sz)
75
105
@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...)` """
76
107
@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)` """
77
109
@inline Fill (x:: T , sz:: Tuple{Vararg{<:Any,N}} ) where {T, N} = Fill {T, N} (x, sz)
78
110
79
111
# 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
189
221
190
222
for (Typ, funcs, func) in ((:Zeros , :zeros , :zero ), (:Ones , :ones , :one ))
191
223
@eval begin
224
+ """ `$($ Typ) {T, N, Axes} <: AbstractFill{T, N, Axes}` (lazy `$($ funcs) ` with axes)"""
192
225
struct $ Typ{T, N, Axes} <: AbstractFill{T, N, Axes}
193
226
axes:: Axes
194
227
@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))
202
235
@inline $ Typ {T, 0} (sz:: Tuple{} ) where {T} = $ Typ {T,0,Tuple{}} (sz)
203
236
@inline $ Typ {T, N} (sz:: Tuple{Vararg{<:Integer, N}} ) where {T, N} = $ Typ {T,N} (Base. OneTo .(sz))
204
237
@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...)`"""
205
239
@inline $ Typ {T} (sz:: Vararg{Integer,N} ) where {T, N} = $ Typ {T, N} (sz)
206
240
@inline $ Typ {T} (sz:: SZ ) where SZ<: Tuple{Vararg{Any,N}} where {T, N} = $ Typ {T, N} (sz)
207
241
@inline $ Typ (sz:: Vararg{Any,N} ) where N = $ Typ {Float64,N} (sz)
@@ -552,4 +586,13 @@ Base.print_matrix_row(io::IO,
552
586
i:: Integer , cols:: AbstractVector , sep:: AbstractString ) =
553
587
axes_print_matrix_row (axes (X), io, X, A, i, cols, sep)
554
588
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
+
555
598
end # module
0 commit comments