Skip to content
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

Support init keyword of sum #1119

Closed
ranocha opened this issue Jan 16, 2023 · 0 comments
Closed

Support init keyword of sum #1119

ranocha opened this issue Jan 16, 2023 · 0 comments

Comments

@ranocha
Copy link
Contributor

ranocha commented Jan 16, 2023

StaticArrays.jl does not seem to support the keyword init of sum. It would be nice to be able to use this Base interface.

With Julia v1.8 and StaticArrays v1.5.11, I get

julia> using StaticArrays

julia> x = SVector(1, 2)
2-element SVector{2, Int64} with indices SOneTo(2):
 1
 2

julia> sum(identity, x; init = 0)
ERROR: MethodError: no method matching sum(::typeof(identity), ::SVector{2, Int64}; init=0)
Closest candidates are:
  sum(::Union{Function, Type}, ::StaticArray{<:Tuple, T}; dims) where T at ~/.julia/packages/StaticArrays/B0HhH/src/mapreduce.jl:283 got unsupported keyword argument "init"
  sum(::Any, ::StaticArray{<:Tuple, T}; dims) where T at ~/.julia/packages/StaticArrays/B0HhH/src/mapreduce.jl:282 got unsupported keyword argument "init"
  sum(::Any, ::AbstractArray; dims, kw...) at reducedim.jl:995
  ...
Stacktrace:
 [1] kwerr(::NamedTuple{(:init,), Tuple{Int64}}, ::Function, ::Function, ::SVector{2, Int64})
   @ Base ./error.jl:165
 [2] top-level scope
   @ REPL[3]:1

help?> sum
search: sum sum! summary cumsum cumsum! isnumeric VersionNumber issubnormal get_zero_subnormals set_zero_subnormals

  sum(f, itr; [init])

  Sum the results of calling function f on each element of itr.

  The return type is Int for signed integers of less than system word size, and UInt for unsigned integers of less
  than system word size. For all other arguments, a common return type is found to which all arguments are
  promoted.

  The value returned for empty itr can be specified by init. It must be the additive identity (i.e. zero) as it is
  unspecified whether init is used for non-empty collections.

  │ Julia 1.6
  │
  │  Keyword argument init requires Julia 1.6 or later.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> sum(abs2, [2; 3; 4])
  29

  Note the important difference between sum(A) and reduce(+, A) for arrays with small integer eltype:

  julia> sum(Int8[100, 28])
  128
  
  julia> reduce(+, Int8[100, 28])
  -128

  In the former case, the integers are widened to system word size and therefore the result is 128. In the latter
  case, no such widening happens and integer overflow results in -128.

[...]

This came up in trixi-framework/Trixi.jl#1326

CC @JoshuaLampert

thchr added a commit to thchr/StaticArrays.jl that referenced this issue Oct 18, 2024
thchr added a commit to thchr/StaticArrays.jl that referenced this issue Oct 18, 2024
thchr added a commit to thchr/StaticArrays.jl that referenced this issue Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant