diff --git a/integrate/romberg.go b/integrate/romberg.go new file mode 100644 index 00000000..585b5868 --- /dev/null +++ b/integrate/romberg.go @@ -0,0 +1,65 @@ +// Copyright ©2019 The Gonum Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package integrate + +import ( + "math" + "math/bits" +) + +// Romberg returns an approximate value of the integral +// \int_a^b f(x)dx +// computed using the Romberg's method. The function f is given +// as a slice of equally-spaced samples, that is, +// f[i] = f(a + i*dx) +// and dx is the spacing between the samples. +// +// The length of f must be 2^k + 1, where k is a positive integer, +// and dx must be positive. +// +// See https://en.wikipedia.org/wiki/Romberg%27s_method for a description of +// the algorithm +func Romberg(f []float64, dx float64) float64 { + if len(f) < 3 { + panic("integral: invalid slice length: must be at least 3") + } + + if dx <= 0 { + panic("integral: invalid spacing: must be larger than 0") + } + + n := len(f) - 1 + k := bits.Len(uint(n - 1)) + + if len(f) != 1< test.tol { + t.Errorf("test #%d: got=%v want=%v diff=%v\n", i, v, test.want, diff) + } + } +}