github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/integrate/simpsons_test.go (about) 1 // Copyright ©2019 The Gonum Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package integrate 6 7 import ( 8 "math" 9 "testing" 10 11 "golang.org/x/exp/rand" 12 13 "github.com/jingcheng-WU/gonum/integrate/testquad" 14 ) 15 16 func TestSimpsons(t *testing.T) { 17 t.Parallel() 18 rnd := rand.New(rand.NewSource(1)) 19 for i, test := range []struct { 20 integral testquad.Integral 21 n int 22 tol float64 23 }{ 24 {integral: testquad.Constant(0), n: 3, tol: 0}, 25 {integral: testquad.Constant(0), n: 4, tol: 0}, 26 {integral: testquad.Constant(0), n: 10, tol: 0}, 27 {integral: testquad.Poly(0), n: 3, tol: 1e-14}, 28 {integral: testquad.Poly(0), n: 4, tol: 1e-14}, 29 {integral: testquad.Poly(0), n: 10, tol: 1e-14}, 30 {integral: testquad.Poly(1), n: 3, tol: 1e-14}, 31 {integral: testquad.Poly(1), n: 4, tol: 1e-14}, 32 {integral: testquad.Poly(1), n: 10, tol: 1e-14}, 33 {integral: testquad.Poly(2), n: 3, tol: 1e-14}, 34 {integral: testquad.Poly(2), n: 4, tol: 1e-14}, 35 {integral: testquad.Poly(2), n: 10, tol: 1e-14}, 36 {integral: testquad.Poly(3), n: 1000, tol: 1e-8}, 37 {integral: testquad.Poly(4), n: 1000, tol: 1e-8}, 38 {integral: testquad.Poly(5), n: 1000, tol: 1e-7}, 39 {integral: testquad.Sin(), n: 100, tol: 1e-8}, 40 {integral: testquad.XExpMinusX(), n: 201, tol: 1e-8}, 41 {integral: testquad.Sqrt(), n: 1e4, tol: 1e-6}, 42 {integral: testquad.ExpOverX2Plus1(), n: 100, tol: 1e-7}, 43 } { 44 n := test.n 45 a := test.integral.A 46 b := test.integral.B 47 48 x := jitterSpan(n, a, b, rnd) 49 y := make([]float64, n) 50 for i, xi := range x { 51 y[i] = test.integral.F(xi) 52 } 53 54 got := Simpsons(x, y) 55 56 want := test.integral.Value 57 diff := math.Abs(got - want) 58 if diff > test.tol { 59 t.Errorf("Test #%d: %v, n=%v: unexpected result; got=%v want=%v diff=%v", 60 i, test.integral.Name, n, got, want, diff) 61 } 62 } 63 }