Skip to content

Drop normalize for quaternions #69

@sethaxen

Description

@sethaxen

LinearAlgebra only implements 2 normalize methods:

julia> using LinearAlgebra

julia> methods(normalize)
# 2 methods for generic function "normalize":
[1] normalize(a::AbstractArray) in LinearAlgebra at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/LinearAlgebra/src/generic.jl:1788
[2] normalize(a::AbstractArray, p::Real) in LinearAlgebra at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/LinearAlgebra/src/generic.jl:1788

Notably, there is no normalize(::Real) or normalize(::Complex). For good reason, as for numbers, this is the role of the sign function, which is defined generically for all numbers:
https://github.com/JuliaLang/julia/blob/bb2d8630f3aeb99c38c659a35ee9aa57bb71012a/base/number.jl#L161
We should deprecate normalize then and refer users to the sign function.

A difference between our normalize and sign functions, however, is how they treat zero quaternions:

julia> normalize(q)
QuaternionF64(0.1015814137657451, 0.8417214120379644, 0.5262271589168154, 0.0653548629598791, true)

julia> sign(q)
QuaternionF64(0.1015814137657451, 0.8417214120379644, 0.5262271589168154, 0.0653548629598791, false)

julia> normalize(zero(q))
QuaternionF64(NaN, NaN, NaN, NaN, true)

julia> sign(zero(q))
QuaternionF64(0.0, 0.0, 0.0, 0.0, false)

There are at least 4 different ways one could handle normalize for zero quaternions:

  • return a 0 quaternion (sign)
  • return NaNs (current behavior)
  • return a random unit quaternion
  • return a deterministic unit quaternion (e.g. Quaternion(1))

I don't like the last 2, as they are arbitrary. The question is whether the current behavior is a bug or the desired behavior. If a bug, then we are free to define Base.@deprecate normalize(x) sign(x). If desired, then we should define a different function for normalizing instead of overriding LinearAlgebra.normalize and deprecate normalize.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions