Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Eichhorn committed Sep 10, 2023
1 parent 4469789 commit 018b9ce
Show file tree
Hide file tree
Showing 23 changed files with 388 additions and 133 deletions.
19 changes: 5 additions & 14 deletions Algorithm/Clustering/DBSCAN.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ private function expandCluster(
/**
* Find neighbors of a point
*
* @param PointInterface $point Base point for potential neighbors
* @param float $epsion Max distance to neighbor
* @param PointInterface $point Base point for potential neighbors
* @param float $epsilon Max distance to neighbor
*
* @return array
*
Expand Down Expand Up @@ -210,6 +210,7 @@ private function generateDistanceMatrix(array $points) : array
}
}

/** @var float[] $distances */
return $distances;
}

Expand All @@ -228,25 +229,15 @@ public function cluster(PointInterface $point) : int
foreach ($this->clusters as $c => $cluster) {
$points = [];
foreach ($cluster as $p) {
$points[] = [
'x' => \reset($p->coordinates),
'y' => \end($p->coordinates),
];
$points[] = $p->coordinates;
}

$this->convexHulls[$c] = MonotoneChain::createConvexHull($points);
}
}

foreach ($this->convexHulls as $c => $hull) {
if (Polygon::isPointInPolygon(
[
'x' => \reset($point->coordinates),
'y' => \end($point->coordinates),
],
$hull
) <= 0
) {
if (Polygon::isPointInPolygon($point->coordinates, $hull) <= 0) {
return $c;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Algorithm/Clustering/MeanShift.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ private function groupPoints(array $points) : array
/**
* Find the closest cluster/group of a point
*
* @param PointInterface $point Point to find the cluster for
* @param PointInterface[] $group Clusters
* @param PointInterface $point Point to find the cluster for
* @param array<PointInterface[]> $groups Clusters
*
* @return int
*
Expand Down
5 changes: 3 additions & 2 deletions Algorithm/Clustering/PointInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
/**
* Point interface.
*
* @property int $group Group
* @property string $name Name
* @property int $group Group
* @property string $name Name
* @property array $coordinates Coordinates
*
* @package phpOMS\Algorithm\Clustering;
* @license OMS License 2.0
Expand Down
2 changes: 1 addition & 1 deletion Algorithm/Frequency/Apriori.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private static function generateSubsets(array $arr) : array
*
* @param array<array> $sets Sets of a set (e.g. [[1,2,3,4], [1,2], [1]])
*
* @return array<array>
* @return array
*
* @since 1.0.0
*/
Expand Down
29 changes: 29 additions & 0 deletions Algorithm/Graph/MarkovChain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Jingga
*
* PHP Version 8.1
*
* @package phpOMS\Algorithm\Graph
* @copyright Dennis Eichhorn
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);

namespace phpOMS\Algorithm\Graph;

/**
* Markov chain
*
* @package phpOMS\Algorithm\Graph
* @license OMS License 2.0
* @link https://jingga.app
* @since 1.0.0
*
* @todo Implement
*/
final class MarkovChain
{
}
4 changes: 2 additions & 2 deletions Algorithm/Optimization/GeneticOptimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ public static function optimize(

$fitnesses = [];

foreach ($population as $parameters) {
$fitnesses[$population] = ($fitness)($parameters);
foreach ($population as $key => $parameters) {
$fitnesses[$key] = ($fitness)($parameters);
}

\asort($fitnesses);
Expand Down
2 changes: 1 addition & 1 deletion Algorithm/Optimization/TabuSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static function optimize(

for ($i = 0; $i < $iterations; ++$i) {
$neighbors = [];
for ($i = 0; $i < $tabuListSize; ++$i) {
for ($j = 0; $j < $tabuListSize; ++$j) {
$neighbor = ($neighbor)($currentSolution);
$neighbors[] = $neighbor;
}
Expand Down
2 changes: 1 addition & 1 deletion Algorithm/Rating/BradleyTerry.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
* @package phpOMS\Algorithm\Rating
* @license OMS License 2.0
* @link https://jingga.app
* @since 1.0.0
* @see https://en.wikipedia.org/wiki/Bradley%E2%80%93Terry_model
* @since 1.0.0
*/
final class BradleyTerry
{
Expand Down
7 changes: 3 additions & 4 deletions Algorithm/Rating/Glicko1.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ final class Glicko1
*
* @see calculateC();
*
* @var int
* @var float
* @since 1.0.0
*/
public float $DEFAULT_C = 34.6;
Expand Down Expand Up @@ -127,7 +127,6 @@ public function rating(
) : array
{
// Step 1:
$s = [];
$E = [];
$gRD = [];

Expand All @@ -145,8 +144,8 @@ public function rating(
// Step 2:
foreach ($oElo as $id => $e) {
$gRD_t = 1 / (\sqrt(1 + 3 * self::Q * self::Q * $oRd[$id] * $oRd[$id] / (\M_PI * \M_PI)));
$gRD[] = $gRD_t;
$E[] = 1 / (1 + \pow(10, $gRD_t * ($elo - $e) / -400));
$gRD[$id] = $gRD_t;
$E[$id] = 1 / (1 + \pow(10, $gRD_t * ($elo - $e) / -400));
}

$d = 0;
Expand Down
6 changes: 3 additions & 3 deletions Algorithm/Rating/Glicko2.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ public function rating(

// Step 1:
$g = [];
foreach ($oRd as $rd) {
$g[] = 1 / \sqrt(1 + 3 * $rd * $rd / (\M_PI * \M_PI));
foreach ($oRd as $idx => $rd) {
$g[$idx] = 1 / \sqrt(1 + 3 * $rd * $rd / (\M_PI * \M_PI));
}

$E = [];
foreach ($oElo as $idx => $elo) {
$E[] = 1 / (1 + \exp(-$g[$idx] * ($elo - $elo)));
$E[$idx] = 1 / (1 + \exp(-$g[$idx] * ($elo - $elo)));
}

$v = 0;
Expand Down
4 changes: 2 additions & 2 deletions Api/CreditRating/CreditSafe.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function auth(string $username, string $password) : string
$response = Rest::request($request);

return $response->header->status === 200
? ($response->get('token') ?? '')
? ($response->getDataString('token') ?? '')
: '';
}

Expand Down Expand Up @@ -212,7 +212,7 @@ public function investigate(

$response = Rest::request($request);

return $response->get('orderID') ?? '';
return $response->getDataString('orderID') ?? '';
}

/**
Expand Down
11 changes: 7 additions & 4 deletions Business/Recommendation/BayesianPersonalizedRanking.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
* @package phpOMS\Business\Recommendation
* @license OMS License 2.0
* @link https://jingga.app
* @since 1.0.0
* @see https://arxiv.org/ftp/arxiv/papers/1205/1205.2618.pdf
* @since 1.0.0
*
* @todo Implement, current implementation probably wrong
*/
Expand All @@ -40,13 +40,15 @@ final class BayesianPersonalizedRanking
// num_factors determines the dimensionality of the latent factor space.
// learning_rate controls the step size for updating the latent factors during optimization.
// regularization prevents overfitting by adding a penalty for large parameter values.
public function __construct(int $numFactors, float $learningRate, float $regularization) {
public function __construct(int $numFactors, float $learningRate, float $regularization)
{
$this->numFactors = $numFactors;
$this->learningRate = $learningRate;
$this->regularization = $regularization;
}

private function generateRandomFactors() {
private function generateRandomFactors()
{
$factors = [];
for ($i = 0; $i < $this->numFactors; ++$i) {
$factors[$i] = \mt_rand() / \mt_getrandmax();
Expand All @@ -67,7 +69,8 @@ public function predict($userId, $itemId) {
return $score;
}

public function updateFactors($userId, $posItemId, $negItemId) : void {
public function updateFactors($userId, $posItemId, $negItemId) : void
{
if (!isset($this->userFactors[$userId])) {
$this->userFactors[$userId] = $this->generateRandomFactors();
}
Expand Down
22 changes: 7 additions & 15 deletions DataStorage/Session/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ final class JWT
/**
* Create JWT signature part
*
* @param string $secret Secret (at least 256 bit)
* @param array{alg:string, typ:string} $header Header
* @param string $secret Secret (at least 256 bit)
* @param array{alg:string, typ:string} $header Header
* @param array{sub:string, ?uid:string, ?name:string, iat:string} $payload Payload
*
* @return string hmac(Header64 . Payload64, secret)
Expand All @@ -60,15 +60,15 @@ private static function createSignature(string $secret, array $header, array $pa
/**
* Create JWT token
*
* @param string $secret Secret (at least 256 bit)
* @param array{alg:string, typ:string} $header Header
* @param string $secret Secret (at least 256 bit)
* @param array{alg:string, typ:string} $header Header
* @param array{sub:string, ?uid:string, ?name:string, iat:string} $payload Payload
*
* @return string Header64 . Payload64 . hmac(Header64 . Payload64, secret)
*
* @since 1.0.0
*/
public static function createJWT(string $secret, array $header = [], array $payload = []) : string
public static function createJWT(string $secret, array $header, array $payload) : string
{
$header64 = Base64Url::encode(\json_encode($header));
$payload64 = Base64Url::encode(\json_encode($payload));
Expand All @@ -94,11 +94,7 @@ public static function getHeader(string $jwt) : array
return [];
}

try {
return \json_decode(Base64Url::decode($explode[0]), true);
} catch (\Throwable $_) {
return [];
}
return \json_decode(Base64Url::decode($explode[0]), true);
}

/**
Expand All @@ -118,11 +114,7 @@ public static function getPayload(string $jwt) : array
return [];
}

try {
return \json_decode(Base64Url::decode($explode[1]), true);
} catch (\Throwable $_) {
return [];
}
return \json_decode(Base64Url::decode($explode[1]), true);
}

/**
Expand Down
10 changes: 7 additions & 3 deletions Event/EventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,15 @@ public function attach(string $group, string | callable $callback, bool $remove
*/
public function triggerSimilar(string $group, string $id = '', mixed $data = null) : bool
{
if (empty($this->callbacks)) {
return false;
}

$groupIsRegex = \stripos($group, '/') === 0;
$idIsRegex = \stripos($id, '/') === 0;

$groups = [];
foreach ($this->groups as $groupName => $value) {
foreach ($this->groups as $groupName => $_) {
$groupNameIsRegex = \stripos($groupName, '/') === 0;

if ($groupIsRegex) {
Expand All @@ -189,8 +193,8 @@ public function triggerSimilar(string $group, string $id = '', mixed $data = nul
}
}

foreach ($groups as $groupName => $groupValues) {
foreach ($this->groups[$groupName] as $idName => $value) {
foreach ($groups as $groupName => $_) {
foreach ($this->groups[$groupName] as $idName => $_2) {
$idNameIsRegex = \stripos($idName, '/') === 0;

if ($idIsRegex) {
Expand Down
1 change: 1 addition & 0 deletions Localization/ISO3166Trait.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ trait ISO3166Trait
*/
public static function getBy2Code(string $code) : mixed
{
/** @var string $code3 */
$code3 = ISO3166TwoEnum::getName($code);

return self::getByName($code3);
Expand Down
4 changes: 2 additions & 2 deletions Math/Matrix/Vector.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function setMatrixV(array $vector) : self
*/
public function cosine(self $v) : float
{
$dotProduct = 0;
$dotProduct = 0.0;
for ($i = 0; $i < $this->m; ++$i) {
$dotProduct += $this->matrix[$i][0] * $v[$i];
}
Expand All @@ -110,7 +110,7 @@ public function cosine(self $v) : float
}
$magnitude1 = \sqrt($sumOfSquares);

$sumOfSquares = 0;
$sumOfSquares = 0.0;
foreach ($v->matrix as $value) {
$sumOfSquares += $value[0] * $value[0];
}
Expand Down
6 changes: 3 additions & 3 deletions Math/Topology/MetricsND.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ public static function euclidean(array $a, array $b) : float
*/
public static function cosine(array $a, array $b) : float
{
if (\count($a) !== \count($b)) {
if (($length = \count($a)) !== \count($b)) {
throw new InvalidDimensionException(\count($a) . 'x' . \count($b));
}

$dotProduct = 0;
for ($i = 0; $i < \count($a); ++$i) {
for ($i = 0; $i < $length; ++$i) {
$dotProduct += $a[$i] * $b[$i];
}

Expand All @@ -128,7 +128,7 @@ public static function cosine(array $a, array $b) : float
}
$magnitude2 = \sqrt($sumOfSquares);

if ($magnitude1 === 0 || $magnitude2 === 0) {
if ($magnitude1 == 0 || $magnitude2 == 0) {
return \PHP_FLOAT_MAX;
}

Expand Down
Loading

0 comments on commit 018b9ce

Please sign in to comment.