From 25fa6f198b37a1ad0b9f1bb40218131b1e3b4228 Mon Sep 17 00:00:00 2001 From: arghhhh Date: Mon, 5 Jun 2017 21:48:14 -0400 Subject: [PATCH 1/6] Two argument version of clamp and clamp! --- base/math.jl | 18 +++++++++++++++--- test/math.jl | 11 +++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/base/math.jl b/base/math.jl index c033f2d8e8efd..918311b998804 100644 --- a/base/math.jl +++ b/base/math.jl @@ -41,9 +41,11 @@ end """ clamp(x, lo, hi) + clamp(x, T) Return `x` if `lo <= x <= hi`. If `x < lo`, return `lo`. If `x > hi`, return `hi`. Arguments -are promoted to a common type. +are promoted to a common type. If the type `T` is given `lo` and `hi` are typemin(T) +and typemax(T) respectively and the result is converted to `T` ```jldoctest julia> clamp.([pi, 1.0, big(10.)], 2., 9.) @@ -59,10 +61,13 @@ clamp(x::X, lo::L, hi::H) where {X,L,H} = convert(promote_type(X,L,H), lo), convert(promote_type(X,L,H), x))) +clamp( x, ::Type{T} ) where {T} = clamp( x, typemin(T), typemax(T) ) % T """ clamp!(array::AbstractArray, lo, hi) - -Restrict values in `array` to the specified range, in-place. + clamp!(array::AbstractArray, T) + +Restrict values in `array` to the specified range, in-place. If the type `T` is +given `lo` and `hi` are typemin(T) and typemax(T) respectively See also [`clamp`](@ref). """ function clamp!(x::AbstractArray, lo, hi) @@ -72,6 +77,13 @@ function clamp!(x::AbstractArray, lo, hi) x end +function clamp!(x::AbstractArray, ::Type{T}) where {T} + @inbounds for i in eachindex(x) + x[i] = clamp(x[i], T ) + end + x +end + # evaluate p[1] + x * (p[2] + x * (....)), i.e. a polynomial via Horner's rule macro horner(x, p...) ex = esc(p[end]) diff --git a/test/math.jl b/test/math.jl index 59cb22f12446d..07c18678566ce 100644 --- a/test/math.jl +++ b/test/math.jl @@ -15,11 +15,22 @@ @test clamp.([0, 1, 2, 3, 4], 1.0, 3.0) == [1.0, 1.0, 2.0, 3.0, 3.0] @test clamp.([0 1; 2 3], 1.0, 3.0) == [1.0 1.0; 2.0 3.0] + + @test clamp(-200,Int8) === typemin(Int8) + @test clamp(100,Int8) === Int8(100) + @test clamp(200,Int8) === typemax(Int8) + begin x = [0.0, 1.0, 2.0, 3.0, 4.0] clamp!(x, 1, 3) @test x == [1.0, 1.0, 2.0, 3.0, 3.0] end + begin + x = [-1000,0,1000] + clamp!(x, UInt8) + @test x == UInt8[0, 0, 255] + end + end @testset "constants" begin From 4a59f6512529ea3b5f84f86dac11d501eb3ee8a9 Mon Sep 17 00:00:00 2001 From: arghhhh Date: Mon, 5 Jun 2017 22:46:00 -0400 Subject: [PATCH 2/6] Removed whitespace --- base/math.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/math.jl b/base/math.jl index 918311b998804..c6256355ae6bc 100644 --- a/base/math.jl +++ b/base/math.jl @@ -65,7 +65,7 @@ clamp( x, ::Type{T} ) where {T} = clamp( x, typemin(T), typemax(T) ) % T """ clamp!(array::AbstractArray, lo, hi) clamp!(array::AbstractArray, T) - + Restrict values in `array` to the specified range, in-place. If the type `T` is given `lo` and `hi` are typemin(T) and typemax(T) respectively See also [`clamp`](@ref). From 27d9634e9326e050e285be516b4269e3ef7baac6 Mon Sep 17 00:00:00 2001 From: arghhhh Date: Tue, 6 Jun 2017 09:16:09 -0400 Subject: [PATCH 3/6] Removed clamp\!, removed whitespace --- base/math.jl | 12 ++---------- test/math.jl | 12 +++--------- 2 files changed, 5 insertions(+), 19 deletions(-) diff --git a/base/math.jl b/base/math.jl index c6256355ae6bc..adafda13f278b 100644 --- a/base/math.jl +++ b/base/math.jl @@ -61,13 +61,12 @@ clamp(x::X, lo::L, hi::H) where {X,L,H} = convert(promote_type(X,L,H), lo), convert(promote_type(X,L,H), x))) -clamp( x, ::Type{T} ) where {T} = clamp( x, typemin(T), typemax(T) ) % T +clamp(::Type{T}, x) where {T} = clamp(x, typemin(T), typemax(T)) % T """ clamp!(array::AbstractArray, lo, hi) clamp!(array::AbstractArray, T) -Restrict values in `array` to the specified range, in-place. If the type `T` is -given `lo` and `hi` are typemin(T) and typemax(T) respectively +Restrict values in `array` to the specified range, in-place. See also [`clamp`](@ref). """ function clamp!(x::AbstractArray, lo, hi) @@ -77,13 +76,6 @@ function clamp!(x::AbstractArray, lo, hi) x end -function clamp!(x::AbstractArray, ::Type{T}) where {T} - @inbounds for i in eachindex(x) - x[i] = clamp(x[i], T ) - end - x -end - # evaluate p[1] + x * (p[2] + x * (....)), i.e. a polynomial via Horner's rule macro horner(x, p...) ex = esc(p[end]) diff --git a/test/math.jl b/test/math.jl index 07c18678566ce..a23c82933cdf1 100644 --- a/test/math.jl +++ b/test/math.jl @@ -16,21 +16,15 @@ @test clamp.([0, 1, 2, 3, 4], 1.0, 3.0) == [1.0, 1.0, 2.0, 3.0, 3.0] @test clamp.([0 1; 2 3], 1.0, 3.0) == [1.0 1.0; 2.0 3.0] - @test clamp(-200,Int8) === typemin(Int8) - @test clamp(100,Int8) === Int8(100) - @test clamp(200,Int8) === typemax(Int8) + @test clamp(Int8,-200) === typemin(Int8) + @test clamp(Int8,100) === Int8(100) + @test clamp(Int8,200) === typemax(Int8) begin x = [0.0, 1.0, 2.0, 3.0, 4.0] clamp!(x, 1, 3) @test x == [1.0, 1.0, 2.0, 3.0, 3.0] end - begin - x = [-1000,0,1000] - clamp!(x, UInt8) - @test x == UInt8[0, 0, 255] - end - end @testset "constants" begin From ed370376d3a355907348aa801737f3336c226633 Mon Sep 17 00:00:00 2001 From: Alex Arslan Date: Tue, 6 Jun 2017 10:33:08 -0700 Subject: [PATCH 4/6] Fix docs --- base/math.jl | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/base/math.jl b/base/math.jl index adafda13f278b..7a7d1404e3841 100644 --- a/base/math.jl +++ b/base/math.jl @@ -41,11 +41,11 @@ end """ clamp(x, lo, hi) - clamp(x, T) + clamp(T, x) Return `x` if `lo <= x <= hi`. If `x < lo`, return `lo`. If `x > hi`, return `hi`. Arguments -are promoted to a common type. If the type `T` is given `lo` and `hi` are typemin(T) -and typemax(T) respectively and the result is converted to `T` +are promoted to a common type. If a type `T` is given, `lo` and `hi` are `typemin(T)` +and `typemax(T)` respectively, and the result is converted to `T`. ```jldoctest julia> clamp.([pi, 1.0, big(10.)], 2., 9.) @@ -64,7 +64,6 @@ clamp(x::X, lo::L, hi::H) where {X,L,H} = clamp(::Type{T}, x) where {T} = clamp(x, typemin(T), typemax(T)) % T """ clamp!(array::AbstractArray, lo, hi) - clamp!(array::AbstractArray, T) Restrict values in `array` to the specified range, in-place. See also [`clamp`](@ref). From fcd526e0a43ea21aaa4a2f8716c9aff47e71e3f8 Mon Sep 17 00:00:00 2001 From: arghhhh Date: Wed, 7 Jun 2017 11:23:34 -0400 Subject: [PATCH 5/6] Separated docs for new clamp, restricted T<:Integer --- base/math.jl | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/base/math.jl b/base/math.jl index adafda13f278b..22ebc59f28648 100644 --- a/base/math.jl +++ b/base/math.jl @@ -41,11 +41,9 @@ end """ clamp(x, lo, hi) - clamp(x, T) Return `x` if `lo <= x <= hi`. If `x < lo`, return `lo`. If `x > hi`, return `hi`. Arguments -are promoted to a common type. If the type `T` is given `lo` and `hi` are typemin(T) -and typemax(T) respectively and the result is converted to `T` +are promoted to a common type. ```jldoctest julia> clamp.([pi, 1.0, big(10.)], 2., 9.) @@ -61,7 +59,21 @@ clamp(x::X, lo::L, hi::H) where {X,L,H} = convert(promote_type(X,L,H), lo), convert(promote_type(X,L,H), x))) -clamp(::Type{T}, x) where {T} = clamp(x, typemin(T), typemax(T)) % T +""" + clamp(T, x) + +Clamp `x` between `typemin(T)` and `typemax(T)` and convert the result to type `T`. + +```jldoctest +julia> clamp(Int8, 200) +127 + +julia> clamp(Int8, -200) +-128 +``` +""" +clamp(::Type{T}, x) where {T<:Integer} = clamp(x, typemin(T), typemax(T)) % T + """ clamp!(array::AbstractArray, lo, hi) clamp!(array::AbstractArray, T) From 224330786d4317d7985bde857370ffb5e1a05528 Mon Sep 17 00:00:00 2001 From: arghhhh Date: Wed, 7 Jun 2017 20:32:12 -0400 Subject: [PATCH 6/6] Removed clamp! from doc string --- base/math.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/base/math.jl b/base/math.jl index 22ebc59f28648..6f5eca0635cd9 100644 --- a/base/math.jl +++ b/base/math.jl @@ -76,7 +76,6 @@ clamp(::Type{T}, x) where {T<:Integer} = clamp(x, typemin(T), typemax(T)) % T """ clamp!(array::AbstractArray, lo, hi) - clamp!(array::AbstractArray, T) Restrict values in `array` to the specified range, in-place. See also [`clamp`](@ref).