Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
28 / 28 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| Integration | |
100.00% |
28 / 28 |
|
100.00% |
5 / 5 |
11 | |
100.00% |
1 / 1 |
| intLeftRect | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| intRightRect | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| intMiddleRect | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| intTrapezium | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| intSimpson | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Math\Numerics |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Math\Numerics; |
| 16 | |
| 17 | /** |
| 18 | * Numerical integration. |
| 19 | * |
| 20 | * @package phpOMS\Math\Numerics |
| 21 | * @license OMS License 2.0 |
| 22 | * @link https://jingga.app |
| 23 | * @since 1.0.0 |
| 24 | */ |
| 25 | final class Integration |
| 26 | { |
| 27 | /** |
| 28 | * Integrate function by using rectangles from the left side |
| 29 | * |
| 30 | * @param float $from Start interval |
| 31 | * @param float $to End interval |
| 32 | * @param float $n Steps |
| 33 | * @param Callable $func Function to integrate |
| 34 | * |
| 35 | * @return float |
| 36 | * |
| 37 | * @since 1.0.0 |
| 38 | */ |
| 39 | public static function intLeftRect(float $from, float $to, float $n, callable $func) : float |
| 40 | { |
| 41 | $h = ($to - $from) / $n; |
| 42 | $sum = 0.0; |
| 43 | |
| 44 | for ($x = $from; $x <= ($to - $h); $x += $h) { |
| 45 | $sum += $func($x); |
| 46 | } |
| 47 | |
| 48 | return $h * $sum; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Integrate function by using rectangles from the right side |
| 53 | * |
| 54 | * @param float $from Start interval |
| 55 | * @param float $to End interval |
| 56 | * @param float $n Steps |
| 57 | * @param Callable $func Function to integrate |
| 58 | * |
| 59 | * @return float |
| 60 | * |
| 61 | * @since 1.0.0 |
| 62 | */ |
| 63 | public static function intRightRect(float $from, float $to, float $n, callable $func) : float |
| 64 | { |
| 65 | $h = ($to - $from) / $n; |
| 66 | $sum = 0.0; |
| 67 | |
| 68 | for ($x = $from; $x <= ($to - $h); $x += $h) { |
| 69 | $sum += $func($x + $h); |
| 70 | } |
| 71 | |
| 72 | return $h * $sum; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Integrate function by using rectangles from a moving center point |
| 77 | * |
| 78 | * @param float $from Start interval |
| 79 | * @param float $to End interval |
| 80 | * @param float $n Steps |
| 81 | * @param Callable $func Function to integrate |
| 82 | * |
| 83 | * @return float |
| 84 | * |
| 85 | * @since 1.0.0 |
| 86 | */ |
| 87 | public static function intMiddleRect(float $from, float $to, float $n, callable $func) : float |
| 88 | { |
| 89 | $h = ($to - $from) / $n; |
| 90 | $sum = 0.0; |
| 91 | |
| 92 | for ($x = $from; $x <= ($to - $h); $x += $h) { |
| 93 | $sum += $func($x + $h / 2.0); |
| 94 | } |
| 95 | |
| 96 | return $h * $sum; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Integrate function by using trapezium |
| 101 | * |
| 102 | * @param float $from Start interval |
| 103 | * @param float $to End interval |
| 104 | * @param float $n Steps |
| 105 | * @param Callable $func Function to integrate |
| 106 | * |
| 107 | * @return float |
| 108 | * |
| 109 | * @since 1.0.0 |
| 110 | */ |
| 111 | public static function intTrapezium(float $from, float $to, float $n, callable $func) : float |
| 112 | { |
| 113 | $h = ($to - $from) / $n; |
| 114 | $sum = $func($from) + $func($to); |
| 115 | |
| 116 | for ($i = 1; $i < $n; ++$i) { |
| 117 | $sum += 2 * $func($from + $i * $h); |
| 118 | } |
| 119 | |
| 120 | return $h * $sum / 2.0; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Integrate by using the simpson rule |
| 125 | * |
| 126 | * @param float $from Start interval |
| 127 | * @param float $to End interval |
| 128 | * @param float $n Steps |
| 129 | * @param Callable $func Function to integrate |
| 130 | * |
| 131 | * @return float |
| 132 | * |
| 133 | * @since 1.0.0 |
| 134 | */ |
| 135 | public static function intSimpson(float $from, float $to, float $n, callable $func) : float |
| 136 | { |
| 137 | $h = ($to - $from) / $n; |
| 138 | $sum1 = 0.0; |
| 139 | $sum2 = 0.0; |
| 140 | |
| 141 | for ($i = 0; $i < $n; ++$i) { |
| 142 | $sum1 += $func($from + $h * $i + $h / 2.0); |
| 143 | } |
| 144 | |
| 145 | for ($i = 1; $i < $n; ++$i) { |
| 146 | $sum2 += $func($from + $h * $i); |
| 147 | } |
| 148 | |
| 149 | return $h / 6.0 * ($func($from) + $func($to) + 4.0 * $sum1 + 2.0 * $sum2); |
| 150 | } |
| 151 | } |