JuliaLang / JuliaLang/LinearAlgebra.jl
LinearAlgebra.normalize() can't handle Vector{Unitful.Quantity}
- Dominant language
- Julia
- Stars
- 77
- Forks
- 65
- Avg merge
- 3d 23h
- Merged PRs (30d)
- 10
Description
# Test case
```
julia> LinearAlgebra.norm([sqrt(2)*u"m", sqrt(2)*u"m"])
2.0 m
julia> LinearAlgebra.normalize([sqrt(2)*u"m", sqrt(2)*u"m"])
ERROR: DimensionError: and m are not dimensionally compatible.
Stacktrace:
[1] #s37#154 at C:\Users\bstuhl\.julia\packages\Unitful\9XsxK\src\conversion.jl:12 [inlined]
[2] #s37#154(::Any, ::Any, ::Any) at .\none:0
[3] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any,N} where N) at .\boot.jl:526
[4] uconvert(::Unitful.FreeUnits{(),NoDims,nothing}, ::Quantity{Int64,�,Unitful.FreeUnits{(m,),�,nothing}}) at C:\Users\bstuhl\.julia\packages\Unitful\9XsxK\src\conversion.jl:78
[5] convert(::Type{Float64}, ::Quantity{Int64,�,Unitful.FreeUnits{(m,),�,nothing}}) at C:\Users\bstuhl\.julia\packages\Unitful\9XsxK\src\conversion.jl:145
[6] setindex! at .\array.jl:826 [inlined]
[7] copyto!(::Array{Float64,1}, ::Array{Quantity{Int64,�,Unitful.FreeUnits{(m,),�,nothing}},1}) at .\multidimensional.jl:962
[8] AbstractArray at .\array.jl:542 [inlined]
[9] convert at .\abstractarray.jl:16 [inlined]
[10] copy_oftype at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\LinearAlgebra\src\LinearAlgebra.jl:348 [inlined]
[11] normalize(::Array{Quantity{Int64,�,Unitful.FreeUnits{(m,),�,nothing}},1}, ::Int64) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\LinearAlgebra\src\generic.jl:1659
[12] normalize(::Array{Quantity{Int64,�,Unitful.FreeUnits{(m,),�,nothing}},1}) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\LinearAlgebra\src\generic.jl:1657
[13] top-level scope at REPL[690]:1
```
### Expected
If `norm()` works, `normalize()` should too -- even though `normalize!()` does not work for `Unitful` vectors, since the normalized vector is unitless and therefore has a different type than the input vector.
The trivial implementation `normalize(x, p) = x ./ norm(x, p)` would fix this bug but would give up the numerical stability controls of `__normalize!()`.
## Version
```
julia> versioninfo()
Julia Version 1.4.1
Commit 381693d3df* (2020-04-14 17:20 UTC)
Platform Info:
OS: Windows (x86_64-w64-mingw32)
CPU: Intel(R) Core(TM) i7-6500U CPU @ 2.50GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-8.0.1 (ORCJIT, skylake)
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Reproduce the Unitful vector case and inspect LinearAlgebra/src/LinearAlgebra.jl around generic.jl:1657-1659, including the __normalize!() path. Compare normalize() with norm() and normalize!(), then add coverage showing that normalization works for Unitful quantities without losing numerical stability.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- data
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100