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