Skip to content

Commit

Permalink
Merge pull request lindahua#5 from yuyichao/0.4-binding
Browse files Browse the repository at this point in the history
Fix for 0.4
  • Loading branch information
lindahua committed Oct 1, 2015
2 parents 59ffa79 + 76416d6 commit 947c61f
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 186 deletions.
24 changes: 8 additions & 16 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
```
Expand All @@ -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
```
Expand All @@ -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
Expand All @@ -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.

3 changes: 2 additions & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
julia 0.3-
julia 0.3
Compat
7 changes: 0 additions & 7 deletions runtests.jl

This file was deleted.

9 changes: 5 additions & 4 deletions src/NumericFuns.jl
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,6 +21,5 @@ module NumericFuns
include("mathfuns.jl")
include("functors.jl")
include("rtypes.jl")


end # module
37 changes: 19 additions & 18 deletions src/functors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ 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)
end
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)
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -311,11 +316,11 @@ export Hankelh1Fun, Hankelh2Fun
@functor1a_ord hankelh1 Number
@functor1a_ord hankelh2 Number

#######################################
#######################################
#
# Ternary functors
#
#######################################
#######################################

export FMA, IfelseFun

Expand All @@ -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)




16 changes: 8 additions & 8 deletions src/mathfuns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)...)

Expand Down
Loading

0 comments on commit 947c61f

Please sign in to comment.