@@ -148,6 +148,27 @@ Fill{T,0}(x::T, ::Tuple{}) where T = Fill{T,0,Tuple{}}(x, ()) # ambiguity fix
148
148
@inline axes (F:: Fill ) = F. axes
149
149
@inline size (F:: Fill ) = map (length, F. axes)
150
150
151
+ """
152
+ getindex_value(F::AbstractFill)
153
+
154
+ Return the value that `F` is filled with.
155
+
156
+ # Examples
157
+
158
+ ```jldoctest
159
+ julia> f = Ones(3);
160
+
161
+ julia> FillArrays.getindex_value(f)
162
+ 1.0
163
+
164
+ julia> g = Fill(2, 10);
165
+
166
+ julia> FillArrays.getindex_value(g)
167
+ 2
168
+ ```
169
+ """
170
+ getindex_value
171
+
151
172
@inline getindex_value (F:: Fill ) = F. value
152
173
153
174
AbstractArray {T} (F:: Fill{V,N} ) where {T,V,N} = Fill {T} (convert (T, F. value):: T , F. axes)
@@ -156,7 +177,12 @@ AbstractFill{T}(F::AbstractFill) where T = AbstractArray{T}(F)
156
177
157
178
copy (F:: Fill ) = Fill (F. value, F. axes)
158
179
159
- """ Throws an error if `arr` does not contain one and only one unique value. """
180
+ """
181
+ unique_value(arr::AbstractArray)
182
+
183
+ Return `only(unique(arr))` without intermediate allocations.
184
+ Throws an error if `arr` does not contain one and only one unique value.
185
+ """
160
186
function unique_value (arr:: AbstractArray )
161
187
if isempty (arr) error (" Cannot convert empty array to Fill" ) end
162
188
val = first (arr)
438
464
439
465
440
466
# # Array
441
- Base. Array {T,N} (F:: AbstractFill{V,N} ) where {T,V,N} = fill (convert (T, getindex_value (F)), size (F))
467
+ Base. Array {T,N} (F:: AbstractFill{V,N} ) where {T,V,N} =
468
+ convert (Array{T,N}, fill (convert (T, getindex_value (F)), size (F)))
442
469
443
470
# These are in case `zeros` or `ones` are ever faster than `fill`
444
471
for (Typ, funcs, func) in ((:Zeros , :zeros , :zero ), (:Ones , :ones , :one ))
449
476
450
477
# temporary patch. should be a PR(#48895) to LinearAlgebra
451
478
Diagonal {T} (A:: AbstractFillMatrix ) where T = Diagonal {T} (diag (A))
452
- function convert (:: Type{T} , A:: AbstractFillMatrix ) where T<: Diagonal
479
+ function convert (:: Type{T} , A:: AbstractFillMatrix ) where T<: Diagonal
453
480
checksquare (A)
454
481
isdiag (A) ? T (A) : throw (InexactError (:convert , T, A))
455
482
end
0 commit comments