Skip to content

Commit ca85f94

Browse files
authored
Merge branch 'master' into patch-1
2 parents 58d6a17 + 3c12489 commit ca85f94

File tree

3 files changed

+30
-49
lines changed

3 files changed

+30
-49
lines changed

src/FillArrays.jl

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Base: size, getindex, setindex!, IndexStyle, checkbounds, convert,
1010

1111
import LinearAlgebra: rank, svdvals!, tril, triu, tril!, triu!, diag, transpose, adjoint, fill!,
1212
dot, norm2, norm1, normInf, normMinusInf, normp, lmul!, rmul!, diagzero, AdjointAbsVec, TransposeAbsVec,
13-
issymmetric, ishermitian, AdjOrTransAbsVec
13+
issymmetric, ishermitian, AdjOrTransAbsVec, checksquare
1414

1515

1616
import Base.Broadcast: broadcasted, DefaultArrayStyle, broadcast_shape
@@ -149,18 +149,9 @@ Fill{T,0}(x::T, ::Tuple{}) where T = Fill{T,0,Tuple{}}(x, ()) # ambiguity fix
149149

150150
@inline getindex_value(F::Fill) = F.value
151151

152-
AbstractArray{T}(F::Fill{T}) where T = F
153-
AbstractArray{T,N}(F::Fill{T,N}) where {T,N} = F
154152
AbstractArray{T}(F::Fill{V,N}) where {T,V,N} = Fill{T}(convert(T, F.value)::T, F.axes)
155153
AbstractArray{T,N}(F::Fill{V,N}) where {T,V,N} = Fill{T}(convert(T, F.value)::T, F.axes)
156-
157-
convert(::Type{AbstractArray{T}}, F::Fill{T}) where T = F
158-
convert(::Type{AbstractArray{T,N}}, F::Fill{T,N}) where {T,N} = F
159-
convert(::Type{AbstractArray{T}}, F::Fill) where {T} = AbstractArray{T}(F)
160-
convert(::Type{AbstractArray{T,N}}, F::Fill) where {T,N} = AbstractArray{T,N}(F)
161-
convert(::Type{AbstractFill}, F::AbstractFill) = F
162-
convert(::Type{AbstractFill{T}}, F::AbstractFill) where T = convert(AbstractArray{T}, F)
163-
convert(::Type{AbstractFill{T,N}}, F::AbstractFill) where {T,N} = convert(AbstractArray{T,N}, F)
154+
AbstractFill{T}(F::AbstractFill) where T = AbstractArray{T}(F)
164155

165156
copy(F::Fill) = Fill(F.value, F.axes)
166157

@@ -305,21 +296,23 @@ for (Typ, funcs, func) in ((:Zeros, :zeros, :zero), (:Ones, :ones, :one))
305296
@inline size(Z::$Typ) = length.(Z.axes)
306297
@inline getindex_value(Z::$Typ{T}) where T = $func(T)
307298

308-
AbstractArray{T}(F::$Typ{T}) where T = F
309-
AbstractArray{T,N}(F::$Typ{T,N}) where {T,N} = F
310299
AbstractArray{T}(F::$Typ) where T = $Typ{T}(F.axes)
311300
AbstractArray{T,N}(F::$Typ{V,N}) where {T,V,N} = $Typ{T}(F.axes)
312-
convert(::Type{AbstractArray{T}}, F::$Typ{T}) where T = AbstractArray{T}(F)
313-
convert(::Type{AbstractArray{T,N}}, F::$Typ{T,N}) where {T,N} = AbstractArray{T,N}(F)
314-
convert(::Type{AbstractArray{T}}, F::$Typ) where T = AbstractArray{T}(F)
315-
convert(::Type{AbstractArray{T,N}}, F::$Typ) where {T,N} = AbstractArray{T,N}(F)
316301

317302
copy(F::$Typ) = F
318303

319304
getindex(F::$Typ{T,0}) where T = getindex_value(F)
320305
end
321306
end
322307

308+
# conversions
309+
for TYPE in (:Fill, :AbstractFill, :Ones, :Zeros), STYPE in (:AbstractArray, :AbstractFill)
310+
@eval begin
311+
@inline $STYPE{T}(F::$TYPE{T}) where T = F
312+
@inline $STYPE{T,N}(F::$TYPE{T,N}) where {T,N} = F
313+
end
314+
end
315+
323316
"""
324317
fillsimilar(a::AbstractFill, axes)
325318
@@ -471,32 +464,22 @@ for (Typ, funcs, func) in ((:Zeros, :zeros, :zero), (:Ones, :ones, :one))
471464
end
472465
end
473466

474-
function convert(::Type{Diagonal}, Z::ZerosMatrix{T}) where T
475-
n,m = size(Z)
476-
n m && throw(BoundsError(Z))
477-
Diagonal(zeros(T, n))
478-
end
479-
480-
function convert(::Type{Diagonal{T}}, Z::ZerosMatrix) where T
481-
n,m = size(Z)
482-
n m && throw(BoundsError(Z))
483-
Diagonal(zeros(T, n))
467+
# temporary patch. should be a PR(#48895) to LinearAlgebra
468+
Diagonal{T}(A::AbstractFillMatrix) where T = Diagonal{T}(diag(A))
469+
function convert(::Type{T}, A::AbstractFillMatrix) where T<:Diagonal
470+
checksquare(A)
471+
isdiag(A) ? T(A) : throw(InexactError(:convert, T, A))
484472
end
485473

486474
## Sparse arrays
487-
488-
convert(::Type{SparseVector}, Z::ZerosVector{T}) where T = spzeros(T, length(Z))
489-
convert(::Type{SparseVector{T}}, Z::ZerosVector) where T = spzeros(T, length(Z))
490-
convert(::Type{SparseVector{Tv,Ti}}, Z::ZerosVector) where {Tv,Ti} = spzeros(Tv, Ti, length(Z))
475+
SparseVector{T}(Z::ZerosVector) where T = spzeros(T, length(Z))
476+
SparseVector{Tv,Ti}(Z::ZerosVector) where {Tv,Ti} = spzeros(Tv, Ti, length(Z))
491477

492478
convert(::Type{AbstractSparseVector}, Z::ZerosVector{T}) where T = spzeros(T, length(Z))
493479
convert(::Type{AbstractSparseVector{T}}, Z::ZerosVector) where T= spzeros(T, length(Z))
494480

495-
convert(::Type{SparseMatrixCSC}, Z::ZerosMatrix{T}) where T = spzeros(T, size(Z)...)
496-
convert(::Type{SparseMatrixCSC{T}}, Z::ZerosMatrix) where T = spzeros(T, size(Z)...)
497-
convert(::Type{SparseMatrixCSC{Tv,Ti}}, Z::ZerosMatrix) where {Tv,Ti} = spzeros(Tv, Ti, size(Z)...)
498-
convert(::Type{SparseMatrixCSC{Tv,Ti}}, Z::Zeros{T,2,Axes}) where {Tv,Ti<:Integer,T,Axes} =
499-
spzeros(Tv, Ti, size(Z)...)
481+
SparseMatrixCSC{T}(Z::ZerosMatrix) where T = spzeros(T, size(Z)...)
482+
SparseMatrixCSC{Tv,Ti}(Z::Zeros{T,2,Axes}) where {Tv,Ti<:Integer,T,Axes} = spzeros(Tv, Ti, size(Z)...)
500483

501484
convert(::Type{AbstractSparseMatrix}, Z::ZerosMatrix{T}) where T = spzeros(T, size(Z)...)
502485
convert(::Type{AbstractSparseMatrix{T}}, Z::ZerosMatrix) where T = spzeros(T, size(Z)...)
@@ -506,11 +489,9 @@ convert(::Type{AbstractSparseArray{Tv}}, Z::Zeros{T}) where {T,Tv} = spzeros(Tv,
506489
convert(::Type{AbstractSparseArray{Tv,Ti}}, Z::Zeros{T}) where {T,Tv,Ti} = spzeros(Tv, Ti, size(Z)...)
507490
convert(::Type{AbstractSparseArray{Tv,Ti,N}}, Z::Zeros{T,N}) where {T,Tv,Ti,N} = spzeros(Tv, Ti, size(Z)...)
508491

509-
510-
convert(::Type{SparseMatrixCSC}, Z::Eye{T}) where T = SparseMatrixCSC{T}(I, size(Z)...)
511-
convert(::Type{SparseMatrixCSC{Tv}}, Z::Eye{T}) where {T,Tv} = SparseMatrixCSC{Tv}(I, size(Z)...)
492+
SparseMatrixCSC{Tv}(Z::Eye{T}) where {T,Tv} = SparseMatrixCSC{Tv}(I, size(Z)...)
512493
# works around missing `speye`:
513-
convert(::Type{SparseMatrixCSC{Tv,Ti}}, Z::Eye{T}) where {T,Tv,Ti<:Integer} =
494+
SparseMatrixCSC{Tv,Ti}(Z::Eye{T}) where {T,Tv,Ti<:Integer} =
514495
convert(SparseMatrixCSC{Tv,Ti}, SparseMatrixCSC{Tv}(I, size(Z)...))
515496

516497
convert(::Type{AbstractSparseMatrix}, Z::Eye{T}) where {T} = SparseMatrixCSC{T}(I, size(Z)...)
@@ -551,7 +532,7 @@ cumsum(x::ZerosVector) = x
551532
cumsum(x::ZerosVector{Bool}) = x
552533
cumsum(x::OnesVector{II}) where II<:Integer = convert(AbstractVector{II}, oneto(length(x)))
553534
cumsum(x::OnesVector{Bool}) = oneto(length(x))
554-
cumsum(x::AbstractFillVector{Bool}) = cumsum(convert(AbstractFill{Int}, x))
535+
cumsum(x::AbstractFillVector{Bool}) = cumsum(AbstractFill{Int}(x))
555536

556537

557538
#########

src/fillalgebra.jl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ end
225225
# Zeros +/- Fill and Fill +/- Zeros
226226
function +(a::AbstractFill{T}, b::Zeros{V}) where {T, V}
227227
size(a) size(b) && throw(DimensionMismatch("dimensions must match."))
228-
return convert(AbstractFill{promote_type(T, V)}, a)
228+
return AbstractFill{promote_type(T, V)}(a)
229229
end
230230
+(a::Zeros, b::AbstractFill) = b + a
231231
-(a::AbstractFill, b::Zeros) = a + b
@@ -253,12 +253,12 @@ end
253253
function +(a::ZerosVector{T}, b::AbstractRange) where {T}
254254
size(a) size(b) && throw(DimensionMismatch("dimensions must match."))
255255
Tout = promote_type(T, eltype(b))
256-
return convert(Tout, first(b)):convert(Tout, step(b)):convert(Tout, last(b))
256+
return Tout(first(b)):Tout(step(b)):Tout(last(b))
257257
end
258-
function +(a::ZerosVector{T}, b::UnitRange) where {T}
258+
function +(a::ZerosVector{T}, b::UnitRange) where {T<:Integer}
259259
size(a) size(b) && throw(DimensionMismatch("dimensions must match."))
260260
Tout = promote_type(T, eltype(b))
261-
return convert(Tout, first(b)):convert(Tout, last(b))
261+
return AbstractUnitRange{Tout}(b)
262262
end
263263

264264
function -(a::ZerosVector, b::AbstractRange)
@@ -267,8 +267,6 @@ function -(a::ZerosVector, b::AbstractRange)
267267
end
268268
-(a::AbstractRange, b::ZerosVector) = a + b
269269

270-
271-
272270
####
273271
# norm
274272
####

test/runtests.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,10 @@ end
392392

393393
@test Diagonal(Zeros(8,5)) == Diagonal(zeros(5))
394394
@test convert(Diagonal, Zeros(5,5)) == Diagonal(zeros(5))
395-
@test_throws BoundsError convert(Diagonal, Zeros(8,5))
395+
@test_throws DimensionMismatch convert(Diagonal, Zeros(8,5))
396396

397397
@test convert(Diagonal{Int}, Zeros(5,5)) == Diagonal(zeros(Int,5))
398-
@test_throws BoundsError convert(Diagonal{Int}, Zeros(8,5))
398+
@test_throws DimensionMismatch convert(Diagonal{Int}, Zeros(8,5))
399399

400400

401401
@test Diagonal(Eye(8,5)) == Diagonal(ones(5))
@@ -414,6 +414,7 @@ end
414414

415415
@testset "Sparse vectors and matrices" begin
416416
@test SparseVector(Zeros(5)) ==
417+
SparseVector{Int}(Zeros(5)) ==
417418
SparseVector{Float64}(Zeros(5)) ==
418419
SparseVector{Float64,Int}(Zeros(5)) ==
419420
convert(AbstractSparseArray,Zeros(5)) ==
@@ -426,6 +427,7 @@ end
426427
for (Mat, SMat) in ((Zeros(5,5), spzeros(5,5)), (Zeros(6,5), spzeros(6,5)),
427428
(Eye(5), sparse(I,5,5)), (Eye(6,5), sparse(I,6,5)))
428429
@test SparseMatrixCSC(Mat) ==
430+
SparseMatrixCSC{Int}(Mat) ==
429431
SparseMatrixCSC{Float64}(Mat) ==
430432
SparseMatrixCSC{Float64,Int}(Mat) ==
431433
convert(AbstractSparseArray,Mat) ==

0 commit comments

Comments
 (0)