github.com/gopherd/gonum@v0.0.4/integrate/testquad/testquad.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 testquad provides integrals for testing quadrature algorithms.
     6  package testquad
     7  
     8  import (
     9  	"fmt"
    10  	"math"
    11  )
    12  
    13  // Integral is a definite integral
    14  //  ∫_a^b f(x)dx
    15  // with a known value.
    16  type Integral struct {
    17  	Name  string
    18  	A, B  float64               // Integration limits
    19  	F     func(float64) float64 // Integrand
    20  	Value float64
    21  }
    22  
    23  // Constant returns the integral of a constant function
    24  //  ∫_{-1}^2 alpha dx
    25  func Constant(alpha float64) Integral {
    26  	return Integral{
    27  		Name: fmt.Sprintf("∫_{-1}^{2} %vdx", alpha),
    28  		A:    -1,
    29  		B:    2,
    30  		F: func(float64) float64 {
    31  			return alpha
    32  		},
    33  		Value: 3 * alpha,
    34  	}
    35  }
    36  
    37  // Poly returns the integral of a polynomial
    38  //  ∫_{-1}^2 x^degree dx
    39  func Poly(degree int) Integral {
    40  	d := float64(degree)
    41  	return Integral{
    42  		Name: fmt.Sprintf("∫_{-1}^{2} x^%vdx", degree),
    43  		A:    -1,
    44  		B:    2,
    45  		F: func(x float64) float64 {
    46  			return math.Pow(x, d)
    47  		},
    48  		Value: (math.Pow(2, d+1) - math.Pow(-1, d+1)) / (d + 1),
    49  	}
    50  }
    51  
    52  // Sin returns the integral
    53  //  ∫_0^1 sin(x)dx
    54  func Sin() Integral {
    55  	return Integral{
    56  		Name: "∫_0^1 sin(x)dx",
    57  		A:    0,
    58  		B:    1,
    59  		F: func(x float64) float64 {
    60  			return math.Sin(x)
    61  		},
    62  		Value: 1 - math.Cos(1),
    63  	}
    64  }
    65  
    66  // XExpMinusX returns the integral
    67  //  ∫_0^1 x*exp(-x)dx
    68  func XExpMinusX() Integral {
    69  	return Integral{
    70  		Name: "∫_0^1 x*exp(-x)dx",
    71  		A:    0,
    72  		B:    1,
    73  		F: func(x float64) float64 {
    74  			return x * math.Exp(-x)
    75  		},
    76  		Value: (math.E - 2) / math.E,
    77  	}
    78  }
    79  
    80  // Sqrt returns the integral
    81  //  ∫_0^1 sqrt(x)dx
    82  func Sqrt() Integral {
    83  	return Integral{
    84  		Name: "∫_0^1 sqrt(x)dx",
    85  		A:    0,
    86  		B:    1,
    87  		F: func(x float64) float64 {
    88  			return math.Sqrt(x)
    89  		},
    90  		Value: 2 / 3.0,
    91  	}
    92  }
    93  
    94  // ExpOverX2Plus1 returns the integral
    95  //  ∫_0^1 exp(x)/(x*x+1)dx
    96  func ExpOverX2Plus1() Integral {
    97  	return Integral{
    98  		Name: "∫_0^1 exp(x)/(x*x+1)dx",
    99  		A:    0,
   100  		B:    1,
   101  		F: func(x float64) float64 {
   102  			return math.Exp(x) / (x*x + 1)
   103  		},
   104  		Value: 1.270724139833620220138,
   105  	}
   106  }