MyNixOS website logo
Description

Numerical integration.

One-dimensional numerical integration using the NumericalIntegration C++ library.

numerical-integration

One-dimensional numerical integration using the 'NumericalIntegration' C++ library.


Example. Integrate x² between 0 and 1 with desired absolute error 0, desired relative error 1e-5 and using 200 subdivisions. Exact value: 1/3.

example :: IO IntegralResult -- value, error estimate, error code
example = integration (\x -> x*x) 0 1 0.0 1e-5 200
-- IntegralResult {
--   _value = 0.3333333333333334, 
--   _error = 3.7007434154171895e-15, 
--   _code = 0
-- }

The error code 0 indicates the success.


A highly oscillatory function. The function shown below is highly oscillatory. It is know that the exact value of its integral from 0 to 1 is π exp(-10) / 2 ≈ 7.131404e-05.

Let's try to evaluate it with R with 200000 subdivisions.

f <- function(x) {
  5*cos(2*x/(1-x)) / (25*(1-x)**2 + x**2)
}
integrate(f, 0, 1, subdivisions = 200000)
# 7.76249e-05 with absolute error < 3.7e-05

R does not complain, however the result is not very good.

Now let's try with the present library, with 100000 subdivisions.

intgr :: IO IntegralResult 
intgr = integration (\t -> 5 * cos(2*t/(1-t)) / (25*(1-t)**2 + t**2)) 0 1 0.0 1e-4 100000
-- IntegralResult {
--    _value = 7.131328051415349e-5, 
--    _error = 4.991435083852171e-7, 
--    _code = 2
-- }

As compared to R, the computation is very slow. But the result is quite better. Note that the error code is 2, thereby indicating a failure of convergence. So let's try 250000 subdivisions. This will take a while.

intgr :: IO IntegralResult 
intgr = integration (\t -> 5 * cos(2*t/(1-t)) / (25*(1-t)**2 + t**2)) 0 1 0.0 1e-4 250000
-- IntegralResult {
--   _value = 7.131328051415349e-5, 
--   _error = 4.991435083852171e-7, 
--   _code = 2
-- }

The result is the same!


The tanh-sinh procedure. I don't master the Haskell library 'integration' but I give it a try below. It implements the tanh-sinh quadrature.

import Numeric.Integration.TanhSinh

tanhsinh :: Result
tanhsinh = absolute 1e-6 $ parTrap (\t -> 5 * cos(2*t/(1-t)) / (25*(1-t)**2 + t**2)) 0 1
-- Result {
--   result = -6.463872646093162e-3, 
--   errorEstimate = 2.577460946077898e-2, 
--   evaluations = 769
-- }

The result is totally wrong, which is not surprising in view of the weak number of evaluations. But again, I don't master this library so I will not conclude anything from this result.

Metadata

Version

0.1.2.3

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows