Giter Club home page Giter Club logo

numericfuns.jl's Introduction

NumericFuns

Numerical functions and functors. Build Status

Note: This package was originally part of the NumericExtensions package. I realized later that the functors and the type inference machinery can be useful in other packages. Hence, I separate this part to construct a standalone package.

This package provides:

  • Additional numerical functions, such as sqr, rsqrt, xlogx, sigmoid, logit, etc.
  • Vectorized methods of the additional numerical functions.
  • Typed functors.

New Numeric Functions

This package provides several commonly used numerical functions that are not in the Julia Base.

function equivalent expression
sqr(x) x * x
rcp(x) 1 / x
rsqrt(x) 1 / sqrt(x)
rcbrt(x) 1 / cbrt(x)
xlogx(x) ifelse(x > 0, x * log(x), 0)
xlogy(x, y) ifelse(x > 0, x * log(y), 0)
sigmoid(x) 1 / (1 + exp(-x))
logit(x) log(x / (1 - x))
softplus(x) log(1 + exp(x))
invsoftplus(x) log(exp(x) - 1)
logsumexp(x, y) log(exp(x) + exp(y))

Note that the equivalent expressions above are just for the purpose to conveying the semantics. The actual implementation might be different, which would takes a more optimal route that takes care of risk of overflow, type stability, and computational efficiency.

Functors

Functors are typed instances used in indicate a particular function. Since Julia is not able to specialize on functions (yet), functors provide an effective way that allow mutliple dispatch and functional programming to work together.

The package defines an abstract type Functor as

abstract Functor{N}

where, N is an integer indicating the number of arguments. All functor types are subtypes of Functor.

Each functor type comes with an evaluate method, which evaluates the corresponding function given arguments.

Define a functor

Here is an example that illustrates how one can define a functor

type Add <: Functor{2} end
evaluate{T1<:Number,T2<:Number}(::Add, x::Number, y::Number) = x + y

Two macros @functor1 and @functor2 are provided for simplifying the definition of unary and binary functors:

@functor1(Cbrt, cbrt, Real)
@functor2(Add, +, Number)

These macros accept three arguments: the functor type name, the corresponding function, and the super type of all acceptable argument types.

Note: The packages also defines a large collection of functors for various mathematical operations (so you don't have to define them yourself).

Functors for operators

Here is a table of functor types for operators:

functor type operator domain
Negate - Number
Add + Number
Subtract - Number
Multiply * Number
Divide / Number
RDivide \ Number
Pow ^ Number
And & Bool
Or | Bool
Not ! Bool
BitwiseAnd & Integer
BitwiseOr | Integer
BitwiseNot ~ Integer
BitwiseXor $ Integer
LT < Real
GT > Real
LE <= Real
GE >= Real
EQ == Number
NE != Number

Functors for math functions

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:

  • arithmetic functions

    abs, abs2, real, imag, sqr, rcp,
    sign, signbit, div, fld, rem, mod
    
  • rounding functions

    floor, ceil, trunc, round,
    ifloor, iceil, itrunc, iround
    
  • number classification functions

    isnan, isinf, isfinite

  • algebraic functions

    sqrt, rsqrt, cbrt, rcbrt, hypot

  • exponential & logarithm

    exp, exp2, exp10, expm1,
    log, log2, log10, log1p,
    
    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,
    asind, acosd, atand, acotd, asecd, acscd
    
  • hyperbolic functions

    sinh, cosh, tanh, coth, sech, csch,
    asinh, acosh, atanh, acoth, asech, acsch
    
  • special functions

    erf, erfc, erfinv, erfcinv, erfi, erfcx,
    gamma, lgamma, digamma,
    eta, zeta, beta, lbeta,
    airy, airyprime, airyai, airyaiprime, airybi, airybiprime,
    besselj0, besselj1, bessely0, bessely1
    besseli, besselj, besselk, bessely,
    hankelh1, hankelh2
    

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,

result_type(Add(), Int, Float64)  # --> returns Float64
result_type(SqrtFun(), Int)   # --> returns Float64

The package also provides other convenient methods for type inference, which include fptype and arithtype. Particularly, we have

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.

numericfuns.jl's People

Contributors

lindahua avatar yuyichao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

numericfuns.jl's Issues

[PkgEval] NumericFuns may have a testing issue on Julia 0.3 (2014-06-03)

PackageEvaluator.jl is a script that runs nightly. It attempts to load all Julia packages and run their tests (if available) on both the stable version of Julia (0.2) and the nightly build of the unstable version (0.3). The results of this script are used to generate a package listing enhanced with testing results.

On Julia 0.3

  • On 2014-06-02 the testing status was Tests pass..
  • On 2014-06-03 the testing status changed to Tests fail, but package loads..

Tests pass. means that PackageEvaluator found the tests for your package, executed them, and they all passed.

Tests fail, but package loads. means that PackageEvaluator found the tests for your package, executed them, and they didn't pass. However, trying to load your package with using worked.

This issue was filed because your testing status became worse. No additional issues will be filed if your package remains in this state, and no issue will be filed if it improves. If you'd like to opt-out of these status-change messages, reply to this message saying you'd like to and @IainNZ will add an exception. If you'd like to discuss PackageEvaluator.jl please file an issue at the repository. For example, your package may be untestable on the test machine due to a dependency - an exception can be added.

Test log:

INFO: Installing NumericFuns v0.2.3
INFO: Package database updated
ERROR: result_type(GammaFun(), Int8) should be Float64 (but predicted Float32)
 in error at error.jl:21
 in check_rtype at /home/idunning/pkgtest/.julia/v0.3/NumericFuns/test/rtypes.jl:10
 in anonymous at no file:234
 in include at boot.jl:244
 in anonymous at no file:6
 in include at boot.jl:244
 in include_from_node1 at loading.jl:128
while loading /home/idunning/pkgtest/.julia/v0.3/NumericFuns/test/rtypes.jl, in expression starting on line 232
while loading /home/idunning/pkgtest/.julia/v0.3/NumericFuns/runtests.jl, in expression starting on line 3
INFO: Package database updated

[PackageEvaluator.jl] Your package NumericFuns may have a testing issue.

This issue is being filed by a script, but if you reply, I will see it.

PackageEvaluator.jl is a script that runs nightly. It attempts to load all Julia packages and run their test (if available) on both the stable version of Julia (0.2) and the nightly build of the unstable version (0.3).

The results of this script are used to generate a package listing enhanced with testing results.

The status of this package, NumericFuns, on...

  • Julia 0.2 is 'Package was untestable.' PackageEvaluator.jl
  • Julia 0.3 is 'Tests fail, but package loads.' PackageEvaluator.jl

'No tests, but package loads.' can be due to their being no tests (you should write some if you can!) but can also be due to PackageEvaluator not being able to find your tests. Consider adding a test/runtests.jl file.

'Package doesn't load.' is the worst-case scenario. Sometimes this arises because your package doesn't have BinDeps support, or needs something that can't be installed with BinDeps. If this is the case for your package, please file an issue and an exception can be made so your package will not be tested.

This automatically filed issue is a one-off message. Starting soon, issues will only be filed when the testing status of your package changes in a negative direction (gets worse). If you'd like to opt-out of these status-change messages, reply to this message.

Info about upcoming removal of packages in the General registry

As described in https://discourse.julialang.org/t/ann-plans-for-removing-packages-that-do-not-yet-support-1-0-from-the-general-registry/ we are planning on removing packages that do not support 1.0 from the General registry. This package has been detected to not support 1.0 and is thus slated to be removed. The removal of packages from the registry will happen approximately a month after this issue is open.

To transition to the new Pkg system using Project.toml, see https://github.com/JuliaRegistries/Registrator.jl#transitioning-from-require-to-projecttoml.
To then tag a new version of the package, see https://github.com/JuliaRegistries/Registrator.jl#via-the-github-app.

If you believe this package has erroneously been detected as not supporting 1.0 or have any other questions, don't hesitate to discuss it here or in the thread linked at the top of this post.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.