phpcs fixes + continue distribution implementations

This commit is contained in:
Dennis Eichhorn 2019-10-09 17:15:12 +02:00
parent 1145144a98
commit b9d0e63beb
53 changed files with 1791 additions and 195 deletions

View File

@ -105,6 +105,7 @@ class PermissionAbstract implements \JsonSerializable
* @param null|int $unit Unit Unit to check (null if all are acceptable)
* @param null|string $app App App to check (null if all are acceptable)
* @param null|string $module Module Module to check (null if all are acceptable)
* @param int $from Provided by which module
* @param null|int $type Type (e.g. customer) (null if all are acceptable)
* @param null|int $element (e.g. customer id) (null if all are acceptable)
* @param null|int $component (e.g. address) (null if all are acceptable)

View File

@ -41,6 +41,16 @@ class BitonicSort implements SortInterface
return self::merge(\array_merge($first, $second), $order);
}
/**
* Splitting, merging and sorting list
*
* @param array $list List to sort
* @param int $order Sort order
*
* @return array
*
* @since 1.0.0
*/
private static function merge(array $list, int $order) : array
{
$n = \count($list);

View File

@ -0,0 +1,84 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\Algorithm\Sort;
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\Algorithm\Sort;
/**
* HeapSort class.
*
* @package phpOMS\Algorithm\Sort;
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class HeapSort implements SortInterface
{
/**
* {@inheritdoc}
*/
public static function sort(array $list, int $order = SortOrder::ASC) : array
{
$n = \count($list);
$copy = $list;
for ($p = ($n - 1) / 2; $p >= 0; --$p) {
self::heapify($copy, $n, $p, $order);
}
for ($i = $n - 1; $i > 0; --$i) {
$temp = $copy[$i];
$copy[$i] = $copy[0];
$copy[0] = $temp;
--$n;
self::heapify($copy, $n, 0, $order);
}
return $copy;
}
/**
* Convert into heap data structure
*
* @param array $list Data to sort
* @param int $size Heap size
* @param int $index Index element
* @param itn $order Sort order
*
* @return void
*
* @since 1.0.0
*/
private static function heapify(array &$list, int $size, int $index, int $order) : void
{
$left = ($index + 1) * 2 - 1;
$right = ($index + 1) * 2;
$pivot = 0;
// todo: also check $left > $size if test failes for desc!
$pivot = $left < $size && $list[$left]->compare($list[$index], $order) ? $left : $index;
if ($right < $size && $list[$right]->compare($list[$pivot], $order)) {
$pivot = $right;
}
if ($pivot !== $index) {
$temp = $list[$index];
$list[$index] = $list[$pivot];
$list[$pivot] = $temp;
self::heapify($list, $size, $pivot, $order);
}
}
}

View File

@ -0,0 +1,47 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\Algorithm\Sort;
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\Algorithm\Sort;
/**
* InsertionSort class.
*
* @package phpOMS\Algorithm\Sort;
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class InsertionSort implements SortInterface
{
/**
* {@inheritdoc}
*/
public static function sort(array $list, int $order = SortOrder::ASC) : array
{
$n = \count($list);
for ($i = 1; $i < $n; ++$i) {
$j = $i;
while ($j > 0 && $list[$j - 1]->compare($list[$j], $order)) {
$list[$j + 1] = $list[$j];
--$j;
}
$list[$j + 1] = $list[$i];
}
return $list;
}
}

View File

@ -0,0 +1,78 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\Algorithm\Sort;
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\Algorithm\Sort;
/**
* IntroSort class.
*
* @package phpOMS\Algorithm\Sort;
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class IntroSort implements SortInterface
{
/**
* {@inheritdoc}
*/
public static function sort(array $list, int $order = SortOrder::ASC) : array
{
$clone = $list;
$size = self::partition($clone, 0, \count($list) - 1, $order);
if ($size < 16) {
return InsertionSort::sort($clone, $order);
}
if ($size > \log(\count($list)) * 2) {
return HeapSort::sort($clone, $order);
}
return QuickSort::sort($clone);
}
/**
* Partition list and return the size
*
* @param array $list List reference
* @param int $lo Low or left side
* @param int $hi High or right side
* @param int $order Order type
*
* @return int
*
* @since 1.0.0
*/
private static function partition(array &$list, int $lo, int $hi, int $order) : int
{
$pivot = $list[$hi];
$i = $lo;
for ($j = $lo; $j < $hi; ++$j) {
if ($list[$j]->compare($pivot, $order)) {
$temp = $list[$j];
$list[$j] = $list[$i];
$list[$i] = $temp;
++$i;
}
}
$list[$hi] = $list[$i];
$list[$i] = $pivot;
return $i;
}
}

View File

@ -35,6 +35,18 @@ class QuickSort implements SortInterface
return $copy;
}
/**
* Recursive quick sort
*
* @param array $list Data to sort
* @param int $lo Low or left point to sort
* @param int $hi High or right point to sort
* @param int $order Sort order
*
* @return void
*
* @since 1.0.0
*/
private static function qsort(array &$list, int $lo, int $hi, int $order) : void
{
if ($lo < $hi) {
@ -44,6 +56,18 @@ class QuickSort implements SortInterface
}
}
/**
* Partition data and count the partitions
*
* @param array $list Data to sort
* @param int $lo Low or left point to sort
* @param int $hi High or right point to sort
* @param int $order Sort order
*
* @return int
*
* @since 1.0.0
*/
private static function partition(array &$list, int $lo, int $hi, int $order) : int
{
$pivot = $list[$lo + ((int) (($hi - $lo) / 2))];

View File

@ -22,43 +22,6 @@ namespace phpOMS\DataStorage\Database\Query;
* @link https://orange-management.org
* @since 1.0.0
*/
class Column
class Column extends Builder
{
/**
* Column name.
*
* @var string
* @since 1.0.0
*/
private string $column = '';
/**
* Constructor.
*
* @param string $column Column
*
* @since 1.0.0
*/
public function __construct(string $column)
{
$this->column = $column;
}
/**
* Get column string.
*
* @return string
*
* @since 1.0.0
*/
public function getColumn() : string
{
return $this->column;
}
public function setColumn(string $column) : void
{
$this->column = $column;
}
}

View File

@ -15,7 +15,15 @@ declare(strict_types=1);
namespace phpOMS\DataStorage\Database\Query;
class Expression
/**
* Database query builder.
*
* @package phpOMS\DataStorage\Database\Query
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class Expression extends Builder
{
}

View File

@ -0,0 +1,27 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\DataStorage\Database\Query
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\DataStorage\Database\Query;
/**
* Database query builder.
*
* @package phpOMS\DataStorage\Database\Query
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class From extends Builder
{
}

View File

@ -254,7 +254,7 @@ class Grammar extends GrammarAbstract
} elseif ($element['column'] instanceof Builder) {
$expression .= '(' . $element['column']->toSql() . ')';
} elseif ($element['column'] instanceof Where) {
$expression .= '(' . $this->compileWhere($element['column'], $query->getPrefix()) . ')';
$expression .= '(' . \rtrim($this->compileWhereQuery($element['column']), ';') . ')';
}
if (isset($element['value'])) {
@ -267,10 +267,46 @@ class Grammar extends GrammarAbstract
return $expression;
}
protected function compileWhere(Where $where, string $prefix = '', bool $first = true) : string
/**
* Compile where query.
*
* @param Where $where Where query
*
* @return string
*
* @since 1.0.0
*/
protected function compileWhereQuery(Where $where) : string
{
return $where->toSql();
}
return '';
/**
* Compile from query.
*
* @param From $from Where query
*
* @return string
*
* @since 1.0.0
*/
protected function compileFromQuery(From $from) : string
{
return $from->toSql();
}
/**
* Compile column query.
*
* @param column $column Where query
*
* @return string
*
* @since 1.0.0
*/
protected function compileColumnQuery(column $column) : string
{
return $column->toSql();
}
/**
@ -313,7 +349,7 @@ class Grammar extends GrammarAbstract
} elseif (\is_float($value)) {
return \rtrim(\rtrim(\number_format($value, 5, '.', ''), '0'), '.');
} elseif ($value instanceof Column) {
return $this->compileSystem($value->getColumn(), $prefix);
return '(' . \rtrim($this->compileColumnQuery($value), ';') . ')';
} elseif ($value instanceof Builder) {
return '(' . \rtrim($value->toSql(), ';') . ')';
} elseif ($value instanceof \JsonSerializable) {
@ -440,7 +476,7 @@ class Grammar extends GrammarAbstract
} elseif ($element['column'] instanceof Builder) {
$expression .= '(' . $element['column']->toSql() . ')';
} elseif ($element['column'] instanceof Where) {
$expression .= '(' . $this->compileWhere($element['column'], $query->getPrefix()) . ')';
$expression .= '(' . \rtrim($this->compileWhereQuery($element['column']), ';') . ')';
}
if (isset($element['value'])) {

View File

@ -0,0 +1,27 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\DataStorage\Database\Query
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\DataStorage\Database\Query;
/**
* Database query builder.
*
* @package phpOMS\DataStorage\Database\Query
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class Into extends Builder
{
}

View File

@ -0,0 +1,27 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\DataStorage\Database\Query
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\DataStorage\Database\Query;
/**
* Database query builder.
*
* @package phpOMS\DataStorage\Database\Query
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class Select extends Builder
{
}

View File

@ -22,6 +22,6 @@ namespace phpOMS\DataStorage\Database\Query;
* @link https://orange-management.org
* @since 1.0.0
*/
class Where
class Where extends Builder
{
}

View File

@ -27,6 +27,12 @@ use phpOMS\DataStorage\Database\Query\Builder as QueryBuilder;
*/
class Builder extends QueryBuilder
{
/**
* Table to create.
*
* @var string
* @since 1.0.0
*/
public string $createTable = '';
/**
@ -133,6 +139,15 @@ class Builder extends QueryBuilder
return $builder;
}
/**
* Drop database
*
* @param string $database Database to drop
*
* @return self
*
* @since 1.0.0
*/
public function dropDatabase(string $database) : self
{
$this->type = QueryType::DROP_DATABASE;
@ -141,6 +156,15 @@ class Builder extends QueryBuilder
return $this;
}
/**
* Drop table
*
* @param string $table Table to drop
*
* @return self
*
* @since 1.0.0
*/
public function dropTable(string $table) : self
{
$this->type = QueryType::DROP_TABLE;
@ -149,6 +173,13 @@ class Builder extends QueryBuilder
return $this;
}
/**
* Select all tables
*
* @return self
*
* @since 1.0.0
*/
public function selectTables() : self
{
$this->type = QueryType::TABLES;
@ -156,6 +187,15 @@ class Builder extends QueryBuilder
return $this;
}
/**
* Select all fields of table
*
* @param string $table Table to select fields from
*
* @return self
*
* @since 1.0.0
*/
public function selectFields(string $table) : self
{
$this->type = QueryType::FIELDS;
@ -164,6 +204,15 @@ class Builder extends QueryBuilder
return $this;
}
/**
* Create table
*
* @param string $name Table to create
*
* @return self
*
* @since 1.0.0
*/
public function createTable(string $name) : self
{
$this->type = QueryType::CREATE_TABLE;
@ -172,7 +221,22 @@ class Builder extends QueryBuilder
return $this;
}
// todo: consider to work with flags instead of all these booleans
/**
* Define field for create
*
* @param string $name Field name
* @param string $type Field type
* @param mixed $default Default value
* @param bool $isNullable Can be null
* @param bool $isPrimary Is a primary field
* @param bool $autoincrement Autoincrements
* @param string $foreignTable Foreign table (in case of foreign key)
* @param string $foreignKey Foreign key
*
* @return self
*
* @since 1.0.0
*/
public function field(
string $name, string $type, $default = null,
bool $isNullable = true, bool $isPrimary = false, bool $autoincrement = false,

View File

@ -15,6 +15,14 @@ declare(strict_types=1);
namespace phpOMS\DataStorage\Database\Schema\Grammar;
/**
* Grammar interface.
*
* @package phpOMS\DataStorage\Database\Schema\Grammar
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
interface GrammarInterface
{

View File

@ -15,6 +15,14 @@ declare(strict_types=1);
namespace phpOMS\DataStorage\Database\Schema\Grammar;
/**
* Database query grammar.
*
* @package phpOMS\DataStorage\Database\Schema\Grammar
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class OracleGrammar extends Grammar
{

View File

@ -15,6 +15,14 @@ declare(strict_types=1);
namespace phpOMS\DataStorage\Database\Schema\Grammar;
/**
* Database query grammar.
*
* @package phpOMS\DataStorage\Database\Schema\Grammar
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class PostgresGrammar extends Grammar
{

View File

@ -15,6 +15,14 @@ declare(strict_types=1);
namespace phpOMS\DataStorage\Database\Schema\Grammar;
/**
* Database query grammar.
*
* @package phpOMS\DataStorage\Database\Schema\Grammar
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class SQLiteGrammar extends Grammar
{
/**

View File

@ -15,6 +15,14 @@ declare(strict_types=1);
namespace phpOMS\DataStorage\Database\Schema\Grammar;
/**
* Database query grammar.
*
* @package phpOMS\DataStorage\Database\Schema\Grammar
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class SqlServerGrammar extends Grammar
{

View File

@ -103,7 +103,7 @@ class BernoulliDistribution
}
/**
* Get expected value.
* Get median.
*
* @param float $p Value p
*
@ -119,7 +119,7 @@ class BernoulliDistribution
return 1;
}
return 0;
return 0.0;
}
/**

View File

@ -13,7 +13,125 @@
declare(strict_types=1);
namespace phpOMS\Math\Stochastic\Distribution;
use phpOMS\Math\Functions\Functions;
/**
* Beta distribution.
*
* @package phpOMS\Math\Stochastic\Distribution
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class BetaDistribution
{
/**
* Get expected value.
*
* @param float $alpha Alpha
* @param float $beta Beta
*
* @return float
*
* @since 1.0.0
*/
public static function getMean(float $alpha, float $beta) : float
{
return $alpha / ($alpha + $beta);
}
/**
* Get mode.
*
* @param float $alpha Alpha
* @param float $beta Beta
*
* @return float
*
* @since 1.0.0
*/
public static function getMode(float $alpha, float $beta) : float
{
if ($alpha > 1 && $beta > 1) {
return ($alpha - 1) / ($alpha + $beta - 2);
}
if ($alpha <= 1.0 && $beta > 1) {
return 0.0;
}
return 1.0;
}
/**
* Get variance.
*
* @param float $alpha Alpha
* @param float $beta Beta
*
* @return float
*
* @since 1.0.0
*/
public static function getVariance(float $alpha, float $beta) : float
{
return $alpha * $beta / (($alpha + $beta) ** 2 * ($alpha + $beta + 1));
}
/**
* Get skewness.
*
* @param float $alpha Alpha
* @param float $beta Beta
*
* @return float
*
* @since 1.0.0
*/
public static function getSkewness(float $alpha, float $beta) : float
{
return 2 * ($beta - $alpha) * \sqrt($alpha + $beta + 1) / (($alpha + $beta + 2) * \sqrt($alpha * $beta));
}
/**
* Get Ex. kurtosis.
*
* @param float $alpha Alpha
* @param float $beta Beta
*
* @return float
*
* @since 1.0.0
*/
public static function getExKurtosis(float $alpha, float $beta) : float
{
return 6 * (($alpha - $beta) ** 2 * ($alpha + $beta + 1) - $alpha * $beta * ($alpha + $beta + 2))
/ ($alpha * $beta * ($alpha + $beta + 2) * ($alpha + $beta + 3));
}
/**
* Get moment generating function.
*
* @param float $t Value t
* @param float $alpha Alpha
* @param float $beta Beta
*
* @return float
*
* @since 1.0.0
*/
public static function getMgf(float $t, float $alpha, float $beta) : float
{
$sum = 0;
for ($k = 1; $k < 100000; ++$k) {
$product = 1;
for ($r = 0; $r < $k - 1; ++$r) {
$product *= ($alpha + $r) / ($alpha + $beta + $r);
}
$sum += $product * $t ** $k / Functions::fact($k);
}
return 1 + $sum;
}
}

View File

@ -144,7 +144,7 @@ class BinomialDistribution
}
/**
* Get expected value.
* Get median.
*
* @param int $n Value n
* @param float $p Value p

View File

@ -71,7 +71,7 @@ class CauchyDistribution
}
/**
* Get expected value.
* Get median.
*
* @param float $x0 Value x0
*

View File

@ -192,7 +192,7 @@ class ChiSquaredDistribution
}
/**
* Get expected value.
* Get median.
*
* @param int $df Degrees of freedom
*

View File

@ -81,7 +81,7 @@ class ExponentialDistribution
}
/**
* Get expected value.
* Get median.
*
* @param float $lambda Lambda
*

View File

@ -13,7 +13,86 @@
declare(strict_types=1);
namespace phpOMS\Math\Stochastic\Distribution;
/**
* F distribution.
*
* @package phpOMS\Math\Stochastic\Distribution
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class FDistribution
{
/**
* Get expected value.
*
* @param int $d2 Degree of freedom
*
* @return float
*
* @since 1.0.0
*/
public static function getMean(int $d2) : float
{
if ($d2 === 2) {
return 0.0;
}
return $d2 / ($d2 - 2);
}
/**
* Get mode.
*
* @param int $d1 Degree of freedom
* @param int $d2 Degree of freedom
*
* @return float
*
* @since 1.0.0
*/
public static function getMode(int $d1, int $d2) : float
{
return ($d1 - 2) / $d1 * $d2 / ($d2 + 2);
}
/**
* Get variance.
*
* @param int $d1 Degree of freedom
* @param int $d2 Degree of freedom
*
* @return float
*
* @since 1.0.0
*/
public static function getVariance(int $d1, int $d2) : float
{
if ($d2 === 2 || $d2 === 4) {
return 0.0;
}
return 2 * $d2 ** 2 * ($d1 + $d2 - 2)
/ ($d1 * ($d2 - 2) ** 2 * ($d2 - 4));
}
/**
* Get skewness.
*
* @param int $d1 Degree of freedom
* @param int $d2 Degree of freedom
*
* @return float
*
* @since 1.0.0
*/
public static function getSkewness(int $d1, int $d2) : float
{
if ($d2 < 7) {
return 0.0;
}
return (2 * $d1 + $d2 - 2) * \sqrt(8 * ($d2 - 4))
/ (($d2 - 6) * \sqrt($d1 * ($d1 + $d2 - 2)));
}
}

View File

@ -81,7 +81,7 @@ class GeometricDistribution
}
/**
* Get expected value.
* Get median.
*
* @param float $p Value p
*

View File

@ -13,7 +13,126 @@
declare(strict_types=1);
namespace phpOMS\Math\Stochastic\Distribution;
use phpOMS\Math\Functions\Functions;
/**
* Hypergeometric distribution.
*
* @package phpOMS\Math\Stochastic\Distribution
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class HypergeometricDistribution
{
/**
* Get probability mass function.
*
* @param int $K Successful states in the population
* @param int $N Population size
* @param int $k Observed successes
* @param int $n Number of draws
*
* @return float
*
* @todo: this can be heavily optimized
*
* @since 1.0.0
*/
public static function getPmf(int $K, int $N, int $k, int $n) : float
{
return Functions::fact($K, $k) * Functions::fact($N - $K, $n - $k) / Functions::fact($N, $n);
}
/**
* Get expected value.
*
* @param int $K Successful states in the population
* @param int $N Population size
* @param int $n Number of draws
*
* @return float
*
* @todo: this can be heavily optimized
*
* @since 1.0.0
*/
public static function getMean(int $K, int $N, int $n) : float
{
return $n * $K / $N;
}
/**
* Get mode.
*
* @param int $K Successful states in the population
* @param int $N Population size
* @param int $n Number of draws
*
* @return int
*
* @todo: this can be heavily optimized
*
* @since 1.0.0
*/
public static function getMode(int $K, int $N, int $n) : int
{
return (int) (($n + 1) * ($K + 1) / ($N + 2));
}
/**
* Get variance.
*
* @param int $K Successful states in the population
* @param int $N Population size
* @param int $n Number of draws
*
* @return int
*
* @todo: this can be heavily optimized
*
* @since 1.0.0
*/
public static function getVariance(int $K, int $N, int $n) : float
{
return $n * $K / $N * ($N - $K) / $N * ($N - $n) / ($N - 1);
}
/**
* Get skewness.
*
* @param int $K Successful states in the population
* @param int $N Population size
* @param int $n Number of draws
*
* @return int
*
* @todo: this can be heavily optimized
*
* @since 1.0.0
*/
public static function getSkewness(int $K, int $N, int $n) : float
{
return ($N - 2 * $K) * \sqrt($N - 1) * ($N - 2 * $n)
/ (\sqrt($n * $K * ($N - $K) * ($N - $n)) * ($N - 2));
}
/**
* Get Ex. kurtosis.
*
* @param int $K Successful states in the population
* @param int $N Population size
* @param int $n Number of draws
*
* @return int
*
* @todo: this can be heavily optimized
*
* @since 1.0.0
*/
public static function getExKurtosis(int $K, int $N, int $n) : float
{
return 1 / ($n * $K * ($N - $K) * ($N - $n) * ($N - 2) * ($N - 3))
* (($N - 1) * $N ** 2 * ($N * ($N + 1) - 6 * $K * ($N - $K) - 6 * $n * ($N - $n)) + 6 * $n * $K * ($N - $K) * ($N - $n) * (5 * $N - 6));
}
}

View File

@ -85,7 +85,7 @@ class LaplaceDistribution
}
/**
* Get expected value.
* Get median.
*
* @param float $mu Value mu
*

View File

@ -13,7 +13,84 @@
declare(strict_types=1);
namespace phpOMS\Math\Stochastic\Distribution;
/**
* Log distribution.
*
* @package phpOMS\Math\Stochastic\Distribution
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class LogDistribution
{
/**
* Get probability mass function.
*
* @param float $p Value p
* @param int $k Value k
*
* @return float
*
* @since 1.0.0
*/
public static function getPmf(float $p, int $k) : float
{
return -1 / \log(1 - $p) * $p ** $k / $k;
}
/**
* Get expected value.
*
* @param float $p Value p
*
* @return float
*
* @since 1.0.0
*/
public static function getMean(float $p) : float
{
return -1 / \log(1 - $p) * $p / (1 - $p);
}
/**
* Get mode.
*
* @return int
*
* @since 1.0.0
*/
public static function getMode() : int
{
return 1;
}
/**
* Get variance.
*
* @param float $p Value p
*
* @return float
*
* @since 1.0.0
*/
public static function getVariance(float $p) : float
{
return -($p ** 2 + $p * \log(1 - $p))
/ ((1 - $p) ** 2 * \log(1 - $p) ** 2);
}
/**
* Get moment generating function.
*
* @param float $p Value p
* @param int $t Value t
*
* @return float
*
* @since 1.0.0
*/
public static function getMgf(float $p, int $t) : float
{
return \log(1 - $p * \exp($t)) / \log(1 - $p);
}
}

View File

@ -13,7 +13,149 @@
declare(strict_types=1);
namespace phpOMS\Math\Stochastic\Distribution;
/**
* Log-normal distribution.
*
* @package phpOMS\Math\Stochastic\Distribution
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class LogNormalDistribution
{
/**
* Get probability density function.
*
* @param float $x Value x
* @param float $mu Mu
* @param float $sigma Sigma
*
* @return float
*
* @since 1.0.0
*/
public static function getPdf(float $x, float $mu, float $sigma) : float
{
return 1 / ($x * $sigma * \sqrt(2 * \M_PI))
* \exp(-(\log($x) - $mu) ** 2 / (2 * $sigma ** 2));
}
/**
* Get expected value.
*
* @param float $mu Mu
* @param float $sigma Sigma
*
* @return float
*
* @since 1.0.0
*/
public static function getMean(float $mu, float $sigma) : float
{
return \exp($mu + $sigma ** 2 / 2);
}
/**
* Get median.
*
* @param float $mu Mu
*
* @return float
*
* @since 1.0.0
*/
public static function getMedian(float $mu) : float
{
return \exp($mu);
}
/**
* Get mode.
*
* @param float $mu Mu
* @param float $sigma Sigma
*
* @return float
*
* @since 1.0.0
*/
public static function getMode(float $mu, float $sigma) : float
{
return \exp($mu - $sigma ** 2);
}
/**
* Get variance.
*
* @param float $mu Mu
* @param float $sigma Sigma
*
* @return float
*
* @since 1.0.0
*/
public static function getVariance(float $mu, float $sigma) : float
{
return (\exp($sigma ** 2) - 1) * \exp(2 * $mu + $sigma ** 2);
}
/**
* Get skewness.
*
* @param float $sigma Sigma
*
* @return float
*
* @since 1.0.0
*/
public static function getSkewness(float $sigma) : float
{
return (\exp($sigma ** 2) + 2) * \sqrt(\exp($sigma ** 2) - 1);
}
/**
* Get Ex. kurtosis.
*
* @param float $sigma Sigma
*
* @return float
*
* @since 1.0.0
*/
public static function getExKurtosis(float $sigma) : float
{
return \exp(4 * $sigma ** 2) + 2 * \exp(3 * $sigma ** 2) + 3 * \exp(2 * $sigma ** 2) - 6;
}
/**
* Get entrpoy.
*
* @param float $mu Mu
* @param float $sigma Sigma
*
* @return float
*
* @since 1.0.0
*/
public static function getEntrpoy(float $mu, float $sigma) : float
{
return \log($sigma * \exp($mu + 1 / 2) * \sqrt(2 * M_1_PI), 2);
}
/**
* Get Fisher information.
*
* @param float $sigma Sigma
*
* @return array
*
* @since 1.0.0
*/
public static function getFisherInformation(float $sigma) : array
{
return [
[1 / ($sigma ** 2), 0],
[0, 1 / (2 * $sigma ** 2)],
];
}
}

View File

@ -13,7 +13,140 @@
declare(strict_types=1);
namespace phpOMS\Math\Stochastic\Distribution;
/**
* Logistic distribution.
*
* @package phpOMS\Math\Stochastic\Distribution
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class LogisticDistribution
{
/**
* Get probability density function.
*
* @param float $x Value x
* @param float $mu Mu location
* @param float $s s scale
*
* @return float
*
* @since 1.0.0
*/
public static function getPdf(float $x, float $mu, float $s) : float
{
return \exp(-($x - $mu) / $s)
/ ($s * (1 + \exp(-($x - $mu) / $s)) ** 2);
}
/**
* Get cummulative distribution function.
*
* @param float $x Value x
* @param float $mu Mu location
* @param float $s s scale
*
* @return float
*
* @since 1.0.0
*/
public static function getCdf(float $x, float $mu, float $s) : float
{
return 1 / (1 + \exp(-($x - $mu) / $s));
}
/**
* Get mode.
*
* @param float $mu Value mu
*
* @return float
*
* @since 1.0.0
*/
public static function getMode(float $mu) : float
{
return $mu;
}
/**
* Get expected value.
*
* @param float $mu Value mu
*
* @return float
*
* @since 1.0.0
*/
public static function getMean(float $mu) : float
{
return $mu;
}
/**
* Get median.
*
* @param float $mu Value mu
*
* @return float
*
* @since 1.0.0
*/
public static function getMedian(float $mu) : float
{
return $mu;
}
/**
* Get variance.
*
* @param float $s s scale
*
* @return float
*
* @since 1.0.0
*/
public static function getVariance(float $s) : float
{
return $s ** 2 * \M_PI ** 2 / 3;
}
/**
* Get skewness.
*
* @return float
*
* @since 1.0.0
*/
public static function getSkewness() : float
{
return 0;
}
/**
* Get skewness.
*
* @return float
*
* @since 1.0.0
*/
public static function getExKurtosis() : float
{
return 6 / 5;
}
/**
* Get entropy.
*
* @param float $s s scale
*
* @return float
*
* @since 1.0.0
*/
public static function getEntropy(float $s) : float
{
return \log($s) + 2;
}
}

View File

@ -106,7 +106,7 @@ class NormalDistribution
}
/**
* Get expected value.
* Get median.
*
* @param float $mu Value mu
*

View File

@ -13,7 +13,163 @@
declare(strict_types=1);
namespace phpOMS\Math\Stochastic\Distribution;
/**
* Pareto distribution.
*
* @package phpOMS\Math\Stochastic\Distribution
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class ParetoDistribution
{
/**
* Get probability density function.
*
* @param float $x Value x
* @param float $xm Lower bound
* @param float $alpha Alpha shape
*
* @return float
*
* @since 1.0.0
*/
public static function getPdf(float $x, float $xm, float $alpha) : float
{
return $alpha * $xm ** $alpha / (\pow($x, $alpha + 1));
}
/**
* Get cumulative distribution function.
*
* @param float $x Value x
* @param float $xm Lower bound
* @param float $alpha Alpha shape
*
* @return float
*
* @since 1.0.0
*/
public static function getCdf(float $x, float $xm, float $alpha) : float
{
return 1 - ($xm / $x) ** $alpha;
}
/**
* Get median
*
* @param float $xm Lower bound
* @param float $alpha Alpha shape
*
* @return float
*
* @since 1.0.0
*/
public static function getMedian(float $xm, float $alpha) : float
{
return $xm * \pow(2, 1 / $alpha);
}
/**
* Get mode.
*
* @param float $xm Lower bound
*
* @return float
*
* @since 1.0.0
*/
public static function getMode($xm) : float
{
return $xm;
}
/**
* Get variance
*
* @param float $xm Lower bound
* @param float $alpha Alpha shape
*
* @return float
*
* @since 1.0.0
*/
public static function getVariance(float $xm, float $alpha) : float
{
if ($alpha < 2) {
return \PHP_FLOAT_MAX;
}
return $xm ** 2 * $alpha / (($alpha - 1) ** 2 * ($alpha - 2));
}
/**
* Get skewness.
*
* @param float $alpha Alpha shape
*
* @return float
*
* @since 1.0.0
*/
public static function getSkewness(float $alpha) : float
{
if ($alpha < 4) {
return 0.0;
}
return 2 * (1 + $alpha) / ($alpha - 3) * \sqrt(($alpha - 2) / $alpha);
}
/**
* Get Ex. kurtosis.
*
* @param float $alpha Alpha shape
*
* @return float
*
* @since 1.0.0
*/
public static function getExKurtosis(float $alpha) : float
{
if ($alpha < 5) {
return 0.0;
}
return 6 * ($alpha ** 3 + $alpha ** 2 - 6 * $alpha - 2)
/ ($alpha * ($alpha - 3) * ($alpha - 4));
}
/**
* Get entropy.
*
* @param float $xm Lower bound
* @param float $alpha Alpha shape
*
* @return float
*
* @since 1.0.0
*/
public static function getEntropy(float $xm, float $alpha) : float
{
return \log(($xm / $alpha) * \exp(1 + 1 / $alpha));
}
/**
* Get Fisher information.
*
* @param float $xm Lower bound
* @param float $alpha Alpha shape
*
* @return array
*
* @since 1.0.0
*/
public static function getFisherInformation(float $xm, float $alpha) : array
{
return [
[$alpha / $xm ** 2, -1 / $xm],
[-1 / $xm, 1 / ($alpha ** 2)]
];
}
}

View File

@ -94,7 +94,7 @@ class PoissonDistribution
}
/**
* Get expected value.
* Get median.
*
* @param float $lambda Lambda
*

View File

@ -11,9 +11,82 @@
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\Math\Stochastic\Distribution;
/**
* Bernulli distribution.
*
* @package phpOMS\Math\Stochastic\Distribution
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class TDistribution
{
/**
* Get expected value.
*
* @return float
*
* @since 1.0.0
*/
public static function getMean() : int
{
return 0;
}
/**
* Get median.
*
* @return int
*
* @since 1.0.0
*/
public static function getMedian() : int
{
return 0;
}
/**
* Get mode.
*
* @return int
*
* @since 1.0.0
*/
public static function getMode() : int
{
return 0;
}
/**
* Get skewness.
*
* @return int
*
* @since 1.0.0
*/
public static function getSkewness() : int
{
return 0;
}
/**
* Get variance.
*
* @param int $nu Degrees of freedom
*
* @return float
*
* @since 1.0.0
*/
public static function getVariance(int $nu) : float
{
if ($nu < 2) {
return \PHP_FLOAT_MAX;
}
return $nu / ($nu - 2);
}
}

View File

@ -119,7 +119,7 @@ class UniformDistributionContinuous
}
/**
* Get expected value.
* Get median.
*
* @param float $a Value a
* @param float $b Value b

View File

@ -108,7 +108,7 @@ class UniformDistributionDiscrete
}
/**
* Get expected value.
* Get median.
*
* @param float $a Value a
* @param float $b Value b

View File

@ -13,7 +13,100 @@
declare(strict_types=1);
namespace phpOMS\Math\Stochastic\Distribution;
/**
* Weibull distribution.
*
* @package phpOMS\Math\Stochastic\Distribution
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class WeibullDistribution
{
/**
* Get probability density function.
*
* @param float $x Value x
* @param float $lambda Scale lambda
* @param float $k Shape k
*
* @return float
*
* @since 1.0.0
*/
public static function getPdf(float $x, float $lambda, float $k) : float
{
if ($x < 0) {
return 0.0;
}
return $k / $lambda * \pow($x / $lambda, $k - 1) * \exp(-($x / $lambda) ** $k);
}
/**
* Get cumulative distribution function.
*
* @param float $x Value x
* @param float $lambda Scale lambda
* @param float $k Shape k
*
* @return float
*
* @since 1.0.0
*/
public static function getCdf(float $x, float $lambda, float $k) : float
{
if ($x < 0) {
return 0.0;
}
return 1 - \exp(-($x / $lambda) ** $k);
}
/**
* Get median.
*
* @param float $lambda Scale lambda
* @param float $k Shape k
*
* @return float
*
* @since 1.0.0
*/
public static function getMedian(float $lambda, float $k) : float
{
return $lambda * \pow(\log(2), 1 / $k);
}
/**
* Get mode.
*
* @param float $lambda Scale lambda
* @param float $k Shape k
*
* @return float
*
* @since 1.0.0
*/
public static function getMode(float $lambda, float $k) : float
{
return $lambda * \pow(($k - 1) / $k, 1 / $k);
}
/**
* Get entropy.
*
* @param float $lambda Scale lambda
* @param float $k Shape k
*
* @return float
*
* @since 1.0.0
*/
public static function getEntropy(float $lambda, float $k) : float
{
$gamma = 0.57721566490153286060651209008240243104215933593992;
return $gamma * (1 - 1 / $k) + \log($lambda / $k) + 1;
}
}

View File

@ -128,15 +128,18 @@ class Server extends SocketAbstract
public function handshake($client, $headers) : bool
{
// todo: different handshake for normal tcp connection
return true;
//return true;
if (\preg_match("/Sec-WebSocket-Version: (.*)\r\n/", $headers, $match)) {
$version = $match[1];
} else {
if (\preg_match("/Sec-WebSocket-Version: (.*)\r\n/", $headers, $match) === false) {
return false;
}
$version = (int) $match[1];
if ($version !== 13) {
return false;
}
if ($version == 13) {
if (\preg_match("/GET (.*) HTTP/", $headers, $match)) {
$root = $match[1];
}
@ -165,9 +168,6 @@ class Server extends SocketAbstract
$client->setHandshake(true);
return true;
} else {
return false;
}
}
/**

View File

@ -65,10 +65,9 @@ class Edge
*
* @param Node $node1 Graph node (start node in case of directed edge)
* @param Node $node2 Graph node (end node in case of directed edge)
* @param float $weight Weight/cost of the edge.
* @param bool $isDirected Is directed edge
*
* @return Graph
*
* @since 1.0.0
*/
public function __construct(Node $node1, Node $node2, float $weight = 0.0, bool $isDirected = false)

View File

@ -0,0 +1,57 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\tests\Algorithm\Sort;
use phpOMS\Algorithm\Sort\HeapSort;
use phpOMS\Algorithm\Sort\SortOrder;
require_once __DIR__ . '/../../Autoloader.php';
/**
* @testdox phpOMS\tests\Algorithm\Sort: Bucket sort test
*
* @internal
*/
class HeapSortTest extends \PHPUnit\Framework\TestCase
{
protected $list = [];
protected function setUp() : void
{
$this->list = [
new NumericElement(5),
new NumericElement(1),
new NumericElement(4),
new NumericElement(2),
new NumericElement(8),
];
}
public function testSortASC() : void
{
$newList = HeapSort::sort($this->list);
self::assertEquals(
[1, 2, 4, 5, 8], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,]
);
}
public function testSortDESC() : void
{
$newList = HeapSort::sort($this->list, SortOrder::DESC);
self::assertEquals(
[8, 5, 4, 2, 1], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,]
);
}
}

View File

@ -0,0 +1,57 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\tests\Algorithm\Sort;
use phpOMS\Algorithm\Sort\InsertionSort;
use phpOMS\Algorithm\Sort\SortOrder;
require_once __DIR__ . '/../../Autoloader.php';
/**
* @testdox phpOMS\tests\Algorithm\Sort: Bucket sort test
*
* @internal
*/
class InsertionSortTest extends \PHPUnit\Framework\TestCase
{
protected $list = [];
protected function setUp() : void
{
$this->list = [
new NumericElement(5),
new NumericElement(1),
new NumericElement(4),
new NumericElement(2),
new NumericElement(8),
];
}
public function testSortASC() : void
{
$newList = InsertionSort::sort($this->list);
self::assertEquals(
[1, 2, 4, 5, 8], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,]
);
}
public function testSortDESC() : void
{
$newList = InsertionSort::sort($this->list, SortOrder::DESC);
self::assertEquals(
[8, 5, 4, 2, 1], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,]
);
}
}

View File

@ -0,0 +1,57 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\tests\Algorithm\Sort;
use phpOMS\Algorithm\Sort\IntroSort;
use phpOMS\Algorithm\Sort\SortOrder;
require_once __DIR__ . '/../../Autoloader.php';
/**
* @testdox phpOMS\tests\Algorithm\Sort: Bucket sort test
*
* @internal
*/
class IntroSortTest extends \PHPUnit\Framework\TestCase
{
protected $list = [];
protected function setUp() : void
{
$this->list = [
new NumericElement(5),
new NumericElement(1),
new NumericElement(4),
new NumericElement(2),
new NumericElement(8),
];
}
public function testSortASC() : void
{
$newList = IntroSort::sort($this->list);
self::assertEquals(
[1, 2, 4, 5, 8], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,]
);
}
public function testSortDESC() : void
{
$newList = IntroSort::sort($this->list, SortOrder::DESC);
self::assertEquals(
[8, 5, 4, 2, 1], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,]
);
}
}