impl. standard epsilon value

This commit is contained in:
Dennis Eichhorn 2022-03-11 23:14:38 +01:00
parent 755ee54f0c
commit 7ed21a1ec0
7 changed files with 61 additions and 19 deletions

View File

@ -24,6 +24,14 @@ namespace phpOMS\Math\Functions;
*/ */
final class Functions final class Functions
{ {
/**
* Epsilon for float comparison.
*
* @var float
* @since 1.0.0
*/
public const EPSILON = 4.88e-04;
/** /**
* Constructor. * Constructor.
* *
@ -268,7 +276,7 @@ final class Functions
$sum += $term / (2 * $i + 1); $sum += $term / (2 * $i + 1);
++$i; ++$i;
} while ($sum !== 0.0 && \abs($term / $sum) > 0.0000001); } while ($sum !== 0.0 && \abs($term / $sum) > self::EPSILON);
return 2 / \sqrt(\M_PI) * $sum; return 2 / \sqrt(\M_PI) * $sum;
} }
@ -311,7 +319,7 @@ final class Functions
$n += 0.5; $n += 0.5;
$q1 = $q2; $q1 = $q2;
$q2 = $b / $d; $q2 = $b / $d;
} while (\abs($q1 - $q2) / $q2 > 0.0000001); } while (\abs($q1 - $q2) / $q2 > self::EPSILON);
return 1 / \sqrt(\M_PI) * \exp(-$value * $value) * $q2; return 1 / \sqrt(\M_PI) * \exp(-$value * $value) * $q2;
} }

View File

@ -30,7 +30,7 @@ final class Polygon implements D2ShapeInterface
* @var float * @var float
* @since 1.0.0 * @since 1.0.0
*/ */
public const EPSILON = 0.00001; public const EPSILON = 4.88e-04;
/** /**
* Coordinates. * Coordinates.

View File

@ -30,6 +30,14 @@ use phpOMS\Math\Geometry\Shape\D2\Triangle;
*/ */
final class EigenvalueDecomposition final class EigenvalueDecomposition
{ {
/**
* Epsilon for float comparison.
*
* @var float
* @since 1.0.0
*/
public const EPSILON = 4.88e-04;
/** /**
* Dimension m * Dimension m
* *
@ -279,14 +287,13 @@ final class EigenvalueDecomposition
$f = 0.0; $f = 0.0;
$tst1 = 0.0; $tst1 = 0.0;
$eps = 0.00001;
for ($l = 0; $l < $this->m; ++$l) { for ($l = 0; $l < $this->m; ++$l) {
$tst1 = \max($tst1, \abs($this->D[$l]) + \abs($this->E[$l])); $tst1 = \max($tst1, \abs($this->D[$l]) + \abs($this->E[$l]));
$m = $l; $m = $l;
while ($m < $this->m) { while ($m < $this->m) {
if (\abs($this->E[$m]) <= $eps * $tst1) { if (\abs($this->E[$m]) <= self::EPSILON * $tst1) {
break; break;
} }
@ -344,7 +351,7 @@ final class EigenvalueDecomposition
$p = -$s * $s2 * $c3 * $el1 * $this->E[$l] / $dl1; $p = -$s * $s2 * $c3 * $el1 * $this->E[$l] / $dl1;
$this->E[$l] = $s * $p; $this->E[$l] = $s * $p;
$this->D[$l] = $c * $p; $this->D[$l] = $c * $p;
} while (\abs($this->E[$l]) > $eps * $tst1); } while (\abs($this->E[$l]) > self::EPSILON * $tst1);
} }
$this->D[$l] += $f; $this->D[$l] += $f;
@ -506,7 +513,6 @@ final class EigenvalueDecomposition
$n = $nn - 1; $n = $nn - 1;
$low = 0; $low = 0;
$high = $nn - 1; $high = $nn - 1;
$eps = 0.00001;
$exshift = 0.0; $exshift = 0.0;
$p = 0; $p = 0;
$q = 0; $q = 0;
@ -535,7 +541,7 @@ final class EigenvalueDecomposition
$s = $norm; $s = $norm;
} }
if (\abs($this->H[$l][$l - 1]) < $eps * $s) { if (\abs($this->H[$l][$l - 1]) < self::EPSILON * $s) {
break; break;
} }
@ -656,7 +662,7 @@ final class EigenvalueDecomposition
$r /= $s; $r /= $s;
if ($m === $l if ($m === $l
|| \abs($this->H[$m][$m - 1]) * (\abs($q) + \abs($r)) < $eps * (\abs($p) * (\abs($this->H[$m - 1][$m - 1]) + \abs($z) + \abs($this->H[$m + 1][$m + 1]))) || \abs($this->H[$m][$m - 1]) * (\abs($q) + \abs($r)) < self::EPSILON * (\abs($p) * (\abs($this->H[$m - 1][$m - 1]) + \abs($z) + \abs($this->H[$m + 1][$m + 1])))
) { ) {
break; break;
} }
@ -774,7 +780,7 @@ final class EigenvalueDecomposition
$l = $i; $l = $i;
if ($this->E[$i] == 0) { if ($this->E[$i] == 0) {
$this->H[$i][$n] = $w != 0 ? -$r / $w : -$r / ($eps * $norm); $this->H[$i][$n] = $w != 0 ? -$r / $w : -$r / (self::EPSILON * $norm);
} else { } else {
$x = $this->H[$i][$i + 1]; $x = $this->H[$i][$i + 1];
$y = $this->H[$i + 1][$i]; $y = $this->H[$i + 1][$i];
@ -785,7 +791,7 @@ final class EigenvalueDecomposition
} }
$t = \abs($this->H[$i][$n]); $t = \abs($this->H[$i][$n]);
if (($eps * $t) * $t > 1) { if ((self::EPSILON * $t) * $t > 1) {
for ($j = $i; $j <= $n; ++$j) { for ($j = $i; $j <= $n; ++$j) {
$this->H[$j][$n] = $this->H[$j][$n] / $t; $this->H[$j][$n] = $this->H[$j][$n] / $t;
} }
@ -836,7 +842,7 @@ final class EigenvalueDecomposition
$vi = ($this->D[$i] - $p) * 2.0 * $q; $vi = ($this->D[$i] - $p) * 2.0 * $q;
if ($vr == 0 & $vi == 0) { if ($vr == 0 & $vi == 0) {
$vr = $eps * $norm * (\abs($w) + \abs($q) + \abs($x) + \abs($y) + \abs($z)); $vr = self::EPSILON * $norm * (\abs($w) + \abs($q) + \abs($x) + \abs($y) + \abs($z));
} }
$this->cdiv($x * $r - $z * $ra + $q * $sa, $x * $s - $z * $sa - $q * $ra, $vr, $vi); $this->cdiv($x * $r - $z * $ra + $q * $sa, $x * $s - $z * $sa - $q * $ra, $vr, $vi);
@ -855,7 +861,7 @@ final class EigenvalueDecomposition
} }
$t = \max(\abs($this->H[$i][$n - 1]), \abs($this->H[$i][$n])); $t = \max(\abs($this->H[$i][$n - 1]), \abs($this->H[$i][$n]));
if (($eps * $t) * $t > 1) { if ((self::EPSILON * $t) * $t > 1) {
for ($j = $i; $j <= $n; ++$j) { for ($j = $i; $j <= $n; ++$j) {
$this->H[$j][$n - 1] = $this->H[$j][$n - 1] / $t; $this->H[$j][$n - 1] = $this->H[$j][$n - 1] / $t;
$this->H[$j][$n] = $this->H[$j][$n] / $t; $this->H[$j][$n] = $this->H[$j][$n] / $t;

View File

@ -29,6 +29,14 @@ use phpOMS\Math\Matrix\Exception\InvalidDimensionException;
*/ */
class Matrix implements \ArrayAccess, \Iterator class Matrix implements \ArrayAccess, \Iterator
{ {
/**
* Epsilon for float comparison.
*
* @var float
* @since 1.0.0
*/
public const EPSILON = 4.88e-04;
/** /**
* Matrix. * Matrix.
* *
@ -326,7 +334,7 @@ class Matrix implements \ArrayAccess, \Iterator
for ($i = 0; $i < $nDim; ++$i) { for ($i = 0; $i < $nDim; ++$i) {
for ($j = 0; $j < $mDim; ++$j) { for ($j = 0; $j < $mDim; ++$j) {
if (!$selected[$j] && \abs($matrix[$j][$i]) > 0.0001) { if (!$selected[$j] && \abs($matrix[$j][$i]) > self::EPSILON) {
break; break;
} }
} }
@ -342,7 +350,7 @@ class Matrix implements \ArrayAccess, \Iterator
} }
for ($k = 0; $k < $mDim; ++$k) { for ($k = 0; $k < $mDim; ++$k) {
if ($k !== $j && \abs($matrix[$k][$i]) > 0.0001) { if ($k !== $j && \abs($matrix[$k][$i]) > self::EPSILON) {
for ($p = $i + 1; $p < $nDim; ++$p) { for ($p = $i + 1; $p < $nDim; ++$p) {
$matrix[$k][$p] -= $matrix[$j][$p] * $matrix[$k][$i]; $matrix[$k][$p] -= $matrix[$j][$p] * $matrix[$k][$i];
} }

View File

@ -24,6 +24,10 @@ namespace phpOMS\Math\Number;
*/ */
final class Numbers final class Numbers
{ {
public const SFLOAT = 1.175494351E-38;
public const EPSILON = 4.88e-04;
/** /**
* Constructor. * Constructor.
* *
@ -94,7 +98,7 @@ final class Numbers
*/ */
public static function isSquare(int $n) : bool public static function isSquare(int $n) : bool
{ {
return \abs(((int) \sqrt($n)) * ((int) \sqrt($n)) - $n) < 0.001; return \abs(((int) \sqrt($n)) * ((int) \sqrt($n)) - $n) < self::EPSILON;
} }
/** /**

View File

@ -24,6 +24,14 @@ namespace phpOMS\Math\Number;
*/ */
final class Prime final class Prime
{ {
/**
* Epsilon for float comparison.
*
* @var float
* @since 1.0.0
*/
public const EPSILON = 4.88e-04;
/** /**
* Constructor. * Constructor.
* *
@ -47,7 +55,7 @@ final class Prime
{ {
$mersenne = \log($n + 1, 2); $mersenne = \log($n + 1, 2);
return $mersenne - (int) $mersenne < 0.00001; return $mersenne - (int) $mersenne < self::EPSILON;
} }
/** /**

View File

@ -25,6 +25,14 @@ use phpOMS\Math\Functions\Beta;
*/ */
final class LogDistribution final class LogDistribution
{ {
/**
* Epsilon for float comparison.
*
* @var float
* @since 1.0.0
*/
public const EPSILON = 4.88e-04;
/** /**
* Get probability mass function. * Get probability mass function.
* *
@ -53,9 +61,9 @@ final class LogDistribution
public static function getCdf(float $p, int $k) : float public static function getCdf(float $p, int $k) : float
{ {
// This is a workaround! // This is a workaround!
// Actually 0 should be used instead of 0.0001. // Actually 0 should be used instead of self::EPSILON.
// This is only used because the incomplete beta function doesn't work for p or q = 0 // This is only used because the incomplete beta function doesn't work for p or q = 0
return 1 + Beta::incompleteBeta($p, $k + 1, 0.0001) / \log(1 - $p); return 1 + Beta::incompleteBeta($p, $k + 1, self::EPSILON) / \log(1 - $p);
} }
/** /**