Giter Club home page Giter Club logo

itp's Introduction

itp

AppVeyor Build Status R-CMD-check Coverage Status CRAN_Status_Badge Downloads (monthly) Downloads (total)

The Interpolate, Truncate, Project (ITP) Root-Finding Algorithm

The itp package implements the Interpolate, Truncate, Project (ITP) root-finding algorithm of Oliveira and Takahashi (2021). Each iteration of the algorithm results in a bracketing interval for the root that is narrower than the previous interval. It’s performance compares favourably with existing methods on both well-behaved functions and ill-behaved functions while retaining the worst-case reliability of the bisection method. For details see the authors’ Kudos summary and the Wikipedia article ITP method.

Examples

We use three examples from Section 3 of Oliveira and Takahashi (2021) to illustrate the use of the itp function. Each of these functions has a root in the interval $(-1, 1)$. The function can be supplied either as an R function or as an external pointer to a C++ function.

library(itp)

A continuous function

The Lambert function $l(x) = xe^x - 1$ is continuous.

The itp function finds an estimate of the root, that is, $x^{\ast}$ for which $f(x^{\ast})$ is (approximately) equal to 0. The algorithm continues until the length of the interval that brackets the root is smaller than $2 \epsilon$, where $\epsilon$ is a user-supplied tolerance. The default is $\epsilon = 10^{-10}$.

First, we supply an R function that evaluates the Lambert function.

# Lambert, using an R function
lambert <- function(x) x * exp(x) - 1
itp(lambert, c(-1, 1))
#> function: lambert 
#>       root     f(root)  iterations  
#>     0.5671   2.048e-12           8

Now, we create an external pointer to a C++ function that has been provided in the itp package and pass this pointer to the function itp(). For more information see the Overview of the itp package vignette.

# Lambert, using an external pointer to a C++ function
lambert_ptr <- xptr_create("lambert")
itp(lambert_ptr, c(-1, 1))
#> function: lambert_ptr 
#>       root     f(root)  iterations  
#>     0.5671   2.048e-12           8

The function itp_c

Also provided is the function itp_c, which is equivalent to itp, but the calculations are performed entirely using C++, and the arguments differ slightly: itp_c has a named required argument pars rather than ... and it does not have the arguments interval, f.a or f.b.

# Calling itp_c()
res <- itp_c(lambert_ptr, pars = list(), a = -1, b = 1)
res
#> function:  
#>       root     f(root)  iterations  
#>     0.5671   2.048e-12           8

A discontinuous function

The staircase function $s(x) = \lceil 10 x - 1 \rceil + 1/2$ is discontinuous.

The itp function finds the discontinuity at $x = 0$ at which the sign of the function changes. The value of 0.5 returned for the root res$root is the midpoint of the bracketing interval [res$a, res$b] at convergence.

# Staircase
staircase <- function(x) ceiling(10 * x - 1) + 1 / 2
res <- itp(staircase, c(-1, 1))
print(res, all = TRUE)
#> function: staircase 
#>       root     f(root)  iterations           a           b         f.a  
#>  7.404e-11         0.5          31           0   1.481e-10        -0.5  
#>        f.b   precision  
#>        0.5   7.404e-11

A function with multiple roots

The Warsaw function $w(x) = I(x &gt; -1)\left(1 + \sin\left(\frac{1}{1+x}\right)\right)-1$ has multiple roots.

When the initial interval is $[-1, 1]$ the itp function finds the root $x \approx -0.6817$. There are other roots that could be found from a different initial interval.

# Warsaw
warsaw <- function(x) ifelse(x > -1, sin(1 / (x + 1)), -1)
itp(warsaw, c(-1, 1))
#> function: warsaw 
#>       root     f(root)  iterations  
#>    -0.6817  -5.472e-11          11

Installation

To get the current released version from CRAN:

install.packages("itp")

Vignette

See the Overview of the itp package vignette, which can also be accessed using vignette("itp-vignette", package = "itp").

itp's People

Contributors

paulnorthrop avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

itp's Issues

Possible error in Wikipedia's ITP article

I just learned about ITP yesterday and tried an implementation that is based on Wikipedia's pseudocode implementation of the algorithm.

It says:

if yitp > 0 then b = xitp and yb = yitp
else if yitp < 0 then a = xitp and ya = yitp
else a = xitp and y = xitp

In your implementation you multiply yITP with the sign of yb in the conditions:

    if (yITP * signyb > 0.0) {
      new_b = xITP ;
      yb = yITP ;
    } else if (yITP * signyb < 0.0) {
      new_a = xITP ;
      ya = yITP ;
    } else {

I do not understand the algorithm, but changing the conditions to multiply yitp with the sign of yb (or simply yb) made the implementation work. Also, I know from other algorithms (like Regula Falsi) that this multiplication is done in the conditions.

Since you seem to have deep understanding of the ITP method, my question would be if you can confirm that there is an error in the algorithm in the Wikipedia article.

Support for models?

Hi Paul,

Thanks for putting together this package, it's great!

I was wondering whether there was a way, or whether support was planned for calling the itp::itp() function on a model object, e.g. a model produced by stats::lm()?

Cheers,

Zeke

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.