Skip to content

Commit

Permalink
Handle Statistics dep as Pkg extension (#1172)
Browse files Browse the repository at this point in the history
* Handle Statistics dep as Pkg extension

* rm import

* `using` unexported functions

* Apply suggestions from code review

Co-authored-by: Fredrik Ekre <[email protected]>

* declare usage of Statistics in tests

* list Statistics.jl in extras

* Apply suggestions from code review

Co-authored-by: Thomas Christensen <[email protected]>

* broken test passes on v1.10

---------

Co-authored-by: Fredrik Ekre <[email protected]>
Co-authored-by: Thomas Christensen <[email protected]>
  • Loading branch information
3 people authored Jul 3, 2023
1 parent b4fbf2f commit b05454f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
11 changes: 9 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
name = "StaticArrays"
uuid = "90137ffa-7385-5640-81b9-e52037218182"
version = "1.5.26"
version = "1.6.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StaticArraysCore = "1e83bf80-4336-4d27-bf5d-d5a4f845583c"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[weakdeps]
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[extensions]
StaticArraysStatisticsExt = "Statistics"

[compat]
julia = "1.6"
StaticArraysCore = "~1.4.0"
Expand All @@ -16,8 +22,9 @@ StaticArraysCore = "~1.4.0"
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[targets]
test = ["InteractiveUtils", "Test", "BenchmarkTools", "OffsetArrays", "Unitful"]
test = ["InteractiveUtils", "Test", "BenchmarkTools", "OffsetArrays", "Statistics", "Unitful"]
15 changes: 15 additions & 0 deletions ext/StaticArraysStatisticsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module StaticArraysStatisticsExt

import Statistics: mean

using StaticArrays
using StaticArrays: _InitialValue, _reduce, _mapreduce

_mean_denom(a, ::Colon) = length(a)
_mean_denom(a, dims::Int) = size(a, dims)
_mean_denom(a, ::Val{D}) where {D} = size(a, D)

@inline mean(a::StaticArray; dims=:) = _reduce(+, a, dims) / _mean_denom(a, dims)
@inline mean(f::Function, a::StaticArray; dims=:) = _mapreduce(f, +, dims, _InitialValue(), Size(a), a) / _mean_denom(a, dims)

end # module
6 changes: 4 additions & 2 deletions src/StaticArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import Base: getindex, setindex!, size, similar, vec, show, length, convert, pro
iszero, sum, prod, count, any, all, minimum, maximum, extrema,
copy, read, read!, write, reverse

import Statistics: mean

using Random
import Random: rand, randn, randexp, rand!, randn!, randexp!
using Core.Compiler: return_type
Expand Down Expand Up @@ -133,6 +131,10 @@ include("flatten.jl")
include("io.jl")
include("pinv.jl")

@static if !isdefined(Base, :get_extension) # VERSION < v"1.9-"
include("../ext/StaticArraysStatisticsExt.jl")
end

include("precompile.jl")
_precompile_()

Expand Down
8 changes: 0 additions & 8 deletions src/mapreduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,6 @@ reduce(::typeof(hcat), A::StaticArray{<:Tuple,<:StaticVecOrMatLike}) =

@inline Base.in(x, a::StaticArray) = _mapreduce(==(x), |, :, false, Size(a), a)

_mean_denom(a, dims::Colon) = length(a)
_mean_denom(a, dims::Int) = size(a, dims)
_mean_denom(a, ::Val{D}) where {D} = size(a, D)
_mean_denom(a, ::Type{Val{D}}) where {D} = size(a, D)

@inline mean(a::StaticArray; dims=:) = _reduce(+, a, dims) / _mean_denom(a, dims)
@inline mean(f::Function, a::StaticArray; dims=:) = _mapreduce(f, +, dims, _InitialValue(), Size(a), a) / _mean_denom(a, dims)

@inline minimum(a::StaticArray; dims=:) = _reduce(min, a, dims) # base has mapreduce(identity, scalarmin, a)
@inline minimum(f::Function, a::StaticArray; dims=:) = _mapreduce(f, min, dims, _InitialValue(), Size(a), a)

Expand Down
6 changes: 5 additions & 1 deletion test/svd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ using StaticArrays, Test, LinearAlgebra
# This only seems to work on v"1.5" due to unknown compiler improvements; seems
# to have stopped working again on v"1.6" and later?
svd_full_false(A) = svd(A, full=false)
@test_broken @inferred(svd_full_false(m_sing2)).S svd(Matrix(m_sing2)).S
if VERSION < v"1.10-"
@test svd_full_false(m_sing2).S svd(Matrix(m_sing2)).S
else
@test @inferred(svd_full_false(m_sing2)).S svd(Matrix(m_sing2)).S
end

@testinf svd(mc_sing) \ v svd(Matrix(mc_sing)) \ Vector(v)
@testinf svd(mc_sing) \ vc svd(Matrix(mc_sing)) \ Vector(vc)
Expand Down

2 comments on commit b05454f

@mateuszbaran
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/86758

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.6.0 -m "<description of version>" b05454f8cc4a821248b4b4dc4a3544d4aa1a6415
git push origin v1.6.0

Please sign in to comment.