Namespace, naming etc. fixes

This commit is contained in:
Dennis Eichhorn 2016-06-14 14:50:42 +02:00
parent 4c9905e73e
commit bc75d9ce81
23 changed files with 226 additions and 241 deletions

View File

@ -1,119 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math\Algebra;
/**
* Scheduler factory.
*
* @category Framework
* @package phpOMS\Utils\TaskSchedule
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
final class PointPolygonIntersection
{
/**
* Epsilon.
*
* @var float
* @since 1.0.0
*/
const EPSILON = 1E-6;
/**
* Point polygon relative position
*
* @param array $point Point location
* @param array $vertices Vertice locations
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function pointInPolygon(array $point, array $vertices) : int
{
$length = count($vertices);
// Polygon has to start and end with same point
if ($vertices[0]['x'] !== $vertices[$length - 1]['x'] || $vertices[0]['y'] !== $vertices[$length - 1]['y']) {
$vertices[] = $vertices[0];
}
// On vertex?
if (self::isOnVertex($point, $vertices)) {
return 0;
}
// Inside or ontop?
$countIntersect = 0;
$vertices_count = count($vertices);
// todo: return based on highest possibility not by first match
for ($i = 1; $i < $vertices_count; $i++) {
$vertex1 = $vertices[$i - 1];
$vertex2 = $vertices[$i];
if (abs($vertex1['y'] - $vertex2['y']) < self::EPSILON && abs($vertex1['y'] - $point['y']) < self::EPSILON && $point['x'] > min($vertex1['x'], $vertex2['x']) && $point['x'] < max($vertex1['x'], $vertex2['x'])) {
return 0; // boundary
}
if ($point['y'] > min($vertex1['y'], $vertex2['y']) && $point['y'] <= max($vertex1['y'], $vertex2['y']) && $point['x'] <= max($vertex1['x'], $vertex2['x']) && abs($vertex1['y'] - $vertex2['y']) >= self::EPSILON) {
$xinters = ($point['y'] - $vertex1['y']) * ($vertex2['x'] - $vertex1['x']) / ($vertex2['y'] - $vertex1['y']) + $vertex1['x'];
if (abs($xinters - $point['x']) < self::EPSILON) {
return 0; // boundary
}
if (abs($vertex1['x'] - $vertex2['x']) < self::EPSILON || $point['x'] < $xinters) {
$countIntersect++;
}
}
}
if ($countIntersect % 2 != 0) {
return -1;
}
return 1;
}
/**
* Is point on vertex?
*
* @param array $point Point location
* @param array $vertices Vertice locations
*
* @return bool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
private static function isOnVertex($point, $vertices) : bool
{
foreach ($vertices as $vertex) {
if (abs($point['x'] - $vertex['x']) < self::EPSILON && abs($point['y'] - $vertex['y']) < self::EPSILON) {
return true;
}
}
return false;
}
}

View File

@ -1,72 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math\Algebra;
/**
* Regression class.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class Regression
{
/**
* Calculate linear regression.
*
* Example: ([1, 4, 6, 8, 9], [5, 3, 8, 6, 2])
*
* @param array $x X coordinates
* @param array $y Y coordinates
*
* @return array
*
* @throws \Exception
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function linearRegression(array $x, array $y) : array
{
$count = count($x);
if ($count !== count($y)) {
throw new \Exception('Dimensions');
}
$xSum = array_sum($x);
$ySum = array_sum($y);
$xxSum = 0;
$xySum = 0;
for ($i = 0; $i < $count; $i++) {
$xySum += ($x[$i] * $y[$i]);
$xxSum += ($x[$i] * $x[$i]);
}
$m = (($count * $xySum) - ($xSum * $ySum)) / (($count * $xxSum) - ($xSum * $xSum));
$b = ($ySum - ($m * $xSum)) / $count;
return ['m' => $m, 'b' => $b];
}
}

View File

@ -62,7 +62,7 @@ class Numbers
* @since 1.0.0
* @author Dennis Eichhorn
*/
public static function selfdescribing(int $n) : bool
public static function isSelfdescribing(int $n) : bool
{
$split = str_split($n);
foreach ($split as $place => $value) {

View File

View File

@ -42,7 +42,7 @@ class GaussianElimination
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
private function swapRows(&$a, &$b, int $r1, int $r2)
private static function swapRows(&$a, &$b, int $r1, int $r2)
{
if ($r1 == $r2) {
return;
@ -68,7 +68,7 @@ class GaussianElimination
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function solve(Matrix $A, Matrix $b) : Matrix
public static function solve(Matrix $A, Matrix $b) : Matrix
{
$limit = min($A->getM(), $A->getN());
$A = $A->getMatrix();

View File

@ -60,7 +60,7 @@ class BruteForce
$this->cityPool = $pool;
if ($this->cityPool->count() > self::LIMIT) {
throw new \Exception('64 bit overflow');
throw new \Exception('Overflow');
}
}
@ -98,7 +98,7 @@ class BruteForce
*/
private function bruteForce(array $cities, Tour $tour, Population $population)
{
if (count($cities) === 0) {
if (empty($cities)) {
$population->addTour($tour);
}

View File

@ -89,20 +89,20 @@ class GA
$shift = self::ELITISM ? 1 : 0;
$newPopulation = new Population($this->cityPool, $count = $population->count(), false);
$newPopulation->addTour($population->getFittest());
$newPopulation->add($population->getFittest());
for ($i = $shift; $i < $count; $i++) {
$parent1 = $this->tournamentSelection($population);
$parent2 = $this->tournamentSelection($population);
$child = $this->crossover($parent1, $parent2);
$newPopulation->setTour($i, $child);
$newPopulation->set($i, $child);
}
$count = $newPopulation->count();
for ($i = $shift; $i < $count; $i++) {
$this->mutate($newPopulation->getTour($i));
$this->mutate($newPopulation->get($i));
}
return $newPopulation;
@ -197,7 +197,7 @@ class GA
$populationSize = $population->count();
for ($i = 0; $i < self::TOURNAMENT; $i++) {
$tournament->addTour($population->getTour(mt_rand(0, $populationSize)));
$tournament->add($population->get(mt_rand(0, $populationSize)));
}
return $tournament->getFittest();

View File

@ -65,7 +65,7 @@ class Population implements \Countable
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function insertTourAt(int $index, Tour $tour)
public function insertAt(int $index, Tour $tour)
{
$this->tours = array_slice($this->tours, 0, $index) + [$tour] + array_slice($this->tours, $index);
}
@ -79,7 +79,7 @@ class Population implements \Countable
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function setTour(int $index, Tour $tour)
public function set(int $index, Tour $tour)
{
$this->tours[$index] = $tour;
asort($this->tours);
@ -93,7 +93,7 @@ class Population implements \Countable
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function addTour(Tour $tour)
public function add(Tour $tour)
{
$this->tours[] = $tour;
}
@ -108,7 +108,7 @@ class Population implements \Countable
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getTour(int $index)
public function get(int $index)
{
return $this->tours[$index] ?? null;
}

View File

@ -13,7 +13,7 @@
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math\Algebra;
namespace phpOMS\Math\Shape\D2;
/**
* Circle shape.
@ -26,7 +26,7 @@ namespace phpOMS\Math\Algebra;
* @link http://orange-management.com
* @since 1.0.0
*/
class Circle
class Circle implements D2ShapeInterface
{
/**
@ -39,7 +39,7 @@ class Circle
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getArea(float $r)
public static function getSurface(float $r)
{
return pi() * $r ** 2;
}
@ -54,7 +54,7 @@ class Circle
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getCircumference(float $r)
public static function getPerimeter(float $r)
{
return 2 * pi() * $r;
}
@ -62,16 +62,16 @@ class Circle
/**
* Radius
*
* @param float $area Area
* @param float $surface Surface
*
* @return float
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getRadiusByArea(float $area)
public static function getRadiusBySurface(float $surface)
{
return sqrt($area / pi());
return sqrt($surface / pi());
}
/**
@ -84,7 +84,7 @@ class Circle
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getRadiusByCircumference(float $C)
public static function getRadiusByPerimeter(float $C)
{
return $C / (2 * pi());
}

View File

@ -0,0 +1,34 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math\Shape\D2;
use phpOMS\Math\Shape\ShapeInterface;
/**
* Shape interface.
*
* @category Framework
* @package phpOMS\Math
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
interface D2ShapeInterface extends ShapeInterface
{
public static function getPerimeter() : float;
}

View File

@ -13,7 +13,7 @@
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math\Algebra;
namespace phpOMS\Math\Shape\D2;
/**
* Ellipse shape.
@ -26,7 +26,7 @@ namespace phpOMS\Math\Algebra;
* @link http://orange-management.com
* @since 1.0.0
*/
class Ellipse
class Ellipse implements D2ShapeInterface
{
/**
@ -45,7 +45,7 @@ class Ellipse
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getArea(float $a, float $b)
public static function getSurface(float $a, float $b)
{
return pi() * $a * $b;
}
@ -66,7 +66,7 @@ class Ellipse
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getCircumference(float $a, float $b)
public static function getPerimeter(float $a, float $b)
{
return pi() * ($a + $b) * (3 * ($a - $b) ** 2 / (($a + $b) ** 2 * (sqrt(-3 * ($a - $b) ** 2 / (($a + $b) ** 2) + 4) + 10)) + 1);
}

View File

@ -14,8 +14,6 @@
* @link http://orange-management.com
*/
namespace phpOMS\Math\Shape\D2;
use phpOMS\Math\Shape\ShapeInterface;
/**
* Polygon class.
@ -28,7 +26,7 @@ use phpOMS\Math\Shape\ShapeInterface;
* @link http://orange-management.com
* @since 1.0.0
*/
class Polygon implements ShapeInterface
class Polygon implements D2ShapeInterface
{
/**
@ -283,4 +281,82 @@ class Polygon implements ShapeInterface
return $this->barycenter;
}
/**
* Point polygon relative position
*
* @param array $point Point location
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function pointInPolygon(array $point) : int
{
$length = count($this->coord);
// Polygon has to start and end with same point
if ($this->coord[0]['x'] !== $this->coord[$length - 1]['x'] || $this->coord[0]['y'] !== $this->coord[$length - 1]['y']) {
$this->coord[] = $this->coord[0];
}
// On vertex?
if (self::isOnVertex($point, $this->coord)) {
return 0;
}
// Inside or ontop?
$countIntersect = 0;
$this->coord_count = count($this->coord);
// todo: return based on highest possibility not by first match
for ($i = 1; $i < $this->coord_count; $i++) {
$vertex1 = $this->coord[$i - 1];
$vertex2 = $this->coord[$i];
if (abs($vertex1['y'] - $vertex2['y']) < self::EPSILON && abs($vertex1['y'] - $point['y']) < self::EPSILON && $point['x'] > min($vertex1['x'], $vertex2['x']) && $point['x'] < max($vertex1['x'], $vertex2['x'])) {
return 0; // boundary
}
if ($point['y'] > min($vertex1['y'], $vertex2['y']) && $point['y'] <= max($vertex1['y'], $vertex2['y']) && $point['x'] <= max($vertex1['x'], $vertex2['x']) && abs($vertex1['y'] - $vertex2['y']) >= self::EPSILON) {
$xinters = ($point['y'] - $vertex1['y']) * ($vertex2['x'] - $vertex1['x']) / ($vertex2['y'] - $vertex1['y']) + $vertex1['x'];
if (abs($xinters - $point['x']) < self::EPSILON) {
return 0; // boundary
}
if (abs($vertex1['x'] - $vertex2['x']) < self::EPSILON || $point['x'] < $xinters) {
$countIntersect++;
}
}
}
if ($countIntersect % 2 != 0) {
return -1;
}
return 1;
}
/**
* Is point on vertex?
*
* @param array $point Point location
*
* @return bool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
private static function isOnVertex(array $point) : bool
{
foreach ($this->coord as $vertex) {
if (abs($point['x'] - $vertex['x']) < self::EPSILON && abs($point['y'] - $vertex['y']) < self::EPSILON) {
return true;
}
}
return false;
}
}

View File

@ -13,7 +13,7 @@
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math\Algebra;
namespace phpOMS\Math\Shape\D2;
/**
* Rectangle shape.
@ -26,7 +26,7 @@ namespace phpOMS\Math\Algebra;
* @link http://orange-management.com
* @since 1.0.0
*/
class Rectangle
class Rectangle implements D2ShapeInterface
{
/**
@ -40,7 +40,7 @@ class Rectangle
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getArea(float $a, float $b)
public static function getSurface(float $a, float $b)
{
return $a * $b;
}

View File

@ -13,7 +13,7 @@
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math\Algebra;
namespace phpOMS\Math\Shape\D2;
/**
* Trapezoid shape.
@ -26,7 +26,7 @@ namespace phpOMS\Math\Algebra;
* @link http://orange-management.com
* @since 1.0.0
*/
class Trapezoid
class Trapezoid implements D2ShapeInterface
{
/**
@ -47,7 +47,7 @@ class Trapezoid
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getArea(float $a, float $b, float $h)
public static function getSurface(float $a, float $b, float $h)
{
return ($a + $b) / 2 * $h;
}

View File

@ -13,7 +13,7 @@
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math\Algebra;
namespace phpOMS\Math\Shape\D2;
/**
* Triangle shape.
@ -26,7 +26,7 @@ namespace phpOMS\Math\Algebra;
* @link http://orange-management.com
* @since 1.0.0
*/
class Triangle
class Triangle implements D2ShapeInterface
{
/**
@ -46,7 +46,7 @@ class Triangle
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getArea(float $b, float $h)
public static function getSurface(float $b, float $h)
{
return $h * $b / 2;
}

View File

@ -13,7 +13,7 @@
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math\Algebra;
namespace phpOMS\Math\Shape\D3;
/**
* Cone shape.
@ -26,7 +26,7 @@ namespace phpOMS\Math\Algebra;
* @link http://orange-management.com
* @since 1.0.0
*/
class Cone
class Cone implements D3ShapeInterface
{
/**

View File

@ -13,7 +13,7 @@
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math\Algebra;
namespace phpOMS\Math\Shape\D3;
/**
* Cuboid shape.
@ -26,7 +26,7 @@ namespace phpOMS\Math\Algebra;
* @link http://orange-management.com
* @since 1.0.0
*/
class Cuboid
class Cuboid implements D3ShapeInterface
{
/**

View File

@ -13,7 +13,7 @@
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math\Algebra;
namespace phpOMS\Math\Shape\D3;
/**
* Cylinder shape.
@ -26,7 +26,7 @@ namespace phpOMS\Math\Algebra;
* @link http://orange-management.com
* @since 1.0.0
*/
class Cylinder
class Cylinder implements D3ShapeInterface
{
/**

View File

@ -0,0 +1,34 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math\Shape\D3;
use phpOMS\Math\Shape\ShapeInterface;
/**
* Shape interface.
*
* @category Framework
* @package phpOMS\Math
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
interface D2ShapeInterface extends ShapeInterface
{
public static function getVolume() : float;
}

View File

@ -13,7 +13,7 @@
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math\Algebra;
namespace phpOMS\Math\Shape\D3;
/**
* Rectangular pyramid shape.
@ -26,7 +26,7 @@ namespace phpOMS\Math\Algebra;
* @link http://orange-management.com
* @since 1.0.0
*/
class RectangularPyramid
class RectangularPyramid implements D3ShapeInterface
{
/**

View File

@ -26,7 +26,7 @@ namespace phpOMS\Math\Shape\D3;
* @link http://orange-management.com
* @since 1.0.0
*/
class Sphere
class Sphere implements D3ShapeInterface
{
/**
@ -73,7 +73,7 @@ class Sphere
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getVolume(float $r)
public static function getVolumeByRadius(float $r)
{
return 4 / 3 * pi() * $r ** 3;
}
@ -103,7 +103,7 @@ class Sphere
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getSurface(float $r)
public static function getSurfaceByRadius(float $r)
{
return 4 * pi() * $r ** 2;
}
@ -122,4 +122,35 @@ class Sphere
{
return sqrt($S / (4 * pi()));
}
public static function byRadius(float $r) : Sphere
{
return new self($r);
}
public static function byVolume(float $v) : Sphere
{
return new self(self::getRadiusByVolume($v));
}
public static function bySurface(float $s) : Sphere
{
return new self(self::getRadiusBySurface($s));
}
public function __construct(float $radius) {
$this->radius = $radius;
}
public function getVolume() : float {
return self::getVolumeByRadius($this->radius);
}
public function getRadius() : float {
return $this->radius;
}
public function getSurface() : float {
return self::getSurfaceByRadius($this->radius);
}
}

View File

@ -13,7 +13,7 @@
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math\Algebra;
namespace phpOMS\Math\Shape\D3;
/**
* Tetraedron shape.
@ -26,7 +26,7 @@ namespace phpOMS\Math\Algebra;
* @link http://orange-management.com
* @since 1.0.0
*/
class Tetrahedron
class Tetrahedron implements D3ShapeInterface
{
/**

View File

@ -28,4 +28,5 @@ namespace phpOMS\Math\Shape;
*/
interface ShapeInterface
{
public static function getSurface() : float;
}