Pint applied for formatting

This commit is contained in:
Alex Wulf
2022-07-05 15:55:47 +02:00
parent bfc558d1f2
commit 7d4547df6a
68 changed files with 670 additions and 402 deletions

View File

@ -1,4 +1,6 @@
<?php namespace DNW\Skills\Numerics;
<?php
namespace DNW\Skills\Numerics;
/**
* Basic math functions.
@ -8,10 +10,10 @@
*/
class BasicMath
{
/**
* Squares the input (x^2 = x * x)
* @param number $x Value to square (x)
*
* @param number $x Value to square (x)
* @return number The squared value (x^2)
*/
public static function square($x)
@ -21,13 +23,15 @@ class BasicMath
/**
* Sums the items in $itemsToSum
* @param array $itemsToSum The items to sum,
* @param callback $callback The function to apply to each array element before summing.
*
* @param array $itemsToSum The items to sum,
* @param callable $callback The function to apply to each array element before summing.
* @return number The sum.
*/
public static function sum(array $itemsToSum, $callback)
{
$mappedItems = array_map($callback, $itemsToSum);
return array_sum($mappedItems);
}
}
}

View File

@ -1,4 +1,6 @@
<?php namespace DNW\Skills\Numerics;
<?php
namespace DNW\Skills\Numerics;
class DiagonalMatrix extends Matrix
{
@ -20,4 +22,4 @@ class DiagonalMatrix extends Matrix
}
}
}
}
}

View File

@ -1,4 +1,6 @@
<?php namespace DNW\Skills\Numerics;
<?php
namespace DNW\Skills\Numerics;
/**
* Computes Gaussian (bell curve) values.
@ -9,12 +11,15 @@
class GaussianDistribution
{
private $_mean;
private $_standardDeviation;
// precision and precisionMean are used because they make multiplying and dividing simpler
// (the the accompanying math paper for more details)
private $_precision;
private $_precisionMean;
private $_variance;
public function __construct($mean = 0.0, $standardDeviation = 1.0)
@ -76,6 +81,7 @@ class GaussianDistribution
$result->_variance = $this->_variance;
$result->_precision = $this->_precision;
$result->_precisionMean = $this->_precisionMean;
return $result;
}
@ -94,6 +100,7 @@ class GaussianDistribution
$result->_standardDeviation = \INF;
$result->_mean = \NAN;
}
return $result;
}
@ -129,6 +136,7 @@ class GaussianDistribution
$meanDifference = $left->_mean - $right->_mean;
$logSqrt2Pi = log(sqrt(2 * M_PI));
return -$logSqrt2Pi - (log($varianceSum) / 2.0) - (BasicMath::square($meanDifference) / (2.0 * $varianceSum));
}
@ -165,6 +173,7 @@ class GaussianDistribution
$multiplier = 1.0 / ($standardDeviation * sqrt(2 * M_PI));
$expPart = exp((-1.0 * BasicMath::square($x - $mean)) / (2 * BasicMath::square($standardDeviation)));
$result = $multiplier * $expPart;
return $result;
}
@ -172,6 +181,7 @@ class GaussianDistribution
{
$invsqrt2 = -0.707106781186547524400844362104;
$result = GaussianDistribution::errorFunctionCumulativeTo($invsqrt2 * $x);
return 0.5 * $result;
}
@ -183,7 +193,7 @@ class GaussianDistribution
$t = 2.0 / (2.0 + $z);
$ty = 4 * $t - 2;
$coefficients = array(
$coefficients = [
-1.3026537197817094,
6.4196979235649026e-1,
1.9476473204185836e-2,
@ -211,7 +221,7 @@ class GaussianDistribution
-1.523e-15,
-9.4e-17,
1.21e-16,
-2.8e-17);
-2.8e-17, ];
$ncof = count($coefficients);
$d = 0.0;
@ -224,6 +234,7 @@ class GaussianDistribution
}
$ans = $t * exp(-$z * $z + 0.5 * ($coefficients[0] + $ty * $d) - $dd);
return ($x >= 0.0) ? $ans : (2.0 - $ans);
}
@ -258,6 +269,6 @@ class GaussianDistribution
public function __toString()
{
return sprintf("mean=%.4f standardDeviation=%.4f", $this->_mean, $this->_standardDeviation);
return sprintf('mean=%.4f standardDeviation=%.4f', $this->_mean, $this->_standardDeviation);
}
}
}

View File

@ -1,4 +1,6 @@
<?php namespace DNW\Skills\Numerics;
<?php
namespace DNW\Skills\Numerics;
class IdentityMatrix extends DiagonalMatrix
{
@ -6,4 +8,4 @@ class IdentityMatrix extends DiagonalMatrix
{
parent::__construct(array_fill(0, $rows, 1));
}
}
}

View File

@ -1,4 +1,6 @@
<?php namespace DNW\Skills\Numerics;
<?php
namespace DNW\Skills\Numerics;
use Exception;
@ -7,7 +9,9 @@ class Matrix
const ERROR_TOLERANCE = 0.0000000001;
private $_matrixRowData;
private $_rowCount;
private $_columnCount;
public function __construct($rows = 0, $columns = 0, $matrixData = null)
@ -19,7 +23,7 @@ class Matrix
public static function fromColumnValues($rows, $columns, $columnValues)
{
$data = array();
$data = [];
$result = new Matrix($rows, $columns, $data);
for ($currentColumn = 0; $currentColumn < $columns; $currentColumn++) {
@ -73,7 +77,7 @@ class Matrix
public function getTranspose()
{
// Just flip everything
$transposeMatrix = array();
$transposeMatrix = [];
$rowMatrixData = $this->_matrixRowData;
for ($currentRowTransposeMatrix = 0;
@ -98,8 +102,8 @@ class Matrix
public function getDeterminant()
{
// Basic argument checking
if (!$this->isSquare()) {
throw new Exception("Matrix must be square!");
if (! $this->isSquare()) {
throw new Exception('Matrix must be square!');
}
if ($this->_rowCount == 1) {
@ -117,6 +121,7 @@ class Matrix
$b = $this->_matrixRowData[0][1];
$c = $this->_matrixRowData[1][0];
$d = $this->_matrixRowData[1][1];
return $a * $d - $b * $c;
}
@ -141,8 +146,8 @@ class Matrix
public function getAdjugate()
{
if (!$this->isSquare()) {
throw new Exception("Matrix must be square!");
if (! $this->isSquare()) {
throw new Exception('Matrix must be square!');
}
// See http://en.wikipedia.org/wiki/Adjugate_matrix
@ -165,7 +170,7 @@ class Matrix
}
// The idea is that it's the transpose of the cofactors
$result = array();
$result = [];
for ($currentColumn = 0; $currentColumn < $this->_columnCount; $currentColumn++) {
for ($currentRow = 0; $currentRow < $this->_rowCount; $currentRow++) {
@ -194,7 +199,7 @@ class Matrix
{
$rows = $matrix->getRowCount();
$columns = $matrix->getColumnCount();
$newValues = array();
$newValues = [];
for ($currentRow = 0; $currentRow < $rows; $currentRow++) {
for ($currentColumn = 0; $currentColumn < $columns; $currentColumn++) {
@ -212,12 +217,12 @@ class Matrix
||
($left->getColumnCount() != $right->getColumnCount())
) {
throw new Exception("Matrices must be of the same size");
throw new Exception('Matrices must be of the same size');
}
// simple addition of each item
$resultMatrix = array();
$resultMatrix = [];
for ($currentRow = 0; $currentRow < $left->getRowCount(); $currentRow++) {
for ($currentColumn = 0; $currentColumn < $right->getColumnCount(); $currentColumn++) {
@ -237,13 +242,13 @@ class Matrix
// See http://en.wikipedia.org/wiki/Matrix_multiplication for details
if ($left->getColumnCount() != $right->getRowCount()) {
throw new Exception("The width of the left matrix must match the height of the right matrix");
throw new Exception('The width of the left matrix must match the height of the right matrix');
}
$resultRows = $left->getRowCount();
$resultColumns = $right->getColumnCount();
$resultMatrix = array();
$resultMatrix = [];
for ($currentRow = 0; $currentRow < $resultRows; $currentRow++) {
for ($currentColumn = 0; $currentColumn < $resultColumns; $currentColumn++) {
@ -268,7 +273,7 @@ class Matrix
// See http://en.wikipedia.org/wiki/Minor_(linear_algebra)
// I'm going to use a horribly naïve algorithm... because I can :)
$result = array();
$result = [];
$actualRow = 0;
@ -334,4 +339,4 @@ class Matrix
return true;
}
}
}

View File

@ -1,4 +1,6 @@
<?php namespace DNW\Skills\Numerics;
<?php
namespace DNW\Skills\Numerics;
// The whole purpose of this class is to make the code for the SkillCalculator(s)
// look a little cleaner
@ -8,13 +10,13 @@ use Exception;
class Range
{
private $_min;
private $_max;
public function __construct($min, $max)
{
if ($min > $max)
{
throw new Exception("min > max");
if ($min > $max) {
throw new Exception('min > max');
}
$this->_min = $min;
@ -50,11 +52,11 @@ class Range
public static function atLeast($minimumValue)
{
return static::create($minimumValue, PHP_INT_MAX );
return static::create($minimumValue, PHP_INT_MAX);
}
public function isInRange($value)
{
return ($this->_min <= $value) && ($value <= $this->_max);
}
}
}

View File

@ -1,14 +1,16 @@
<?php namespace DNW\Skills\Numerics;
<?php
namespace DNW\Skills\Numerics;
class SquareMatrix extends Matrix
{
public function __construct()
{
$allValues = func_get_args();
$rows = (int)sqrt(count($allValues));
$rows = (int) sqrt(count($allValues));
$cols = $rows;
$matrixData = array();
$matrixData = [];
$allValuesIndex = 0;
for ($currentRow = 0; $currentRow < $rows; $currentRow++) {
@ -19,4 +21,4 @@ class SquareMatrix extends Matrix
parent::__construct($rows, $cols, $matrixData);
}
}
}

View File

@ -1,13 +1,15 @@
<?php namespace DNW\Skills\Numerics;
<?php
namespace DNW\Skills\Numerics;
class Vector extends Matrix
{
public function __construct(array $vectorValues)
{
$columnValues = array();
$columnValues = [];
foreach ($vectorValues as $currentVectorValue) {
$columnValues[] = array($currentVectorValue);
$columnValues[] = [$currentVectorValue];
}
parent::__construct(count($vectorValues), 1, $columnValues);
}
}
}