Skip to content

Commit 26a54e1

Browse files
authored
Fix type instability of norm(::AbstractFill, p::Int) (#418)
Fixes #417.
1 parent baace96 commit 26a54e1

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
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 = "1.14.0"
3+
version = "1.14.1"
44

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

src/fillalgebra.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,8 +496,14 @@ end
496496
normp(a::AbstractZeros, p) = norm(getindex_value(a))
497497

498498
norm1(a::AbstractFill) = length(a)*norm(getindex_value(a))
499-
norm2(a::AbstractFill) = sqrt(length(a))*norm(getindex_value(a))
500-
normp(a::AbstractFill, p) = (length(a))^(1/p)*norm(getindex_value(a))
499+
function norm2(a::AbstractFill)
500+
nrm1 = norm(getindex_value(a))
501+
sqrt(oftype(nrm1, length(a)))*nrm1
502+
end
503+
function normp(a::AbstractFill, p)
504+
nrm1 = norm(getindex_value(a))
505+
(length(a))^(1/oftype(nrm1, p))*nrm1
506+
end
501507
normInf(a::AbstractFill) = norm(getindex_value(a))
502508
normMinusInf(a::AbstractFill) = norm(getindex_value(a))
503509

test/runtests.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,11 +1888,12 @@ end
18881888
end
18891889

18901890
@testset "norm" begin
1891-
for a in (Zeros{Int}(5), Zeros(5,3), Zeros(2,3,3),
1892-
Ones{Int}(5), Ones(5,3), Ones(2,3,3),
1893-
Fill(2.3,5), Fill([2.3,4.2],5), Fill(4)),
1891+
for a in (Zeros{Int}(5), Zeros(5,3), Zeros(2,3,3), Zeros{ComplexF32}(5),
1892+
Ones{Int}(5), Ones(5,3), Ones(2,3,3), Ones{ComplexF32}(5),
1893+
Fill(2.3,5), Fill([2.3,4.2],5), Fill(4), Fill(2f0+3f0im,5)),
18941894
p in (-Inf, 0, 0.1, 1, 2, 3, Inf)
1895-
@test norm(a,p) norm(Array(a),p)
1895+
@test @inferred(norm(a,p)) norm(Array(a),p)
1896+
@test typeof(norm(a,p)) == typeof(norm(Array(a),p))
18961897
end
18971898
end
18981899

0 commit comments

Comments
 (0)