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 41b1780..c058098 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 @@ -16,10 +16,10 @@ 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/phpunit --coverage-clover build/logs/clover.xml + - composer phpcs + - composer phpmd + - composer phpcpd + - composer test after_script: - - sh -c "if [ $TRAVIS_PHP_VERSION != 'hhvm' ]; then php vendor/bin/coveralls -v; fi" + - 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 1a6a7fb..9608d9c 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": "v1.0.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,13 @@ }, "autoload-dev": { "psr-0": { - "NumPHPTest\\": "tests/", - "NumPHPExamples\\": "examples/" + "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/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(); 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 0ef9c0b..7693926 100644 --- a/src/NumPHP/Core/NumArray.php +++ b/src/NumPHP/Core/NumArray.php @@ -382,12 +382,23 @@ public function mean($axis = null) * @since 1.0.0 */ public function abs() + { + return $this->map('abs'); + } + + /** + * 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, - function ($data) { - return abs($data); - } + $callable ); $this->flushCache(); 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)) { 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 59b615a..fc6a328 100644 --- a/tests/NumPHPTest/Core/NumArrayTest.php +++ b/tests/NumPHPTest/Core/NumArrayTest.php @@ -58,4 +58,34 @@ 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->assertEquals( + new NumArray(2), + $numArray->map(function ($value) { + return $value * 2; + }) + ); + + $numArray = new NumArray([[3]]); + $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; + }) + ); + } }