Added Zero and Dimension exceptions

This commit is contained in:
Dennis Eichhorn 2017-07-25 17:28:18 +02:00
parent c4927f588f
commit 76e8e76234
10 changed files with 135 additions and 31 deletions

View File

@ -0,0 +1,44 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @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 <dev@oms.com>
* @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);
}
}

View File

@ -17,6 +17,7 @@ declare(strict_types=1);
namespace phpOMS\Math\Finance; namespace phpOMS\Math\Finance;
use phpOMS\Math\Statistic\Average; use phpOMS\Math\Statistic\Average;
use phpOMS\Math\Matrix\Exception\InvalidDimensionException;
/** /**
* Finance class. * Finance class.
@ -1036,7 +1037,7 @@ class FinanceFormulas
* *
* @return float * @return float
* *
* @throws \Exception * @throws InvalidDimensionException Throws this exception if the length of the array is 0
* *
* @since 1.0.0 * @since 1.0.0
*/ */
@ -1045,7 +1046,7 @@ class FinanceFormulas
$count = count($C); $count = count($C);
if ($count === 0) { if ($count === 0) {
throw new \Exception('Dimension'); throw new InvalidDimensionException($count);
} }
$npv = -$C[0]; $npv = -$C[0];

View File

@ -0,0 +1,44 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @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 <dev@oms.com>
* @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);
}
}

View File

@ -16,6 +16,8 @@ declare(strict_types=1);
namespace phpOMS\Math\Matrix; namespace phpOMS\Math\Matrix;
use phpOMS\Math\Matrix\Exception\InvalidDimensionException;
/** /**
* Matrix class * Matrix class
* *
@ -85,14 +87,14 @@ class Matrix implements \ArrayAccess, \Iterator
* @param int $n Column * @param int $n Column
* @param int $value Value * @param int $value Value
* *
* @throws DimensionException * @throws InvalidDimensionException
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public function set(int $m, int $n, $value) /* : void */ public function set(int $m, int $n, $value) /* : void */
{ {
if (!isset($this->matrix[$m][$n])) { if (!isset($this->matrix[$m][$n])) {
throw new DimensionException($m, $n); throw new InvalidDimensionException($m . 'x' . $n);
} }
$this->matrix[$m][$n] = $value; $this->matrix[$m][$n] = $value;
@ -106,14 +108,14 @@ class Matrix implements \ArrayAccess, \Iterator
* *
* @return mixed * @return mixed
* *
* @throws DimensionException * @throws InvalidDimensionException
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public function get(int $m, int $n) public function get(int $m, int $n)
{ {
if (!isset($this->matrix[$m][$n])) { if (!isset($this->matrix[$m][$n])) {
throw new DimensionException($m, $n); throw new InvalidDimensionException($m . 'x' . $n);
} }
return $this->matrix[$m][$n]; return $this->matrix[$m][$n];
@ -172,7 +174,7 @@ class Matrix implements \ArrayAccess, \Iterator
public function setMatrix(array $matrix) : Matrix public function setMatrix(array $matrix) : Matrix
{ {
if ($this->m !== count($matrix) || $this->n !== count($matrix[0])) { 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; $this->matrix = $matrix;
@ -238,7 +240,7 @@ class Matrix implements \ArrayAccess, \Iterator
private function addMatrix(Matrix $matrix) : Matrix private function addMatrix(Matrix $matrix) : Matrix
{ {
if ($this->m !== $matrix->getM() || $this->n !== $matrix->getN()) { 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(); $matrixArr = $matrix->getMatrix();
@ -346,7 +348,7 @@ class Matrix implements \ArrayAccess, \Iterator
$mDim = $matrix->getM(); $mDim = $matrix->getM();
if ($this->n !== $mDim) { if ($this->n !== $mDim) {
throw new DimensionException($mDim, $nDim); throw new InvalidDimensionException($mDim . 'x' . $nDim);
} }
$matrixArr = $matrix->getMatrix(); $matrixArr = $matrix->getMatrix();
@ -485,14 +487,14 @@ class Matrix implements \ArrayAccess, \Iterator
* *
* @return Matrix * @return Matrix
* *
* @throws \Exception * @throws InvalidDimensionException
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public function inverse(int $algorithm = InverseType::GAUSS_JORDAN) : Matrix public function inverse(int $algorithm = InverseType::GAUSS_JORDAN) : Matrix
{ {
if ($this->n !== $this->m) { if ($this->n !== $this->m) {
throw new DimensionException($this->m, $this->n); throw new InvalidDimensionException($this->m . 'x' . $this->n);
} }
switch ($algorithm) { switch ($algorithm) {

View File

@ -16,6 +16,9 @@ declare(strict_types=1);
namespace phpOMS\Math\Statistic; namespace phpOMS\Math\Statistic;
use phpOMS\Math\Exception\ZeroDevisionException;
use phpOMS\Math\Matrix\Exception\InvalidDimensionException;
/** /**
* Average class. * Average class.
* *
@ -130,14 +133,14 @@ class Average
* *
* @return float * @return float
* *
* @throws \Exception * @throws InvalidDimensionException This exception is thrown in case both parameters have different array length
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public static function weightedAverage(array $values, array $weight) : float public static function weightedAverage(array $values, array $weight) : float
{ {
if (($count = count($values)) !== count($weight)) { if (($count = count($values)) !== count($weight)) {
throw new \Exception('Dimension'); throw new InvalidDimensionException(count($values) . 'x' . count($weight));
} }
$avg = 0.0; $avg = 0.0;
@ -167,7 +170,7 @@ class Average
$count = count($values); $count = count($values);
if ($count === 0) { if ($count === 0) {
throw new \Exception('Division zero'); throw new ZeroDevisionException();
} }
return array_sum($values) / $count; return array_sum($values) / $count;
@ -239,7 +242,7 @@ class Average
$count = count($values); $count = count($values);
if ($count === 0) { if ($count === 0) {
throw new \Exception('Division zero'); throw new ZeroDevisionException();
} }
return pow(array_product($values), 1 / $count); return pow(array_product($values), 1 / $count);
@ -272,7 +275,7 @@ class Average
foreach ($values as $value) { foreach ($values as $value) {
if ($value === 0) { if ($value === 0) {
throw new \Exception('Division zero'); throw new ZeroDevisionException();
} }
$sum += 1 / $value; $sum += 1 / $value;

View File

@ -16,6 +16,8 @@ declare(strict_types=1);
namespace phpOMS\Math\Statistic\Forecast\Regression; namespace phpOMS\Math\Statistic\Forecast\Regression;
use phpOMS\Math\Matrix\Exception\InvalidDimensionException;
/** /**
* Regression class. * Regression class.
* *
@ -33,8 +35,8 @@ class LevelLogRegression extends RegressionAbstract
*/ */
public static function getRegression(array $x, array $y) : array public static function getRegression(array $x, array $y) : array
{ {
if (($c = count($x)) != count($y)) { if (($c = count($x)) !== count($y)) {
throw new \Exception('Dimension'); throw new InvalidDimensionException($c . 'x' . count($y));
} }
for ($i = 0; $i < $c; $i++) { for ($i = 0; $i < $c; $i++) {

View File

@ -16,6 +16,8 @@ declare(strict_types=1);
namespace phpOMS\Math\Statistic\Forecast\Regression; namespace phpOMS\Math\Statistic\Forecast\Regression;
use phpOMS\Math\Matrix\Exception\InvalidDimensionException;
/** /**
* Regression class. * Regression class.
* *
@ -33,8 +35,8 @@ class LogLevelRegression extends RegressionAbstract
*/ */
public static function getRegression(array $x, array $y) : array public static function getRegression(array $x, array $y) : array
{ {
if (($c = count($x)) != count($y)) { if (($c = count($x)) !== count($y)) {
throw new \Exception('Dimension'); throw new InvalidDimensionException($c . 'x' . count($y));
} }
for ($i = 0; $i < $c; $i++) { for ($i = 0; $i < $c; $i++) {

View File

@ -16,6 +16,8 @@ declare(strict_types=1);
namespace phpOMS\Math\Statistic\Forecast\Regression; namespace phpOMS\Math\Statistic\Forecast\Regression;
use phpOMS\Math\Matrix\Exception\InvalidDimensionException;
/** /**
* Regression class. * Regression class.
* *
@ -33,8 +35,8 @@ class LogLogRegression extends RegressionAbstract
*/ */
public static function getRegression(array $x, array $y) : array public static function getRegression(array $x, array $y) : array
{ {
if (($c = count($x)) != count($y)) { if (($c = count($x)) !== count($y)) {
throw new \Exception('Dimension'); throw new InvalidDimensionException($c . 'x' . count($y));
} }
for ($i = 0; $i < $c; $i++) { for ($i = 0; $i < $c; $i++) {

View File

@ -18,6 +18,7 @@ declare(strict_types=1);
use phpOMS\Math\Statistic\Average; use phpOMS\Math\Statistic\Average;
use phpOMS\Math\Statistic\Forecast\ForecastIntervalMultiplier; use phpOMS\Math\Statistic\Forecast\ForecastIntervalMultiplier;
use phpOMS\Math\Statistic\MeasureOfDispersion; use phpOMS\Math\Statistic\MeasureOfDispersion;
use phpOMS\Math\Matrix\Exception\InvalidDimensionException;
abstract class RegressionAbstract abstract class RegressionAbstract
{ {
@ -31,14 +32,14 @@ abstract class RegressionAbstract
* *
* @return array [b0 => ?, b1 => ?] * @return array [b0 => ?, b1 => ?]
* *
* @throws \Exception * @throws InvalidDimensionException Throws this exception if the dimension of both arrays is not equal.
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public static function getRegression(array $x, array $y) : array public static function getRegression(array $x, array $y) : array
{ {
if (count($x) != count($y)) { if (count($x) !== count($y)) {
throw new \Exception('Dimension'); throw new InvalidDimensionException(count($x) . 'x' . count($y));
} }
$b1 = self::getBeta1($x, $y); $b1 = self::getBeta1($x, $y);

View File

@ -16,6 +16,9 @@ declare(strict_types=1);
namespace phpOMS\Math\Statistic; namespace phpOMS\Math\Statistic;
use phpOMS\Math\Exception\ZeroDevisionException;
use phpOMS\Math\Matrix\Exception\InvalidDimensionException;
/** /**
* Measure of dispersion. * Measure of dispersion.
* *
@ -67,7 +70,7 @@ class MeasureOfDispersion
$mean = Average::arithmeticMean($values); $mean = Average::arithmeticMean($values);
if ($mean === 0) { if ($mean === 0) {
throw new \Exception('Division zero'); throw new ZeroDevisionException();
} }
return self::standardDeviation($values) / $mean; return self::standardDeviation($values) / $mean;
@ -107,7 +110,7 @@ class MeasureOfDispersion
$count = count($values); $count = count($values);
if ($count < 2) { if ($count < 2) {
throw new \Exception('Division zero'); throw new ZeroDevisionException();
} }
return $count * self::empiricalVariance($values) / ($count - 1); return $count * self::empiricalVariance($values) / ($count - 1);
@ -131,7 +134,7 @@ class MeasureOfDispersion
$count = count($values); $count = count($values);
if ($count === 0) { if ($count === 0) {
throw new \Exception('Division zero'); throw new ZeroDevisionException();
} }
$mean = Average::arithmeticMean($values); $mean = Average::arithmeticMean($values);
@ -154,7 +157,7 @@ class MeasureOfDispersion
* *
* @return float * @return float
* *
* @throws \Exception * @throws InvalidDimensionException
* *
* @since 1.0.0 * @since 1.0.0
*/ */
@ -163,11 +166,11 @@ class MeasureOfDispersion
$count = count($x); $count = count($x);
if ($count < 2) { if ($count < 2) {
throw new \Exception('Division zero'); throw new ZeroDevisionException();
} }
if ($count !== count($y)) { if ($count !== count($y)) {
throw new \Exception('Dimensions'); throw new InvalidDimensionException($count . 'x' . count($y));
} }
$xMean = Average::arithmeticMean($x); $xMean = Average::arithmeticMean($x);