diff --git a/.travis.yml b/.travis.yml index c21f97f..e0ea53d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,18 +1,10 @@ -language: cpp -compiler: - - clang +language: julia +os: + - linux + - osx +julia: + - 0.3 + - 0.4 + - nightly notifications: email: false -env: - matrix: - #- JULIAVERSION="juliareleases" - - JULIAVERSION="julianightlies" -before_install: - - sudo add-apt-repository ppa:staticfloat/julia-deps -y - - sudo add-apt-repository ppa:staticfloat/${JULIAVERSION} -y - - sudo apt-get update -qq -y - - sudo apt-get install libpcre3-dev julia -y -script: - - julia -e 'Pkg.init(); run(`ln -s $(pwd()) $(Pkg.dir("NumericFuns"))`); Pkg.pin("NumericFuns"); Pkg.resolve()' - - julia -e 'using NumericFuns; @assert isdefined(:NumericFuns); @assert typeof(NumericFuns) === Module' - - julia runtests.jl diff --git a/README.md b/README.md index dd6d031..9a54951 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ Here is a table of functor types for operators: #### Functors for math functions -The package also defined functors for named functions. The naming of functor types follows the ``$(capitalize(funname))Fun`` rule. +The package also defined functors for named functions. The naming of functor types follows the ``$(capitalize(funname))Fun`` rule. For example, the functor type for ``sqrt`` is ``SqrtFun``, and that for ``lgamma`` is ``LgammaFun``, etc. In particular, the package defines functors for the following functions: @@ -112,7 +112,7 @@ In particular, the package defines functors for the following functions: * rounding functions ``` - floor, ceil, trunc, round, + floor, ceil, trunc, round, ifloor, iceil, itrunc, iround ``` @@ -127,20 +127,20 @@ In particular, the package defines functors for the following functions: * exponential & logarithm ``` - exp, exp2, exp10, expm1, + exp, exp2, exp10, expm1, log, log2, log10, log1p, - sigmoid, logit, xlogx, xlogy, + sigmoid, logit, xlogx, xlogy, softplus, invsoftplus, logsumexp ``` * trigonometric functions ``` - sin, cos, tan, cot, sec, csc, - asin, acos, atan, acot, asec, acsc, atan2, - sinc, cosc, sinpi, cospi, - sind, cosd, tand, cotd, secd, cscd, + sin, cos, tan, cot, sec, csc, + asin, acos, atan, acot, asec, acsc, atan2, + sinc, cosc, sinpi, cospi, + sind, cosd, tand, cotd, secd, cscd, asind, acosd, atand, acotd, asecd, acscd ``` @@ -165,7 +165,7 @@ In particular, the package defines functors for the following functions: ## Result Type Inference -Each functor defined in this package comes with ``result_type`` methods that return the type of the result, given the argument types. These methods are thoroughly tested to ensure correctness. For example, +Each functor defined in this package comes with ``result_type`` methods that return the type of the result, given the argument types. These methods are thoroughly tested to ensure correctness. For example, ```julia result_type(Add(), Int, Float64) # --> returns Float64 @@ -175,11 +175,10 @@ result_type(SqrtFun(), Int) # --> returns Float64 The package also provides other convenient methods for type inference, which include ``fptype`` and ``arithtype``. Particularly, we have ```julia -fptype{T<:Real}(::Type{T}) == typeof(Convert(FloatingPoint, one(T))) +fptype{T<:Real}(::Type{T}) == typeof(Convert(AbstractFloat, one(T))) fptype{T<:Real}(::Type{Complex{T}}) == Complex{fptype(T)} arithtype{T1<:Number, T2<:Number} == typeof(one(T1) + one(T2)) ``` The internal implementation of these functions are very efficient, usually without actually evaluating the expressions. - diff --git a/REQUIRE b/REQUIRE index 2e43c1e..df77a41 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1 +1,2 @@ -julia 0.3- \ No newline at end of file +julia 0.3 +Compat diff --git a/runtests.jl b/runtests.jl deleted file mode 100644 index d57f097..0000000 --- a/runtests.jl +++ /dev/null @@ -1,7 +0,0 @@ -tests = ["functors", "rtypes"] - -for t in tests - fp = joinpath("test", "$(t).jl") - println("running $(fp) ...") - include(fp) -end diff --git a/src/NumericFuns.jl b/src/NumericFuns.jl index 79dbbb5..14d518b 100644 --- a/src/NumericFuns.jl +++ b/src/NumericFuns.jl @@ -1,13 +1,15 @@ module NumericFuns - export + using Compat + + export # mathfuns - sqr, rcp, rsqrt, rcbrt, xlogx, xlogy, + sqr, rcp, rsqrt, rcbrt, xlogx, xlogy, sigmoid, logistic, logit, softplus, invsoftplus, logsumexp, # functors (Note: export of specific functors are in functors.jl) - Functor, UnaryFunctor, BinaryFunctor, TernaryFunctor, + Functor, UnaryFunctor, BinaryFunctor, TernaryFunctor, evaluate, @functor1, @functor2, # rtypes @@ -19,6 +21,5 @@ module NumericFuns include("mathfuns.jl") include("functors.jl") include("rtypes.jl") - end # module diff --git a/src/functors.jl b/src/functors.jl index 886c3cd..fe8e4e5 100644 --- a/src/functors.jl +++ b/src/functors.jl @@ -13,7 +13,7 @@ typealias TernaryFunctor Functor{3} ## macros for defining functors macro functor1(F, fun, T) - quote + quote type $F <: Functor{1} end global evaluate evaluate(::$F, x::$T) = $(fun)(x) @@ -21,14 +21,14 @@ macro functor1(F, fun, T) end macro functor2(F, fun, T) - quote + quote type $F <: Functor{2} end global evaluate evaluate(::$F, x::$T, y::$T) = $(fun)(x, y) end end -default_functorsym(f::Symbol) = +default_functorsym(f::Symbol) = (fstr = string(f); symbol(string(uppercase(fstr[1]), fstr[2:end], "Fun"))) macro functor1a(fun, T) @@ -53,7 +53,7 @@ macro functor1a_ord(fun, T) F = default_functorsym(fun) quote global $(F) - immutable $F{OT<:Real} <: Functor{1} + immutable $F{OT<:Real} <: Functor{1} order::OT end $(F){OT<:Real}(ord::OT) = $(F){OT}(ord) @@ -151,10 +151,15 @@ export IfloorFun, IceilFun, ItruncFun, IroundFun @functor1a trunc Real @functor1a round Real -@functor1a ifloor Real -@functor1a iceil Real -@functor1a itrunc Real -@functor1a iround Real +_ifloor(x) = floor(Integer, x) +_iceil(x) = ceil(Integer, x) +_itrunc(x) = trunc(Integer, x) +_iround(x) = round(Integer, x) + +@functor1 IfloorFun _ifloor Real +@functor1 IceilFun _iceil Real +@functor1 ItruncFun _itrunc Real +@functor1 IroundFun _iround Real ## number classification @@ -298,10 +303,10 @@ export Hankelh1Fun, Hankelh2Fun @functor1a airybi Number @functor1a airybiprime Number -@functor1a besselj0 Number -@functor1a besselj1 Number -@functor1a bessely0 Number -@functor1a bessely1 Number +@functor1a besselj0 Number +@functor1a besselj1 Number +@functor1a bessely0 Number +@functor1a bessely1 Number @functor1a_ord besseli Number @functor1a_ord besselj Number @@ -311,11 +316,11 @@ export Hankelh1Fun, Hankelh2Fun @functor1a_ord hankelh1 Number @functor1a_ord hankelh2 Number -####################################### +####################################### # # Ternary functors # -####################################### +####################################### export FMA, IfelseFun @@ -324,7 +329,3 @@ evaluate(::FMA, x::Number, y::Number, z::Number) = (x + y * z) type IfelseFun <: Functor{3} end evaluate{T<:Number}(::IfelseFun, c::Bool, x::T, y::T) = ifelse(c, x, y) - - - - diff --git a/src/mathfuns.jl b/src/mathfuns.jl index 0fd248b..74d8c16 100644 --- a/src/mathfuns.jl +++ b/src/mathfuns.jl @@ -3,29 +3,29 @@ sqr(x::Number) = x * x rcp(x::Number) = one(x) / x -rsqrt(x::Number) = one(x) / sqrt(x) +rsqrt(x::Number) = one(x) / sqrt(x) rcbrt(x::Real) = one(x) / cbrt(x) -xlogx(x::FloatingPoint) = x > zero(x) ? x * log(x) : zero(x) +xlogx(x::AbstractFloat) = x > zero(x) ? x * log(x) : zero(x) xlogx(x::Real) = xlogx(float(x)) -xlogy{T<:FloatingPoint}(x::T, y::T) = x > zero(T) ? x * log(y) : zero(x) +xlogy{T<:AbstractFloat}(x::T, y::T) = x > zero(T) ? x * log(y) : zero(x) xlogy{T<:Real}(x::T, y::T) = xlogy(float(x), float(y)) xlogy(x::Real, y::Real) = xlogy(promote(x, y)...) -logistic(x::FloatingPoint) = rcp(one(x) + exp(-x)) +logistic(x::AbstractFloat) = rcp(one(x) + exp(-x)) logistic(x::Real) = logistic(float(x)) -logit(x::FloatingPoint) = log(x / (one(x) - x)) +logit(x::AbstractFloat) = log(x / (one(x) - x)) logit(x::Real) = logit(float(x)) -softplus(x::FloatingPoint) = x <= 0 ? log1p(exp(x)) : x + log1p(exp(-x)) +softplus(x::AbstractFloat) = x <= 0 ? log1p(exp(x)) : x + log1p(exp(-x)) softplus(x::Real) = softplus(float(x)) -invsoftplus(x::FloatingPoint) = log(exp(x) - one(x)) +invsoftplus(x::AbstractFloat) = log(exp(x) - one(x)) invsoftplus(x::Real) = invsoftplus(float(x)) -logsumexp{T<:FloatingPoint}(x::T, y::T) = x > y ? x + log1p(exp(y - x)) : y + log1p(exp(x - y)) +logsumexp{T<:AbstractFloat}(x::T, y::T) = x > y ? x + log1p(exp(y - x)) : y + log1p(exp(x - y)) logsumexp{T<:Real}(x::T, y::T) = logsumexp(float(x), float(y)) logsumexp(x::Real, y::Real) = logsumexp(promote(x, y)...) diff --git a/src/rtypes.jl b/src/rtypes.jl index 6d39979..3a3ad7f 100644 --- a/src/rtypes.jl +++ b/src/rtypes.jl @@ -8,36 +8,40 @@ ## fptype -fptype{T<:Union(Bool,Int8,Int16,Uint8,Uint16)}(::Type{T}) = Float32 +# @compat fptype{T<:Union{Bool,Int8,Int16,UInt8,UInt16}}(::Type{T}) = Float32 fptype{T<:Integer}(::Type{T}) = Float64 -fptype{T<:FloatingPoint}(::Type{T}) = T +fptype{T<:AbstractFloat}(::Type{T}) = T fptype{T<:Integer}(::Type{Complex{T}}) = Complex{fptype(T)} -fptype{T<:FloatingPoint}(::Type{Complex{T}}) = Complex{T} +fptype{T<:AbstractFloat}(::Type{Complex{T}}) = Complex{T} ## signed & unsigned type signedtype{T<:Signed}(::Type{T}) = T -signedtype(::Type{Uint8}) = Int8 -signedtype(::Type{Uint16}) = Int16 -signedtype(::Type{Uint32}) = Int32 -signedtype(::Type{Uint64}) = Int64 -signedtype(::Type{Uint128}) = Int128 +signedtype(::Type{UInt8}) = Int8 +signedtype(::Type{UInt16}) = Int16 +signedtype(::Type{UInt32}) = Int32 +signedtype(::Type{UInt64}) = Int64 +signedtype(::Type{UInt128}) = Int128 unsignedtype{T<:Unsigned}(::Type{T}) = T -unsignedtype(::Type{Int8}) = Uint8 -unsignedtype(::Type{Int16}) = Uint16 -unsignedtype(::Type{Int32}) = Uint32 -unsignedtype(::Type{Int64}) = Uint64 -unsignedtype(::Type{Int128}) = Uint128 +unsignedtype(::Type{Int8}) = UInt8 +unsignedtype(::Type{Int16}) = UInt16 +unsignedtype(::Type{Int32}) = UInt32 +unsignedtype(::Type{Int64}) = UInt64 +unsignedtype(::Type{Int128}) = UInt128 ## arithtype (unary) +if VERSION >= v"0.4-dev" + arithtype{T<:Union{Signed,Unsigned}}(::Type{T}) = T +else + arithtype{T<:Signed}(::Type{T}) = Int64 + arithtype{T<:Unsigned}(::Type{T}) = UInt64 +end arithtype(::Type{Bool}) = Int64 -arithtype{T<:Signed}(::Type{T}) = Int64 -arithtype{T<:Unsigned}(::Type{T}) = Uint64 arithtype(::Type{Int128}) = Int128 -arithtype(::Type{Uint128}) = Uint128 +arithtype(::Type{UInt128}) = UInt128 arithtype(::Type{Float32}) = Float32 arithtype(::Type{Float64}) = Float64 @@ -86,8 +90,8 @@ result_type{T<:Number}(::SqrFun, ::Type{T}) = arithtype(T) # real & imag -result_type{T<:Real}(::Union(RealFun,ImagFun), ::Type{T}) = T -result_type{T<:Real}(::Union(RealFun,ImagFun), ::Type{Complex{T}}) = T +@compat result_type{T<:Real}(::Union{RealFun,ImagFun}, ::Type{T}) = T +@compat result_type{T<:Real}(::Union{RealFun,ImagFun}, ::Type{Complex{T}}) = T # sign & signbit @@ -96,7 +100,7 @@ result_type{T<:Real}(::SignbitFun, ::Type{T}) = Bool # add & subtract -result_type{T1<:Number,T2<:Number}(::Union(Add,Subtract), ::Type{T1}, ::Type{T2}) = arithtype(T1, T2) +@compat result_type{T1<:Number,T2<:Number}(::Union{Add,Subtract}, ::Type{T1}, ::Type{T2}) = arithtype(T1, T2) # multiply @@ -105,37 +109,43 @@ result_type(::Multiply, ::Type{Bool}, ::Type{Bool}) = Bool result_type{T<:Real}(::Multiply, ::Type{Bool}, ::Type{T}) = T result_type{T<:Real}(::Multiply, ::Type{T}, ::Type{Bool}) = T -result_type{T1<:Real,T2<:Real}(::Multiply, ::Type{Complex{T1}}, ::Type{Complex{T2}}) = +result_type{T1<:Real,T2<:Real}(::Multiply, ::Type{Complex{T1}}, ::Type{Complex{T2}}) = Complex{arithtype(T1, T2)} -result_type{T1<:Real,T2<:Real}(op::Multiply, ::Type{T1}, ::Type{Complex{T2}}) = +result_type{T1<:Real,T2<:Real}(op::Multiply, ::Type{T1}, ::Type{Complex{T2}}) = Complex{result_type(op, T1, T2)} -result_type{T1<:Real,T2<:Real}(op::Multiply, ::Type{Complex{T1}}, ::Type{T2}) = +result_type{T1<:Real,T2<:Real}(op::Multiply, ::Type{Complex{T1}}, ::Type{T2}) = Complex{result_type(op, T1, T2)} # divide result_type{T1<:Real,T2<:Real}(::Divide, ::Type{T1}, ::Type{T2}) = promote_type(T1, T2) -result_type{T1<:Integer,T2<:Integer}(::Divide, ::Type{T1}, ::Type{T2}) = +result_type{T1<:Integer,T2<:Integer}(::Divide, ::Type{T1}, ::Type{T2}) = promote_type(fptype(T1), fptype(T2)) -result_type{T1<:Real,T2<:Complex}(op::Divide, ::Type{T1}, ::Type{T2}) = +result_type{T1<:Real,T2<:Complex}(op::Divide, ::Type{T1}, ::Type{T2}) = result_type(Multiply(), T1, fptype(T2)) -result_type{T1<:Real,T2<:Real}(op::Divide, ::Type{Complex{T1}}, ::Type{T2}) = +result_type{T1<:Real,T2<:Real}(op::Divide, ::Type{Complex{T1}}, ::Type{T2}) = Complex{result_type(op, T1, T2)} -result_type{T1<:Complex,T2<:Complex}(op::Divide, ::Type{T1}, ::Type{T2}) = - result_type(Multiply(), T1, fptype(T2)) +if VERSION >= v"0.4-dev" + result_type{T1<:Real,T2<:Real}(op::Divide, ::Type{Complex{T1}}, + ::Type{Complex{T2}}) = + Complex{result_type(op, T1, T2)} +else + result_type{T1<:Complex,T2<:Complex}(op::Divide, ::Type{T1}, ::Type{T2}) = + result_type(Multiply(), T1, fptype(T2)) +end # rdivide -result_type{T1<:Number,T2<:Number}(::RDivide, ::Type{T1}, ::Type{T2}) = +result_type{T1<:Number,T2<:Number}(::RDivide, ::Type{T1}, ::Type{T2}) = result_type(Divide(), T2, T1) # rcp result_type{T<:Number}(::RcpFun, ::Type{T}) = fptype(T) - + # pow result_type(::Pow, ::Type{Bool}, ::Type{Bool}) = Bool @@ -148,11 +158,11 @@ result_type{T1<:Real,T2<:Real}(::Pow, ::Type{T1}, ::Type{T2}) = fptype(promote_t result_type{T1<:Real}(::Pow, ::Type{Complex{T1}}, ::Type{Bool}) = Complex{T1} result_type{T1<:Real,T2<:Integer}(::Pow, ::Type{Complex{T1}}, ::Type{T2}) = Complex{arithtype(T1)} -result_type{T1<:Real,T2<:Real}(::Pow, ::Type{Complex{T1}}, ::Type{T2}) = +result_type{T1<:Real,T2<:Real}(::Pow, ::Type{Complex{T1}}, ::Type{T2}) = Complex{fptype(promote_type(T1, T2))} -result_type{T1<:Real,T2<:Real}(::Pow, ::Type{T1}, ::Type{Complex{T2}}) = +result_type{T1<:Real,T2<:Real}(::Pow, ::Type{T1}, ::Type{Complex{T2}}) = Complex{fptype(promote_type(T1, T2))} -result_type{T1<:Real,T2<:Real}(::Pow, ::Type{Complex{T1}}, ::Type{Complex{T2}}) = +result_type{T1<:Real,T2<:Real}(::Pow, ::Type{Complex{T1}}, ::Type{Complex{T2}}) = Complex{fptype(promote_type(T1, T2))} result_type{T<:Real, Tx<:Real}(::FixPow{T}, ::Type{Tx}) = result_type(Pow(), Tx, T) @@ -160,35 +170,45 @@ result_type{T<:Real, Tx<:Real}(::FixAbsPow{T}, ::Type{Tx}) = result_type(Pow(), # max & min -result_type{T<:Real}(::Union(MaxFun,MinFun), ::Type{T}, ::Type{T}) = T -result_type{T1<:Real,T2<:Real}(::Union(MaxFun,MinFun), ::Type{T1}, ::Type{T2}) = promote_type(T1, T2) +@compat result_type{T<:Real}(::Union{MaxFun,MinFun}, ::Type{T}, ::Type{T}) = T +@compat result_type{T1<:Real,T2<:Real}(::Union{MaxFun,MinFun}, ::Type{T1}, ::Type{T2}) = promote_type(T1, T2) # quotient & module -result_type{T1<:Real,T2<:Real}(::Union(DivFun,RemFun), ::Type{T1}, ::Type{T2}) = promote_type(T1, T2) - -result_type{T1<:Signed,T2<:Unsigned}(::Union(DivFun,RemFun), ::Type{T1}, ::Type{T2}) = - signedtype(promote_type(T1, T2)) -result_type{T1<:Unsigned,T2<:Signed}(::Union(DivFun,RemFun), ::Type{T1}, ::Type{T2}) = - unsignedtype(promote_type(T1, T2)) - -result_type{T1<:Real,T2<:Real}(::FldFun, ::Type{T1}, ::Type{T2}) = +@compat result_type{T1<:Real,T2<:Real}(::Union{DivFun,RemFun}, ::Type{T1}, ::Type{T2}) = promote_type(T1, T2) + +if VERSION >= v"0.4-dev" + @compat result_type{T1<:Signed,T2<:Unsigned}(op::Union{DivFun,RemFun}, ::Type{T1}, ::Type{T2}) = + signedtype(result_type(op, unsignedtype(T1), T2)) + @compat result_type{T1<:Unsigned,T2<:Signed}(op::Union{DivFun,RemFun}, ::Type{T1}, ::Type{T2}) = + result_type(op, T1, unsignedtype(T2)) +else + @compat result_type{T1<:Signed,T2<:Unsigned}(::Union{DivFun,RemFun}, ::Type{T1}, ::Type{T2}) = + signedtype(promote_type(T1, T2)) + @compat result_type{T1<:Unsigned,T2<:Signed}(::Union{DivFun,RemFun}, ::Type{T1}, ::Type{T2}) = + unsignedtype(promote_type(T1, T2)) +end + +result_type{T1<:Real,T2<:Real}(::FldFun, ::Type{T1}, ::Type{T2}) = arithtype(T1, T2) -result_type{T1<:Union(Unsigned,Bool), T2<:Union(Unsigned,Bool)}(::FldFun, ::Type{T1}, ::Type{T2}) = +@compat result_type{T1<:Union{Unsigned,Bool}, T2<:Union{Unsigned,Bool}}(::FldFun, ::Type{T1}, ::Type{T2}) = promote_type(T1, T2) -result_type{T1<:Signed,T2<:Unsigned}(::FldFun, ::Type{T1}, ::Type{T2}) = - signedtype(promote_type(T1, T2)) -result_type{T1<:Unsigned,T2<:Signed}(::FldFun, ::Type{T1}, ::Type{T2}) = - unsignedtype(promote_type(T1, T2)) + +result_type{T1<:Signed,T2<:Unsigned}(::FldFun, ::Type{T1}, ::Type{T2}) = + result_type(DivFun(), T1, T2) +result_type{T1<:Unsigned,T2<:Signed}(::FldFun, ::Type{T1}, ::Type{T2}) = + result_type(DivFun(), T1, T2) result_type{T1<:Real,T2<:Real}(::ModFun, ::Type{T1}, ::Type{T2}) = promote_type(T1, T2) -result_type{T1<:Signed,T2<:Unsigned}(::ModFun, ::Type{T1}, ::Type{T2}) = unsignedtype(promote_type(T1, T2)) -result_type{T1<:Unsigned,T2<:Signed}(::ModFun, ::Type{T1}, ::Type{T2}) = signedtype(promote_type(T1, T2)) +result_type{T1<:Signed,T2<:Unsigned}(::ModFun, ::Type{T1}, ::Type{T2}) = + unsignedtype(result_type(DivFun(), T1, T2)) +result_type{T1<:Unsigned,T2<:Signed}(::ModFun, ::Type{T1}, ::Type{T2}) = + signedtype(result_type(DivFun(), T1, T2)) # fma -result_type{T1<:Number,T2<:Number,T3<:Number}(::FMA, ::Type{T1}, ::Type{T2}, ::Type{T3}) = +result_type{T1<:Number,T2<:Number,T3<:Number}(::FMA, ::Type{T1}, ::Type{T2}, ::Type{T3}) = result_type(Add(), T1, result_type(Multiply(), T2, T3)) # ifelse @@ -205,86 +225,87 @@ result_type(::Or, ::Type{Bool}, ::Type{Bool}) = Bool result_type{T<:Integer}(::BitwiseNot, ::Type{T}) = T -result_type{T1<:Integer,T2<:Integer}(::Union(BitwiseAnd,BitwiseOr,BitwiseXor), ::Type{T1}, ::Type{T2}) = +@compat result_type{T1<:Integer,T2<:Integer}(::Union{BitwiseAnd,BitwiseOr,BitwiseXor}, ::Type{T1}, ::Type{T2}) = promote_type(T1, T2) # comparison -result_type{T1<:Real,T2<:Real}(::Union(LT,GT,LE,GE), ::Type{T1}, ::Type{T2}) = Bool -result_type{T1<:Number,T2<:Number}(::Union(EQ,NE), ::Type{T1}, ::Type{T2}) = Bool +@compat result_type{T1<:Real,T2<:Real}(::Union{LT,GT,LE,GE}, ::Type{T1}, ::Type{T2}) = Bool +@compat result_type{T1<:Number,T2<:Number}(::Union{EQ,NE}, ::Type{T1}, ::Type{T2}) = Bool # rounding -result_type{T<:Real}(::Union(FloorFun, CeilFun, TruncFun, RoundFun), ::Type{T}) = T -result_type{T<:Integer}(::Union(IfloorFun, IceilFun, ItruncFun, IroundFun), ::Type{T}) = T -result_type{T<:FloatingPoint}(::Union(IfloorFun, IceilFun, ItruncFun, IroundFun), ::Type{T}) = Int64 +@compat result_type{T<:Real}(::Union{FloorFun, CeilFun, TruncFun, RoundFun}, ::Type{T}) = T +@compat result_type{T<:Integer}(::Union{IfloorFun, IceilFun, ItruncFun, IroundFun}, ::Type{T}) = T +@compat result_type{T<:AbstractFloat}(::Union{IfloorFun, IceilFun, ItruncFun, IroundFun}, ::Type{T}) = Int64 # number classification -result_type{T<:Real}(::Union(IsnanFun, IsinfFun, IsfiniteFun), ::Type{T}) = Bool +@compat result_type{T<:Real}(::Union{IsnanFun, IsinfFun, IsfiniteFun}, ::Type{T}) = Bool # algebraic functions -result_type{T<:Number}(::Union(SqrtFun,CbrtFun,RsqrtFun,RcbrtFun), ::Type{T}) = fptype(T) +@compat result_type{T<:Number}(::Union{SqrtFun,CbrtFun,RsqrtFun,RcbrtFun}, ::Type{T}) = fptype(T) result_type{T1<:Real,T2<:Real}(::HypotFun, ::Type{T1}, ::Type{T2}) = promote_type(fptype(T1), fptype(T2)) # exponential & logarithm -result_type{T<:Number}(::Union(ExpFun,Exp2Fun,Exp10Fun), ::Type{T}) = fptype(T) -result_type{T<:Number}(::Union(LogFun,Log2Fun,Log10Fun), ::Type{T}) = fptype(T) -result_type{T<:Real}(::Union(Expm1Fun,Log1pFun), ::Type{T}) = fptype(T) +@compat result_type{T<:Number}(::Union{ExpFun,Exp2Fun,Exp10Fun}, ::Type{T}) = fptype(T) +@compat result_type{T<:Number}(::Union{LogFun,Log2Fun,Log10Fun}, ::Type{T}) = fptype(T) +@compat result_type{T<:Real}(::Union{Expm1Fun,Log1pFun}, ::Type{T}) = fptype(T) -result_type{T<:Real}(::Union(XlogxFun,LogisticFun,LogitFun,SoftplusFun,InvsoftplusFun), ::Type{T}) = fptype(T) +@compat result_type{T<:Real}(::Union{XlogxFun,LogisticFun,LogitFun,SoftplusFun,InvsoftplusFun}, ::Type{T}) = fptype(T) result_type{T1<:Real,T2<:Real}(::XlogyFun, ::Type{T1}, ::Type{T2}) = fptype(promote_type(T1, T2)) result_type{T1<:Real,T2<:Real}(::LogsumexpFun, ::Type{T1}, ::Type{T2}) = fptype(promote_type(T1, T2)) # trigonometric functions -result_type{T<:Number}(::Union(SinFun,CosFun,TanFun,CotFun,SecFun,CscFun), ::Type{T}) = fptype(T) -result_type{T<:Number}(::Union(AsinFun,AcosFun,AtanFun,AcotFun,AsecFun,AcscFun), ::Type{T}) = fptype(T) -result_type{T<:Number}(::Union(SincFun,CoscFun,SinpiFun,CospiFun), ::Type{T}) = fptype(T) -result_type{T<:Number}(::Union(SindFun,CosdFun,TandFun,CotdFun,SecdFun,CscdFun), ::Type{T}) = fptype(T) -result_type{T<:Number}(::Union(AsindFun,AcosdFun,AtandFun,AcotdFun,AsecdFun,AcscdFun), ::Type{T}) = fptype(T) +@compat result_type{T<:Number}(::Union{SinFun,CosFun,TanFun,CotFun,SecFun,CscFun}, ::Type{T}) = fptype(T) +@compat result_type{T<:Number}(::Union{AsinFun,AcosFun,AtanFun,AcotFun,AsecFun,AcscFun}, ::Type{T}) = fptype(T) +@compat result_type{T<:Number}(::Union{SincFun,CoscFun,SinpiFun,CospiFun}, ::Type{T}) = fptype(T) +@compat result_type{T<:Number}(::Union{SindFun,CosdFun,TandFun,CotdFun,SecdFun,CscdFun}, ::Type{T}) = fptype(T) +@compat result_type{T<:Number}(::Union{AsindFun,AcosdFun,AtandFun,AcotdFun,AsecdFun,AcscdFun}, ::Type{T}) = fptype(T) result_type{T<:Integer}(::SincFun, ::Type{T}) = T -result_type{T<:Integer}(::SinpiFun, ::Type{T}) = T -result_type{T<:Integer}(::CospiFun, ::Type{T}) = arithtype(T) +if VERSION < v"0.4-dev" + result_type{T<:Integer}(::SinpiFun, ::Type{T}) = T + result_type{T<:Integer}(::CospiFun, ::Type{T}) = arithtype(T) +end result_type{T1<:Real,T2<:Real}(::Atan2Fun, ::Type{T1}, ::Type{T2}) = promote_type(fptype(T1), fptype(T2)) # hyperbolic functions -result_type{T<:Number}(::Union(SinhFun,CoshFun,TanhFun,CothFun,SechFun,CschFun), ::Type{T}) = fptype(T) -result_type{T<:Number}(::Union(AsinhFun,AcoshFun,AtanhFun,AcothFun,AsechFun,AcschFun), ::Type{T}) = fptype(T) +@compat result_type{T<:Number}(::Union{SinhFun,CoshFun,TanhFun,CothFun,SechFun,CschFun}, ::Type{T}) = fptype(T) +@compat result_type{T<:Number}(::Union{AsinhFun,AcoshFun,AtanhFun,AcothFun,AsechFun,AcschFun}, ::Type{T}) = fptype(T) # erf & friends -result_type{T<:Number}(::Union(ErfFun,ErfcFun,ErfiFun,ErfcxFun), ::Type{T}) = fptype(T) -result_type{T<:Number}(::Union(ErfinvFun,ErfcinvFun), ::Type{T}) = fptype(T) +@compat result_type{T<:Number}(::Union{ErfFun,ErfcFun,ErfiFun,ErfcxFun}, ::Type{T}) = fptype(T) +@compat result_type{T<:Number}(::Union{ErfinvFun,ErfcinvFun}, ::Type{T}) = fptype(T) # gamma, beta, & friends -result_type{T<:Number}(::Union(GammaFun,LgammaFun,EtaFun,ZetaFun), ::Type{T}) = fptype(T) -result_type{T<:Integer}(::Union(EtaFun,ZetaFun), ::Type{T}) = Float64 +@compat result_type{T<:Number}(::Union{GammaFun,LgammaFun,EtaFun,ZetaFun}, ::Type{T}) = fptype(T) +@compat result_type{T<:Integer}(::Union{EtaFun,ZetaFun}, ::Type{T}) = Float64 -result_type(::Union(GammaFun,LgammaFun), ::Type{Complex64}) = Complex128 +@compat result_type(::Union{GammaFun,LgammaFun}, ::Type{Complex64}) = Complex128 -result_type{T<:FloatingPoint}(::DigammaFun, ::Type{T}) = T +result_type{T<:AbstractFloat}(::DigammaFun, ::Type{T}) = T result_type{T<:Integer}(::DigammaFun, ::Type{T}) = Float64 -result_type{T1<:Real,T2<:Real}(::Union(BetaFun,LbetaFun), ::Type{T1}, ::Type{T2}) = +@compat result_type{T1<:Real,T2<:Real}(::Union{BetaFun,LbetaFun}, ::Type{T1}, ::Type{T2}) = promote_type(fptype(T1),fptype(T2)) -result_type{T1<:Integer,T2<:Integer}(::Union(BetaFun,LbetaFun), ::Type{T1}, ::Type{T2}) = Float64 +@compat result_type{T1<:Integer,T2<:Integer}(::Union{BetaFun,LbetaFun}, ::Type{T1}, ::Type{T2}) = Float64 # airy & friends -result_type{T<:Number}(::Union(AiryFun,AiryprimeFun,AiryaiFun,AiryaiprimeFun,AirybiFun,AirybiprimeFun),::Type{T}) = +@compat result_type{T<:Number}(::Union{AiryFun,AiryprimeFun,AiryaiFun,AiryaiprimeFun,AirybiFun,AirybiprimeFun},::Type{T}) = fptype(T) # bessel & friends -result_type{T<:Number}(::Union(Besselj0Fun, Besselj1Fun, Bessely0Fun, Bessely1Fun), ::Type{T}) = fptype(T) -result_type{T<:Number}(::Union(BesseliFun, BesseljFun, BesselkFun, BesselyFun), ::Type{T}) = fptype(T) -result_type{T<:Number}(::Union(Hankelh1Fun, Hankelh2Fun), ::Type{T}) = fptype(T) - +@compat result_type{T<:Number}(::Union{Besselj0Fun, Besselj1Fun, Bessely0Fun, Bessely1Fun}, ::Type{T}) = fptype(T) +@compat result_type{T<:Number}(::Union{BesseliFun, BesseljFun, BesselkFun, BesselyFun}, ::Type{T}) = fptype(T) +@compat result_type{T<:Number}(::Union{Hankelh1Fun, Hankelh2Fun}, ::Type{T}) = fptype(T) diff --git a/test/functors.jl b/test/functors.jl index a890d8e..fbf804f 100644 --- a/test/functors.jl +++ b/test/functors.jl @@ -1,16 +1,16 @@ using NumericFuns using Base.Test - +using Compat println(" arithmetic operators") @test evaluate(Negate(), 2) == -2 -for (F, sf) in [(Add, +), - (Subtract, -), - (Multiply, *), - (Divide, /), - (RDivide, \), +for (F, sf) in [(Add, +), + (Subtract, -), + (Multiply, *), + (Divide, /), + (RDivide, \), (Pow, ^)] @test evaluate(F(), 6, 2) == sf(6, 2) @@ -19,10 +19,10 @@ end println(" comparison operators") -for (F, sf) in [(LT, <), +for (F, sf) in [(LT, <), (GT, >), - (LE, <=), - (GE, >=), + (LE, <=), + (GE, >=), (EQ, ==), (NE, !=)] @@ -90,14 +90,14 @@ end println(" rounding functions") -for (F, sf) in [(FloorFun, floor), - (CeilFun, ceil), - (TruncFun, trunc), - (RoundFun, round), - (IfloorFun, ifloor), - (IceilFun, iceil), - (ItruncFun, itrunc), - (IroundFun, iround)] +for (F, sf) in [(FloorFun, floor), + (CeilFun, ceil), + (TruncFun, trunc), + (RoundFun, round), + (IfloorFun, x->floor(Integer, x)), + (IceilFun, x->ceil(Integer, x)), + (ItruncFun, x->trunc(Integer, x)), + (IroundFun, x->round(Integer, x))] @test evaluate(F(), 3.4) === sf(3.4) @test evaluate(F(), 3.8) === sf(3.8) diff --git a/test/rtypes.jl b/test/rtypes.jl index a4788fb..8a1a169 100644 --- a/test/rtypes.jl +++ b/test/rtypes.jl @@ -1,5 +1,6 @@ using NumericFuns using Base.Test +using Compat ## auxiliary function @@ -22,26 +23,26 @@ end ## type categories -const inttypes = [Int8, - Int16, - Int32, - Int64, - Int128, - Uint8, - Uint16, - Uint32, - Uint64, - Uint128] +const inttypes = [Int8, + Int16, + Int32, + Int64, + Int128, + UInt8, + UInt16, + UInt32, + UInt64, + UInt128] const integraltypes = [Bool, inttypes...] const fptypes = [Float32, Float64] const realtypes = [integraltypes..., fptypes...] const complextypes = [Complex{Bool}, - Complex{Int16}, - Complex{Int32}, - Complex{Int64}, - Complex64, + Complex{Int16}, + Complex{Int32}, + Complex{Int64}, + Complex64, Complex128] const numerictypes_r = [realtypes..., Complex64, Complex128] @@ -52,7 +53,7 @@ const numerictypes = [realtypes..., complextypes...] println(" fptype") for t in realtypes - ft = typeof(convert(FloatingPoint, zero(t))) + ft = typeof(convert(AbstractFloat, zero(t))) @test fptype(t) == ft end @@ -66,7 +67,7 @@ println(" arithmetics (unary)") for F in [Negate, AbsFun, Abs2Fun, RealFun, ImagFun, SqrFun, RcpFun] for T in numerictypes - check_rtype(F(), T) + check_rtype(F(), T) end end @@ -102,7 +103,7 @@ for F in [LT, GT, LE, GE, EQ, NE] println(" $F") for T1 in realtypes, T2 in realtypes check_rtype(F(), T1, T2) - end + end end @@ -206,7 +207,7 @@ end println(" hyperbolic functions") -for F in [SinhFun, CoshFun, TanhFun, CothFun, SechFun, CschFun, +for F in [SinhFun, CoshFun, TanhFun, CothFun, SechFun, CschFun, AsinhFun, AcoshFun, AtanhFun, AcothFun, AsechFun, AcschFun] for T in numerictypes_r check_rtype(F(), T) @@ -244,7 +245,7 @@ println(" beta & lbeta") for F in [BetaFun, LbetaFun] for T1 in realtypes, T2 in realtypes check_rtype(F(), T1, T2) - end + end end println(" airy & friends") @@ -262,4 +263,3 @@ for F in [Besselj0Fun, Besselj1Fun, Bessely0Fun, Bessely1Fun] check_rtype(F(), T) end end - diff --git a/test/runtests.jl b/test/runtests.jl new file mode 100644 index 0000000..7ebcebf --- /dev/null +++ b/test/runtests.jl @@ -0,0 +1,7 @@ +tests = ["mathfuns", "functors", "rtypes"] + +for t in tests + fp = "$(t).jl" + println("running $(fp) ...") + include(fp) +end