// 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<