gonumeth

package module
v0.0.0-...-2b94362 Latest Latest
Warning

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

Go to latest
Published: May 15, 2014 License: GPL-3.0 Imports: 2 Imported by: 0

README

gonumeth

A small numerical methods library for the Go language

Scope

This library contains several important primitives for numerical analysis.

In particular it includes functions for numerical differentiation, numerical integration, numerical solving of simple equations and of systems of equations.

License

The library is licensed under the GPLv3. For more information about the license, check the file LICENSE.

Usage

For example usage, check the file example_test.go in the root directory.

Dependencies

The only dependency of the library is the go.matrix library (https://github.com/skelterjohn/go.matrix).

Installation

The library can be installed as customary in Go:
go get github.com/yavorpap/gonumeth.

Documentation

Overview

Package gonumeth provides basic primitives for performing Numerical computations. In particular, routines for numerical differentiation, integration, equation and system solving are provided. Type is assumed to be float64.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NDifferentiateBackward

func NDifferentiateBackward(f SingleVarFunction, x float64,
	h float64) (result float64)

NDifferentiateBackward works the same way as NDifferentiateCentral, except that it uses points x, x-h, x-2*h, x-3*h and is suitable for function undefined for y>x.

Example
var diff = NDifferentiateBackward(sinFunc, 0, derivH)
fmt.Printf("%.4e\n", diff)
Output:

1.0000e+00

func NDifferentiateBackwardThreePoint

func NDifferentiateBackwardThreePoint(f SingleVarFunction, x float64,
	h float64) (result float64)

NDifferentiateBackwardThreePoint takes a single variable function (f), a value (x) and a step (h) and returns the numerical value of the derivative of f at x. Note that an appropriate value of h is essential. This function uses a 3-point forward rule with x as rightmost point.

Example
var diff = NDifferentiateBackwardThreePoint(sinFunc, 0, deriv3H)
fmt.Printf("%.4e\n", diff)
Output:

1.0000e+00

func NDifferentiateCentral

func NDifferentiateCentral(f SingleVarFunction, x float64,
	h float64) (result float64)

NDifferentiateCentral takes a single variable function (f), a value (x) and a step (h) and returns the numerical value of the derivative of f at x. Note that an appropriate value of h is essential. This function uses a 4-point rule with x at the center.

Example
var diff = NDifferentiateCentral(sinFunc, math.Pi/2, derivH)
fmt.Printf("%.4e\n", diff)
Output:

-2.7756e-14

func NDifferentiateCentralThreePoint

func NDifferentiateCentralThreePoint(f SingleVarFunction, x float64,
	h float64) (result float64)

NDifferentiateCentralThreePoint takes a single variable function (f), a value (x) and a step (h) and returns the numerical value of the derivative of f at x. Note that an appropriate value of h is essential. This function uses a 2-point rule with x at the center.

Example
var diff = NDifferentiateCentralThreePoint(sinFunc, math.Pi/2, deriv3H)
fmt.Printf("%.4e\n", diff)
Output:

0.0000e+00

func NDifferentiateForward

func NDifferentiateForward(f SingleVarFunction, x float64,
	h float64) (result float64)

NDifferentiateForward works the same way as NDifferentiateCentral, except that it uses points x, x+h, x+2*h, x+3*h and is suitable for function undefined for y<x.

Example
var diff = NDifferentiateForward(sinFunc, math.Pi, derivH)
fmt.Printf("%.4e\n", diff)
Output:

-1.0000e+00

func NDifferentiateForwardThreePoint

func NDifferentiateForwardThreePoint(f SingleVarFunction, x float64,
	h float64) (result float64)

NDifferentiateForwardThreePoint takes a single variable function (f), a value (x) and a step (h) and returns the numerical value of the derivative of f at x. Note that an appropriate value of h is essential. This function uses a 3-point forward rule with x as leftmost point.

Example
var diff = NDifferentiateForwardThreePoint(sinFunc, math.Pi, deriv3H)
fmt.Printf("%.4e\n", diff)
Output:

-1.0000e+00

func NIntegrateGaussKronrodNonAdaptive

func NIntegrateGaussKronrodNonAdaptive(f SingleVarFunction, a float64,
	b float64, goalErrorAbs float64, goalErrorRel float64) (result float64,
	errorEstimate float64)

NIntegrateGaussKronrodNonAdaptive attempts to find the numeric value of the integral of f in the interval [a, b] using the Gauss-Kronrod rules. This method does not adapt to the "stiffness" of the function. If the error is greater than any of the targets (goalErrorAbs or goalErrorRel * result), the function has failed. The method will reuse all values of the function calculated. The function returns the result and error estimation as result and errorEstimate respectively.

Example
var res, _ = NIntegrateGaussKronrodNonAdaptive(sinFunc, 0,
	math.Pi, 0.001, 0.001)
fmt.Printf("%.4e\n", res)
Output:

2.0000e+00

func NIntegrateSimpsonAdaptive

func NIntegrateSimpsonAdaptive(f SingleVarFunction, a float64, b float64,
	goalErrorAbs float64, goalErrorRel float64) (result float64,
	errorEstimate float64)

NIntegrateSimpsonAdaptive attempts to find the numeric value of the integral of f in the interval [a, b] using the Simspon rule by further "sectioning" subintervals until desired error goals are achieved. The function returns the result and error estimation as result and errorEstimate respectively.

Example
var res, _ = NIntegrateSimpsonAdaptive(sinFunc, 0,
	math.Pi/2, 0.001, 0.001)
fmt.Printf("%.4e\n", res)
Output:

9.9999e-01

func NSimpleSolveBisection

func NSimpleSolveBisection(f0 SingleVarFunction, x0 float64, maxIterations int,
	epsilon float64) (result float64)

NSimpleSolveBisection attempts to find a root of the function f starting at x0 by using the Bisection method. A `root` value of NaN means the function failed.

Example
var res = NSimpleSolveBisection(sinFunc, 3*math.Pi/4, maxIterations,
	defEpsilon)
fmt.Printf("%.4e\n", res)
Output:

3.1415e+00

func NSimpleSolveGeneric

func NSimpleSolveGeneric(f1 SingleVarFunction, x0 float64, maxIterations int,
	epsilon float64) (root float64)

Work in progress! Don't rely on this NSimpleSolveGeneric attempts to find a root of the function f starting at x0 using a variety of methods. This is *NOT* the fastest function to do so. Use at your own discretion. A `root` value of NaN means the function failed.

func NSimpleSolveHalley

func NSimpleSolveHalley(f0 SingleVarFunction, x0 float64, maxIterations int,
	epsilon float64) (root float64)

NSimpleSolveHalley attempts to find a root of the function f starting at x0 using the Halley method. This method is the (potentially) fastest method provided in this package, however it requires "very good" behavior of f locally (in particular it being differentiable and a non-zero value of the function and its first derivative). A `root` value of NaN means the function failed.

Example
var res = NSimpleSolveHalley(sinFunc, 7*math.Pi/4, maxIterations,
	defEpsilon)
fmt.Printf("%.4e\n", res)
Output:

6.2832e+00

func NSimpleSolveNewton

func NSimpleSolveNewton(f0 SingleVarFunction, x0 float64, maxIterations int,
	epsilon float64) (result float64)

NSimpleSolveNewton attempts to find a root of the function f starting at x0 using the Newton method. This method generally requires a "good" behavior of f locally (it being differentiable and having non-zero deriv.). If this is not true, the function might fail to produce the proper result. A `root` value of NaN means the function failed.

Example
var res = NSimpleSolveNewton(sinFunc, 5*math.Pi/4, maxIterations,
	defEpsilon)
fmt.Printf("%.4e\n", res)
Output:

3.1416e+00

func NSimpleSolveSecant

func NSimpleSolveSecant(f1 SingleVarFunction, x0 float64, maxIterations int,
	epsilon float64) (root float64)

NSimpleSolveSecant attempts to find a root of the function f starting at x0 using the Secant method. The function chooses a second starting point. A `root` value of NaN means the function failed.

Example
var res = NSimpleSolveSecant(sinFunc, 9*math.Pi/4, maxIterations,
	defEpsilon)
fmt.Printf("%.4e\n", res)
Output:

6.2832e+00

func NSolveSystemDeriv

func NSolveSystemDeriv(f MultiVarFunction, x0 matrix.Matrix, maxIterations int,
	epsilon float64) (root matrix.Matrix)

NSolveSystemDeriv tries to solve a nonlinear system of equations by calculating n derivatives at each step. The parameter x0 indicates the starting point of the iteration process as vector. The result is returned as `root`. A value of NaN indicates the function failed.

func NSolveSystemFixedPoint

func NSolveSystemFixedPoint(f MultiVarFunction, x0 matrix.Matrix,
	maxIterations int, epsilon float64) (root matrix.Matrix)

NSolveSystemFixedPoint tries to solve a nonlinear system of equations by the fixed-point iteration method. The result is returned as `root`. A value of nil indicates the function failed to find a root. The parameter x0 indicates the starting point of the iteration process as a vector.

Example
var x0 matrix.Matrix = matrix.Zeros(1, 2)
x0.Set(0, 0, 0.6)
x0.Set(0, 1, 0.6)
var res = NSolveSystemFixedPoint(test2d, x0, maxIterations, defEpsilon)
fmt.Printf("%.4e %.4e\n", res.Get(0, 0), res.Get(0, 1))
Output:

6.4624e-01 6.0803e-01

func NSolveSystemNewton

func NSolveSystemNewton(f MultiVarFunction, x0 matrix.Matrix, maxIterations int,
	epsilon float64) (root matrix.Matrix)

NSolveSystemNewton tries to solve a nonlinear system of equations using the Newton's method for systems. The parameter x0 indicates the starting point of the iteration process as vector. The result is returned as `root`. A value of nil indicates the function failed.

Types

type CachedSingleVarFunction

type CachedSingleVarFunction SingleVarFunction

func CacheFunction

func CacheFunction(f SingleVarFunction) (fres CachedSingleVarFunction)

type MultiVarFunction

type MultiVarFunction func(matrix.Matrix) matrix.Matrix

MultiVarFunction is a type used to indicate a function that takes multiple arguments and (in general) returns a vector. Vectors are represented as matrices, so the return type is Matrix.

type NSimpleSolver

type NSimpleSolver func(f SingleVarFunction, x0 float64, maxIterations int,
	epsilon float64) float64

A common type that all simple solvers implement

type NSolveSystemMethod

type NSolveSystemMethod func(f MultiVarFunction, x0 matrix.Matrix,
	maxIterations int, epsilon float64)

Common type that encompasses all solvers for systems of equations

type SingleVarFunction

type SingleVarFunction func(float64) float64

SingleVarFunction is a type used to represent a function that takes a single parameter. It should take a float64 argument and return a float64 result. This primitive is used throughout the package.

func NDerivative

func NDerivative(f SingleVarFunction, h float64) (f_prime SingleVarFunction)

NDerivative returns a function that approximates the original function's derivative. The derivative is calculculated locally. Essentially this is a convenience wrapper for NDifferentiateCentral

func NDerivativeHigher

func NDerivativeHigher(f SingleVarFunction, order int,
	h float64) (f_ith SingleVarFunction)

NDerivativeHigher returns the order-th derivative of f. It is used just like NDerivative

Jump to

Keyboard shortcuts

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