From 2050f92f648b7e14acc7dec0977d515fd5902a09 Mon Sep 17 00:00:00 2001 From: Yichao Yu Date: Fri, 15 Sep 2017 16:13:44 -0400 Subject: [PATCH] Multiple 0.7 compat def * Third argument for `Base.rtoldefault` * `select`* renaming * `ctranspose`* renaming * Math constant module and renaming --- README.md | 23 +++++++++++++++- src/Compat.jl | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 50 +++++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c1a07a441..2d7322927 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,15 @@ Currently, the `@compat` macro supports the following syntaxes: * `retry` for the more flexible `retry` method introduced in 0.6 which includes support for kwargs ([#19331], [#21419]). -## Renamed functions and types +* `Base.rtoldefault` how takes a third parameter `atol`. + The two argument form is deprecated in favor of the three arguments form with `atol=0`. + +* The `corrected` optional argument of `cov` becomes a keyword argument. + Due to conflict with 0.5 deprecation, + `cov(::AbstractVector; corrected=)` and `cov(::AbstractVector, ::AbstractVector; corrected=)` + are only available on 0.6. ([#21709]) + +## Renaming * `$` is now `xor` or `⊻` ([#18977]) @@ -184,6 +192,15 @@ Currently, the `@compat` macro supports the following syntaxes: * `Range` is now `AbstractRange` ([#23570]) +* `select`* functions (`select`, `select!`, `selectperm`, `selectperm!`) are renamed to + `partialsort`* (`partialsort`, `partialsort!`, `partialsortperm`, `partialsortperm!`) ([#23051]) + +* `ctranspose` and `ctranspose!` are now `adjoint` and `adjoint!` ([#23235]) + +* Math constants (`π`, `pi`, `e`, `γ`, `eulergamma`, `catalan`, `φ`, `golden`) are moved to the + `MathConstants` module (available as `Compat.MathConstants`). + The name exported from `Base` for `e` is changed to `ℯ`. ([#23427]) + ## New macros * `@__DIR__` has been added ([#18380]) @@ -296,6 +313,7 @@ includes this fix. Find the minimum version from there. [#21257]: https://github.com/JuliaLang/julia/issues/21257 [#21346]: https://github.com/JuliaLang/julia/issues/21346 [#21378]: https://github.com/JuliaLang/julia/issues/21378 +[#21709]: https://github.com/JuliaLang/julia/issues/21709 [#22064]: https://github.com/JuliaLang/julia/issues/22064 [#22182]: https://github.com/JuliaLang/julia/issues/22182 [#22350]: https://github.com/JuliaLang/julia/issues/22350 @@ -306,4 +324,7 @@ includes this fix. Find the minimum version from there. [#22751]: https://github.com/JuliaLang/julia/issues/22751 [#22761]: https://github.com/JuliaLang/julia/issues/22761 [#22864]: https://github.com/JuliaLang/julia/issues/22864 +[#23051]: https://github.com/JuliaLang/julia/issues/23051 +[#23235]: https://github.com/JuliaLang/julia/issues/23235 +[#23427]: https://github.com/JuliaLang/julia/issues/23427 [#23570]: https://github.com/JuliaLang/julia/issues/23570 diff --git a/src/Compat.jl b/src/Compat.jl index 5be1fef91..9f25d55bf 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -582,12 +582,81 @@ if VERSION < v"0.7.0-DEV.1285" Base.OverflowError(msg) = OverflowError() end +if VERSION < v"0.7.0-DEV.755" + # This is a hack to only add keyword signature that won't work on all julia versions. + # However, since we really only need to support a few (0.5, 0.6 and early 0.7) versions + # this should be good enough. + let Tf = typeof(cov), Tkw = Core.Core.kwftype(Tf) + @eval begin + @inline function _get_corrected(kws) + corrected = true + nkw = length(kws) >> 1 + for i in 1:nkw + if kws[i * 2 - 1] !== :corrected + Base.kwerr(kws) + end + corrected = kws[i * 2] + end + return corrected::Bool + end + if VERSION >= v"0.6" + (::$Tkw)(kws::Vector{Any}, ::$Tf, x::AbstractVector) = cov(x, _get_corrected(kws)) + (::$Tkw)(kws::Vector{Any}, ::$Tf, X::AbstractVector, Y::AbstractVector) = + cov(X, Y, _get_corrected(kws)) + end + (::$Tkw)(kws::Vector{Any}, ::$Tf, x::AbstractMatrix, vardim::Int) = + cov(x, vardim, _get_corrected(kws)) + (::$Tkw)(kws::Vector{Any}, ::$Tf, X::AbstractVecOrMat, Y::AbstractVecOrMat, + vardim::Int) = cov(X, Y, vardim, _get_corrected(kws)) + end + end +end + +# 0.7.0-DEV.1415 +@static if !isdefined(Base, :adjoint) + const adjoint = ctranspose + const adjoint! = ctranspose! + export adjoint, adjoint! +end + +# 0.7.0-DEV.1592 +@static if !isdefined(Base, :MathConstants) + @eval module MathConstants + # All other ones are already exported by Base (so should be already in the users namespace) + # and will be automatically be in this module. + export ℯ + const ℯ = e + end + const ℯ = e + export ℯ +else + import Base.MathConstants +end + +# 0.7.0-DEV.1535 +@static if !isdefined(Base, :partialsort) + const partialsort = select + const partialsort! = select! + const partialsortperm = selectperm + const partialsortperm! = selectperm! + export partialsort, partialsort!, partialsortperm, partialsortperm! +end + # 0.7.0-DEV.1721 @static if !isdefined(Base, :AbstractRange) const AbstractRange = Range export AbstractRange end +if VERSION < v"0.7.0-DEV.1325" + function Base.rtoldefault(x, y, atol::Real) + T = isa(x, Type) ? x : typeof(x) + S = isa(y, Type) ? y : typeof(y) + rtol = max(Base.rtoldefault(real(T)), Base.rtoldefault(real(S))) + return atol > 0 ? zero(rtol) : rtol + end +end + include("deprecated.jl") end # module Compat diff --git a/test/runtests.jl b/test/runtests.jl index 7bb0d7c21..97bc379b6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -357,6 +357,7 @@ A = view(rand(5,5), 1:3, 1:3) # julia#13998 for x in (3.1, -17, 3//4, big(111.1), Inf) + local x @test min(x) == max(x) == x @test minmax(x) == (x, x) end @@ -770,6 +771,55 @@ no_specialize(@nospecialize(x::Integer)) = sin(2) # 0.7 @test isa(1:2, AbstractRange) +# 0.7 +let M = [1 + 2im 3 + 4im; 5 + 6im 7 + 8im], + M2 = adjoint(M), + Mc = [1 - 2im 5 - 6im; 3 - 4im 7 - 8im] + + @test adjoint(M) == Mc + M2 .= 0 + adjoint!(M2, M) + @test M2 == Mc +end + +# 0.7 +module TestMathConstants +using Compat.MathConstants +end +for name in [:π, :pi, :ℯ, :e, :γ, :eulergamma, :catalan, :φ, :golden] + @test isdefined(TestMathConstants, name) && !Base.isdeprecated(TestMathConstants, name) + @test isdefined(Compat.MathConstants, name) && !Base.isdeprecated(Compat.MathConstants, name) +end +module TestMathConstants2 +using Compat +end +@test isdefined(TestMathConstants2, :ℯ) && !Base.isdeprecated(TestMathConstants, :ℯ) + +# 0.7 +@test partialsort([3,6,30,1,9], 2, rev=true) == 9 +@test partialsort([3,6,30,1,9], 2, by=x->1/x) == 9 +@test partialsortperm([3,6,30,1,9], 2, rev=true) == 5 +@test partialsortperm([3,6,30,1,9], 2, by=x->1/x) == 5 + +# 0.7 +@test isa(Base.rtoldefault(1.0, 2.0, 0), Float64) +@test isa(Base.rtoldefault(Float64, 2.0, 0), Float64) +@test isa(Base.rtoldefault(1.0, Float64, 0), Float64) +@test isa(Base.rtoldefault(Float64, Float64, 0), Float64) +@test Base.rtoldefault(Float64, Float64, 1.0) === 0.0 + +# 0.7 +@test cov([1 2; 3 4], 1, corrected=true) == fill(2.0, 2, 2) +@test cov([1 2; 3 4], 1, corrected=false) == fill(1.0, 2, 2) +@test cov([1 2; 3 4], [0 4; 8 9], 1, corrected=true) == [8.0 5.0; 8.0 5.0] +@test cov([1 2; 3 4], [0 4; 8 9], 1, corrected=false) == [4.0 2.5; 4.0 2.5] +if VERSION >= v"0.6" + @test cov([1, 2], corrected=true) === 0.5 + @test cov([1, 2], corrected=false) === 0.25 + @test cov([1, 2], [0, 10], corrected=true) === 5.0 + @test cov([1, 2], [0, 10], corrected=false) === 2.5 +end + if VERSION < v"0.6.0" include("deprecated.jl") end