Skip to content

Commit

Permalink
broaden/fix signature of nextprod (JuliaLang#35791)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj authored and eschnett committed May 11, 2020
1 parent 487be64 commit 3bdb998
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ New library features

Standard library changes
------------------------

* The `nextprod` function now accepts tuples and other array types for its first argument ([#35791]).

#### LinearAlgebra

Expand Down
13 changes: 8 additions & 5 deletions base/combinatorics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -290,27 +290,30 @@ invperm(P::Any16) = Tuple(invperm(collect(P)))

#XXX This function should be moved to Combinatorics.jl but is currently used by Base.DSP.
"""
nextprod([k_1, k_2,...], n)
nextprod(factors::Union{Tuple,AbstractVector}, n)
Next integer greater than or equal to `n` that can be written as ``\\prod k_i^{p_i}`` for integers
``p_1``, ``p_2``, etc.
``p_1``, ``p_2``, etcetera, for factors ``k_i`` in `factors`.
# Examples
```jldoctest
julia> nextprod([2, 3], 105)
julia> nextprod((2, 3), 105)
108
julia> 2^2 * 3^3
108
```
!!! compat "Julia 1.6"
The method that accepts a tuple requires Julia 1.6 or later.
"""
function nextprod(a::Vector{Int}, x)
function nextprod(a::Union{Tuple{Vararg{<:Integer}},AbstractVector{<:Integer}}, x::Real)
if x > typemax(Int)
throw(ArgumentError("unsafe for x > typemax(Int), got $x"))
end
k = length(a)
v = fill(1, k) # current value of each counter
mx = [nextpow(ai,x) for ai in a] # maximum value of each counter
mx = map(a -> nextpow(a,x), a) # maximum value of each counter
v[1] = mx[1] # start at first case that is >= x
p::widen(Int) = mx[1] # initial value of product in this case
best = p
Expand Down
3 changes: 2 additions & 1 deletion test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2013,8 +2013,9 @@ end
end
@testset "nextprod" begin
@test_throws ArgumentError nextprod([2,3,5],Int128(typemax(Int))+1)
@test nextprod([2,3,5],30) == 30
@test nextprod([2,3,5],30) == nextprod((2,3,5),30) == 30
@test nextprod([2,3,5],33) == 36
@test nextprod([3,5],33) == nextprod(3:2:5,33) == 45
end
@testset "nextfloat/prevfloat" begin
@test nextfloat(0.0) == 5.0e-324
Expand Down

0 comments on commit 3bdb998

Please sign in to comment.