mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-11 17:58:41 +00:00
fix docblock
This commit is contained in:
parent
6336898978
commit
559ebf200d
150
Math/Numerics/Integration.php
Normal file
150
Math/Numerics/Integration.php
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 7.4
|
||||
*
|
||||
* @package phpOMS\Math\Numerics
|
||||
* @copyright Dennis Eichhorn
|
||||
* @license OMS License 1.0
|
||||
* @version 1.0.0
|
||||
* @link https://orange-management.org
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace phpOMS\Math\Numerics;
|
||||
|
||||
/**
|
||||
* Numerical integration.
|
||||
*
|
||||
* @package phpOMS\Math\Numerics
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class Integration
|
||||
{
|
||||
/**
|
||||
* Integrate function by using rectangles from the left side
|
||||
*
|
||||
* @param float $from Start interval
|
||||
* @param float $to End interval
|
||||
* @param float $n Steps
|
||||
* @param \Closure $func Function to integrate
|
||||
*
|
||||
* @return float
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function intLeftRect(float $from, float $to, float $n, \Closure $func) : float
|
||||
{
|
||||
$h = ($to - $from) / $n;
|
||||
$sum = 0.0;
|
||||
|
||||
for ($x = $from; $x <= ($to - $h); $x += $h) {
|
||||
$sum += $func($x);
|
||||
}
|
||||
|
||||
return $h * $sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Integrate function by using rectangles from the right side
|
||||
*
|
||||
* @param float $from Start interval
|
||||
* @param float $to End interval
|
||||
* @param float $n Steps
|
||||
* @param \Closure $func Function to integrate
|
||||
*
|
||||
* @return float
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function intRightRect(float $from, float $to, float $n, \Closure $func): float
|
||||
{
|
||||
$h = ($to - $from) / $n;
|
||||
$sum = 0.0;
|
||||
|
||||
for ($x = $from; $x <= ($to - $h); $x += $h) {
|
||||
$sum += $func($x + $h);
|
||||
}
|
||||
|
||||
return $h * $sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Integrate function by using rectangles from a moving center point
|
||||
*
|
||||
* @param float $from Start interval
|
||||
* @param float $to End interval
|
||||
* @param float $n Steps
|
||||
* @param \Closure $func Function to integrate
|
||||
*
|
||||
* @return float
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function intMiddleRect(float $from, float $to, float $n, \Closure $func): float
|
||||
{
|
||||
$h = ($to - $from) / $n;
|
||||
$sum = 0.0;
|
||||
|
||||
for ($x = $from; $x <= ($to - $h); $x += $h) {
|
||||
$sum += $func($x + $h / 2.0);
|
||||
}
|
||||
|
||||
return $h * $sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Integrate function by using trapezium
|
||||
*
|
||||
* @param float $from Start interval
|
||||
* @param float $to End interval
|
||||
* @param float $n Steps
|
||||
* @param \Closure $func Function to integrate
|
||||
*
|
||||
* @return float
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function intTrapezium(float $from, float $to, float $n, \Closure $func): float
|
||||
{
|
||||
$h = ($to - $from) / $n;
|
||||
$sum = $func($from) + $func($to);
|
||||
|
||||
for ($i = 1; $i < $n; ++$i) {
|
||||
$sum += 2 * $func($from + $i * $h);
|
||||
}
|
||||
|
||||
return $h * $sum / 2.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Integrate by using the simpson rule
|
||||
*
|
||||
* @param float $from Start interval
|
||||
* @param float $to End interval
|
||||
* @param float $n Steps
|
||||
* @param \Closure $func Function to integrate
|
||||
*
|
||||
* @return float
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function intSimpson(float $from, float $to, float $n, \Closure $func): float
|
||||
{
|
||||
$h = ($to - $from) / $n;
|
||||
$sum1 = 0.0;
|
||||
$sum2 = 0.0;
|
||||
|
||||
for ($i = 1; $i < $n; ++$i) {
|
||||
$sum1 += $func($from + $h * $i + $h / 2.0);
|
||||
$sum2 += $func($from + $h * $i);
|
||||
}
|
||||
|
||||
return $h / 6.0 * ($func($from) + $func($to) + 4.0 * $sum1 + 2.0 * $sum2);
|
||||
}
|
||||
}
|
||||
|
|
@ -15,7 +15,7 @@ declare(strict_types=1);
|
|||
namespace phpOMS\Math\Numerics\Interpolation;
|
||||
|
||||
/**
|
||||
* Web module interface.
|
||||
* Cubic spline interpolation.
|
||||
*
|
||||
* @package phpOMS\Math\Numerics\Interpolation
|
||||
* @license OMS License 1.0
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ declare(strict_types=1);
|
|||
namespace phpOMS\Math\Numerics\Interpolation;
|
||||
|
||||
/**
|
||||
* Web module interface.
|
||||
* Linear spline interpolation.
|
||||
*
|
||||
* @package phpOMS\Math\Numerics\Interpolation
|
||||
* @license OMS License 1.0
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ declare(strict_types=1);
|
|||
namespace phpOMS\Math\Numerics\Interpolation;
|
||||
|
||||
/**
|
||||
* Web module interface.
|
||||
* Polynomial spline interpolation.
|
||||
*
|
||||
* @package phpOMS\Math\Numerics\Interpolation
|
||||
* @license OMS License 1.0
|
||||
|
|
|
|||
65
tests/Math/Numerics/IntegrationTest.php
Normal file
65
tests/Math/Numerics/IntegrationTest.php
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 7.4
|
||||
*
|
||||
* @package tests
|
||||
* @copyright Dennis Eichhorn
|
||||
* @license OMS License 1.0
|
||||
* @version 1.0.0
|
||||
* @link https://orange-management.org
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace phpOMS\tests\Math\Numerics;
|
||||
|
||||
use phpOMS\Math\Numerics\Integration;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class IntegrationTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testLRect(): void
|
||||
{
|
||||
self::assertEqualsWithDelta(0.235322, Integration::intLeftRect(0.0, 1.0, 100.0, function($x) { return $x**3; }), 0.001);
|
||||
self::assertEqualsWithDelta(4.654000, Integration::intLeftRect(1.0, 100.0, 1000.0, function($x) { return 1 / $x; }), 0.001);
|
||||
self::assertEqualsWithDelta(12499992.500730, Integration::intLeftRect(0.0, 5000.0, 5000000.0, function($x) { return $x; }), 0.001);
|
||||
self::assertEqualsWithDelta(17999991.001392, Integration::intLeftRect(0.0, 6000.0, 6000000.0, function($x) { return $x; }), 0.001);
|
||||
}
|
||||
|
||||
public function testRRect(): void
|
||||
{
|
||||
self::assertEqualsWithDelta(0.245025, Integration::intRightRect(0.0, 1.0, 100.0, function($x) { return $x**3; }), 0.001);
|
||||
self::assertEqualsWithDelta(4.555991, Integration::intRightRect(1.0, 100.0, 1000.0, function($x) { return 1 / $x; }), 0.001);
|
||||
self::assertEqualsWithDelta(12499997.500729, Integration::intRightRect(0.0, 5000.0, 5000000.0, function($x) { return $x; }), 0.001);
|
||||
self::assertEqualsWithDelta(17999997.001390, Integration::intRightRect(0.0, 6000.0, 6000000.0, function($x) { return $x; }), 0.001);
|
||||
}
|
||||
|
||||
public function testMRect(): void
|
||||
{
|
||||
self::assertEqualsWithDelta(0.240137, Integration::intMiddleRect(0.0, 1.0, 100.0, function($x) { return $x**3; }), 0.001);
|
||||
self::assertEqualsWithDelta(4.603772, Integration::intMiddleRect(1.0, 100.0, 1000.0, function($x) { return 1 / $x; }), 0.001);
|
||||
self::assertEqualsWithDelta(12499995.000729, Integration::intMiddleRect(0.0, 5000.0, 5000000.0, function($x) { return $x; }), 0.001);
|
||||
self::assertEqualsWithDelta(17999994.001391, Integration::intMiddleRect(0.0, 6000.0, 6000000.0, function($x) { return $x; }), 0.001);
|
||||
}
|
||||
|
||||
public function testTrapeze(): void
|
||||
{
|
||||
self::assertEqualsWithDelta(0.250025, Integration::intTrapezium(0.0, 1.0, 100.0, function($x) { return $x**3; }), 0.001);
|
||||
self::assertEqualsWithDelta(4.605986, Integration::intTrapezium(1.0, 100.0, 1000.0, function($x) { return 1 / $x; }), 0.001);
|
||||
self::assertEqualsWithDelta(12500000.0, Integration::intTrapezium(0.0, 5000.0, 5000000.0, function($x) { return $x; }), 0.001);
|
||||
self::assertEqualsWithDelta(18000000.0, Integration::intTrapezium(0.0, 6000.0, 6000000.0, function($x) { return $x; }), 0.001);
|
||||
}
|
||||
|
||||
public function testSimpson(): void
|
||||
{
|
||||
self::assertEqualsWithDelta(0.25, Integration::intSimpson(0.0, 1.0, 100.0, function ($x) { return $x ** 3; }), 0.001);
|
||||
self::assertEqualsWithDelta(4.605170, Integration::intSimpson(1.0, 100.0, 1000.0, function ($x) { return 1 / $x; }), 0.001);
|
||||
self::assertEqualsWithDelta(12500000.0, Integration::intSimpson(0.0, 5000.0, 5000000.0, function ($x) { return $x; }), 0.001);
|
||||
self::assertEqualsWithDelta(18000000.0, Integration::intSimpson(0.0, 6000.0, 6000000.0, function ($x) { return $x; }), 0.001);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user