Skip to content

Avoid allocation when computing norm of MVector #1131

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

Merged
merged 2 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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.15"
version = "1.5.16"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
2 changes: 1 addition & 1 deletion src/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ _inner_eltype(x::Number) = typeof(x)
end

@inline maxabs_nested(a::Number) = abs(a)
function maxabs_nested(a::AbstractArray)
@inline function maxabs_nested(a::AbstractArray)
prod(size(a)) == 0 && (return _init_zero(a))

m = maxabs_nested(a[1])
Expand Down
4 changes: 1 addition & 3 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,7 @@ end
@test @inferred(add_bc!(MMatrix(SA[10 20; 30 40]), (1,2))) ::MMatrix{2,2,Int} == SA[11 21; 32 42]

# Tuples of SA
@test SA[1,2,3] .* (SA[1,0],) === SVector{3,SVector{2,Int}}(((1,0), (2,0), (3,0)))
# Unfortunately this case of nested broadcasting is not inferred
@test_broken @inferred(SA[1,2,3] .* (SA[1,0],))
@test (@inferred SA[1,2,3] .* (SA[1,0],)) === SVector{3,SVector{2,Int}}(((1,0), (2,0), (3,0)))
end

@testset "SDiagonal" begin
Expand Down
23 changes: 23 additions & 0 deletions test/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Base.getindex(m::RotMat2, i::Int) = getindex(m.elements, i)
# Rotation matrices must be unitary so `similar_type` has to return an SMatrix.
StaticArrays.similar_type(::Union{RotMat2,Type{RotMat2}}) = SMatrix{2,2,Float64,4}

Base.@kwdef mutable struct KPS4{S, T, P}
v_apparent::T = zeros(S, 3)
end

@testset "Linear algebra" begin

@testset "SArray as a (mathematical) vector space" begin
Expand Down Expand Up @@ -313,6 +317,25 @@ StaticArrays.similar_type(::Union{RotMat2,Type{RotMat2}}) = SMatrix{2,2,Float64,
@test norm(SVector{0,Float64}()) isa Float64
@test norm(SA[SVector{0,Int}(),SVector{0,Int}()]) isa float(Int)
@test norm(SA[SVector{0,Int}(),SVector{0,Int}()]) == norm([Int[], Int[]])

# no allocation for MArray -- issue #1126

@inline function calc_particle_forces!(s, pos1, pos2)
segment = pos1 - pos2
norm1 = norm(segment)
unit_vector = segment / norm1

v_app_perp = s.v_apparent - s.v_apparent ⋅ unit_vector * unit_vector
half_drag_force = norm(v_app_perp)
nothing
end
kps4 = KPS4{Float64, MVector{3, Float64}, 6+4+1}()

pos1 = MVector{3, Float64}(1.0, 2.0, 3.0)
pos2 = MVector{3, Float64}(2.0, 3.0, 4.0)
calc_particle_forces!(kps4, pos1, pos2)
calc_particle_forces!(kps4, pos1, pos2)
@test (@allocated calc_particle_forces!(kps4, pos1, pos2)) == 0
end

@testset "trace" begin
Expand Down