File: testquad.go

package info (click to toggle)
golang-gonum-v1-gonum 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,792 kB
  • sloc: asm: 6,252; fortran: 5,271; sh: 377; ruby: 211; makefile: 98
file content (114 lines) | stat: -rw-r--r-- 2,159 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// 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 testquad provides integrals for testing quadrature algorithms.
package testquad

import (
	"fmt"
	"math"
)

// Integral is a definite integral
//
//	∫_a^b f(x)dx
//
// with a known value.
type Integral struct {
	Name  string
	A, B  float64               // Integration limits
	F     func(float64) float64 // Integrand
	Value float64
}

// Constant returns the integral of a constant function
//
//	∫_{-1}^2 alpha dx
func Constant(alpha float64) Integral {
	return Integral{
		Name: fmt.Sprintf("∫_{-1}^{2} %vdx", alpha),
		A:    -1,
		B:    2,
		F: func(float64) float64 {
			return alpha
		},
		Value: 3 * alpha,
	}
}

// Poly returns the integral of a polynomial
//
//	∫_{-1}^2 x^degree dx
func Poly(degree int) Integral {
	d := float64(degree)
	return Integral{
		Name: fmt.Sprintf("∫_{-1}^{2} x^%vdx", degree),
		A:    -1,
		B:    2,
		F: func(x float64) float64 {
			return math.Pow(x, d)
		},
		Value: (math.Pow(2, d+1) - math.Pow(-1, d+1)) / (d + 1),
	}
}

// Sin returns the integral
//
//	∫_0^1 sin(x)dx
func Sin() Integral {
	return Integral{
		Name: "∫_0^1 sin(x)dx",
		A:    0,
		B:    1,
		F: func(x float64) float64 {
			return math.Sin(x)
		},
		Value: 1 - math.Cos(1),
	}
}

// XExpMinusX returns the integral
//
//	∫_0^1 x*exp(-x)dx
func XExpMinusX() Integral {
	return Integral{
		Name: "∫_0^1 x*exp(-x)dx",
		A:    0,
		B:    1,
		F: func(x float64) float64 {
			return x * math.Exp(-x)
		},
		Value: (math.E - 2) / math.E,
	}
}

// Sqrt returns the integral
//
//	∫_0^1 sqrt(x)dx
func Sqrt() Integral {
	return Integral{
		Name: "∫_0^1 sqrt(x)dx",
		A:    0,
		B:    1,
		F: func(x float64) float64 {
			return math.Sqrt(x)
		},
		Value: 2 / 3.0,
	}
}

// ExpOverX2Plus1 returns the integral
//
//	∫_0^1 exp(x)/(x*x+1)dx
func ExpOverX2Plus1() Integral {
	return Integral{
		Name: "∫_0^1 exp(x)/(x*x+1)dx",
		A:    0,
		B:    1,
		F: func(x float64) float64 {
			return math.Exp(x) / (x*x + 1)
		},
		Value: 1.270724139833620220138,
	}
}