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

Fixed norm #30481

Merged
merged 8 commits into from
Jan 7, 2019
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
Prev Previous commit
Next Next commit
Added AbstractFloat for Quaternion Type
  • Loading branch information
raghav9-97 committed Dec 29, 2018
commit d63a87de23959c05782ea676d327dfeccae16145
26 changes: 13 additions & 13 deletions stdlib/LinearAlgebra/src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -273,40 +273,40 @@ diag(A::AbstractVector) = throw(ArgumentError("use diagm instead of diag to cons
# special cases of norm; note that they don't need to handle isempty(x)
function generic_normMinusInf(x)
(v, s) = iterate(x)::Tuple
minabs = norm(float(v))
minabs = norm(v)
while true
y = iterate(x, s)
y === nothing && break
(v, s) = y
vnorm = norm(float(v))
vnorm = norm(v)
minabs = ifelse(isnan(minabs) | (minabs < vnorm), minabs, vnorm)
end
return float(minabs)
end

function generic_normInf(x)
(v, s) = iterate(x)::Tuple
maxabs = norm(float(v))
maxabs = norm(v)
while true
y = iterate(x, s)
y === nothing && break
(v, s) = y
vnorm = norm(float(v))
vnorm = norm(v)
maxabs = ifelse(isnan(maxabs) | (maxabs > vnorm), maxabs, vnorm)
end
return float(maxabs)
end

function generic_norm1(x)
(v, s) = iterate(x)::Tuple
av = float(norm(float(v)))
av = float(norm(v))
T = typeof(av)
sum::promote_type(Float64, T) = av
while true
y = iterate(x, s)
y === nothing && break
(v, s) = y
sum += norm(float(v))
sum += norm(v)
end
return convert(T, sum)
end
Expand All @@ -331,12 +331,12 @@ function generic_norm2(x)
end
return convert(T, sqrt(sum))
else
sum = abs2(norm(float(v))/maxabs)
sum = abs2(norm(v)/maxabs)
while true
y = iterate(x, s)
y === nothing && break
(v, s) = y
sum += (norm(float(v))/maxabs)^2
sum += (norm(v)/maxabs)^2
end
return convert(T, maxabs*sqrt(sum))
end
Expand All @@ -355,21 +355,21 @@ function generic_normp(x, p)
end
spp::promote_type(Float64, T) = p
if -1 <= p <= 1 || (isfinite(length(x)*maxabs^spp) && maxabs^spp != 0) # scaling not necessary
sum::promote_type(Float64, T) = norm(float(v))^spp
sum::promote_type(Float64, T) = norm(v)^spp
while true
y = iterate(x, s)
y === nothing && break
(v, s) = y
sum += norm(float(v))^spp
sum += norm(v)^spp
end
return convert(T, sum^inv(spp))
else # rescaling
sum = (norm(float(v))/maxabs)^spp
sum = (norm(v)/maxabs)^spp
while true
y = iterate(x, s)
y == nothing && break
(v, s) = y
sum += (norm(float(v))/maxabs)^spp
sum += (norm(v)/maxabs)^spp
end
return convert(T, maxabs*sum^inv(spp))
end
Expand Down Expand Up @@ -487,7 +487,7 @@ julia> norm(-2, Inf)
2
```
"""
@inline norm(x::Number, p::Real=2) = p == 0 ? (x==0 ? zero(abs(x)) : oneunit(abs(x))) : abs(x)
@inline norm(x::Number, p::Real=2) = p == 0 ? (x==0 ? zero(abs(float(x))) : oneunit(abs(float(x)))) : abs(float(x))
norm(::Missing, p::Real=2) = missing

# special cases of opnorm
Expand Down
1 change: 1 addition & 0 deletions test/testhelpers/Quaternions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct Quaternion{T<:Real} <: Number
end
Quaternion(s::Real, v1::Real, v2::Real, v3::Real) = Quaternion(promote(s, v1, v2, v3)...)
Base.abs2(q::Quaternion) = q.s*q.s + q.v1*q.v1 + q.v2*q.v2 + q.v3*q.v3
Base.AbstractFloat(q::Quaternion) = Quaternion(float(q.s), float(q.v1), float(q.v2), float(q.v3))
Base.abs(q::Quaternion) = sqrt(abs2(q))
Base.real(::Type{Quaternion{T}}) where {T} = T
Base.conj(q::Quaternion) = Quaternion(q.s, -q.v1, -q.v2, -q.v3)
Expand Down