diff --git a/Math/Exception/ZeroDevisionException.php b/Math/Exception/ZeroDevisionException.php new file mode 100644 index 000000000..e8a904cdb --- /dev/null +++ b/Math/Exception/ZeroDevisionException.php @@ -0,0 +1,44 @@ + + * @copyright Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +declare(strict_types=1); + +namespace phpOMS\Math\Exception; + +/** + * Zero devision exception. + * + * @category Framework + * @package phpOMS/Uri + * @author OMS Development Team + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class ZeroDevisionException extends \UnexpectedValueException +{ + /** + * Constructor. + * + * @param string $message Exception message + * @param int $code Exception code + * @param \Exception Previous exception + * + * @since 1.0.0 + */ + public function __construct(string $message = '', int $code = 0, \Exception $previous = null) + { + parent::__construct('Devision by zero is not defined.', $code, $previous); + } +} diff --git a/Math/Finance/FinanceFormulas.php b/Math/Finance/FinanceFormulas.php index dec6931fb..dfa52c8fa 100644 --- a/Math/Finance/FinanceFormulas.php +++ b/Math/Finance/FinanceFormulas.php @@ -17,6 +17,7 @@ declare(strict_types=1); namespace phpOMS\Math\Finance; use phpOMS\Math\Statistic\Average; +use phpOMS\Math\Matrix\Exception\InvalidDimensionException; /** * Finance class. @@ -1036,7 +1037,7 @@ class FinanceFormulas * * @return float * - * @throws \Exception + * @throws InvalidDimensionException Throws this exception if the length of the array is 0 * * @since 1.0.0 */ @@ -1045,7 +1046,7 @@ class FinanceFormulas $count = count($C); if ($count === 0) { - throw new \Exception('Dimension'); + throw new InvalidDimensionException($count); } $npv = -$C[0]; diff --git a/Math/Matrix/Exception/InvalidDimensionException.php b/Math/Matrix/Exception/InvalidDimensionException.php new file mode 100644 index 000000000..c4ac860d6 --- /dev/null +++ b/Math/Matrix/Exception/InvalidDimensionException.php @@ -0,0 +1,44 @@ + + * @copyright Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +declare(strict_types=1); + +namespace phpOMS\Math\Matrix\Exception; + +/** + * Zero devision exception. + * + * @category Framework + * @package phpOMS/Uri + * @author OMS Development Team + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class InvalidDimensionException extends \UnexpectedValueException +{ + /** + * Constructor. + * + * @param string $message Exception message + * @param int $code Exception code + * @param \Exception Previous exception + * + * @since 1.0.0 + */ + public function __construct(string $message, int $code = 0, \Exception $previous = null) + { + parent::__construct('Dimension "' . $message . '" is not valid.', $code, $previous); + } +} diff --git a/Math/Matrix/Matrix.php b/Math/Matrix/Matrix.php index 83ac066ec..b6ce4181a 100644 --- a/Math/Matrix/Matrix.php +++ b/Math/Matrix/Matrix.php @@ -16,6 +16,8 @@ declare(strict_types=1); namespace phpOMS\Math\Matrix; +use phpOMS\Math\Matrix\Exception\InvalidDimensionException; + /** * Matrix class * @@ -85,14 +87,14 @@ class Matrix implements \ArrayAccess, \Iterator * @param int $n Column * @param int $value Value * - * @throws DimensionException + * @throws InvalidDimensionException * * @since 1.0.0 */ public function set(int $m, int $n, $value) /* : void */ { if (!isset($this->matrix[$m][$n])) { - throw new DimensionException($m, $n); + throw new InvalidDimensionException($m . 'x' . $n); } $this->matrix[$m][$n] = $value; @@ -106,14 +108,14 @@ class Matrix implements \ArrayAccess, \Iterator * * @return mixed * - * @throws DimensionException + * @throws InvalidDimensionException * * @since 1.0.0 */ public function get(int $m, int $n) { if (!isset($this->matrix[$m][$n])) { - throw new DimensionException($m, $n); + throw new InvalidDimensionException($m . 'x' . $n); } return $this->matrix[$m][$n]; @@ -172,7 +174,7 @@ class Matrix implements \ArrayAccess, \Iterator public function setMatrix(array $matrix) : Matrix { if ($this->m !== count($matrix) || $this->n !== count($matrix[0])) { - throw new DimensionException(count($matrix), count($matrix[0])); + throw new InvalidDimensionException(count($matrix) . 'x' . count($matrix[0])); } $this->matrix = $matrix; @@ -238,7 +240,7 @@ class Matrix implements \ArrayAccess, \Iterator private function addMatrix(Matrix $matrix) : Matrix { if ($this->m !== $matrix->getM() || $this->n !== $matrix->getN()) { - throw new DimensionException($matrix->getM(), $matrix->getN()); + throw new InvalidDimensionException($matrix->getM() . 'x' . $matrix->getN()); } $matrixArr = $matrix->getMatrix(); @@ -346,7 +348,7 @@ class Matrix implements \ArrayAccess, \Iterator $mDim = $matrix->getM(); if ($this->n !== $mDim) { - throw new DimensionException($mDim, $nDim); + throw new InvalidDimensionException($mDim . 'x' . $nDim); } $matrixArr = $matrix->getMatrix(); @@ -485,14 +487,14 @@ class Matrix implements \ArrayAccess, \Iterator * * @return Matrix * - * @throws \Exception + * @throws InvalidDimensionException * * @since 1.0.0 */ public function inverse(int $algorithm = InverseType::GAUSS_JORDAN) : Matrix { if ($this->n !== $this->m) { - throw new DimensionException($this->m, $this->n); + throw new InvalidDimensionException($this->m . 'x' . $this->n); } switch ($algorithm) { diff --git a/Math/Statistic/Average.php b/Math/Statistic/Average.php index b24bce3d9..9ff91afa9 100644 --- a/Math/Statistic/Average.php +++ b/Math/Statistic/Average.php @@ -16,6 +16,9 @@ declare(strict_types=1); namespace phpOMS\Math\Statistic; +use phpOMS\Math\Exception\ZeroDevisionException; +use phpOMS\Math\Matrix\Exception\InvalidDimensionException; + /** * Average class. * @@ -130,14 +133,14 @@ class Average * * @return float * - * @throws \Exception + * @throws InvalidDimensionException This exception is thrown in case both parameters have different array length * * @since 1.0.0 */ public static function weightedAverage(array $values, array $weight) : float { if (($count = count($values)) !== count($weight)) { - throw new \Exception('Dimension'); + throw new InvalidDimensionException(count($values) . 'x' . count($weight)); } $avg = 0.0; @@ -167,7 +170,7 @@ class Average $count = count($values); if ($count === 0) { - throw new \Exception('Division zero'); + throw new ZeroDevisionException(); } return array_sum($values) / $count; @@ -239,7 +242,7 @@ class Average $count = count($values); if ($count === 0) { - throw new \Exception('Division zero'); + throw new ZeroDevisionException(); } return pow(array_product($values), 1 / $count); @@ -272,7 +275,7 @@ class Average foreach ($values as $value) { if ($value === 0) { - throw new \Exception('Division zero'); + throw new ZeroDevisionException(); } $sum += 1 / $value; diff --git a/Math/Statistic/Forecast/Regression/LevelLogRegression.php b/Math/Statistic/Forecast/Regression/LevelLogRegression.php index 63a219150..24d858911 100644 --- a/Math/Statistic/Forecast/Regression/LevelLogRegression.php +++ b/Math/Statistic/Forecast/Regression/LevelLogRegression.php @@ -16,6 +16,8 @@ declare(strict_types=1); namespace phpOMS\Math\Statistic\Forecast\Regression; +use phpOMS\Math\Matrix\Exception\InvalidDimensionException; + /** * Regression class. * @@ -33,8 +35,8 @@ class LevelLogRegression extends RegressionAbstract */ public static function getRegression(array $x, array $y) : array { - if (($c = count($x)) != count($y)) { - throw new \Exception('Dimension'); + if (($c = count($x)) !== count($y)) { + throw new InvalidDimensionException($c . 'x' . count($y)); } for ($i = 0; $i < $c; $i++) { diff --git a/Math/Statistic/Forecast/Regression/LogLevelRegression.php b/Math/Statistic/Forecast/Regression/LogLevelRegression.php index bc0102c33..ea1b3d663 100644 --- a/Math/Statistic/Forecast/Regression/LogLevelRegression.php +++ b/Math/Statistic/Forecast/Regression/LogLevelRegression.php @@ -16,6 +16,8 @@ declare(strict_types=1); namespace phpOMS\Math\Statistic\Forecast\Regression; +use phpOMS\Math\Matrix\Exception\InvalidDimensionException; + /** * Regression class. * @@ -33,8 +35,8 @@ class LogLevelRegression extends RegressionAbstract */ public static function getRegression(array $x, array $y) : array { - if (($c = count($x)) != count($y)) { - throw new \Exception('Dimension'); + if (($c = count($x)) !== count($y)) { + throw new InvalidDimensionException($c . 'x' . count($y)); } for ($i = 0; $i < $c; $i++) { diff --git a/Math/Statistic/Forecast/Regression/LogLogRegression.php b/Math/Statistic/Forecast/Regression/LogLogRegression.php index 2350cc3a4..cb823cf78 100644 --- a/Math/Statistic/Forecast/Regression/LogLogRegression.php +++ b/Math/Statistic/Forecast/Regression/LogLogRegression.php @@ -16,6 +16,8 @@ declare(strict_types=1); namespace phpOMS\Math\Statistic\Forecast\Regression; +use phpOMS\Math\Matrix\Exception\InvalidDimensionException; + /** * Regression class. * @@ -33,8 +35,8 @@ class LogLogRegression extends RegressionAbstract */ public static function getRegression(array $x, array $y) : array { - if (($c = count($x)) != count($y)) { - throw new \Exception('Dimension'); + if (($c = count($x)) !== count($y)) { + throw new InvalidDimensionException($c . 'x' . count($y)); } for ($i = 0; $i < $c; $i++) { diff --git a/Math/Statistic/Forecast/Regression/RegressionAbstract.php b/Math/Statistic/Forecast/Regression/RegressionAbstract.php index ca674f01e..7c6821a82 100644 --- a/Math/Statistic/Forecast/Regression/RegressionAbstract.php +++ b/Math/Statistic/Forecast/Regression/RegressionAbstract.php @@ -18,6 +18,7 @@ declare(strict_types=1); use phpOMS\Math\Statistic\Average; use phpOMS\Math\Statistic\Forecast\ForecastIntervalMultiplier; use phpOMS\Math\Statistic\MeasureOfDispersion; +use phpOMS\Math\Matrix\Exception\InvalidDimensionException; abstract class RegressionAbstract { @@ -31,14 +32,14 @@ abstract class RegressionAbstract * * @return array [b0 => ?, b1 => ?] * - * @throws \Exception + * @throws InvalidDimensionException Throws this exception if the dimension of both arrays is not equal. * * @since 1.0.0 */ public static function getRegression(array $x, array $y) : array { - if (count($x) != count($y)) { - throw new \Exception('Dimension'); + if (count($x) !== count($y)) { + throw new InvalidDimensionException(count($x) . 'x' . count($y)); } $b1 = self::getBeta1($x, $y); diff --git a/Math/Statistic/MeasureOfDispersion.php b/Math/Statistic/MeasureOfDispersion.php index 6c66d9622..7fe6bd519 100644 --- a/Math/Statistic/MeasureOfDispersion.php +++ b/Math/Statistic/MeasureOfDispersion.php @@ -16,6 +16,9 @@ declare(strict_types=1); namespace phpOMS\Math\Statistic; +use phpOMS\Math\Exception\ZeroDevisionException; +use phpOMS\Math\Matrix\Exception\InvalidDimensionException; + /** * Measure of dispersion. * @@ -67,7 +70,7 @@ class MeasureOfDispersion $mean = Average::arithmeticMean($values); if ($mean === 0) { - throw new \Exception('Division zero'); + throw new ZeroDevisionException(); } return self::standardDeviation($values) / $mean; @@ -107,7 +110,7 @@ class MeasureOfDispersion $count = count($values); if ($count < 2) { - throw new \Exception('Division zero'); + throw new ZeroDevisionException(); } return $count * self::empiricalVariance($values) / ($count - 1); @@ -131,7 +134,7 @@ class MeasureOfDispersion $count = count($values); if ($count === 0) { - throw new \Exception('Division zero'); + throw new ZeroDevisionException(); } $mean = Average::arithmeticMean($values); @@ -154,7 +157,7 @@ class MeasureOfDispersion * * @return float * - * @throws \Exception + * @throws InvalidDimensionException * * @since 1.0.0 */ @@ -163,11 +166,11 @@ class MeasureOfDispersion $count = count($x); if ($count < 2) { - throw new \Exception('Division zero'); + throw new ZeroDevisionException(); } if ($count !== count($y)) { - throw new \Exception('Dimensions'); + throw new InvalidDimensionException($count . 'x' . count($y)); } $xMean = Average::arithmeticMean($x);