quad

package
v0.15.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 16, 2024 License: BSD-3-Clause Imports: 4 Imported by: 15

Documentation

Overview

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

Example
package main

import (
	"fmt"
	"math"
	"runtime"

	"gonum.org/v1/gonum/integrate/quad"
	"gonum.org/v1/gonum/stat/distuv"
)

func main() {
	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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fixed

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.

Types

type FixedLocationSingler

type FixedLocationSingler interface {
	// 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.
	FixedLocationSingle(n, k int, min, max float64) (x, weight float64)
}

FixedLocationSingler wraps the FixedLocationSingle method.

type FixedLocationer

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

type Hermite struct{}

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

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

func (Hermite) FixedLocations

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

type Legendre

type Legendre struct{}

Legendre integrates an unweighted function over finite bounds

int_min^max f(x) dx

func (Legendre) FixedLocationSingle

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

func (Legendre) FixedLocations

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL