Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,23 @@ end
/(u::AdjointAbsVec, D::Diagonal) = (D' \ u')'
/(u::TransposeAbsVec, D::Diagonal) = transpose(transpose(D) \ transpose(u))

# norm
function generic_normMinusInf(D::Diagonal)
norm_diag = norm(D.diag, -Inf)
return size(D,1) > 1 ? min(norm_diag, zero(norm_diag)) : norm_diag
end
generic_normInf(D::Diagonal) = norm(D.diag, Inf)
generic_norm1(D::Diagonal) = norm(D.diag, 1)
generic_norm2(D::Diagonal) = norm(D.diag)
function generic_normp(D::Diagonal, p)
v = norm(D.diag, p)
if size(D,1) > 1 && p < 0
v = norm(zero(v), p)
end
return v
end
norm_x_minus_y(D1::Diagonal, D2::Diagonal) = norm_x_minus_y(D1.diag, D2.diag)

_opnorm1(A::Diagonal) = maximum(norm(x) for x in A.diag)
_opnormInf(A::Diagonal) = maximum(norm(x) for x in A.diag)
_opnorm12Inf(A::Diagonal, p) = maximum(opnorm(x, p) for x in A.diag)
Expand Down
13 changes: 13 additions & 0 deletions test/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1576,4 +1576,17 @@ end
@test D == D2
end

@testset "norm" begin
D = Diagonal(float.(1:3))
Copy link
Member

@stevengj stevengj Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe

for D in ( Diagonal(1:3), Diagonal(1:1), Diagonal(1:0) )

to test the 1x1 and 0x0 cases?

Also, skip the float conversion to make sure the result is floating-point, maybe add a ::Float64 assert to the result

A = Array(D)
@testset for p in -2:2
p == 0 && continue
@test norm(D, p) ≈ sum(abs.(D).^p)^(1/p)
@test norm(D, p) ≈ norm(A, p)
end
@test norm(D, Inf) ≈ norm(A, Inf)
@test norm(D, -Inf) ≈ norm(A, -Inf)
@test norm(D, 0) ≈ norm(A, 0)
end

end # module TestDiagonal