Closed
Description
The implementation of norm(a::StaticArray)
assumes that the elements of a
have an abs2
implementation, i.e. that they are scalars. Unfortunately, that means that if a
's elements are also e.g. SVector
, it will throw:
julia> a = SVector{2}(SVector{2}(0.0, 0.0), SVector{2}(1.0, 1.0))
2-element SVector{2, SVector{2, Float64}} with indices SOneTo(2):
[0.0, 0.0]
[1.0, 1.0]
julia> norm(a)
ERROR: MethodError: no method matching abs2(::SVector{2, Float64})
Closest candidates are:
abs2(::Bool) at bool.jl:80
abs2(::Real) at number.jl:151
abs2(::Complex) at complex.jl:265
...
Stacktrace:
[1] macro expansion
@ ~/.julia/packages/StaticArrays/rKBnO/src/linalg.jl:225 [inlined]
[2] _norm
@ ~/.julia/packages/StaticArrays/rKBnO/src/linalg.jl:213 [inlined]
[3] norm(a::SVector{2, SVector{2, Float64}})
@ StaticArrays ~/.julia/packages/StaticArrays/rKBnO/src/linalg.jl:212
...
One of the unhappy consequences of this is that things like isapprox
breaks for nested StaticArray
s.
The norm
implementation is here:
Lines 213 to 228 in d03a5e6
AFAICT LinearAlgebra is avoiding this by calling out to an internal function norm_sqr
instead of abs2
, which is just abs2
for scalar input, but recursively calls norm
otherwise. There's actually a norm_sqr
function in StaticArrays too, but it seems unused?
If the suggested fix seems acceptable, I can make a PR.
Metadata
Metadata
Assignees
Labels
No labels