From 8ed3d7e8f2707f5f20ea4d8233afc4b92400dd31 Mon Sep 17 00:00:00 2001 From: Gordon Lesti Date: Mon, 21 Oct 2019 21:35:46 +0200 Subject: [PATCH 1/8] test on travis only 5.6 7.0 7.1 7.2 7.3 --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 41b1780..9372f6c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,11 @@ language: php php: - - 5.4 - - 5.5 - 5.6 - 7.0 - - hhvm + - 7.1 + - 7.2 + - 7.3 sudo: false From 77ac798eeea34bbf33a8b38e11262840e017821b Mon Sep 17 00:00:00 2001 From: Gordon Lesti Date: Mon, 21 Oct 2019 21:41:35 +0200 Subject: [PATCH 2/8] phpmd --- src/NumPHP/Core/NumArray/StringHelper.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/NumPHP/Core/NumArray/StringHelper.php b/src/NumPHP/Core/NumArray/StringHelper.php index 51c8eef..a6904a8 100644 --- a/src/NumPHP/Core/NumArray/StringHelper.php +++ b/src/NumPHP/Core/NumArray/StringHelper.php @@ -48,7 +48,8 @@ protected static function toStringRecursive($data, $level = 0) $repeat = str_repeat(" ", $level); if (is_array($data) && isset($data[0]) && is_array($data[0])) { $string = $repeat."[\n"; - for ($i = 0; $i < count($data)-1; $i++) { + $limit = count($data)-1; + for ($i = 0; $i < $limit; $i++) { $string .= self::toStringRecursive($data[$i], $level+1).",\n"; } if (count($data)) { From 07ec7c597b1f43130af9fb97cdf4407d2105bda7 Mon Sep 17 00:00:00 2001 From: Gordon Lesti Date: Mon, 21 Oct 2019 21:46:54 +0200 Subject: [PATCH 3/8] compsoer update satooshi/php-coveralls --- .travis.yml | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9372f6c..b071a01 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,4 +22,4 @@ script: - php vendor/bin/phpunit --coverage-clover build/logs/clover.xml after_script: - - sh -c "if [ $TRAVIS_PHP_VERSION != 'hhvm' ]; then php vendor/bin/coveralls -v; fi" + - php vendor/bin/coveralls -v diff --git a/composer.json b/composer.json index 1a6a7fb..cf63dca 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "phpmd/phpmd" : "*", "squizlabs/php_codesniffer": "*", "sebastian/phpcpd": "*", - "satooshi/php-coveralls": "v1.0.1", + "satooshi/php-coveralls": "^2.1", "symfony/console": "~2.2" }, "autoload": { From f84ad758d75bc98166403f16e702fb31f1754aa6 Mon Sep 17 00:00:00 2001 From: Gordon Lesti Date: Mon, 21 Oct 2019 23:58:58 +0200 Subject: [PATCH 4/8] remove examples --- .travis.yml | 6 +- composer.json | 14 +- examples/NumPHPExamples/Benchmark.php | 67 --------- .../Benchmark/BenchmarkInterface.php | 31 ---- .../Benchmark/Core/NumArray/Abs.php | 51 ------- .../Benchmark/Core/NumArray/Add.php | 51 ------- .../Benchmark/Core/NumArray/Create.php | 54 ------- .../Benchmark/Core/NumArray/Dot.php | 52 ------- .../Benchmark/Core/NumArray/Sum.php | 53 ------- examples/NumPHPExamples/Benchmark/TestRun.php | 57 -------- examples/NumPHPExamples/Cholesky.php | 61 -------- .../NumPHPExamples/GaussianElimination.php | 132 ------------------ examples/NumPHPExamples/Inverse.php | 60 -------- examples/NumPHPExamples/LUDecomposition.php | 67 --------- examples/README.md | 13 -- examples/numphp-examples | 44 ------ 16 files changed, 9 insertions(+), 804 deletions(-) delete mode 100644 examples/NumPHPExamples/Benchmark.php delete mode 100644 examples/NumPHPExamples/Benchmark/BenchmarkInterface.php delete mode 100644 examples/NumPHPExamples/Benchmark/Core/NumArray/Abs.php delete mode 100644 examples/NumPHPExamples/Benchmark/Core/NumArray/Add.php delete mode 100644 examples/NumPHPExamples/Benchmark/Core/NumArray/Create.php delete mode 100644 examples/NumPHPExamples/Benchmark/Core/NumArray/Dot.php delete mode 100644 examples/NumPHPExamples/Benchmark/Core/NumArray/Sum.php delete mode 100644 examples/NumPHPExamples/Benchmark/TestRun.php delete mode 100644 examples/NumPHPExamples/Cholesky.php delete mode 100644 examples/NumPHPExamples/GaussianElimination.php delete mode 100644 examples/NumPHPExamples/Inverse.php delete mode 100644 examples/NumPHPExamples/LUDecomposition.php delete mode 100644 examples/README.md delete mode 100644 examples/numphp-examples diff --git a/.travis.yml b/.travis.yml index b071a01..fab2465 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,9 +16,9 @@ before_script: - mkdir -p build/logs script: - - php vendor/bin/phpcs --standard=PSR2 src/ tests/ examples/ - - php vendor/bin/phpmd src/,tests/,examples/ text codesize,controversial,design,naming,unusedcode - - php vendor/bin/phpcpd src/ tests/ examples/ + - php vendor/bin/phpcs --standard=PSR2 src/ tests/ + - php vendor/bin/phpmd src/,tests/ text codesize,controversial,design,naming,unusedcode + - php vendor/bin/phpcpd src/ tests/ - php vendor/bin/phpunit --coverage-clover build/logs/clover.xml after_script: diff --git a/composer.json b/composer.json index cf63dca..b99ceac 100644 --- a/composer.json +++ b/composer.json @@ -17,12 +17,11 @@ "php": ">=5.4" }, "require-dev": { - "phpunit/phpunit": "*", - "phpmd/phpmd" : "*", - "squizlabs/php_codesniffer": "*", - "sebastian/phpcpd": "*", - "satooshi/php-coveralls": "^2.1", - "symfony/console": "~2.2" + "phpunit/phpunit": "^5.7", + "phpmd/phpmd": "^2.7", + "squizlabs/php_codesniffer": "^3.5", + "sebastian/phpcpd": "^3.0", + "satooshi/php-coveralls": "^2.1" }, "autoload": { "psr-0": { @@ -31,8 +30,7 @@ }, "autoload-dev": { "psr-0": { - "NumPHPTest\\": "tests/", - "NumPHPExamples\\": "examples/" + "NumPHPTest\\": "tests/" } } } diff --git a/examples/NumPHPExamples/Benchmark.php b/examples/NumPHPExamples/Benchmark.php deleted file mode 100644 index 94c8600..0000000 --- a/examples/NumPHPExamples/Benchmark.php +++ /dev/null @@ -1,67 +0,0 @@ - - */ - -namespace NumPHPExamples; - -use NumPHPExamples\Benchmark\Core\NumArray\Abs; -use NumPHPExamples\Benchmark\Core\NumArray\Add; -use NumPHPExamples\Benchmark\Core\NumArray\Create; -use NumPHPExamples\Benchmark\Core\NumArray\Dot; -use NumPHPExamples\Benchmark\Core\NumArray\Sum; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; - -/** - * Class Benchmark - * - * @package NumPHPExamples - * @author Gordon Lesti - * @copyright 2014-2015 Gordon Lesti (https://gordonlesti.com/) - * @license http://opensource.org/licenses/MIT The MIT License (MIT) - * @link http://numphp.org/ - * @since 1.0.5 - */ -class Benchmark extends Command -{ - /** - * @var array - */ - protected $benchmarks = []; - - protected function configure() - { - $this->setName('benchmark') - ->setDescription('Benchmarks basic function'); - $this->benchmarks = [ - new Create(), - new Add(), - new Sum(), - new Abs(), - new Dot(), - ]; - } - - /** - * @param InputInterface $input - * @param OutputInterface $output - * - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - protected function execute(InputInterface $input, OutputInterface $output) - { - foreach ($this->benchmarks as $benchmark) { - /** @var Benchmark\BenchmarkInterface $benchmark */ - $output->writeln(sprintf("%s", $benchmark->getName())); - $result = $benchmark->run(); - foreach ($result as $testRun) { - /** @var Benchmark\TestRun $testRun */ - $output->writeln(sprintf("\t%d: %f", $testRun->getComplexity(), $testRun->getTime())); - } - } - } -} diff --git a/examples/NumPHPExamples/Benchmark/BenchmarkInterface.php b/examples/NumPHPExamples/Benchmark/BenchmarkInterface.php deleted file mode 100644 index 42f9367..0000000 --- a/examples/NumPHPExamples/Benchmark/BenchmarkInterface.php +++ /dev/null @@ -1,31 +0,0 @@ - - */ - -namespace NumPHPExamples\Benchmark; - -/** - * Interface BenchmarkInterface - * - * @package NumPHPExamples\Benchmark - * @author Gordon Lesti - * @copyright 2014-2015 Gordon Lesti (https://gordonlesti.com/) - * @license http://opensource.org/licenses/MIT The MIT License (MIT) - * @link http://numphp.org/ - * @since 1.0.5 - */ -interface BenchmarkInterface -{ - /** - * @return string - */ - public function getName(); - - /** - * @return array - */ - public function run(); -} diff --git a/examples/NumPHPExamples/Benchmark/Core/NumArray/Abs.php b/examples/NumPHPExamples/Benchmark/Core/NumArray/Abs.php deleted file mode 100644 index ca153f2..0000000 --- a/examples/NumPHPExamples/Benchmark/Core/NumArray/Abs.php +++ /dev/null @@ -1,51 +0,0 @@ - - */ - -namespace NumPHPExamples\Benchmark\Core\NumArray; - -use NumPHP\Core\NumPHP; -use NumPHPExamples\Benchmark\BenchmarkInterface; -use NumPHPExamples\Benchmark\TestRun; - -/** - * Class Abs - * - * @package NumPHPExamples\Benchmark\Core\NumArray - * @author Gordon Lesti - * @copyright 2014-2015 Gordon Lesti (https://gordonlesti.com/) - * @license http://opensource.org/licenses/MIT The MIT License (MIT) - * @link http://numphp.org/ - * @since 1.0.5 - */ -class Abs implements BenchmarkInterface -{ - /** - * @return string - */ - public function getName() - { - return 'NumArray Abs'; - } - - /** - * @return array - */ - public function run() - { - $result = []; - for ($i = 100; $i <= 1000; $i += 100) { - $numArray = NumPHP::ones($i, $i); - $time = microtime(true); - $numArray->abs(); - $timeDiff = microtime(true) - $time; - - $result[$i] = new TestRun($i, $timeDiff); - } - - return $result; - } -} diff --git a/examples/NumPHPExamples/Benchmark/Core/NumArray/Add.php b/examples/NumPHPExamples/Benchmark/Core/NumArray/Add.php deleted file mode 100644 index d41006b..0000000 --- a/examples/NumPHPExamples/Benchmark/Core/NumArray/Add.php +++ /dev/null @@ -1,51 +0,0 @@ - - */ - -namespace NumPHPExamples\Benchmark\Core\NumArray; - -use NumPHP\Core\NumPHP; -use NumPHPExamples\Benchmark\BenchmarkInterface; -use NumPHPExamples\Benchmark\TestRun; - -/** - * Class Add - * @package NumPHPExamples\Benchmark\Core\NumArray - * @author Gordon Lesti - * @copyright 2014-2015 Gordon Lesti (https://gordonlesti.com/) - * @license http://opensource.org/licenses/MIT The MIT License (MIT) - * @link http://numphp.org/ - * @since 1.0.5 - */ -class Add implements BenchmarkInterface -{ - /** - * @return string - */ - public function getName() - { - return 'NumArray Add'; - } - - /** - * @return array - */ - public function run() - { - $result = []; - for ($i = 100; $i <= 1000; $i += 100) { - $numArray1 = NumPHP::ones($i, $i); - $numArray2 = NumPHP::ones($i, $i); - $time = microtime(true); - $numArray1->add($numArray2); - $timeDiff = microtime(true) - $time; - - $result[$i] = new TestRun($i, $timeDiff); - } - - return $result; - } -} diff --git a/examples/NumPHPExamples/Benchmark/Core/NumArray/Create.php b/examples/NumPHPExamples/Benchmark/Core/NumArray/Create.php deleted file mode 100644 index 21297d8..0000000 --- a/examples/NumPHPExamples/Benchmark/Core/NumArray/Create.php +++ /dev/null @@ -1,54 +0,0 @@ - - */ - -namespace NumPHPExamples\Benchmark\Core\NumArray; - -use NumPHP\Core\NumPHP; -use NumPHP\Core\NumArray; -use NumPHPExamples\Benchmark\BenchmarkInterface; -use NumPHPExamples\Benchmark\TestRun; - -/** - * Class Create - * - * @package NumPHPExamples\Benchmark\Core\NumArray - * @author Gordon Lesti - * @copyright 2014-2015 Gordon Lesti (https://gordonlesti.com/) - * @license http://opensource.org/licenses/MIT The MIT License (MIT) - * @link http://numphp.org/ - * @since 1.0.5 - */ -class Create implements BenchmarkInterface -{ - /** - * @return string - */ - public function getName() - { - return 'NumArray Create'; - } - - /** - * @return array - * - * @SuppressWarnings(PHPMD.UnusedLocalVariable) - */ - public function run() - { - $result = []; - for ($i = 100; $i <= 1000; $i += 100) { - $array = NumPHP::ones($i, $i); - $time = microtime(true); - $numArray = new NumArray($array->getData()); - $timeDiff = microtime(true) - $time; - - $result[$i] = new TestRun($i, $timeDiff); - } - - return $result; - } -} diff --git a/examples/NumPHPExamples/Benchmark/Core/NumArray/Dot.php b/examples/NumPHPExamples/Benchmark/Core/NumArray/Dot.php deleted file mode 100644 index 59d86ed..0000000 --- a/examples/NumPHPExamples/Benchmark/Core/NumArray/Dot.php +++ /dev/null @@ -1,52 +0,0 @@ - - */ - -namespace NumPHPExamples\Benchmark\Core\NumArray; - -use NumPHP\Core\NumPHP; -use NumPHPExamples\Benchmark\BenchmarkInterface; -use NumPHPExamples\Benchmark\TestRun; - -/** - * Class Dot - * - * @package NumPHPExamples\Benchmark\Core\NumArray - * @author Gordon Lesti - * @copyright 2014-2015 Gordon Lesti (https://gordonlesti.com/) - * @license http://opensource.org/licenses/MIT The MIT License (MIT) - * @link http://numphp.org/ - * @since 1.0.5 - */ -class Dot implements BenchmarkInterface -{ - /** - * @return string - */ - public function getName() - { - return 'NumArray Dot'; - } - - /** - * @return array - */ - public function run() - { - $result = []; - for ($i = 100; $i <= 500; $i += 100) { - $numArray1 = NumPHP::ones($i, $i); - $numArray2 = NumPHP::ones($i, $i); - $time = microtime(true); - $numArray1->dot($numArray2); - $timeDiff = microtime(true) - $time; - - $result[$i] = new TestRun($i, $timeDiff); - } - - return $result; - } -} diff --git a/examples/NumPHPExamples/Benchmark/Core/NumArray/Sum.php b/examples/NumPHPExamples/Benchmark/Core/NumArray/Sum.php deleted file mode 100644 index 064e49e..0000000 --- a/examples/NumPHPExamples/Benchmark/Core/NumArray/Sum.php +++ /dev/null @@ -1,53 +0,0 @@ - - */ - -namespace NumPHPExamples\Benchmark\Core\NumArray; - -use NumPHP\Core\NumPHP; -use NumPHPExamples\Benchmark\BenchmarkInterface; -use NumPHPExamples\Benchmark\TestRun; - -/** - * Class Sum - * - * @package NumPHPExamples\Benchmark\Core\NumArray - * @author Gordon Lesti - * @copyright 2014-2015 Gordon Lesti (https://gordonlesti.com/) - * @license http://opensource.org/licenses/MIT The MIT License (MIT) - * @link http://numphp.org/ - * @since 1.0.5 - */ -class Sum implements BenchmarkInterface -{ - /** - * @return string - */ - public function getName() - { - return 'NumArray Sum'; - } - - /** - * @return array - * - * @SuppressWarnings(PHPMD.UnusedLocalVariable) - */ - public function run() - { - $result = []; - for ($i = 100; $i <= 1000; $i += 100) { - $numArray = NumPHP::ones($i, $i); - $time = microtime(true); - $sum = $numArray->sum(); - $timeDiff = microtime(true) - $time; - - $result[$i] = new TestRun($i, $timeDiff); - } - - return $result; - } -} diff --git a/examples/NumPHPExamples/Benchmark/TestRun.php b/examples/NumPHPExamples/Benchmark/TestRun.php deleted file mode 100644 index 810b021..0000000 --- a/examples/NumPHPExamples/Benchmark/TestRun.php +++ /dev/null @@ -1,57 +0,0 @@ - - */ - -namespace NumPHPExamples\Benchmark; - -/** - * Class TestRun - * - * @package NumPHPExamples\Benchmark - * @author Gordon Lesti - * @copyright 2014-2015 Gordon Lesti (https://gordonlesti.com/) - * @license http://opensource.org/licenses/MIT The MIT License (MIT) - * @link http://numphp.org/ - * @since 1.0.5 - */ -class TestRun -{ - /** - * @var float - */ - protected $time = 0.0; - - /** - * @var int - */ - protected $complexity = 0; - - /** - * @param int $complexity - * @param float $time - */ - public function __construct($complexity, $time) - { - $this->complexity = $complexity; - $this->time = $time; - } - - /** - * @return int - */ - public function getComplexity() - { - return $this->complexity; - } - - /** - * @return float - */ - public function getTime() - { - return $this->time; - } -} diff --git a/examples/NumPHPExamples/Cholesky.php b/examples/NumPHPExamples/Cholesky.php deleted file mode 100644 index 4c0996c..0000000 --- a/examples/NumPHPExamples/Cholesky.php +++ /dev/null @@ -1,61 +0,0 @@ - - */ - -namespace NumPHPExamples; - -use NumPHP\Core\NumArray; -use NumPHP\LinAlg\LinAlg; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; - -/** - * Class Cholesky - * - * @package NumPHPExamples - * @author Gordon Lesti - * @copyright 2014-2015 Gordon Lesti (https://gordonlesti.com/) - * @license http://opensource.org/licenses/MIT The MIT License (MIT) - * @link http://numphp.org/ - */ -class Cholesky extends Command -{ - protected function configure() - { - $this->setName('cholesky') - ->setDescription('Factors a symmetric positive definite matrix in to a lower triangular matrix'); - } - - /** - * @param InputInterface $input - * @param OutputInterface $output - * - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - protected function execute(InputInterface $input, OutputInterface $output) - { - $matrixA = new NumArray( - [ - [ 49, -525, 7, -315], - [-525, 6921, -3279, 3483], - [ 7, -3279, 8178, -328], - [-315, 3483, -328, 624556] - ] - ); - $output->writeln('Matrix A:'); - $output->writeln($matrixA->__toString()); - - $output->writeln('Cholesky:'); - $time = microtime(true); - $matrixL = LinAlg::cholesky($matrixA); - $timeDiff = microtime(true) - $time; - - $output->writeln($matrixL->__toString()); - - $output->writeln('Time for calculation: '.$timeDiff.' sec'); - } -} diff --git a/examples/NumPHPExamples/GaussianElimination.php b/examples/NumPHPExamples/GaussianElimination.php deleted file mode 100644 index e165fc7..0000000 --- a/examples/NumPHPExamples/GaussianElimination.php +++ /dev/null @@ -1,132 +0,0 @@ - - */ - -namespace NumPHPExamples; - -use NumPHP\Core\NumArray; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; - -/** - * Class GaussianElimination - * - * @package NumPHPExamples - * @author Gordon Lesti - * @copyright 2014-2015 Gordon Lesti (https://gordonlesti.com/) - * @license http://opensource.org/licenses/MIT The MIT License (MIT) - * @link http://numphp.org/ - */ -class GaussianElimination extends Command -{ - protected function configure() - { - $this->setName('gaussian-elimination') - ->setDescription('Solves a small system of linear equations with gaussian elimination'); - } - - /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void - * - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - protected function execute(InputInterface $input, OutputInterface $output) - { - $matrixA = new NumArray( - [ - [11.0, 44.0, 1.0], - [0.1, 0.4, 3.0], - [0.0, 1.0, -1.0], - ] - ); - $output->writeln('Matrix A:'); - $output->writeln($matrixA->__toString()); - - $vectorB = new NumArray( - [1.0, 1.0, 1.0] - ); - $output->writeln('Vector b:'); - $output->writeln($vectorB->__toString()); - - $time = microtime(true); - $gaussianResult = self::gaussianEliminationPivoting($matrixA, $vectorB); - $xVector = self::backSubstitution($gaussianResult['M'], $gaussianResult['b']); - $timeDiff = microtime(true) - $time; - - $output->writeln('Solution of A*x=b?'); - $output->writeln('Vector x:'); - $output->writeln($xVector->__toString()); - $output->writeln('Time for calculation: '.$timeDiff.' sec'); - } - - /** - * @param NumArray $matrix - * @param NumArray $vector - * @return array - */ - protected static function gaussianEliminationPivoting(NumArray $matrix, NumArray $vector) - { - $shape = $matrix->getShape(); - - for ($i = 0; $i < $shape[0]; $i++) { - // find pivo element - $max = abs($matrix->get($i, $i)->getData()); - $maxIndex = $i; - for ($j = $i+1; $j < $shape[0]; $j++) { - $abs = abs($matrix->get($j, $i)->getData()); - if ($abs > $max) { - $max = $abs; - $maxIndex = $j; - } - } - // pivoting - if ($maxIndex !== $i) { - // change maxIndex row with i row in $matrix - $temp = $matrix->get($i); - $matrix->set($i, $matrix->get($maxIndex)); - $matrix->set($maxIndex, $temp); - // change maxIndex row with i row in $vector - $temp = $vector->get($i); - $vector->set($i, $vector->get($maxIndex)); - $vector->set($maxIndex, $temp); - } - for ($j = $i+1; $j < $shape[0]; $j++) { - $fac = -$matrix->get($j, $i)->getData()/$matrix->get($i, $i)->getData(); - $matrix->set($j, $matrix->get($j)->add($matrix->get($i)->dot($fac))); - $vector->set($j, $vector->get($j)->add($vector->get($i)->dot($fac))); - } - } - - return [ - 'M' => $matrix, - 'b' => $vector - ]; - } - - /** - * @param NumArray $matrix - * @param NumArray $vector - * @return NumArray - */ - protected static function backSubstitution(NumArray $matrix, NumArray $vector) - { - $shape = $matrix->getShape(); - $xVector = \NumPHP\Core\NumPHP::zeros($shape[0]); - - for ($i = $shape[0]-1; $i >= 0; $i--) { - $sum = 0; - for ($j = $i+1; $j < $shape[0]; $j++) { - $sum += $matrix->get($i, $j)->dot($xVector->get($j))->getData(); - } - $xVector->set($i, ($vector->get($i)->getData()-$sum)/$matrix->get($i, $i)->getData()); - } - - return $xVector; - } -} diff --git a/examples/NumPHPExamples/Inverse.php b/examples/NumPHPExamples/Inverse.php deleted file mode 100644 index 007a131..0000000 --- a/examples/NumPHPExamples/Inverse.php +++ /dev/null @@ -1,60 +0,0 @@ - - */ - -namespace NumPHPExamples; - -use NumPHP\Core\NumArray; -use NumPHP\LinAlg\LinAlg; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; - -/** - * Class Inverse - * - * @package NumPHPExamples - * @author Gordon Lesti - * @copyright 2014-2015 Gordon Lesti (https://gordonlesti.com/) - * @license http://opensource.org/licenses/MIT The MIT License (MIT) - * @link http://numphp.org/ - */ -class Inverse extends Command -{ - protected function configure() - { - $this->setName('inverse') - ->setDescription('Calculates the inverse of a not singular matrix'); - } - - /** - * @param InputInterface $input - * @param OutputInterface $output - * - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - protected function execute(InputInterface $input, OutputInterface $output) - { - $matrix = new NumArray( - [ - [-3, 4, 7/6], - [ 2, 0.1, 0], - [23, -5, 8] - ] - ); - $output->writeln('Matrix:'); - $output->writeln($matrix->__toString()); - - $output->writeln('Inverse:'); - $time = microtime(true); - $inv = LinAlg::inv($matrix); - $timeDiff = microtime(true) -$time; - - $output->writeln($inv->__toString()); - - $output->writeln('Time for calculation: '.$timeDiff.' sec'); - } -} diff --git a/examples/NumPHPExamples/LUDecomposition.php b/examples/NumPHPExamples/LUDecomposition.php deleted file mode 100644 index 80fbd49..0000000 --- a/examples/NumPHPExamples/LUDecomposition.php +++ /dev/null @@ -1,67 +0,0 @@ - - */ - -namespace NumPHPExamples; - -use NumPHP\Core\NumArray; -use NumPHP\LinAlg\LinAlg; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; - -/** - * Class LUDecomposition - * - * @package NumPHPExamples - * @author Gordon Lesti - * @copyright 2014-2015 Gordon Lesti (https://gordonlesti.com/) - * @license http://opensource.org/licenses/MIT The MIT License (MIT) - * @link http://numphp.org/ - */ -class LUDecomposition extends Command -{ - protected function configure() - { - $this->setName('lu-decomposition') - ->setDescription('Factors a matrix into a permutation matrix, a lower and a upper triangular matrix'); - } - - /** - * @param InputInterface $input - * @param OutputInterface $output - * - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - protected function execute(InputInterface $input, OutputInterface $output) - { - $matrixA = new NumArray( - [ - [1, 6, 1], - [2, 3, 2], - [4, 2, 1], - ] - ); - $output->writeln('Matrix A:'); - $output->writeln($matrixA->__toString()); - - $output->writeln('LU decomposition'); - $time = microtime(true); - list($matrixP, $matrixL, $matrixU) = LinAlg::lud($matrixA); - $timeDiff = microtime(true) - $time; - - $output->writeln('Matrix P:'); - $output->writeln($matrixP->__toString()); - - $output->writeln('Matrix L:'); - $output->writeln($matrixL->__toString()); - - $output->writeln('Matrix U:'); - $output->writeln($matrixU->__toString()); - - $output->writeln('Time for calculation: '.$timeDiff.' sec'); - } -} diff --git a/examples/README.md b/examples/README.md deleted file mode 100644 index d632556..0000000 --- a/examples/README.md +++ /dev/null @@ -1,13 +0,0 @@ -NumPHP-examples -=============== - -These are some examples as Symfony Commands. Run the example command - -``` - php numphp-examples -``` - -* [Gaussian Elimination](NumPHPExamples/GaussianElimination.php) -* [LU Decomposition](NumPHPExamples/LUDecomposition.php) -* [Inverse](NumPHPExamples/Inverse.php) -* [Cholesky](NumPHPExamples/Cholesky.php) diff --git a/examples/numphp-examples b/examples/numphp-examples deleted file mode 100644 index c9cc7f0..0000000 --- a/examples/numphp-examples +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env php -add(new \NumPHPExamples\GaussianElimination()); -$application->add(new \NumPHPExamples\LUDecomposition()); -$application->add(new \NumPHPExamples\Inverse()); -$application->add(new \NumPHPExamples\Cholesky()); -$application->add(new \NumPHPExamples\Benchmark()); - -$application->run(); From 0e9ede5af87036e9e22f10ad10330d43d51fb4e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20H=C3=A9bert?= Date: Fri, 18 Oct 2019 19:41:40 +0200 Subject: [PATCH 5/8] add map --- src/NumPHP/Core/NumArray.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/NumPHP/Core/NumArray.php b/src/NumPHP/Core/NumArray.php index 0ef9c0b..9f0ebf3 100644 --- a/src/NumPHP/Core/NumArray.php +++ b/src/NumPHP/Core/NumArray.php @@ -379,7 +379,7 @@ public function mean($axis = null) * @return $this * * @api - * @since 1.0.0 + * @since 1.2.0 */ public function abs() { @@ -394,6 +394,25 @@ function ($data) { return $this; } + /** + * Applies a callable on every value of the NumArray + * + * @return $this + * + * @api + * @since 1.2.0 + */ + public function map($callable) + { + $this->data = Filter::filterArray( + $this->data, + $callable + ); + $this->flushCache(); + + return $this; + } + /** * Multiplies an array, NumArray or numeric value to the existing NumArray * From b21a97f3bbab7a4a49173dab091a6d3d16f64c0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20H=C3=A9bert?= Date: Fri, 18 Oct 2019 19:53:49 +0200 Subject: [PATCH 6/8] add test --- tests/NumPHPTest/Core/NumArrayTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/NumPHPTest/Core/NumArrayTest.php b/tests/NumPHPTest/Core/NumArrayTest.php index 59b615a..e973746 100644 --- a/tests/NumPHPTest/Core/NumArrayTest.php +++ b/tests/NumPHPTest/Core/NumArrayTest.php @@ -58,4 +58,15 @@ public function testNDim() $numArray = NumPHP::arange(1, 6)->reshape(2, 3); $this->assertSame(2, $numArray->getNDim()); } + + /** + * Tests NumArray::map + */ + public function testmap() + { + $numArray = new NumArray(1); + $this->assertSame(2, $numArray->map(function(&$value){ + $value *= 2; + })); + } } From c120758a45f52323663722258443b84ac00aadb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20H=C3=A9bert?= Date: Wed, 23 Oct 2019 18:35:39 +0200 Subject: [PATCH 7/8] fix test --- tests/NumPHPTest/Core/NumArrayTest.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/NumPHPTest/Core/NumArrayTest.php b/tests/NumPHPTest/Core/NumArrayTest.php index e973746..edb9b33 100644 --- a/tests/NumPHPTest/Core/NumArrayTest.php +++ b/tests/NumPHPTest/Core/NumArrayTest.php @@ -65,8 +65,13 @@ public function testNDim() public function testmap() { $numArray = new NumArray(1); - $this->assertSame(2, $numArray->map(function(&$value){ - $value *= 2; - })); + $this->assertSame(2, $numArray->map(static function ($value) { + return $value * 2; + })->getData()); + + $numArray = new NumArray([[3]]); + $this->assertSame([[6]], $numArray->map(static function ($value) { + return $value * 2; + })->getData()); } } From cae62d83debee8b0f1e1c53e060c6688014dc0c8 Mon Sep 17 00:00:00 2001 From: Gordon Lesti Date: Tue, 5 Nov 2019 18:22:03 +0100 Subject: [PATCH 8/8] version 1.2 --- .gitignore | 7 ++++--- .travis.yml | 8 ++++---- README.md | 2 +- composer.json | 6 ++++++ phpunit.xml | 6 ++++++ src/NumPHP/Core/NumArray.php | 12 ++--------- src/NumPHP/Core/NumPHP.php | 2 +- tests/NumPHPTest/Core/NumArrayTest.php | 28 +++++++++++++++++++------- 8 files changed, 45 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 072705c..7aa851f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ -.idea/ -vendor/ -composer.lock +/.idea/ +/build/ +/composer.lock +/vendor/ diff --git a/.travis.yml b/.travis.yml index fab2465..c058098 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,10 +16,10 @@ before_script: - mkdir -p build/logs script: - - php vendor/bin/phpcs --standard=PSR2 src/ tests/ - - php vendor/bin/phpmd src/,tests/ text codesize,controversial,design,naming,unusedcode - - php vendor/bin/phpcpd src/ tests/ - - php vendor/bin/phpunit --coverage-clover build/logs/clover.xml + - composer phpcs + - composer phpmd + - composer phpcpd + - composer test after_script: - php vendor/bin/coveralls -v diff --git a/README.md b/README.md index 1a00cb4..0f3f7f8 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Develop | [![Build Status](https://img.shields.io/travis/NumPHP/NumPHP/develop.s ## Release Information -*NumPHP 1.1.0* +*NumPHP 1.2.0* ## Requirements diff --git a/composer.json b/composer.json index b99ceac..9608d9c 100644 --- a/composer.json +++ b/composer.json @@ -32,5 +32,11 @@ "psr-0": { "NumPHPTest\\": "tests/" } + }, + "scripts": { + "test": "php vendor/bin/phpunit", + "phpcs": "php vendor/bin/phpcs --standard=PSR2 src/ tests/", + "phpmd": "php vendor/bin/phpmd src/,tests/ text codesize,controversial,design,naming,unusedcode", + "phpcpd": "php vendor/bin/phpcpd src/ tests/" } } diff --git a/phpunit.xml b/phpunit.xml index a60fda8..65f634f 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -19,4 +19,10 @@ ./src/NumPHP + + + + + + diff --git a/src/NumPHP/Core/NumArray.php b/src/NumPHP/Core/NumArray.php index 9f0ebf3..7693926 100644 --- a/src/NumPHP/Core/NumArray.php +++ b/src/NumPHP/Core/NumArray.php @@ -379,19 +379,11 @@ public function mean($axis = null) * @return $this * * @api - * @since 1.2.0 + * @since 1.0.0 */ public function abs() { - $this->data = Filter::filterArray( - $this->data, - function ($data) { - return abs($data); - } - ); - $this->flushCache(); - - return $this; + return $this->map('abs'); } /** diff --git a/src/NumPHP/Core/NumPHP.php b/src/NumPHP/Core/NumPHP.php index 86dfabe..dd518ea 100644 --- a/src/NumPHP/Core/NumPHP.php +++ b/src/NumPHP/Core/NumPHP.php @@ -23,7 +23,7 @@ */ abstract class NumPHP { - const VERSION = '1.1.0'; + const VERSION = '1.2.0'; /** * Returns a NumArray filled with `0` diff --git a/tests/NumPHPTest/Core/NumArrayTest.php b/tests/NumPHPTest/Core/NumArrayTest.php index edb9b33..fc6a328 100644 --- a/tests/NumPHPTest/Core/NumArrayTest.php +++ b/tests/NumPHPTest/Core/NumArrayTest.php @@ -62,16 +62,30 @@ public function testNDim() /** * Tests NumArray::map */ - public function testmap() + public function testMap() { $numArray = new NumArray(1); - $this->assertSame(2, $numArray->map(static function ($value) { - return $value * 2; - })->getData()); + $this->assertEquals( + new NumArray(2), + $numArray->map(function ($value) { + return $value * 2; + }) + ); $numArray = new NumArray([[3]]); - $this->assertSame([[6]], $numArray->map(static function ($value) { - return $value * 2; - })->getData()); + $this->assertEquals( + new NumArray([[-240]]), + $numArray->map(function ($value) { + return $value * -80; + }) + ); + + $numArray = NumPHP::arange(1, 6)->reshape(2, 3); + $this->assertEquals( + NumPHP::arange(54, 324, 54)->reshape(2, 3), + $numArray->map(function ($value) { + return $value * 54; + }) + ); } }