Skip to content

Commit

Permalink
Fix norm of StaticArrays with non-finite elements (#1161)
Browse files Browse the repository at this point in the history
* Add tests for norm of SVector with NaN or Inf elements

* Fix norm of StaticArrays with non-finite elements

* Bump patch version
  • Loading branch information
gerlero authored May 14, 2023
1 parent 3e74bde commit d9d54bd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "StaticArrays"
uuid = "90137ffa-7385-5640-81b9-e52037218182"
version = "1.5.24"
version = "1.5.25"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
3 changes: 2 additions & 1 deletion src/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ end

m = maxabs_nested(a[1])
for j = 2:prod(size(a))
m = @fastmath max(m, maxabs_nested(a[j]))
m = max(m, maxabs_nested(a[j]))
end

return m
Expand All @@ -246,6 +246,7 @@ end
return quote
$(Expr(:meta, :inline))
scale = maxabs_nested(a)
!isfinite(scale) && return scale

iszero(scale) && return _init_zero(a)
return @inbounds scale * sqrt($expr)
Expand Down
17 changes: 17 additions & 0 deletions test/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,23 @@ end
@test norm(SA[SVector{0,Int}(),SVector{0,Int}()]) isa float(Int)
@test norm(SA[SVector{0,Int}(),SVector{0,Int}()]) == norm([Int[], Int[]])

# norm of SVector with NaN and/or Inf elements -- issue #1135
@test isnan(norm(SA[0.0, NaN]))
@test isnan(norm(SA[NaN, 0.0]))
@test norm(SA[0.0, Inf]) == Inf
@test norm(SA[Inf, 0.0]) == Inf
@test norm(SA[0.0, -Inf]) == Inf
@test norm(SA[-Inf, 0.0]) == Inf
@test norm(SA[Inf, Inf]) == Inf
@test norm(SA[-Inf, -Inf]) == Inf
@test norm(SA[Inf, -Inf]) == Inf
@test norm(SA[-Inf, Inf]) == Inf
@test isnan(norm(SA[Inf, NaN]))
@test isnan(norm(SA[NaN, Inf]))
@test isnan(norm(SA[-Inf, NaN]))
@test isnan(norm(SA[NaN, -Inf]))
@test isapprox(SA[0.0, NaN], SA[0.0, NaN], nans=true)

# no allocation for MArray -- issue #1126

@inline function calc_particle_forces!(s, pos1, pos2)
Expand Down

2 comments on commit d9d54bd

@hyrodium
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/83560

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.5.25 -m "<description of version>" d9d54bdf09f98465ebc84679651478de38923aed
git push origin v1.5.25

Please sign in to comment.