Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix p-norm for unitful quantities #1291

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ end
$(Expr(:meta, :inline))
scale = maxabs_nested(a)

scale==0 && return _init_zero(a)
iszero(scale) && return _init_zero(a)
p == 1 && return @inbounds scale * $expr_p1
return @inbounds scale * ($expr)^(inv(p))
end
Expand All @@ -328,7 +328,7 @@ end
p == Inf && return mapreduce(norm, max, a) # no need for scaling

l = p==1 ? @inbounds($expr_p1) : @inbounds(($expr)^(inv(p)))
0<l<Inf && return l
zero(l) < l && isfinite(l) && return l
return _norm_scaled(Size(a), a, p) # p != 0, 2, Inf
end
end
Expand Down
3 changes: 3 additions & 0 deletions test/unitful.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ using Unitful
@test norm(SVector(1.0*u"m")) == 1.0*u"m"
# issue $1127
@test norm(SVector(0.0, 0.0)*u"nm") == 0.0*u"nm"

@test norm(SVector(1.0, 2.0)*u"m", 1) == 3.0*u"m"
@test norm(SVector(0.0, 0.0)*u"nm", 1) == 0.0*u"nm"
end
Loading