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;
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];

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;
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) {

View File

@ -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;

View File

@ -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++) {

View File

@ -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++) {

View File

@ -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++) {

View File

@ -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);

View File

@ -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);