gonum: gonum.org/v1/gonum/integrate/quad Index | Examples | Files

package quad

import "gonum.org/v1/gonum/integrate/quad"

Package quad provides numerical evaluation of definite integrals of single-variable functions.

Code:

fmt.Println("Evaluate the expected value of x^2 + 3 under a Weibull distribution")
f := func(x float64) float64 {
    d := distuv.Weibull{Lambda: 1, K: 1.5}
    return (x*x + 3) * d.Prob(x)
}
ev := quad.Fixed(f, 0, math.Inf(1), 10, nil, 0)
fmt.Printf("EV with 10 points = %0.6v\n", ev)

ev = quad.Fixed(f, 0, math.Inf(1), 30, nil, 0)
fmt.Printf("EV with 30 points = %0.6v\n", ev)

ev = quad.Fixed(f, 0, math.Inf(1), 100, nil, 0)
fmt.Printf("EV with 100 points = %0.6v\n", ev)

ev = quad.Fixed(f, 0, math.Inf(1), 10000, nil, 0)
fmt.Printf("EV with 10000 points = %0.6v\n\n", ev)

fmt.Println("Estimate using parallel evaluations of f.")
concurrent := runtime.GOMAXPROCS(0)
ev = quad.Fixed(f, 0, math.Inf(1), 100, nil, concurrent)
fmt.Printf("EV = %0.6v\n", ev)

Output:

Evaluate the expected value of x^2 + 3 under a Weibull distribution
EV with 10 points = 4.20175
EV with 30 points = 4.19066
EV with 100 points = 4.19064
EV with 10000 points = 4.19064

Estimate using parallel evaluations of f.
EV = 4.19064

Index

Examples

Package Files

doc.go hermite.go hermite_data.go legendre.go quad.go

func Fixed Uses

func Fixed(f func(float64) float64, min, max float64, n int, rule FixedLocationer, concurrent int) float64

Fixed approximates the integral of the function f from min to max using a fixed n-point quadrature rule. During evaluation, f will be evaluated n times using the weights and locations specified by rule. That is, Fixed estimates

int_min^max f(x) dx ≈ \sum_i w_i f(x_i)

If rule is nil, an acceptable default is chosen, otherwise it is assumed that the properties of the integral match the assumptions of rule. For example, Legendre assumes that the integration bounds are finite. If rule is also a FixedLocationSingler, the quadrature points are computed individually rather than as a unit.

If concurrent <= 0, f is evaluated serially, while if concurrent > 0, f may be evaluated with at most concurrent simultaneous evaluations.

min must be less than or equal to max, and n must be positive, otherwise Fixed will panic.

type FixedLocationSingler Uses

type FixedLocationSingler interface {
    FixedLocationSingle(n, k int, min, max float64) (x, weight float64)
}

FixedLocationSingle returns the location and weight for element k in a fixed quadrature rule with n total samples and integral bounds from min to max.

type FixedLocationer Uses

type FixedLocationer interface {
    FixedLocations(x, weight []float64, min, max float64)
}

FixedLocationer computes a set of quadrature locations and weights and stores them in-place into x and weight respectively. The number of points generated is equal to the len(x). The weights and locations should be chosen such that

int_min^max f(x) dx ≈ \sum_i w_i f(x_i)

type Hermite Uses

type Hermite struct{}

Hermite generates sample locations and weights for performing quadrature with with a squared-exponential weight

int_-inf^inf e^(-x^2) f(x) dx .

func (Hermite) FixedLocations Uses

func (h Hermite) FixedLocations(x, weight []float64, min, max float64)

type Legendre Uses

type Legendre struct{}

Legendre integrates an unweighted function over finite bounds

int_min^max f(x) dx

func (Legendre) FixedLocationSingle Uses

func (l Legendre) FixedLocationSingle(n, k int, min, max float64) (x, weight float64)

func (Legendre) FixedLocations Uses

func (l Legendre) FixedLocations(x, weight []float64, min, max float64)

Package quad imports 4 packages (graph) and is imported by 1 packages. Updated 2017-11-02. Refresh now. Tools for package owners.