├── .codeclimate.yml
├── .github
└── FUNDING.yml
├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── composer.json
├── src
└── SciPhp
│ ├── Exception
│ ├── InvalidAttributeException.php
│ └── Message.php
│ ├── LinAlg.php
│ ├── LinAlg
│ ├── CholeskyTrait.php
│ ├── Decorator.php
│ └── NormsTrait.php
│ ├── NdArray.php
│ ├── NdArray
│ ├── ArithmeticTrait.php
│ ├── AttributeTrait.php
│ ├── BasicTrait.php
│ ├── Decorator.php
│ ├── Formatter.php
│ ├── IndexInterface.php
│ ├── IndexTrait.php
│ ├── ShapeTrait.php
│ └── VisitorTrait.php
│ ├── NumPhp.php
│ ├── NumPhp
│ ├── ArithmeticTrait.php
│ ├── Decorator.php
│ ├── DiagonalTrait.php
│ ├── ExponentTrait.php
│ ├── ExtensionsTrait.php
│ ├── FileTrait.php
│ ├── FloatTrait.php
│ ├── LogarithmTrait.php
│ ├── Mathematical
│ │ └── TrigonometricTrait.php
│ ├── MatrixTrait.php
│ ├── NumArrayTrait.php
│ ├── OperationTrait.php
│ ├── RangeTrait.php
│ ├── TriangleTrait.php
│ └── VanderTrait.php
│ ├── Random.php
│ └── Random
│ ├── Decorator.php
│ └── RandomStateTrait.php
└── test
├── Benchmark
├── bench.sh
├── php
│ ├── ndarray.T
│ ├── ndarray.copy
│ ├── ndarray.copysign
│ ├── ndarray.cos
│ ├── ndarray.data
│ ├── ndarray.divide
│ ├── ndarray.exp
│ ├── ndarray.exp2
│ ├── ndarray.expm1
│ ├── ndarray.indexing
│ ├── ndarray.log
│ ├── ndarray.log10
│ ├── ndarray.log2
│ ├── ndarray.multiply
│ ├── ndarray.ndim
│ ├── ndarray.negative
│ ├── ndarray.power
│ ├── ndarray.ravel
│ ├── ndarray.reciprocal
│ ├── ndarray.reshape
│ ├── ndarray.resize
│ ├── ndarray.shape
│ ├── ndarray.signbit
│ ├── ndarray.sin
│ ├── ndarray.size
│ ├── ndarray.sqrt
│ ├── ndarray.square
│ ├── ndarray.subtract
│ ├── ndarray.sum
│ ├── ndarray.trace
│ ├── ndarray.trapz
│ ├── ndarray.vander
│ ├── np.add
│ ├── np.arange
│ ├── np.copysign
│ ├── np.cos
│ ├── np.diag
│ ├── np.diagflat
│ ├── np.diagonal
│ ├── np.divide
│ ├── np.dot
│ ├── np.empty
│ ├── np.empty_like
│ ├── np.exp
│ ├── np.exp2
│ ├── np.expm1
│ ├── np.eye
│ ├── np.full
│ ├── np.full_like
│ ├── np.identity
│ ├── np.linalg.cholesky
│ ├── np.linalg.norm
│ ├── np.linspace
│ ├── np.loadtxt
│ ├── np.log
│ ├── np.log10
│ ├── np.log2
│ ├── np.logspace
│ ├── np.multiply
│ ├── np.negative
│ ├── np.ones
│ ├── np.ones_like
│ ├── np.power
│ ├── np.random.rand
│ ├── np.random.randint
│ ├── np.random.randn
│ ├── np.reciprocal
│ ├── np.signbit
│ ├── np.sin
│ ├── np.sqrt
│ ├── np.square
│ ├── np.subtract
│ ├── np.sum
│ ├── np.trace
│ ├── np.transpose
│ ├── np.trapz
│ ├── np.tri
│ ├── np.tril
│ ├── np.triu
│ ├── np.vander
│ ├── np.zeros
│ └── np.zeros_like
└── python
│ ├── ndarray.T
│ ├── ndarray.copy
│ ├── ndarray.copysign
│ ├── ndarray.cos
│ ├── ndarray.data
│ ├── ndarray.divide
│ ├── ndarray.exp
│ ├── ndarray.exp2
│ ├── ndarray.expm1
│ ├── ndarray.indexing
│ ├── ndarray.log
│ ├── ndarray.log10
│ ├── ndarray.log2
│ ├── ndarray.multiply
│ ├── ndarray.ndim
│ ├── ndarray.negative
│ ├── ndarray.power
│ ├── ndarray.ravel
│ ├── ndarray.reciprocal
│ ├── ndarray.reshape
│ ├── ndarray.resize
│ ├── ndarray.shape
│ ├── ndarray.signbit
│ ├── ndarray.sin
│ ├── ndarray.size
│ ├── ndarray.sqrt
│ ├── ndarray.square
│ ├── ndarray.subtract
│ ├── ndarray.sum
│ ├── ndarray.trace
│ ├── ndarray.trapz
│ ├── ndarray.vander
│ ├── np.add
│ ├── np.arange
│ ├── np.copysign
│ ├── np.cos
│ ├── np.diag
│ ├── np.diagflat
│ ├── np.diagonal
│ ├── np.divide
│ ├── np.dot
│ ├── np.empty
│ ├── np.empty_like
│ ├── np.exp
│ ├── np.exp2
│ ├── np.expm1
│ ├── np.eye
│ ├── np.full
│ ├── np.full_like
│ ├── np.identity
│ ├── np.linalg.cholesky
│ ├── np.linalg.norm
│ ├── np.linspace
│ ├── np.loadtxt
│ ├── np.log
│ ├── np.log10
│ ├── np.log2
│ ├── np.logspace
│ ├── np.multiply
│ ├── np.negative
│ ├── np.ones
│ ├── np.ones_like
│ ├── np.power
│ ├── np.random.rand
│ ├── np.random.randint
│ ├── np.random.randn
│ ├── np.reciprocal
│ ├── np.signbit
│ ├── np.sin
│ ├── np.sqrt
│ ├── np.square
│ ├── np.subtract
│ ├── np.sum
│ ├── np.trace
│ ├── np.transpose
│ ├── np.trapz
│ ├── np.tri
│ ├── np.tril
│ ├── np.triu
│ ├── np.vander
│ ├── np.zeros
│ └── np.zeros_like
├── Resources
└── files
│ ├── simple-headers.csv
│ ├── simple-noheaders.csv
│ ├── simple-spacedelimiter.csv
│ └── winequality-white.csv
└── Unit
├── LinAlg
├── CholeskyTest.php
└── NormsTest.php
├── MultiRunner.php
├── NdArray
├── AlterTest.php
├── BridgeTest.php
├── StringFormatterTest.php
└── Traits
│ ├── ArithmeticTraitTest.php
│ ├── Attribute
│ ├── AttributeTest.php
│ ├── ShapeTest.php
│ ├── TTest.php
│ └── WritingInvalidAttributeTest.php
│ ├── BasicTraitTest.php
│ ├── Index
│ ├── OffsetGetTest.php
│ └── OffsetSetTest.php
│ ├── IndexTraitTest.php
│ ├── Mathematical
│ └── TrigonometricTest.php
│ ├── ShapeTraitTest.php
│ ├── Triangle
│ ├── TrilTest.php
│ └── TriuTest.php
│ └── Visitor
│ └── AxisTest.php
├── NumPhp
├── NumPhpTest.php
└── Traits
│ ├── Arithmetic
│ ├── AddTest.php
│ ├── DivideTest.php
│ ├── DotTest.php
│ ├── MultiplyTest.php
│ ├── ReciprocalTest.php
│ └── SubtractTest.php
│ ├── Diagonal
│ ├── DiagTest.php
│ ├── DiagflatTest.php
│ ├── DiagonalTest.php
│ ├── EyeTest.php
│ ├── IdentityTest.php
│ └── TraceTest.php
│ ├── Exponent
│ ├── Exp2Test.php
│ ├── ExpTest.php
│ ├── Expm1Test.php
│ ├── PowerTest.php
│ ├── SqrtTest.php
│ └── SquareTest.php
│ ├── File
│ └── LoadtxtTest.php
│ ├── Float
│ ├── CopysignTest.php
│ └── SignbitTest.php
│ ├── Logarithm
│ ├── Log10Test.php
│ ├── Log2Test.php
│ └── LogTest.php
│ ├── Mathematical
│ └── TrigonometricTest.php
│ ├── Matrix
│ ├── IsSquareTest.php
│ ├── NegativeTest.php
│ └── TransposeTest.php
│ ├── Misc
│ └── VanderTest.php
│ ├── NumArray
│ ├── BroadcastToTest.php
│ ├── FullLikeTest.php
│ ├── FullTest.php
│ ├── NullsLikeTest.php
│ ├── NullsTest.php
│ ├── OnesLikeTest.php
│ ├── OnesTest.php
│ ├── ZerosLikeTest.php
│ └── ZerosTest.php
│ ├── Operation
│ ├── SumTest.php
│ └── TrapzTest.php
│ ├── Range
│ ├── ArangeTest.php
│ ├── LinspaceTest.php
│ └── LogspaceTest.php
│ └── Triangle
│ ├── TriTest.php
│ ├── TrilTest.php
│ └── TriuTest.php
├── Random
├── RandTest.php
├── RandintTest.php
└── RandnTest.php
├── phpunit.xml.dist
└── phpunit.xml.dist.bak
/.codeclimate.yml:
--------------------------------------------------------------------------------
1 | version: "2"
2 | checks:
3 | argument-count:
4 | enabled: true
5 | config:
6 | threshold: 5
7 | complex-logic:
8 | config:
9 | threshold: 15
10 | method-complexity:
11 | config:
12 | threshold: 15
13 | method-count:
14 | config:
15 | threshold: 25
16 | method-lines:
17 | config:
18 | threshold: 60
19 | nested-control-flow:
20 | config:
21 | threshold: 4
22 | return-statements:
23 | config:
24 | threshold: 8
25 | file-lines:
26 | config:
27 | threshold: 500
28 |
--------------------------------------------------------------------------------
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 |
3 | github: [landrok]
4 | ko_fi: landrok
5 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 | /vendor
3 | composer.phar
4 | composer.lock
5 | .gitignore
6 | .phpunit.result.cache
7 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: php
2 |
3 | dist: bionic
4 |
5 | cache:
6 | directories:
7 | - $HOME/.composer/cache
8 |
9 | php:
10 | - 7.2
11 | - 7.3
12 | - 7.4
13 | - 8.0
14 | - nightly
15 |
16 | matrix:
17 | allow_failures:
18 | - php: nightly
19 |
20 | before_install:
21 | - sudo apt-get install -qq python3-pip python3-scipy
22 | - pip3 install numpy
23 | - python3 -c "import numpy; print(numpy.__version__)"
24 |
25 | install:
26 | - composer install --prefer-source --no-interaction
27 | # PHPUnit
28 | - ./vendor/bin/phpunit --version
29 |
30 | before_script:
31 | # Install Code climate reporter
32 | - if [ "$TRAVIS_PHP_VERSION" = "8.0" ]; then curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter; fi
33 | - if [ "$TRAVIS_PHP_VERSION" = "8.0" ]; then chmod +x ./cc-test-reporter; fi
34 | - if [ "$TRAVIS_PHP_VERSION" = "8.0" ]; then ./cc-test-reporter before-build; fi
35 |
36 | script:
37 | # Run PHP unit tests
38 | - ./vendor/bin/phpunit -c test/Unit/phpunit.xml.dist
39 | # Run benchmark tests
40 | - ./test/Benchmark/bench.sh
41 |
42 | after_script:
43 | # Code climate
44 | - if [ "$TRAVIS_PHP_VERSION" = "8.0" ]; then ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT; fi
45 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016-2017 landrok at github.com/landrok
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 | of the Software, and to permit persons to whom the Software is furnished
10 | to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
22 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "sciphp/numphp",
3 | "type": "library",
4 | "version": "0.4.0",
5 | "description": "PHP library for scientific computing.",
6 | "keywords": ["matrix", "linear-algebra", "numeric", "scientific", "computing"],
7 | "homepage": "https://github.com/sciphp/numphp",
8 | "license": "MIT",
9 | "require": {
10 | "php": ">=7.2",
11 | "webmozart/assert": "^1.2"
12 | },
13 | "require-dev": {
14 | "phpunit/phpunit": "*"
15 | },
16 | "autoload": {
17 | "psr-4": {
18 | "SciPhp\\": "src/SciPhp/",
19 | "SciPhpTest\\": "test/"
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/SciPhp/Exception/InvalidAttributeException.php:
--------------------------------------------------------------------------------
1 | requested->shape]: %s -> %s.';
14 | public const ARRAYS_BROADCAST_NDIM2_ONLY = "This library can't broadcast to a shape with more than 2 dimensions.";
15 | public const MAT_NOT_ALIGNED = "Matrices are not aligned.";
16 | public const ONLY_POSITIVE_INT = 'Must be a positive integer. Given %s.';
17 | public const ONLY_POSITIVE_NUMBER = 'Must be a positive number. Given %s.';
18 | public const MAT_NOT_SQUARE = "Given matrix %s is not a square matrix.";
19 | }
20 |
--------------------------------------------------------------------------------
/src/SciPhp/LinAlg.php:
--------------------------------------------------------------------------------
1 | shape;
34 |
35 | Assert::true(
36 | $matrix->is_square(),
37 | sprintf(
38 | Message::MAT_NOT_SQUARE,
39 | trim((string) np::ar($shape))
40 | )
41 | );
42 |
43 | $size = $shape[0];
44 | $w = $matrix->copy();
45 |
46 | $l = np::zeros($shape);
47 |
48 | for ($a = 0; $a < $size; $a++) {
49 | Assert::greaterThan(
50 | $w[$a][$a],
51 | 0,
52 | 'Not a positive-definite matrix'
53 | );
54 |
55 | $l["{$a}, {$a}"] = sqrt($w[$a][$a]);
56 |
57 | for ($b = $a + 1; $b < $size; $b++) {
58 | Assert::eq(
59 | $matrix["{$b}, {$a}"],
60 | $matrix["{$a}, {$b}"],
61 | 'Not a symetric matrix'
62 | );
63 |
64 | $l["{$b}, {$a}"] = $w["{$b}, {$a}"] / $l["{$a}, {$a}"];
65 |
66 | for ($c = $a + 1; $c <= $b; $c++) {
67 | $w["${b}, ${c}"] = $w["{$b}, {$c}"]
68 | - $l["{$b}, {$a}"]
69 | * $l["{$c}, {$a}"];
70 | }
71 | }
72 | }
73 |
74 | return $l;
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/SciPhp/LinAlg/Decorator.php:
--------------------------------------------------------------------------------
1 | ndim === 0) {
31 | return 0;
32 | }
33 |
34 | return sqrt(
35 | $matrix->power(2)->sum()
36 | );
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/SciPhp/NdArray.php:
--------------------------------------------------------------------------------
1 | data = $data;
32 | }
33 |
34 | /**
35 | * Pretty printer
36 | */
37 | public function __toString(): string
38 | {
39 | return (new Formatter($this))->toString();
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/SciPhp/NdArray/AttributeTrait.php:
--------------------------------------------------------------------------------
1 | __construct(
30 | $this->reshape($value)->data
31 | );
32 | break;
33 | default:
34 | throw new InvalidAttributeException(static::class, $name);
35 | }
36 | }
37 |
38 | /**
39 | * Generic getter
40 | *
41 | * @return int|array|NdArray
42 | * @throws \SciPhp\Exception\InvalidAttributeException
43 | */
44 | final public function __get(string $name)
45 | {
46 | switch ($name) {
47 | case 'data':
48 | return $this->data;
49 | case 'ndim':
50 | return count($this->shape);
51 | case 'size':
52 | return $this->getSize();
53 | case 'shape':
54 | return $this->getShape($this->data, []);
55 | case 'T':
56 | return np::transpose($this);
57 | }
58 |
59 | throw new InvalidAttributeException(static::class, $name);
60 | }
61 |
62 | /**
63 | * Get the total number of elements of the array
64 | */
65 | final protected function getSize(): int
66 | {
67 | if (count($this->data)) {
68 | return array_product($this->shape);
69 | }
70 |
71 | return 0;
72 | }
73 |
74 | /**
75 | * @param array|int|float $data
76 | */
77 | abstract protected function getShape($data, array $shape): array;
78 | }
79 |
--------------------------------------------------------------------------------
/src/SciPhp/NdArray/BasicTrait.php:
--------------------------------------------------------------------------------
1 | data);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/SciPhp/NdArray/Decorator.php:
--------------------------------------------------------------------------------
1 | -?\d+)?\s*
22 | (?P
:) ?\s*
23 | (?P-?\d+) ?\s*
24 | (?P,) ?\s*/ix
25 | ';
26 | }
27 |
--------------------------------------------------------------------------------
/src/SciPhp/NdArray/ShapeTrait.php:
--------------------------------------------------------------------------------
1 | data,
28 | static function ($item) use (&$stack): void {
29 | $stack[] = $item;
30 | }
31 | );
32 |
33 | return np::ar($stack);
34 | }
35 |
36 | /**
37 | * Resize array
38 | *
39 | * @link http://sciphp.org/ndarray.resize Documentation
40 | * @api
41 | */
42 | final public function resize(): NdArray
43 | {
44 | $iterator = new RecursiveIteratorIterator(
45 | new RecursiveArrayIterator($this->data),
46 | RecursiveIteratorIterator::LEAVES_ONLY
47 | );
48 |
49 | $func = function (&$item) use (&$iterator): void {
50 | $item = $this->iterate($iterator);
51 | };
52 |
53 | return np::nulls(np::parseArgs(func_get_args()))
54 | ->walk_recursive($func);
55 | }
56 |
57 | /**
58 | * Reshapes data
59 | *
60 | * @throws \InvalidArgumentException
61 | * if shape parameter has a different size
62 | * @link http://sciphp.org/ndarray.reshape Documentation
63 | * @api
64 | */
65 | final public function reshape(): NdArray
66 | {
67 | $args = np::parseArgs(func_get_args());
68 |
69 | Assert::eq(array_product($args), $this->size);
70 |
71 | $data = $this->ravel()->data;
72 |
73 | while (($num = array_pop($args)) && count($args)) {
74 | $data = array_chunk($data, $num);
75 | }
76 |
77 | return np::ar($data);
78 | }
79 |
80 | /**
81 | * Gets the dimensions of the array
82 | *
83 | * @param int|float|array $data Axis
84 | * @param array $shape
85 | */
86 | final protected function getShape($data, array $shape): array
87 | {
88 | if (! \is_array($data) || ! count($data)) {
89 | return $shape;
90 | }
91 |
92 | $shape[] = count($data);
93 |
94 | return $this->getShape($data[0], $shape);
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/src/SciPhp/NdArray/VisitorTrait.php:
--------------------------------------------------------------------------------
1 | data, $func);
23 |
24 | return $this;
25 | }
26 |
27 | /**
28 | * Walk on last dimension
29 | */
30 | final public function walk_recursive(callable $func): NdArray
31 | {
32 | array_walk_recursive($this->data, $func);
33 |
34 | return $this;
35 | }
36 |
37 | /**
38 | * Iterate on next value
39 | *
40 | * @return int|float
41 | */
42 | final public function iterate(RecursiveIteratorIterator &$iterator)
43 | {
44 | while ($iterator->valid()) {
45 | if (! is_array($iterator->current())) {
46 | $value = $iterator->current();
47 |
48 | $key = $iterator->key();
49 |
50 | $iterator->next();
51 | // At first iteration on 1 dim array,
52 | // key is not incremented
53 | if ($key === $iterator->key()) {
54 | $iterator->next();
55 | }
56 |
57 | return $value;
58 | }
59 |
60 | $iterator->next();
61 | }
62 |
63 | $iterator->rewind();
64 |
65 | return $this->iterate($iterator);
66 | }
67 |
68 | /**
69 | * Execute axis operations and return an aggregate
70 | *
71 | * @param int|float $number
72 | * @return int|NdArray
73 | */
74 | final public function axis(callable $func, $number = null, bool $keepdims = false)
75 | {
76 | if (! \is_null($number)) {
77 | Assert::integer(
78 | $number,
79 | 'Axis number must be an integer. Given: %s'
80 | );
81 |
82 | Assert::greaterThanEq(
83 | $number,
84 | 0,
85 | 'Axis number must be greater than 0. Given: %s'
86 | );
87 |
88 | Assert::lessThan(
89 | $number,
90 | $this->ndim,
91 | 'Axis number must be lower than '
92 | . ($this->ndim - 1)
93 | . 'Given: %s'
94 | );
95 |
96 | Assert::lessThanEq(
97 | $this->ndim,
98 | 2,
99 | 'Axis implementation does not support dimension > 2. Given: %s'
100 | );
101 | }
102 |
103 | $fn = function (&$value, $key) use ($func, $number): void {
104 | $index = $number === 0
105 | ? ": , {$key}"
106 | : "{$key}, :";
107 |
108 | $value = $func($this->offsetGet($index)->data);
109 | };
110 |
111 | // keepdims handling
112 | $targetShape = $this->shape;
113 | if ($keepdims) {
114 | $targetShape[$number] = 1;
115 | } else {
116 | unset($targetShape[$number]);
117 | }
118 |
119 | // No axis number
120 | if (\is_null($number)) {
121 | if ($keepdims) {
122 | $targetShape = array_fill(0, $this->ndim, 1);
123 | return np::full($targetShape, $func($this->data));
124 | }
125 |
126 | return $func($this->data);
127 | }
128 |
129 | return np::zeros($this->shape[$this->ndim - 1 - $number])
130 | ->walk($fn)
131 | ->reshape(...$targetShape);
132 | }
133 | }
134 |
--------------------------------------------------------------------------------
/src/SciPhp/NumPhp.php:
--------------------------------------------------------------------------------
1 | $args
34 | * @api
35 | */
36 | public static function parseArgs(array $args): array
37 | {
38 | if (isset($args[0]) && \is_array($args[0])) {
39 | Assert::oneOf(
40 | self::ar($args[0])->ndim,
41 | [0, 1],
42 | Message::ARG_NOT_ARRAY_TUPLE
43 | );
44 |
45 | Assert::allNumeric($args[0]);
46 |
47 | return $args[0];
48 | }
49 |
50 | Assert::allNumeric($args);
51 |
52 | return $args;
53 | }
54 |
55 | /**
56 | * Transform a PHP array in a NdArray
57 | *
58 | * @param array|NdArray $m
59 | */
60 | public static function transform(&$m, bool $required = false): void
61 | {
62 | if (\is_array($m)) {
63 | $m = self::ar($m);
64 | }
65 |
66 | if ($required) {
67 | Assert::isInstanceof($m, NdArray::class);
68 | }
69 | }
70 |
71 | /**
72 | * Check that all values are numeric
73 | *
74 | * @api
75 | */
76 | public static function allNumeric(): bool
77 | {
78 | return ! count(
79 | array_filter(
80 | func_get_args(),
81 | static function ($value): bool {
82 | return ! is_numeric($value);
83 | }
84 | )
85 | );
86 | }
87 | }
88 |
--------------------------------------------------------------------------------
/src/SciPhp/NumPhp/Decorator.php:
--------------------------------------------------------------------------------
1 | false,
24 | * 'delimiter' => ';'
25 | * ]
26 | *
27 | * @link http://sciphp.org/numphp.loadtxt Documentation
28 | *
29 | * @api
30 | */
31 | final public static function loadtxt(string $file, array $options = []): NdArray
32 | {
33 | Assert::string($file);
34 | Assert::file($file = realpath($file));
35 |
36 | $m = [];
37 | $options = array_merge(
38 | ['headers' => false, 'delimiter' => ';'],
39 | $options
40 | );
41 |
42 | $handle = fopen($file, 'r');
43 | if ($handle !== false) {
44 | $row = 0;
45 | $num = 0;
46 |
47 | while (($data = fgetcsv($handle, 2048, $options['delimiter'])) !== false) {
48 | if ($row === 0) {
49 | $num = count($data);
50 | }
51 |
52 | Assert::eq($num, count($data));
53 |
54 | if ($options['headers'] && $row === 0) {
55 | // skip headers
56 | $options['headers'] = false;
57 | } else {
58 | $m[] = $data;
59 | }
60 |
61 | $row++;
62 | }
63 |
64 | fclose($handle);
65 | }
66 |
67 | return static::ar($m);
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/SciPhp/NumPhp/FloatTrait.php:
--------------------------------------------------------------------------------
1 | copy()->walk_recursive($func);
40 | }
41 |
42 | /**
43 | * Change the sign of m-element to that of n-element, element-wise.
44 | *
45 | * @param \SciPhp\NdArray|array|int|float $m
46 | * @param \SciPhp\NdArray|array|int|float $n
47 | * @return \SciPhp\NdArray|int|float
48 | * @link http://sciphp.org/numphp.copysign Documentation
49 | * @api
50 | */
51 | final public static function copysign($m, $n)
52 | {
53 | if (static::allNumeric($m, $n)) {
54 | return np::signbit($m) === np::signbit($n)
55 | ? $m : -$m;
56 | }
57 |
58 | static::transform($m);
59 | static::transform($n);
60 |
61 | // array / lamba
62 | if (is_numeric($n) && $m instanceof NdArray) {
63 | return $m->copysign(static::full_like($m, $n));
64 | // return np::copysign($n, $m); //$m->copy()->copysign($n);
65 | }
66 |
67 | // lamba / array
68 | if (is_numeric($m) && $n instanceof NdArray) {
69 | return static::full_like($n, $m)->copysign($n);
70 | }
71 |
72 | // array / array
73 | Assert::isInstanceof($m, NdArray::class);
74 | Assert::isInstanceof($n, NdArray::class);
75 |
76 | // n & m are vectors:
77 | if (count($m->shape) === 1 && $m->ndim === $n->ndim) {
78 | Assert::eq($m->shape, $n->shape, Message::MAT_NOT_ALIGNED);
79 | }
80 |
81 | // n is a vector
82 | elseif (!isset($n->shape[1])) {
83 | Assert::eq($m->shape[1], $n->shape[0], Message::MAT_NOT_ALIGNED);
84 | }
85 |
86 | // m is a vector
87 | elseif (!isset($m->shape[1])) {
88 | Assert::eq($m->shape[0], $n->shape[1], Message::MAT_NOT_ALIGNED);
89 |
90 | $m = $m->resize($n->shape);
91 | }
92 |
93 | // array / array
94 | elseif ($m->ndim === $n->ndim) {
95 | Assert::eq($m->shape, $n->shape, Message::MAT_NOT_ALIGNED);
96 | }
97 |
98 | $iterator = new RecursiveIteratorIterator(
99 | new RecursiveArrayIterator($n->data),
100 | RecursiveIteratorIterator::LEAVES_ONLY
101 | );
102 |
103 | $func = static function(&$item) use (&$iterator, $n): void {
104 | if (np::signbit($item) !== np::signbit($n->iterate($iterator))) {
105 | $item = -$item;
106 | }
107 | };
108 |
109 | return $m->copy()->walk_recursive($func);
110 | }
111 | }
112 |
--------------------------------------------------------------------------------
/src/SciPhp/NumPhp/LogarithmTrait.php:
--------------------------------------------------------------------------------
1 | copy()->walk_recursive($func);
38 | }
39 |
40 | /**
41 | * Base 10 logarithm, element-wise.
42 | *
43 | * @param \SciPhp\NdArray|array|int|float $m
44 | * @return \SciPhp\NdArray|int|float
45 | * @link http://sciphp.org/numphp.log10 Documentation
46 | * @api
47 | */
48 | final public static function log10($m)
49 | {
50 | return self::log($m, 10);
51 | }
52 |
53 | /**
54 | * Base 2 logarithm, element-wise.
55 | *
56 | * @param \SciPhp\NdArray|array|int|float $m
57 | * @return \SciPhp\NdArray|int|float
58 | * @link http://sciphp.org/numphp.log2 Documentation
59 | * @api
60 | */
61 | final public static function log2($m)
62 | {
63 | return self::log($m, 2);
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/src/SciPhp/NumPhp/Mathematical/TrigonometricTrait.php:
--------------------------------------------------------------------------------
1 | copy()->walk_recursive($func);
34 | }
35 |
36 | /**
37 | * Trigonometric sine, element-wise.
38 | *
39 | * @param \SciPhp\NdArray|array|int|float $m
40 | * @return \SciPhp\NdArray|int|float
41 | * @link http://sciphp.org/numphp.sin Documentation
42 | * @since 0.4.0
43 | * @api
44 | */
45 | final public static function sin($m)
46 | {
47 | if (\is_numeric($m)) {
48 | return sin($m);
49 | }
50 |
51 | static::transform($m, true);
52 |
53 | $func = static function (&$element): void {
54 | $element = sin($element);
55 | };
56 |
57 | return $m->copy()->walk_recursive($func);
58 | }
59 |
60 | /**
61 | * Compute tangent element-wise.
62 | *
63 | * Equivalent to np::sin(x)->divide(np::cos(x)) element-wise.
64 | *
65 | * @param \SciPhp\NdArray|array|int|float $m
66 | * @return \SciPhp\NdArray|int|float
67 | * @link http://sciphp.org/numphp.tan Documentation
68 | * @since 0.5.0
69 | * @api
70 | */
71 | final public static function tan($m)
72 | {
73 | if (\is_numeric($m)) {
74 | return tan($m);
75 | }
76 |
77 | static::transform($m, true);
78 |
79 | $func = static function (&$element): void {
80 | $element = tan($element);
81 | };
82 |
83 | return $m->copy()->walk_recursive($func);
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/src/SciPhp/NumPhp/MatrixTrait.php:
--------------------------------------------------------------------------------
1 | ndim === 2
25 | && $m->shape[0] === $m->shape[1];
26 | }
27 |
28 | /**
29 | * Numerical negative, element-wise.
30 | *
31 | * @param \SciPhp\NdArray|array|int|float $m
32 | * @return \SciPhp\NdArray|int|float
33 | * @link http://sciphp.org/numphp.negative Documentation
34 | * @api
35 | */
36 | final public static function negative($m)
37 | {
38 | if (is_numeric($m)) {
39 | return -$m;
40 | }
41 |
42 | static::transform($m, true);
43 |
44 | return $m->dot(-1);
45 | }
46 |
47 | /**
48 | * Permute the dimensions of an array.
49 | *
50 | * @param array|\SciPhp\NdArray $m
51 | * @todo Implement axis permutation for ndim > 2
52 | * @throws \InvalidArgumentException
53 | * @link http://sciphp.org/numphp.transpose Documentation
54 | * @api
55 | */
56 | final public static function transpose($m): NdArray
57 | {
58 | static::transform($m, true);
59 |
60 | Assert::oneOf($m->ndim, [0, 1, 2],
61 | __METHOD__ . '() does not support dimension greater than 2'
62 | );
63 |
64 | if (in_array($m->ndim, [0, 1])) {
65 | return $m;
66 | }
67 |
68 | return static::ar(
69 | array_map(
70 | static function() use ($m, &$row): array {
71 | return array_column($m->data, $row++);
72 | },
73 | $m->data[$row = 0]
74 | )
75 | );
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/src/SciPhp/NumPhp/OperationTrait.php:
--------------------------------------------------------------------------------
1 | axis($func, $axis, $keepdims);
36 | }
37 |
38 | /**
39 | * Integrate along the given axis using the composite trapezoidal rule.
40 | *
41 | * @param \SciPhp\NdArray|array $m
42 | * @return int|float|array
43 | * @link http://sciphp.org/numphp.trapz Documentation
44 | * @todo implement dx, x options parameters
45 | * @api
46 | */
47 | final public static function trapz($m)
48 | {
49 | static::transform($m, true);
50 |
51 | Assert::eq(1, $m->ndim);
52 |
53 | // dx = 1
54 | $func = static function ($value, $key) use (& $prev): float {
55 | if ($key === 0) {
56 | $prev = $value;
57 |
58 | return 0.0;
59 | }
60 |
61 | $sum = ($value + $prev) / 2;
62 |
63 | $prev = $value;
64 |
65 | return floatval($sum);
66 | };
67 |
68 | return array_sum(
69 | array_map(
70 | $func,
71 | $m->data,
72 | array_keys($m->data)
73 | )
74 | );
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/SciPhp/NumPhp/VanderTrait.php:
--------------------------------------------------------------------------------
1 | data)
28 | : $num;
29 |
30 | Assert::integer($num);
31 | Assert::greaterThan($num, 0);
32 | Assert::eq($matrix->ndim, 1, __METHOD__ . '() only accepts vectors.');
33 |
34 | return static::ar(
35 | array_map(
36 | self::itemVander($num),
37 | static::ones(count($matrix->data), $num)->data,
38 | $matrix->data
39 | )
40 | );
41 | }
42 |
43 | /**
44 | * Apply decreasing power on each row values
45 | *
46 | * @param int $num Number of wanted columns
47 | */
48 | final protected static function itemVander(int $num): callable
49 | {
50 | return static function ($row, $value) use ($num): array {
51 | return array_map(
52 | static function($key) use ($value, $num) {
53 | return pow($value, $num - $key - 1);
54 | },
55 | array_keys($row)
56 | );
57 | };
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/SciPhp/Random.php:
--------------------------------------------------------------------------------
1 | $args Shape of the destination matrix
23 | * if nothing is passed, return a random float
24 | * @return \SciPhp\NdArray|float
25 | *
26 | * @link http://sciphp.org/random.randn
27 | * Documentation for randn()
28 | *
29 | * @since 0.5.0
30 | * @api
31 | */
32 | final public function randn()
33 | {
34 | if (func_num_args() === 0) {
35 | return $this->nrand(0, 1);
36 | }
37 |
38 | $args = np::parseArgs(func_get_args());
39 |
40 | $func = function (&$element): void {
41 | $element = $this->nrand(0, 1);
42 | };
43 |
44 | return np::nulls($args)->walk_recursive($func);
45 | }
46 |
47 | /**
48 | * Random values in a given shape.
49 | *
50 | * @param array $args Shape of the destination matrix
51 | *
52 | * @link http://sciphp.org/random.rand
53 | * Documentation for rand()
54 | *
55 | * @since 0.5.0
56 | * @api
57 | */
58 | final public function rand(): NdArray
59 | {
60 | Assert::greaterThan(
61 | func_num_args(),
62 | 0,
63 | 'Method rand() must have at least one parameter. Got: %s'
64 | );
65 |
66 | $args = np::parseArgs(func_get_args());
67 |
68 | $func = function (&$element): void {
69 | $element = $this->randomFloat();
70 | };
71 |
72 | return np::nulls($args)->walk_recursive($func);
73 | }
74 |
75 | /**
76 | * Return random integers from low (inclusive) to high (exclusive).
77 | *
78 | * @param int|array $size Shape of the output matrix
79 | * @return \SciPhp\NdArray|int
80 | *
81 | * @link http://sciphp.org/random.randint
82 | * Documentation for randint()
83 | *
84 | * @since 0.5.0
85 | * @api
86 | */
87 | final public function randint(int $low, ?int $high = null, $size = null)
88 | {
89 | $min = is_null($high) ? 0 : $low;
90 | $max = is_null($high) ? $low : $high;
91 |
92 | Assert::greaterThan(
93 | $max,
94 | $min
95 | );
96 |
97 | $range = np::arange($min, $max)->data;
98 |
99 | if (is_null($size)) {
100 | return $range[ array_rand($range) ];
101 | }
102 |
103 | $func = static function (&$element) use ($range): void {
104 | $element = $range[ array_rand($range) ];
105 | };
106 |
107 | return np::nulls($size)->walk_recursive($func);
108 | }
109 |
110 | private function randomFloat(): float
111 | {
112 | return mt_rand() / mt_getrandmax();
113 | }
114 |
115 | /**
116 | * @param float|int $mean
117 | * @param float|int $sd
118 | */
119 | private function nrand($mean, $sd): float
120 | {
121 | $x = $this->randomFloat();
122 | $y = $this->randomFloat();
123 |
124 | return sqrt(-2 * log($x)) * cos(2 * pi() * $y) * $sd + $mean;
125 | }
126 | }
127 |
--------------------------------------------------------------------------------
/test/Benchmark/bench.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash -e
2 |
3 | set -o nounset
4 |
5 | #----------------------------------------------------------------------#
6 | # This script runs small PHP and Python scripts to make a comparison
7 | # between NumPy and NumPhp. At the end, it prints duration for each
8 | # script and a global counter.
9 | #
10 | # Usage
11 | # -----
12 | #
13 | # ./bench.sh
14 | # Executes all scripts
15 | #
16 | # ./bench.sh ndarray
17 | # Executes all scripts whose filename contains "ndarray"
18 | #----------------------------------------------------------------------#
19 |
20 | cd "$(dirname "$0")"
21 | echo "" > "output.txt"
22 |
23 | if [[ -z "${1+x}" ]]; then
24 | NP_FILTER=""
25 | else
26 | NP_FILTER="$1"
27 | fi
28 |
29 | # PHP
30 | for SCRIPT in php/*${NP_FILTER}*
31 | do
32 | if [ -f "$SCRIPT" ]
33 | then
34 | d0=$(date +%s%6N)
35 | php "$SCRIPT" > "/dev/null"
36 | d1=$(date +%s%6N)
37 | printf "%s %'d ns\n" "$SCRIPT" "$(((d1-d0)))" >> "output.txt"
38 | printf "."
39 | fi
40 | done
41 |
42 | printf "\n"
43 |
44 | # Python
45 | for SCRIPT in python/*${NP_FILTER}*
46 | do
47 | if [ -f "$SCRIPT" ]
48 | then
49 | d0=$(date +%s%6N)
50 | python3 "$SCRIPT" > "/dev/null"
51 | d1=$(date +%s%6N)
52 | printf "%s %'d ns\n" "$SCRIPT" "$(((d1-d0)))" >> "output.txt"
53 | printf "."
54 | fi
55 | done
56 |
57 |
58 | # merge times
59 | php_time=$(column -t output.txt | grep php | awk '{print $2}' \
60 | | tr -d ",| " | awk '{ sum += $1 } END { print sum }')
61 | pyt_time=$(column -t output.txt | grep python | awk '{print $2}' \
62 | | tr -d ",| " | awk '{ sum += $1 } END { print sum }')
63 |
64 | # List all tests results
65 | if [ ! -z "$php_time" ] || [ ! -z "$pyt_time" ]
66 | then
67 | # output
68 | printf "\n\n"
69 | column -t output.txt
70 | fi
71 |
72 | # Summary benchmark
73 | if [ ! -z "$php_time" ] && [ ! -z "$pyt_time" ]
74 | then
75 | # calc ratio
76 | php_prct=$(awk "BEGIN {printf \"%.2f\", \
77 | 100 * $php_time / ($php_time + $pyt_time)}")
78 | pyt_prct=$(awk "BEGIN {printf \"%.2f\", \
79 | 100 * $pyt_time / ($php_time + $pyt_time)}")
80 |
81 | # NumPhp & NumPy comparison
82 | printf "\n\n"
83 | ( printf "Global %s %s \n" "NumPhp" "NumPy" ; \
84 | printf "(ns) %'d %'d\n" "$php_time" "$pyt_time" ; \
85 | printf "(%%) %s %s \n" "$php_prct" "$pyt_prct" ) \
86 | | column -t
87 | fi
88 |
89 | printf "\n"
90 |
91 | # garbage
92 | rm output.txt
93 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.T:
--------------------------------------------------------------------------------
1 | reshape(10, 50);
8 |
9 | for ($i = 0; $i < 100; $i++)
10 | {
11 | $m = $m->T;
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.copy:
--------------------------------------------------------------------------------
1 | reshape(10, 50);
8 |
9 | for ($i = 0; $i < 100; $i++)
10 | {
11 | $m = $m->copy();
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.copysign:
--------------------------------------------------------------------------------
1 | copysign($n);
13 | }
14 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.cos:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0; $i < 100; $i++)
10 | $m = $m->cos();
11 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.data:
--------------------------------------------------------------------------------
1 | resize(10, 50);
8 |
9 | for ($i = 0; $i < 100; $i++)
10 | {
11 | $m = np::ar($m->data);
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.divide:
--------------------------------------------------------------------------------
1 | reshape(10, 50);
8 |
9 | for ($i = 1; $i < 50; $i++)
10 | {
11 | $m = $m->divide($i);
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.exp:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0; $i < 50; $i++)
10 | {
11 | $n = $m->exp();
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.exp2:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0; $i < 50; $i++)
10 | {
11 | $n = $m->exp2();
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.expm1:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0; $i < 50; $i++)
10 | {
11 | $n = $m->expm1();
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.indexing:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0; $i < 50; $i++)
10 | {
11 | $d = $m['2:9, 2:4'];
12 |
13 | $m['2:9, 2:4'] = $i;
14 | }
15 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.log:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0; $i < 50; $i++)
10 | {
11 | $n = $m->log();
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.log10:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0; $i < 50; $i++)
10 | {
11 | $n = $m->log10();
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.log2:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0; $i < 50; $i++)
10 | {
11 | $n = $m->log2();
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.multiply:
--------------------------------------------------------------------------------
1 | reshape(10, 50);
8 |
9 | for ($i = 1; $i < 50; $i++)
10 | {
11 | $m = $m->multiply($i);
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.ndim:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0; $i < 100; $i++)
10 | {
11 | $d = $m->ndim;
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.negative:
--------------------------------------------------------------------------------
1 | reshape(10, 50);
8 |
9 | for ($i = 0; $i < 50; $i++)
10 | {
11 | $m = $m->negative();
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.power:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0.1; $i < 5; $i+=0.1)
10 | {
11 | $m->power($i);
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.ravel:
--------------------------------------------------------------------------------
1 | ravel();
12 |
13 | for ($k = 1; $k < 10; $k++)
14 | {
15 | $v = np::zeros($i, $j, $k)->ravel();
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.reciprocal:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0; $i < 50; $i++)
10 | {
11 | $m = $m->reciprocal();
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.reshape:
--------------------------------------------------------------------------------
1 | reshape($j, $i);
12 |
13 | for ($k = 1; $k < 10; $k++)
14 | {
15 | $v = np::ones($i, $j, $k)->reshape($k, $i, $j);
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.resize:
--------------------------------------------------------------------------------
1 | resize($i, $i + $j);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.shape:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0; $i < 100; $i++)
10 | {
11 | $d = $m->shape;
12 |
13 | $m->shape = [
14 | ($m->shape[0] == 10 ? 5 : 10),
15 | ($m->shape[1] == 10 ? 5 : 10)
16 | ];
17 | }
18 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.signbit:
--------------------------------------------------------------------------------
1 | signbit();
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.sin:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0; $i < 100; $i++)
10 | $m = $m->sin();
11 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.size:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0; $i < 100; $i++)
10 | {
11 | $d = $m->size;
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.sqrt:
--------------------------------------------------------------------------------
1 | reshape(5, 5);
8 |
9 | for ($i = 0.1; $i < 5; $i+=0.1)
10 | $m = $m->sqrt();
11 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.square:
--------------------------------------------------------------------------------
1 | reshape(5, 5);
8 |
9 | for ($i = 0.1; $i < 5; $i+=0.1) {
10 | $m = $m->square();
11 | }
12 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.subtract:
--------------------------------------------------------------------------------
1 | reshape(10, 50);
8 | $n = np::linspace(1, 500, 500)->reshape(10, 50);
9 |
10 | for ($i = 0; $i < 5; $i++)
11 | {
12 | $m = $m->subtract($n);
13 | }
14 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.sum:
--------------------------------------------------------------------------------
1 | reshape(2, 10);
8 |
9 | $s = $m->sum();
10 | $s = $m->sum(0);
11 | $s = $m->sum(1);
12 | $s = $m->sum(0, true);
13 | $s = $m->sum(1, true);
14 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.trace:
--------------------------------------------------------------------------------
1 | reshape(5, 5);
8 |
9 | for ($i = 0; $i < 50; $i++)
10 | {
11 | $r = $m->trace();
12 | $r = $m->trace(-1);
13 | $r = $m->trace(1);
14 | }
15 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.trapz:
--------------------------------------------------------------------------------
1 | trapz();
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/ndarray.vander:
--------------------------------------------------------------------------------
1 | vander();
14 | $m1 = $m->vander($j);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.add:
--------------------------------------------------------------------------------
1 | reshape(10, 50);
8 | $n = np::linspace(1, 500, 500)->reshape(10, 50);
9 |
10 | for ($i = 0; $i < 5; $i++)
11 | {
12 | $p = np::add($m, $n);
13 | }
14 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.arange:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0; $i < 100; $i++)
10 | $m = np::cos($m);
11 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.diag:
--------------------------------------------------------------------------------
1 | reshape(10, 50);
8 |
9 | for ($i = 1; $i < 50; $i++)
10 | {
11 | $m = np::divide($m, $i);
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.dot:
--------------------------------------------------------------------------------
1 | reshape(10, 50);
8 | $n = np::linspace(1, 500, 500)->reshape(50, 10);
9 |
10 | for ($i = 0; $i < 5; $i++)
11 | {
12 | $p = np::dot($m, $n);
13 | }
14 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.empty:
--------------------------------------------------------------------------------
1 | reshape(1, 10);
8 | $m100 = np::linspace(0, 10, 100) ->reshape(10, 10);
9 | $m1000= np::linspace(0, 10, 1000) ->reshape(100, 10);
10 |
11 | for ($i = 0; $i < 100; $i++)
12 | {
13 | $v = np::nulls_like($m10);
14 | $v = np::nulls_like($m100);
15 | $v = np::nulls_like($m1000);
16 | }
17 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.exp:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0; $i < 50; $i++)
10 | {
11 | $n = np::exp($m);
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.exp2:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0; $i < 50; $i++)
10 | {
11 | $n = np::exp2($m);
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.expm1:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0; $i < 50; $i++)
10 | {
11 | $n = np::expm1($m);
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.eye:
--------------------------------------------------------------------------------
1 | reshape(1, 10);
8 | $m100 = np::linspace(0, 10, 100) ->reshape(10, 10);
9 | $m1000= np::linspace(0, 10, 1000) ->reshape(100, 10);
10 |
11 | for ($i = 0; $i < 100; $i++)
12 | {
13 | $v = np::full_like($m10, 42);
14 | $v = np::full_like($m100, 42);
15 | $v = np::full_like($m1000, 42);
16 | }
17 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.identity:
--------------------------------------------------------------------------------
1 | cholesky($m);
15 |
16 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.linalg.norm:
--------------------------------------------------------------------------------
1 | reshape(5, 10);
8 |
9 | for ($i = 0; $i < 100; $i++)
10 | $r = np::linalg()->norm($m);
11 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.linspace:
--------------------------------------------------------------------------------
1 | true]);
10 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.log:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0; $i < 50; $i++)
10 | {
11 | $n = np::log($m);
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.log10:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0; $i < 50; $i++)
10 | {
11 | $n = np::log10($m);
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.log2:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0; $i < 50; $i++)
10 | {
11 | $n = np::log2($m);
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.logspace:
--------------------------------------------------------------------------------
1 | reshape(10, 50);
8 |
9 | for ($i = 1; $i < 50; $i++)
10 | {
11 | $m = np::multiply($m, $i);
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.negative:
--------------------------------------------------------------------------------
1 | reshape(10, 50);
8 |
9 | for ($i = 0; $i < 50; $i++)
10 | {
11 | $m = np::negative($m);
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.ones:
--------------------------------------------------------------------------------
1 | reshape(1, 10);
8 | $m100 = np::linspace(0, 10, 100) ->reshape(10, 10);
9 | $m1000= np::linspace(0, 10, 1000) ->reshape(100, 10);
10 |
11 | for ($i = 0; $i < 100; $i++)
12 | {
13 | $v = np::ones_like($m10);
14 | $v = np::ones_like($m100);
15 | $v = np::ones_like($m1000);
16 | }
17 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.power:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0.1; $i < 5; $i += 0.1) {
10 | np::power($m, $i);
11 | }
12 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.random.rand:
--------------------------------------------------------------------------------
1 | rand(5, 5, $i + 1);
9 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.random.randint:
--------------------------------------------------------------------------------
1 | randint($i + 1);
9 | $r = np::random()->randint($i, $i + 5);
10 | $r = np::random()->randint($i, $i + 5, $i + 5);
11 | $r = np::random()->randint($i, $i + 5, [$i + 5, $i + 10]);
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.random.randn:
--------------------------------------------------------------------------------
1 | randn([5, 5, $i + 1]);
9 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.reciprocal:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0; $i < 50; $i++)
10 | {
11 | $m = np::reciprocal($m);
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.signbit:
--------------------------------------------------------------------------------
1 | reshape(10, 5);
8 |
9 | for ($i = 0; $i < 100; $i++)
10 | $m = np::sin($m);
11 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.sqrt:
--------------------------------------------------------------------------------
1 | reshape(5, 5);
8 |
9 | for ($i = 0.1; $i < 5; $i += 0.1) {
10 | $m = np::sqrt($m);
11 | }
12 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.square:
--------------------------------------------------------------------------------
1 | reshape(5, 5);
8 |
9 | for ($i = 0.1; $i < 5; $i += 0.1) {
10 | $m = np::square($m);
11 | }
12 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.subtract:
--------------------------------------------------------------------------------
1 | reshape(10, 50);
8 | $n = np::linspace(1, 500, 500)->reshape(10, 50);
9 |
10 | for ($i = 0; $i < 5; $i++)
11 | {
12 | $p = np::subtract($m, $n);
13 | }
14 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.sum:
--------------------------------------------------------------------------------
1 | reshape(2, 10);
8 |
9 | $s = np::sum($m);
10 | $s = np::sum($m, 0);
11 | $s = np::sum($m, 1);
12 | $s = np::sum($m, 0, true);
13 | $s = np::sum($m, 1, true);
14 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.trace:
--------------------------------------------------------------------------------
1 | reshape(5, 5);
8 |
9 | for ($i = 0; $i < 50; $i++)
10 | {
11 | $r = np::trace($m);
12 | $r = np::trace($m, -1);
13 | $r = np::trace($m, 1);
14 | }
15 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.transpose:
--------------------------------------------------------------------------------
1 | reshape(10, 50);
8 |
9 | for ($i = 0; $i < 100; $i++)
10 | {
11 | $m = np::transpose($m);
12 | }
13 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.trapz:
--------------------------------------------------------------------------------
1 | reshape(15, 15);
8 |
9 |
10 | for ($i = 0; $i < 15; $i++)
11 | {
12 | for ($j = -15; $j < 16; $j++)
13 | {
14 | $m1 = np::tril($m, $j);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.triu:
--------------------------------------------------------------------------------
1 | reshape(15, 15);
8 |
9 |
10 | for ($i = 0; $i < 15; $i++)
11 | {
12 | for ($j = -15; $j < 16; $j++)
13 | {
14 | $m1 = np::triu($m, $j);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/test/Benchmark/php/np.vander:
--------------------------------------------------------------------------------
1 | reshape(1, 10);
8 | $m100 = np::linspace(0, 10, 100) ->reshape(10, 10);
9 | $m1000= np::linspace(0, 10, 1000) ->reshape(100, 10);
10 |
11 | for ($i = 0; $i < 100; $i++)
12 | {
13 | $v = np::zeros_like($m10);
14 | $v = np::zeros_like($m100);
15 | $v = np::zeros_like($m1000);
16 | }
17 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.T:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 500, 500).reshape(10, 50)
5 |
6 | for i in range(0, 100):
7 | m = m.T
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.copy:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50, 500).reshape(10, 50)
5 |
6 | for i in range(0, 100):
7 | m = m.copy()
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.copysign:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(-25, 25)
5 | n = np.linspace(25, -25)
6 |
7 | for i in range(0, 50):
8 | r = np.copysign(m, n)
9 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.cos:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50, 50).reshape(10, 5)
5 |
6 | for i in range(0, 100):
7 | m = np.cos(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.data:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50).reshape(10, 5)
5 |
6 | for i in range(0, 100):
7 | m = np.array(m.data)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.divide:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 500, 500).reshape(10, 50)
5 |
6 | for i in range(1, 50):
7 | m = m / i
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.exp:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50).reshape(10, 5)
5 |
6 | for i in range(0, 50):
7 | n = np.exp(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.exp2:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50).reshape(10, 5)
5 |
6 | for i in range(0, 50):
7 | n = np.exp2(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.expm1:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50).reshape(10, 5)
5 |
6 | for i in range(0, 50):
7 | n = np.expm1(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.indexing:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50).reshape(10, 5)
5 |
6 | for i in range(0, 50):
7 | d = m[2:9, 2:4]
8 | m[2:9, 2:4] = i
9 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.log:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50).reshape(10, 5)
5 |
6 | for i in range(0, 50):
7 | n = np.log(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.log10:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50).reshape(10, 5)
5 |
6 | for i in range(0, 50):
7 | n = np.log10(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.log2:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50).reshape(10, 5)
5 |
6 | for i in range(0, 50):
7 | n = np.log2(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.multiply:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 500, 500).reshape(10, 50)
5 |
6 | for i in range(1, 50):
7 | m = m * i
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.ndim:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50).reshape(10, 5)
5 |
6 | for i in range(0, 100):
7 | d = m.ndim
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.negative:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 500, 500).reshape(10, 50)
5 |
6 | for i in range(0, 50):
7 | m = - m
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.power:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 |
5 | m = np.linspace(1, 50).reshape(10, 5)
6 |
7 | for i in np.arange(0.1, 5.1, 0.1):
8 |
9 | np.power(m, i)
10 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.ravel:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | for i in range(1, 5):
5 |
6 | for j in range(1, 10):
7 | v = np.zeros((i, j)).ravel()
8 |
9 | for k in range(1, 10):
10 | v = np.zeros((i, j, k)).ravel()
11 |
12 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.reciprocal:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50).reshape(10, 5)
5 |
6 | for i in range(0, 50):
7 | m = np.reciprocal(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.reshape:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | for i in range(1, 5):
5 |
6 | for j in range(1, 10):
7 | v = np.ones((i, j)).reshape(j, i)
8 |
9 | for k in range(1, 10):
10 | v = np.ones((i, j, k)).reshape(k, i, j)
11 |
12 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.resize:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 5, 5)
5 |
6 | for i in range(1, 10):
7 | for j in range(1, 10):
8 | v = np.resize(i, i + j)
9 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.shape:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50).reshape(10, 5)
5 |
6 | for i in range(0, 100):
7 | d = m.shape
8 | m.shape = (
9 | (5 if m.shape[0] == 10 else 10),
10 | (5 if m.shape[1] == 10 else 10)
11 | )
12 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.signbit:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(-25, 25)
5 |
6 | for i in range(0, 50):
7 | n = np.signbit(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.sin:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50, 50).reshape(10, 5)
5 |
6 | for i in range(0, 100):
7 | m = np.sin(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.size:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50).reshape(10, 5)
5 |
6 | for i in range(0, 100):
7 | d = m.size
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.sqrt:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 |
5 | m = np.linspace(0, 1, 25).reshape(5, 5)
6 |
7 | for i in np.arange(0.1, 5.1, 0.1):
8 |
9 | m = np.sqrt(m)
10 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.square:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 |
5 | m = np.linspace(0, 1, 25).reshape(5, 5)
6 |
7 | for i in np.arange(0.1, 5.1, 0.1):
8 |
9 | m = np.square(m)
10 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.subtract:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 500, 500).reshape(10, 50)
5 | n = np.linspace(1, 500, 500).reshape(10, 50)
6 |
7 | for i in range(0, 5):
8 | m = m - n
9 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.sum:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 20, 20).reshape(2, 10)
5 |
6 | s = m.sum()
7 | s = m.sum(axis = 0)
8 | s = m.sum(axis = 1)
9 | s = m.sum(axis = 0, keepdims=True)
10 | s = m.sum(axis = 1, keepdims=True)
11 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.trace:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(0, 25, 25).reshape(5, 5)
5 |
6 | for i in range(0, 50):
7 | r = np.trace(m)
8 | r = np.trace(m, -1)
9 | r = np.trace(m, 1)
10 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.trapz:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(-25, 25)
5 |
6 | for i in range(0, 100):
7 | r = np.trapz(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/ndarray.vander:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | for i in range(1, 10, 2):
5 | m = np.linspace(1, 10, i)
6 |
7 | for j in range(1, 20):
8 | m1 = np.vander(m)
9 | m1 = np.vander(m, j)
10 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.add:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 500, 500).reshape(10, 50)
5 | n = np.linspace(1, 500, 500).reshape(10, 50)
6 |
7 | for i in range(0, 5):
8 | p = np.add(m, n)
9 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.arange:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | for i in np.arange(1., 5.):
5 | for j in range(10, 100):
6 |
7 | # default step = 1
8 | v = np.arange(i, j)
9 |
10 | # specific step
11 | for k in range(1, 5):
12 | step = k / i
13 | np.arange(i, j, step)
14 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.copysign:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(-25, 25)
5 | n = np.linspace(25, -25)
6 |
7 | for i in range(0, 50):
8 | r = np.copysign(m, n)
9 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.cos:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50, 50).reshape(10, 5)
5 |
6 | for i in range(0, 100):
7 | m = np.cos(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.diag:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | for i in range(0, 10):
5 | for j in range(0, 10):
6 | for k in range(0, 3):
7 | v1 = np.diag((i, j, k))
8 | v2 = np.diag((i, j, k), 1)
9 | v3 = np.diag((i, j, k), -1)
10 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.diagflat:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | for i in range(0, 10):
5 | for j in range(1, 11):
6 | for k in range(0, 3):
7 | v1 = np.diagflat(
8 | np.linspace(k, i, j)
9 | )
10 | v2 = np.diagflat(
11 | np.linspace(k, i, j),
12 | 1
13 | )
14 | v3 = np.diagflat(
15 | np.linspace(k, i, j),
16 | -1
17 | )
18 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.diagonal:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.array([
5 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
6 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
7 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
8 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
9 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
10 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
11 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
12 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
13 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
14 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
15 | ])
16 |
17 | for i in range(1, 10):
18 | for j in range(1, 10):
19 | v1 = np.diagonal(m)
20 | v2 = np.diagonal(m, j)
21 | v3 = np.diagonal(m, -j)
22 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.divide:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 500, 500).reshape(10, 50)
5 |
6 | for i in range(1, 50):
7 | m = np.divide(m, i)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.dot:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 500, 500).reshape(10, 50)
5 | n = np.linspace(1, 500, 500).reshape(50, 10)
6 |
7 | for i in range(0, 5):
8 | p = np.dot(m, n)
9 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.empty:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | for i in range(1, 10):
5 | v = np.empty(i)
6 |
7 | for j in range(1, 10):
8 | v = np.empty((i, j))
9 |
10 | for k in range(1, 10):
11 | v = np.empty((i, j, k))
12 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.empty_like:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m10 = np.linspace(0, 10, 10) .reshape(1, 10)
5 | m100 = np.linspace(0, 10, 100) .reshape(10, 10)
6 | m1000= np.linspace(0, 10, 1000) .reshape(100, 10)
7 |
8 | for i in range(0, 100):
9 | v = np.empty_like(m10)
10 | v = np.empty_like(m100)
11 | v = np.empty_like(m1000)
12 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.exp:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50).reshape(10, 5)
5 |
6 | for i in range(0, 50):
7 | n = np.exp(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.exp2:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50).reshape(10, 5)
5 |
6 | for i in range(0, 50):
7 | n = np.exp2(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.expm1:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50).reshape(10, 5)
5 |
6 | for i in range(0, 50):
7 | n = np.expm1(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.eye:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | for i in range(1, 11):
5 | for j in range(1, 11):
6 | for k in range(-5, 5):
7 | m = np.eye(i, j, k)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.full:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | for i in range(1, 10):
5 | v = np.full(i, 42)
6 |
7 | for j in range(1, 10):
8 | v = np.full((i, j), 42)
9 |
10 | for k in range(1, 10):
11 | v = np.full((i, j, k), 42)
12 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.full_like:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m10 = np.linspace(0, 10, 10) .reshape(1, 10)
5 | m100 = np.linspace(0, 10, 100) .reshape(10, 10)
6 | m1000= np.linspace(0, 10, 1000) .reshape(100, 10)
7 |
8 | for i in range(0, 100):
9 | v = np.full_like(m10, 42)
10 | v = np.full_like(m100, 42)
11 | v = np.full_like(m1000, 42)
12 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.identity:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | for i in range(1, 100):
5 | m = np.identity(i)
6 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.linalg.cholesky:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = [
5 | [1 , 1 , 1 , 1 ],
6 | [1 , 5 , 5 , 5 ],
7 | [1 , 5 , 14 , 14 ],
8 | [1 , 5 , 14 , 15 ]
9 | ]
10 |
11 | np.linalg.cholesky(m)
12 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.linalg.norm:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(-25, 25).reshape( (5,10) )
5 |
6 | for i in range(0, 100):
7 | r = np.linalg.norm(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.linspace:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | for i in np.arange(1., 5.):
5 | for j in np.arange(10., 100.):
6 | start = j / i
7 |
8 | # default number of elements (50)
9 | v = np.linspace(start, j)
10 |
11 | # specified number of elements
12 | for k in range(1, 6):
13 | np.linspace(start, j, k)
14 | np.linspace(start, j, k, False) # no endpoint
15 | np.linspace(start, j, k, True, True) # step return + endpoint
16 | np.linspace(start, j, k, False, True)# step return - endpoint
17 |
18 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.loadtxt:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 | import os
4 |
5 | path = os.path.dirname(os.path.realpath(__file__)) + '/../../Resources/files/winequality-white.csv'
6 |
7 | r = np.loadtxt(path, skiprows = 1, delimiter = ';')
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.log:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50).reshape(10, 5)
5 |
6 | for i in range(0, 50):
7 | n = np.log(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.log10:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50).reshape(10, 5)
5 |
6 | for i in range(0, 50):
7 | n = np.log10(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.log2:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50).reshape(10, 5)
5 |
6 | for i in range(0, 50):
7 | n = np.log2(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.logspace:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | for i in np.arange(1., 5.):
5 | for j in np.arange(10., 20.):
6 | start = j / i
7 |
8 | # default number of elements (50)
9 | np.logspace(start, j)
10 |
11 | # specified number of elements
12 | for k in range(1, 6):
13 | np.logspace(start, j, k)
14 | np.logspace(start, j, k, False) # no endpoint
15 | np.logspace(start, j, k, True, 2) # base 2 + endpoint
16 | np.logspace(start, j, k, False, 5) # base 5 - endpoint
17 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.multiply:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 500, 500).reshape(10, 50)
5 |
6 | for i in range(1, 50):
7 | m = np.multiply(m, i)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.negative:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 500, 500).reshape(10, 50)
5 |
6 | for i in range(0, 50):
7 | m = np.negative(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.ones:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | for i in range(1, 10):
5 | v = np.ones(i)
6 |
7 | for j in range(1, 10):
8 | v = np.ones((i, j))
9 |
10 | for k in range(1, 10):
11 | v = np.ones((i, j, k))
12 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.ones_like:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m10 = np.linspace(0, 10, 10) .reshape(1, 10)
5 | m100 = np.linspace(0, 10, 100) .reshape(10, 10)
6 | m1000= np.linspace(0, 10, 1000) .reshape(100, 10)
7 |
8 | for i in range(0, 100):
9 | v = np.ones_like(m10)
10 | v = np.ones_like(m100)
11 | v = np.ones_like(m1000)
12 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.power:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 |
5 | m = np.linspace(1, 50).reshape(10, 5)
6 |
7 | for i in np.arange(0.1, 5.1, 0.1):
8 |
9 | np.power(m, i)
10 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.random.rand:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | for i in range(0, 50):
5 | r = np.random.rand(5, 5, i + 1)
6 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.random.randint:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | for i in range(0, 50):
5 | r = np.random.randint(i + 1);
6 | r = np.random.randint(i, i + 5);
7 | r = np.random.randint(i, i + 5, i + 5);
8 | r = np.random.randint(i, i + 5, (i + 5, i + 10));
9 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.random.randn:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | for i in range(0, 50):
5 | r = np.random.randn(5, 5, i + 1)
6 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.reciprocal:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50).reshape(10, 5)
5 |
6 | for i in range(0, 50):
7 | m = np.reciprocal(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.signbit:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(-25, 25)
5 |
6 | for i in range(0, 50):
7 | n = np.signbit(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.sin:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 50, 50).reshape(10, 5)
5 |
6 | for i in range(0, 100):
7 | m = np.sin(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.sqrt:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 |
5 | m = np.linspace(0, 1, 25).reshape(5, 5)
6 |
7 | for i in np.arange(0.1, 5.1, 0.1):
8 |
9 | m = np.sqrt(m)
10 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.square:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 |
5 | m = np.linspace(0, 1, 25).reshape(5, 5)
6 |
7 | for i in np.arange(0.1, 5.1, 0.1):
8 |
9 | m = np.square(m)
10 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.subtract:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 500, 500).reshape(10, 50)
5 | n = np.linspace(1, 500, 500).reshape(10, 50)
6 |
7 | for i in range(0, 5):
8 | p = np.subtract(m, n)
9 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.sum:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 20, 20).reshape(2, 10)
5 |
6 | s = np.sum(m)
7 | s = np.sum(m,axis = 0)
8 | s = np.sum(m,axis = 1)
9 | s = np.sum(m,axis = 0, keepdims=True)
10 | s = np.sum(m,axis = 1, keepdims=True)
11 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.trace:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(0, 25, 25).reshape(5, 5)
5 |
6 | for i in range(0, 50):
7 | r = np.trace(m)
8 | r = np.trace(m, -1)
9 | r = np.trace(m, 1)
10 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.transpose:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 500, 500).reshape(10, 50)
5 |
6 | for i in range(0, 100):
7 | m = np.transpose(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.trapz:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(-25, 25)
5 |
6 | for i in range(0, 100):
7 | r = np.trapz(m)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.tri:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | for i in range(1, 11):
5 | for j in range(1, 11):
6 | for k in range(-5, 5):
7 | m = np.tri(i, j, k)
8 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.tril:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 225, 225).reshape(15, 15);
5 |
6 | for i in range(0, 15):
7 | for j in range(-15, 16):
8 | m = np.tril(m, j)
9 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.triu:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m = np.linspace(1, 225, 225).reshape(15, 15);
5 |
6 | for i in range(0, 15):
7 | for j in range(-15, 16):
8 | m = np.triu(m, j)
9 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.vander:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | for i in range(1, 10, 2):
5 | m = np.linspace(1, 10, i)
6 |
7 | for j in range(1, 20):
8 | m1 = np.vander(m)
9 | m1 = np.vander(m, j)
10 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.zeros:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | for i in range(1, 10):
5 | v = np.zeros(i)
6 |
7 | for j in range(1, 10):
8 | v = np.zeros((i, j))
9 |
10 | for k in range(1, 10):
11 | v = np.zeros((i, j, k))
12 |
--------------------------------------------------------------------------------
/test/Benchmark/python/np.zeros_like:
--------------------------------------------------------------------------------
1 | # coding: utf8
2 | import numpy as np
3 |
4 | m10 = np.linspace(0, 10, 10) .reshape(1, 10)
5 | m100 = np.linspace(0, 10, 100) .reshape(10, 10)
6 | m1000= np.linspace(0, 10, 1000) .reshape(100, 10)
7 |
8 | for i in range(0, 100):
9 | v = np.zeros_like(m10)
10 | v = np.zeros_like(m100)
11 | v = np.zeros_like(m1000)
12 |
--------------------------------------------------------------------------------
/test/Resources/files/simple-headers.csv:
--------------------------------------------------------------------------------
1 | "header1";"header2";"header3"
2 | 7;0.27;0.36
3 | 6.3;0.3;0.34
4 | 8.1;0.28;0.4
5 |
--------------------------------------------------------------------------------
/test/Resources/files/simple-noheaders.csv:
--------------------------------------------------------------------------------
1 | 7;0.27;0.36
2 | 6.3;0.3;0.34
3 | 8.1;0.28;0.4
4 |
--------------------------------------------------------------------------------
/test/Resources/files/simple-spacedelimiter.csv:
--------------------------------------------------------------------------------
1 | "header1" "header2" "header3"
2 | 7 0.27 0.36
3 | 6.3 0.3 0.34
4 | 8.1 0.28 0.4
5 |
--------------------------------------------------------------------------------
/test/Unit/LinAlg/CholeskyTest.php:
--------------------------------------------------------------------------------
1 | equals(np::linalg(), $func, null, $expected, $matrix);
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/test/Unit/LinAlg/NormsTest.php:
--------------------------------------------------------------------------------
1 | data, 3.4641016151378 ], # 2 dim diagonalized
20 | ];
21 | }
22 |
23 | /**
24 | * @dataProvider getScenarios
25 | */
26 | public function testScenario($func, $input, $expected, $args = null)
27 | {
28 | $matrix = np::ar($input);
29 |
30 | // Input won be used here
31 | $this->equals(np::linalg(), $func, null, $expected, $matrix);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/test/Unit/NdArray/AlterTest.php:
--------------------------------------------------------------------------------
1 | $method();
51 | break;
52 | case 2:
53 | $m->$method($args[1]);
54 | break;
55 | case 3:
56 | $m->$method($args[1], $args[2]);
57 | break;
58 | default:
59 | throw new \Exception(
60 | sprintf(
61 | '%s() does not accept %d arguments',
62 | __METHOD__,
63 | func_num_args()
64 | )
65 | );
66 | break;
67 | }
68 |
69 | $this->assertEquals($input, $m->data);
70 | }
71 |
72 | /**
73 | * vander() only accepts vectors
74 | */
75 | public function testVanderOneDim()
76 | {
77 | $input = [1, 2, 3, 4];
78 |
79 | $m = np::ar($input);
80 |
81 | $m->vander();
82 |
83 | $this->assertEquals($input, $m->data);
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/test/Unit/NdArray/Traits/ArithmeticTraitTest.php:
--------------------------------------------------------------------------------
1 | expectException(\InvalidArgumentException::class);
16 |
17 | ( new NdArray([1]) )->add('test');
18 | }
19 |
20 | /**
21 | * add() $input has too many dimensions
22 | */
23 | public function testInputTooManyDimensions()
24 | {
25 | $this->expectException(\InvalidArgumentException::class);
26 |
27 | ( new NdArray([[1]]) )->add([[[1]]]);
28 | }
29 |
30 | /**
31 | * add() self has too many dimensions
32 | */
33 | public function testSelfTooManyDimensions()
34 | {
35 | $this->expectException(\InvalidArgumentException::class);
36 |
37 | ( new NdArray([[[1]]]) )->add([[1]]);
38 | }
39 |
40 | /**
41 | * add() Add an array
42 | */
43 | public function testAddArray()
44 | {
45 | $this->assertEquals(
46 | [[1, 3, 5]],
47 | ( new NdArray([[1, 2, 3]]) )->add([[0, 1, 2]])->data
48 | );
49 | }
50 |
51 | /**
52 | * dot() self.array
53 | */
54 | public function testDotArray()
55 | {
56 | $this->assertEquals(
57 | [16, 22],
58 | ( new NdArray([1, 2, 3]) )
59 | ->dot(
60 | [[0, 1],
61 | [2, 3],
62 | [4, 5]]
63 | )
64 | ->data
65 | );
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/test/Unit/NdArray/Traits/Attribute/AttributeTest.php:
--------------------------------------------------------------------------------
1 | expectException(\SciPhp\Exception\InvalidAttributeException::class);
16 |
17 | ( new NdArray([]) )->ndimmm;
18 | }
19 |
20 | /**
21 | * Tests data attribute
22 | */
23 | public function testData()
24 | {
25 | // expected = sample
26 | $tests = [
27 | [] ,
28 | [1, 2, 3] ,
29 | [[1, 2, 3]] ,
30 | [[[1, 2, 3]]]
31 | ];
32 |
33 | foreach ($tests as $test)
34 | {
35 | $this->assertEquals(
36 | $test,
37 | ( new NdArray($test) )->data,
38 | "Should be " . print_r($test, true)
39 | );
40 | }
41 | }
42 |
43 | /**
44 | * Tests ndim attribute
45 | */
46 | public function testNdim()
47 | {
48 | // expected / sample
49 | $tests = [
50 | [ 0, [] ],
51 | [ 1, [1, 2, 3] ],
52 | [ 2, [[1, 2, 3]] ],
53 | [ 3, [[[1, 2, 3]]] ],
54 | ];
55 |
56 | foreach ($tests as $test)
57 | {
58 | $this->assertEquals(
59 | $test[0],
60 | ( new NdArray($test[1]) )->ndim,
61 | "Should be {$test[0]}"
62 | );
63 | }
64 | }
65 |
66 | /**
67 | * Tests size attribute
68 | */
69 | public function testSize()
70 | {
71 | // expected / sample
72 | $tests = [
73 | [ 0, [] ],
74 | [ 3, [1, 2, 3] ],
75 | [ 3, [[1, 2, 3]] ],
76 | [ 3, [[[1, 2, 3]]] ],
77 | ];
78 |
79 | foreach ($tests as $test)
80 | {
81 | $this->assertEquals(
82 | $test[0],
83 | ( new NdArray($test[1]) )->size,
84 | "Should be {$test[0]}"
85 | );
86 | }
87 | }
88 |
89 | /**
90 | * Tests shape attribute
91 | */
92 | public function testShape()
93 | {
94 | // expected / sample
95 | $tests = [
96 | [ [], [] ],
97 | [ [3], [1, 2, 3] ],
98 | [ [1 ,3], [[1, 2, 3]] ],
99 | [ [1, 1, 3], [[[1, 2, 3]]] ],
100 | ];
101 |
102 | foreach ($tests as $test)
103 | {
104 | $this->assertEquals(
105 | $test[0],
106 | ( new NdArray($test[1]) )->shape,
107 | "Should be " . print_r($test[0], true)
108 | );
109 | }
110 | }
111 | }
112 |
--------------------------------------------------------------------------------
/test/Unit/NdArray/Traits/Attribute/ShapeTest.php:
--------------------------------------------------------------------------------
1 | shape = $args;
36 | $this->assertEquals($expected, $m->data);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/test/Unit/NdArray/Traits/Attribute/TTest.php:
--------------------------------------------------------------------------------
1 | assertEquals(
32 | $test[0],
33 | ( new NdArray($test[1]) )->T->data,
34 | "Should be " . print_r($test[0], true)
35 | );
36 | }
37 | }
38 |
39 | /**
40 | * T dim > 2
41 | */
42 | public function testDimSup2()
43 | {
44 | $this->expectException(\InvalidArgumentException::class);
45 |
46 | $x = np::linspace(0, 12, 12)->reshape(1, 3, 4);
47 | $x->T;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/test/Unit/NdArray/Traits/Attribute/WritingInvalidAttributeTest.php:
--------------------------------------------------------------------------------
1 | expectException(InvalidAttributeException::class);
28 |
29 | $m = new NdArray($input);
30 |
31 | $m->$attr = 5;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/test/Unit/NdArray/Traits/BasicTraitTest.php:
--------------------------------------------------------------------------------
1 | copy()->power(2);
19 |
20 | $this->assertEquals(
21 | [1, 2, 3, 4],
22 | $m->data,
23 | 'Arrays are equivalent'
24 | );
25 | }
26 |
27 | /**
28 | * copy() copied data are the same
29 | */
30 | public function testCopyData()
31 | {
32 | # power only alters the copy arrays
33 | $m = np::ar([1, 2, 3, 4]);
34 | $m->copy();
35 |
36 | $this->assertEquals(
37 | $m->data,
38 | $m->copy()->data,
39 | 'Arrays are equivalent'
40 | );
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/test/Unit/NdArray/Traits/IndexTraitTest.php:
--------------------------------------------------------------------------------
1 | reshape(2, 2);
17 |
18 | unset($x[1]);
19 | $this->assertEquals($x->data, [ [1, 2] ], 'Should be [ [1, 2] ]');
20 |
21 | unset($x[42]);
22 | $this->assertEquals($x->data, [ [1, 2] ], 'Should be [ [1, 2] ]');
23 | }
24 |
25 | /**
26 | * Tests that an offset exists
27 | */
28 | public function testOffsetExists()
29 | {
30 | $x = np::linspace(1, 9, 9)->reshape(3, 3);
31 |
32 | $this->assertTrue(isset($x[1]), 'Should be true');
33 |
34 | $this->assertFalse(isset($x[42]), 'Should be false');
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/test/Unit/NdArray/Traits/Mathematical/TrigonometricTest.php:
--------------------------------------------------------------------------------
1 | equals(NdArray::class, $func, $input, $expected, $args);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/test/Unit/NdArray/Traits/Triangle/TrilTest.php:
--------------------------------------------------------------------------------
1 | reshape(3, 3);
16 | $m1= $m->copy();
17 |
18 | $m->tril();
19 |
20 | $this->assertEquals(
21 | $m,
22 | $m1,
23 | 'Should not modify initial matrix'
24 | );
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/test/Unit/NdArray/Traits/Triangle/TriuTest.php:
--------------------------------------------------------------------------------
1 | reshape(3, 3);
16 | $m1= $m->copy();
17 |
18 | $m->triu();
19 |
20 | $this->assertEquals(
21 | $m,
22 | $m1,
23 | 'Should not modify initial matrix'
24 | );
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/test/Unit/NdArray/Traits/Visitor/AxisTest.php:
--------------------------------------------------------------------------------
1 | 2
22 | ];
23 | }
24 |
25 | /**
26 | * @dataProvider getScenarios
27 | */
28 | public function testScenario($func, $input, $expected, $args = null)
29 | {
30 | $this->equals('\SciPhp\NdArray', $func, $input, $expected, $args);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/NumPhpTest.php:
--------------------------------------------------------------------------------
1 | assertInstanceOf('SciPhp\\NdArray', $v);
18 | }
19 |
20 | /**
21 | * parseArgs() with a 1-dim array, tuple mode
22 | */
23 | public function testParseArgsOneDim()
24 | {
25 | $this->assertEquals(
26 | [1, 2, 3],
27 | np::parseArgs([1, 2, 3])
28 | );
29 |
30 | // Empty tuple
31 | $this->assertEquals(
32 | [],
33 | np::parseArgs([]),
34 | 'Empty tuple failed'
35 | );
36 | }
37 |
38 | /**
39 | * parseArgs() with a 2-dim array, array mode
40 | */
41 | public function testParseArgsTwoDim()
42 | {
43 | $this->assertEquals(
44 | [1, 2, 3],
45 | np::parseArgs([[1, 2, 3]])
46 | );
47 |
48 | // Empty array
49 | $this->assertEquals(
50 | [],
51 | np::parseArgs([[]]),
52 | 'Empty array failed'
53 | );
54 | }
55 |
56 | /**
57 | * parseArgs() with a 3-dim array
58 | */
59 | public function testParseArgsThreeDim()
60 | {
61 | $this->expectException(\InvalidArgumentException::class);
62 |
63 | np::parseArgs([[[1, 2, 3]]]);
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Arithmetic/AddTest.php:
--------------------------------------------------------------------------------
1 | assertEquals(
16 | 5 + 6,
17 | np::add(5, 6)
18 | );
19 | }
20 |
21 | /**
22 | * add(), lambda + array
23 | */
24 | public function testAddLambdaArray()
25 | {
26 | $this->assertEquals(
27 | [5, 7, 9],
28 | np::add(5, [0, 2, 4])->data
29 | );
30 | }
31 |
32 | /**
33 | * add(), vector + vector
34 | */
35 | public function testAddVectorVector()
36 | {
37 | $this->assertEquals(
38 | [5, 7, 9],
39 | np::add([5, 5, 5], [0, 2, 4])->data
40 | );
41 | }
42 |
43 | /**
44 | * add(), vector + vector Vectors not aligned
45 | */
46 | public function testAddVectorVectorNotAlignedVector()
47 | {
48 | $this->expectException(\InvalidArgumentException::class);
49 |
50 | np::add([5, 5, 5], [0, 2, 4, 5]);
51 | }
52 |
53 | /**
54 | * add(), vector + array
55 | */
56 | public function testAddVectorArray()
57 | {
58 | $this->assertEquals(
59 | [[5, 7, 9]],
60 | np::add([5, 5, 5], [[0, 2, 4]])->data,
61 | 'One line array'
62 | );
63 |
64 | $this->assertEquals(
65 | [[5, 7, 9], [11, 13, 15]],
66 | np::add([5, 5, 5], [[0, 2, 4], [6, 8, 10]])->data,
67 | '2 lines array'
68 | );
69 | }
70 |
71 | /**
72 | * add(), vector + array Vector not aligned
73 | */
74 | public function testAddVectorArrayNotAlignedVector()
75 | {
76 | $this->expectException(\InvalidArgumentException::class);
77 |
78 | np::add([5, 5, 5], [[0, 2, 4, 5]]);
79 | }
80 |
81 | /**
82 | * add(), array + vector
83 | */
84 | public function testAddArrayVector()
85 | {
86 | $this->assertEquals(
87 | [[5, 7, 9]],
88 | np::add([[0, 2, 4]], [5, 5, 5])->data
89 | );
90 | }
91 |
92 | /**
93 | * add(), array + vector Vector not aligned
94 | */
95 | public function testAddArrayVectorNotAlignedVector()
96 | {
97 | $this->expectException(\InvalidArgumentException::class);
98 |
99 | np::add([[0, 2, 4, 5]], [5, 5, 5]);
100 | }
101 |
102 | /**
103 | * add(), array + array
104 | */
105 | public function testAddArrayArray()
106 | {
107 | $this->assertEquals(
108 | [[1, 4, 7]],
109 | np::add([[0, 2, 4]], [[1, 2, 3]])->data
110 | );
111 | }
112 |
113 | /**
114 | * add(), array + array Matrices not aligned
115 | */
116 | public function testAddArrayArrayNotAlignedArrays()
117 | {
118 | $this->expectException(\InvalidArgumentException::class);
119 |
120 | np::add([[0, 2, 4, 5]], [[5, 5, 5]]);
121 | }
122 |
123 | /**
124 | * add(), first parameter is not an array_like
125 | */
126 | public function testAddFirstArgumentNonArray()
127 | {
128 | $this->expectException(\InvalidArgumentException::class);
129 |
130 | np::add('hello', [55]);
131 | }
132 |
133 | /**
134 | * add(), Second parameter is not an array_like
135 | */
136 | public function testAddSecondArgumentNonArray()
137 | {
138 | $this->expectException(\InvalidArgumentException::class);
139 |
140 | np::add([55], 'hello');
141 | }
142 | }
143 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Arithmetic/MultiplyTest.php:
--------------------------------------------------------------------------------
1 | assertEquals(
16 | 18,
17 | np::multiply(6, 3)
18 | );
19 | }
20 |
21 | /**
22 | * multiply(), lambda * array
23 | */
24 | public function testLambdaArray()
25 | {
26 | $this->assertEquals(
27 | [8, 16, 32],
28 | np::multiply(8, [1, 2, 4])->data
29 | );
30 | }
31 |
32 | /**
33 | * multiply(), array * lambda
34 | */
35 | public function testArrayLambda()
36 | {
37 | $this->assertEquals(
38 | [0, 10, 20],
39 | np::multiply([0, 2, 4], 5)->data
40 | );
41 | }
42 |
43 | /**
44 | * multiply(), array * array
45 | */
46 | public function testArrayArray()
47 | {
48 | $this->assertEquals(
49 | [[0, 4, 4]],
50 | np::multiply([[0, 2, 4]], [[4, 2, 1]])->data
51 | );
52 | }
53 |
54 | /**
55 | * multiply(), array * vector
56 | */
57 | public function testArrayVector()
58 | {
59 | $this->assertEquals(
60 | [[0, 4, 4]],
61 | np::multiply([[0, 2, 4]], [4, 2, 1])->data
62 | );
63 | }
64 |
65 | /**
66 | * multiply(), vector * array
67 | */
68 | public function testVectorArray()
69 | {
70 | $this->assertEquals(
71 | [[0, 4, 4]],
72 | np::multiply([0, 2, 4], [[4, 2, 1]])->data
73 | );
74 | }
75 |
76 | /**
77 | * multiply(), vector * vector
78 | */
79 | public function testVectorVector()
80 | {
81 | $this->assertEquals(
82 | [0, 4, 4],
83 | np::multiply([0, 2, 4], [4, 2, 1])->data
84 | );
85 | }
86 |
87 | /**
88 | * multiply(), First parameter is not an array_like
89 | */
90 | public function testFirstArgumentNonArray()
91 | {
92 | $this->expectException(\InvalidArgumentException::class);
93 |
94 | np::multiply('hello', [55]);
95 | }
96 |
97 | /**
98 | * multiply(), Second parameter is not an array_like
99 | */
100 | public function testSecondArgumentNonArray()
101 | {
102 | $this->expectException(\InvalidArgumentException::class);
103 |
104 | np::multiply([55], 'hello');
105 | }
106 |
107 | /**
108 | * multiply(), vector * vector not aligned
109 | */
110 | public function testVectorVectorNotAligned()
111 | {
112 | $this->expectException(\InvalidArgumentException::class);
113 |
114 | np::multiply([1, 2, 3], [1, 2]);
115 | }
116 |
117 | /**
118 | * multiply(), vector * array not aligned
119 | */
120 | public function testVectorArrayNotAligned()
121 | {
122 | $this->expectException(\InvalidArgumentException::class);
123 |
124 | np::multiply([1, 2, 3], [[1, 2]]);
125 | }
126 | }
127 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Arithmetic/ReciprocalTest.php:
--------------------------------------------------------------------------------
1 | assertEquals(
16 | 1/6,
17 | np::reciprocal(6)
18 | );
19 | }
20 |
21 | /**
22 | * reciprocal(), array
23 | */
24 | public function testArray()
25 | {
26 | $this->assertEquals(
27 | [1, 1/2, 1/4],
28 | np::reciprocal([1, 2, 4])->data
29 | );
30 | }
31 |
32 | /**
33 | * reciprocal(), array
34 | */
35 | public function test2DimArray()
36 | {
37 | $this->assertEquals(
38 | [[1, 1/2, 1/4]],
39 | np::reciprocal([[1, 2, 4]])->data
40 | );
41 | }
42 |
43 | /**
44 | * reciprocal(), NdArray
45 | */
46 | public function testNdArray()
47 | {
48 | $this->assertEquals(
49 | [1, 1/2, 1/4],
50 | np::reciprocal(np::ar([1, 2, 4]))->data
51 | );
52 | }
53 |
54 | /**
55 | * reciprocal(), Parameter is not an array_like
56 | */
57 | public function testArgumentNonArray()
58 | {
59 | $this->expectException(\InvalidArgumentException::class);
60 |
61 | np::reciprocal('hello');
62 | }
63 |
64 | /**
65 | * reciprocal(), Division by zero lambda
66 | */
67 | public function testDivideByZeroLambda()
68 | {
69 | $this->expectException(\InvalidArgumentException::class);
70 |
71 | np::reciprocal(0);
72 | }
73 |
74 | /**
75 | * reciprocal(), Division by zero array
76 | */
77 | public function testDivideByZeroArray()
78 | {
79 | $this->expectException(\InvalidArgumentException::class);
80 |
81 | np::reciprocal([[1, 2, 3, 0]]);
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Arithmetic/SubtractTest.php:
--------------------------------------------------------------------------------
1 | assertEquals(
16 | 5 - 6,
17 | np::subtract(5, 6)
18 | );
19 | }
20 |
21 | /**
22 | * subtract(), lambda - array
23 | */
24 | public function testLambdaArray()
25 | {
26 | $this->assertEquals(
27 | [5, 3, 1],
28 | np::subtract(5, [0, 2, 4])->data
29 | );
30 | }
31 |
32 | /**
33 | * subtract(), array - lambda
34 | */
35 | public function testArrayLambda()
36 | {
37 | $this->assertEquals(
38 | [-5, -3, -1],
39 | np::subtract([0, 2, 4], 5)->data
40 | );
41 | }
42 |
43 | /**
44 | * subtract(), array - array
45 | */
46 | public function testArrayArray()
47 | {
48 | $this->assertEquals(
49 | [[-4, 0, 4]],
50 | np::subtract([[0, 2, 4]], [[4, 2, 0]])->data
51 | );
52 | }
53 |
54 | /**
55 | * subtract(), array - vector
56 | */
57 | public function testArrayVector()
58 | {
59 | $this->assertEquals(
60 | [[-4, 0, 4]],
61 | np::subtract([[0, 2, 4]], [4, 2, 0])->data
62 | );
63 | }
64 |
65 | /**
66 | * subtract(), vector - array
67 | */
68 | public function testVectorArray()
69 | {
70 | $this->assertEquals(
71 | [[-4, 0, 4]],
72 | np::subtract([0, 2, 4], [[4, 2, 0]])->data
73 | );
74 | }
75 |
76 | /**
77 | * subtract(), First parameter is not an array_like
78 | */
79 | public function testFirstArgumentNonArray()
80 | {
81 | $this->expectException(\InvalidArgumentException::class);
82 |
83 | np::subtract('hello', [55]);
84 | }
85 |
86 | /**
87 | * subtract(), Second parameter is not an array_like
88 | */
89 | public function testSecondArgumentNonArray()
90 | {
91 | $this->expectException(\InvalidArgumentException::class);
92 |
93 | np::subtract([55], 'hello');
94 | }
95 |
96 | /**
97 | * subtract(), array - array, not aligned
98 | */
99 | public function testArrayArrayNotAligned()
100 | {
101 | $this->expectException(\InvalidArgumentException::class);
102 |
103 | np::subtract([[55, 10]], [[1, 2, 3]]);
104 | }
105 |
106 | /**
107 | * subtract(), vector - vector, not aligned
108 | */
109 | public function testVectoVectorNotAligned()
110 | {
111 | $this->expectException(\InvalidArgumentException::class);
112 |
113 | np::subtract([55, 10], [1, 2, 3]);
114 | }
115 | }
116 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Diagonal/DiagTest.php:
--------------------------------------------------------------------------------
1 | reshape(3, 3)->data, [1, 5, 9]],
16 | ['diag', np::linspace(1, 12, 12)->reshape(3, 4), [1, 6, 11]],
17 | ['diag', np::linspace(1, 12, 12)->reshape(4, 3), [1, 5, 9]],
18 |
19 | // Extract diagonals, offset>0
20 | ['diag', np::linspace(1, 9, 9)->reshape(3, 3), [2, 6], [1]],
21 | ['diag', np::linspace(1, 12, 12)->reshape(3, 4), [2, 7, 12], [1]],
22 | ['diag', np::linspace(1, 12, 12)->reshape(4, 3), [2, 6], [1]],
23 |
24 | // Extract diagonals, offset<0
25 | ['diag', np::linspace(1, 9, 9)->reshape(3, 3), [4, 8], [-1]],
26 | ['diag', np::linspace(1, 12, 12)->reshape(3, 4), [5, 10], [-1]],
27 | ['diag', np::linspace(1, 12, 12)->reshape(4, 3), [4, 8, 12], [-1]],
28 |
29 | // Construct diagonal matrix, offset=0
30 | ['diag', np::linspace(1, 3, 3), [[1, 0, 0], [0, 2, 0], [0, 0, 3]] ],
31 |
32 | // Construct diagonal matrix, offset=1
33 | ['diag', np::linspace(1, 3, 3),
34 | [[0, 1, 0, 0],
35 | [0, 0, 2, 0],
36 | [0, 0 ,0, 3]],
37 | [1]
38 | ],
39 | // Construct diagonal matrix, offset=2
40 | ['diag', np::linspace(1, 3, 3),
41 | [[0, 0, 1, 0, 0],
42 | [0, 0, 0, 2, 0],
43 | [0, 0, 0 ,0, 3]],
44 | [2]
45 | ],
46 |
47 | // Construct diagonal matrix, offset=-1
48 | ['diag', np::linspace(1, 3, 3),
49 | [[0, 0, 0],
50 | [1, 0, 0],
51 | [0, 2 ,0],
52 | [0, 0 ,3]],
53 | [-1]
54 | ],
55 | // Construct diagonal matrix, offset=-2
56 | ['diag', np::linspace(1, 3, 3),
57 | [[0, 0, 0],
58 | [0, 0, 0],
59 | [1, 0 ,0],
60 | [0, 2 ,0],
61 | [0, 0 ,3]],
62 | [-2]
63 | ],
64 |
65 | # \InvalidArgumentException
66 | ['diag', 'hello', \InvalidArgumentException::class],
67 | ['diag', np::linspace(0, 12, 12)->reshape(1, 3, 4), \InvalidArgumentException::class],
68 | ];
69 | }
70 |
71 | /**
72 | * @dataProvider getScenarios
73 | */
74 | public function testScenario($func, $input, $expected, $args = null)
75 | {
76 | $this->staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Diagonal/DiagflatTest.php:
--------------------------------------------------------------------------------
1 | reshape(2, 2),
15 | [[1, 0, 0, 0],
16 | [0, 2, 0, 0],
17 | [0, 0, 3, 0],
18 | [0, 0, 0, 4]]
19 | ],
20 |
21 | ['diagflat', np::linspace(1, 4, 4)->reshape(2, 2)->data,
22 | [[1, 0, 0, 0],
23 | [0, 2, 0, 0],
24 | [0, 0, 3, 0],
25 | [0, 0, 0, 4]]
26 | ],
27 |
28 | // offset=1
29 | ['diagflat', np::linspace(1, 4, 4)->reshape(2, 2)->data,
30 | [[0, 1, 0, 0, 0],
31 | [0, 0, 2, 0, 0],
32 | [0, 0, 0, 3, 0],
33 | [0, 0, 0, 0, 4]],
34 | [1]
35 | ],
36 |
37 | // offset=-1
38 | ['diagflat', np::linspace(1, 4, 4)->reshape(2, 2)->data,
39 | [[0, 0, 0, 0],
40 | [1, 0, 0, 0],
41 | [0, 2, 0, 0],
42 | [0, 0, 3, 0],
43 | [0, 0, 0, 4]],
44 | [-1]
45 | ],
46 |
47 | # \InvalidArgumentException
48 | ['diagflat', 'hello', \InvalidArgumentException::class],
49 | // ['diagflat', [1, 2], \InvalidArgumentException::class, [0.5] ],
50 | ['diagflat', [1, 2], \TypeError::class, ['hello'] ],
51 | ];
52 | }
53 |
54 | /**
55 | * @dataProvider getScenarios
56 | */
57 | public function testScenario($func, $input, $expected, $args = null)
58 | {
59 | $this->staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Diagonal/DiagonalTest.php:
--------------------------------------------------------------------------------
1 | reshape(3, 3)->data, [1, 5, 9]],
18 | ['diagonal', np::linspace(1, 12, 12)->reshape(3, 4), [1, 6, 11]],
19 | ['diagonal', np::linspace(1, 12, 12)->reshape(4, 3), [1, 5, 9]],
20 |
21 | // Extract diagonals, offset>0
22 | ['diagonal', np::linspace(1, 9, 9)->reshape(3, 3), [2, 6], [1]],
23 | ['diagonal', np::linspace(1, 12, 12)->reshape(3, 4), [2, 7, 12], [1]],
24 | ['diagonal', np::linspace(1, 12, 12)->reshape(4, 3), [2, 6], [1]],
25 |
26 | // Extract diagonals, offset<0
27 | ['diagonal', np::linspace(1, 9, 9)->reshape(3, 3), [4, 8], [-1]],
28 | ['diagonal', np::linspace(1, 12, 12)->reshape(3, 4), [5, 10], [-1]],
29 | ['diagonal', np::linspace(1, 12, 12)->reshape(4, 3), [4, 8, 12], [-1]],
30 |
31 | # \InvalidArgumentException
32 | ['diagonal', 'hello', \InvalidArgumentException::class],
33 | ['diagonal', [[1, 0], [0, 1]], \TypeError::class, ['hello'] ],
34 | // ['diagonal', [[1, 0], [0, 1]], \InvalidArgumentException::class, [0.5] ],
35 | ['diagonal', np::linspace(0, 12, 12)->reshape(1, 3, 4), \InvalidArgumentException::class],
36 | ];
37 | }
38 |
39 | /**
40 | * @dataProvider getScenarios
41 | */
42 | public function testScenario($func, $input, $expected, $args = null)
43 | {
44 | $this->staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Diagonal/EyeTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Diagonal/IdentityTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Diagonal/TraceTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Exponent/Exp2Test.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Exponent/ExpTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Exponent/Expm1Test.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expm1ected, $args);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Exponent/PowerTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Exponent/SqrtTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Exponent/SquareTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/File/LoadtxtTest.php:
--------------------------------------------------------------------------------
1 | true] ] ],
14 | ['loadtxt', getcwd() . '/test/Resources/files/simple-noheaders.csv' , [[7,0.27,0.36],[6.3,0.3,0.34],[8.1,0.28,0.4]] ],
15 | ['loadtxt', getcwd() . '/test/Resources/files/simple-noheaders.csv' , [[7,0.27,0.36],[6.3,0.3,0.34],[8.1,0.28,0.4]], [ ['headers' => false] ] ],
16 | ['loadtxt', getcwd() . '/test/Resources/files/simple-spacedelimiter.csv' , [[7,0.27,0.36],[6.3,0.3,0.34],[8.1,0.28,0.4]], [ ['headers' => true, 'delimiter' => " "] ] ],
17 |
18 | # \InvalidArgumentException
19 | ['loadtxt', 1, \InvalidArgumentException::class],
20 | ['loadtxt', 'fileDoesNotExist.csv', \InvalidArgumentException::class],
21 | ];
22 | }
23 |
24 | /**
25 | * @dataProvider getScenarios
26 | */
27 | public function testScenario($func, $input, $expected, $args = null)
28 | {
29 | $this->staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Float/CopysignTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Float/SignbitTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Logarithm/Log10Test.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Logarithm/Log2Test.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Logarithm/LogTest.php:
--------------------------------------------------------------------------------
1 | vander(),
22 | [[0, 0, 0],
23 | [2, 1, 0],
24 | [4, 2, 0]], [100]],
25 | # \InvalidArgumentException
26 | ['log', 0, \InvalidArgumentException::class],
27 | ['log', null, \InvalidArgumentException::class],
28 | ['log', false, \InvalidArgumentException::class],
29 | ['log', [1, 0, 1], \InvalidArgumentException::class],
30 | ['log', [[1, 2], [0, 1]], \InvalidArgumentException::class],
31 | ['log', [1, null, 1], \InvalidArgumentException::class],
32 | ['log', [[1, 2], [null, 1]],\InvalidArgumentException::class],
33 | ['log', 'hello', \InvalidArgumentException::class],
34 | ['log', [1, 2], \InvalidArgumentException::class, [0]],
35 | ['log', [1, 2], \InvalidArgumentException::class, [null]],
36 | ['log', [1, 2], \InvalidArgumentException::class, [false]]
37 | ];
38 | }
39 |
40 | /**
41 | * @dataProvider getScenarios
42 | */
43 | public function testScenario($func, $input, $expected, $args = null)
44 | {
45 | $this->staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Mathematical/TrigonometricTest.php:
--------------------------------------------------------------------------------
1 | staticEquals(NumPhp::class, $func, $input, $expected, $args);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Matrix/IsSquareTest.php:
--------------------------------------------------------------------------------
1 | resize(4,4,4)->data, false ], # 4x4x4 is NOT square
17 | ['is_square', [[0, 1], [1, 0]], true ], # 2x2 is square
18 | ['is_square', np::arange(0, 9)->reshape(3,3)->data, true ], # 3x3 is square
19 | ['is_square', np::arange(0, 16)->reshape(4,4)->data, true ], # 4x4 is square
20 |
21 | # \InvalidArgumentException
22 | ['is_square', 'hello', \InvalidArgumentException::class],
23 | // Too many dim
24 | ['is_square', 22, \InvalidArgumentException::class],
25 | ];
26 | }
27 |
28 | /**
29 | * @dataProvider getScenarios
30 | */
31 | public function testScenario($func, $input, $expected, $args = null)
32 | {
33 | $this->staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Matrix/NegativeTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Matrix/TransposeTest.php:
--------------------------------------------------------------------------------
1 | reshape(3, 3),
16 | [[ 1, 4, 7 ],
17 | [ 2, 5, 8 ],
18 | [ 3, 6, 9 ]]
19 | ],
20 | ['transpose', [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
21 | [[ 1, 4, 7 ],
22 | [ 2, 5, 8 ],
23 | [ 3, 6, 9 ]]
24 | ],
25 |
26 | // non square arrays
27 | ['transpose', np::linspace(1, 6, 6)->reshape(2, 3),
28 | [[ 1, 4 ],
29 | [ 2, 5 ],
30 | [ 3, 6 ]]
31 | ],
32 | ['transpose', np::linspace(1, 6, 6)->reshape(3, 2),
33 | [[ 1, 3, 5 ],
34 | [ 2, 4, 6 ]]
35 | ],
36 |
37 | // vectors
38 | ['transpose', np::linspace(1, 3, 3),[1, 2, 3] ],
39 | ['transpose', [1, 2, 3], [1, 2, 3] ],
40 |
41 | # \InvalidArgumentException
42 | ['transpose', 'hello', \InvalidArgumentException::class],
43 | // Too many dim
44 | ['transpose', [[[1, 2, 3]]],\InvalidArgumentException::class],
45 | ];
46 | }
47 |
48 | /**
49 | * @dataProvider getScenarios
50 | */
51 | public function testScenario($func, $input, $expected, $args = null)
52 | {
53 | $this->staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/NumArray/BroadcastToTest.php:
--------------------------------------------------------------------------------
1 | shape (3,3)
14 | ['broadcast_to', [[1], [2], [3]],
15 | [[1,1,1],
16 | [2,2,2],
17 | [3,3,3]], [[3,3]] ],
18 |
19 | // 1 dim - (3,) -> shape (3,3)
20 | ['broadcast_to', [1, 2, 3] ,
21 | [ [1, 2, 3],
22 | [1, 2, 3],
23 | [1, 2, 3] ], [[3,3]] ],
24 |
25 | // 1 dim - (3,) -> shape (1,3)
26 | ['broadcast_to', [1, 2, 3] ,
27 | [[1, 2, 3]], [[1,3]] ],
28 |
29 |
30 | ['broadcast_to', [1, 2, 3], \InvalidArgumentException::class, [[3, 1]] ], // target shape not valid
31 | ['broadcast_to', [1, 2, 3], \InvalidArgumentException::class, [[3, 4]] ], // target shape not valid
32 | ['broadcast_to', [[1], [2], [3]], \InvalidArgumentException::class, [[4, 3, 3]] ], // ndim(target shape) > 2
33 | ['broadcast_to', [[[1], [2], [3]]], \InvalidArgumentException::class, [[3, 3]] ], // $m->ndim > 3
34 | ];
35 | }
36 |
37 | /**
38 | * @dataProvider getScenarios
39 | */
40 | public function testScenario($func, $input, $expected, $args = null)
41 | {
42 | $this->staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/NumArray/FullLikeTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/NumArray/FullTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/NumArray/NullsLikeTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/NumArray/NullsTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/NumArray/OnesLikeTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/NumArray/OnesTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/NumArray/ZerosLikeTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/NumArray/ZerosTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Operation/SumTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
37 | }
38 |
39 | /**
40 | * sum(), lambda
41 | */
42 | public function testLambda()
43 | {
44 | $this->assertEquals(
45 | 6,
46 | np::sum(6)
47 | );
48 | }
49 |
50 | /**
51 | * Invalid argument
52 | */
53 | public function testInvalidArgumentException()
54 | {
55 | $this->expectException(\InvalidArgumentException::class);
56 |
57 | np::sum('hello');
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Operation/TrapzTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Range/ArangeTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Range/LinspaceTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Range/LogspaceTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Triangle/TriTest.php:
--------------------------------------------------------------------------------
1 | staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
142 | }
143 | }
144 |
--------------------------------------------------------------------------------
/test/Unit/NumPhp/Traits/Triangle/TrilTest.php:
--------------------------------------------------------------------------------
1 | reshape(3, 3)->data,
20 | [[1, 0, 0],
21 | [4, 5, 0],
22 | [7, 8, 9]],
23 | ],
24 |
25 | // square arrays
26 | ['tril', np::linspace(1, 9, 9)->reshape(3, 3),
27 | [[1, 0, 0],
28 | [4, 5, 0],
29 | [7, 8, 9]],
30 | ],
31 |
32 | // square arrays, positive offset
33 | ['tril', np::linspace(1, 9, 9)->reshape(3, 3),
34 | [[1, 2, 0],
35 | [4, 5, 6],
36 | [7, 8, 9]],
37 | [1]
38 | ],
39 | ['tril', np::linspace(1, 9, 9)->reshape(3, 3),
40 | [[1, 2, 3],
41 | [4, 5, 6],
42 | [7, 8, 9]],
43 | [2]
44 | ],
45 |
46 | // square arrays, negative offset
47 | ['tril', np::linspace(1, 9, 9)->reshape(3, 3),
48 | [[0, 0, 0],
49 | [4, 0, 0],
50 | [7, 8, 0]],
51 | [-1]
52 | ],
53 | ['tril', np::linspace(1, 9, 9)->reshape(3, 3),
54 | [[0, 0, 0],
55 | [0, 0, 0],
56 | [7, 0, 0]],
57 | [-2]
58 | ],
59 |
60 | // non square arrays, default offset
61 | ['tril', np::linspace(1, 8, 8)->reshape(4, 2),
62 | [[1, 0],
63 | [3, 4],
64 | [5, 6],
65 | [7, 8]],
66 | ],
67 | ['tril', np::linspace(1, 8, 8)->reshape(2, 4),
68 | [[1, 0, 0, 0],
69 | [5, 6, 0, 0]],
70 | ],
71 |
72 | // non square arrays, positive offset
73 | ['tril', np::linspace(1, 8, 8)->reshape(4, 2),
74 | [[1, 2],
75 | [3, 4],
76 | [5, 6],
77 | [7, 8]],
78 | [1]
79 | ],
80 | ['tril', np::linspace(1, 8, 8)->reshape(2, 4),
81 | [[1, 2, 0, 0],
82 | [5, 6, 7, 0]],
83 | [1]
84 | ],
85 | ['tril', np::linspace(1, 8, 8)->reshape(2, 4),
86 | [[1, 2, 3, 0],
87 | [5, 6, 7, 8]],
88 | [2]
89 | ],
90 |
91 | // non square arrays, negative offset
92 | ['tril', np::linspace(1, 8, 8)->reshape(4, 2),
93 | [[0, 0],
94 | [3, 0],
95 | [5, 6],
96 | [7, 8]],
97 | [-1]
98 | ],
99 | ['tril', np::linspace(1, 8, 8)->reshape(4, 2),
100 | [[0, 0],
101 | [0, 0],
102 | [5, 0],
103 | [7, 8]],
104 | [-2]
105 | ],
106 | ['tril', np::linspace(1, 8, 8)->reshape(2, 4),
107 | [[0, 0, 0, 0],
108 | [5, 0, 0, 0]],
109 | [-1]
110 | ],
111 | ['tril', np::linspace(1, 8, 8)->reshape(2, 4),
112 | [[0, 0, 0, 0],
113 | [0, 0, 0, 0]],
114 | [-2]
115 | ],
116 |
117 | # \InvalidArgumentException
118 | ['tril', 'hello', \InvalidArgumentException::class],
119 | ['tril', [1, 2, 3],\InvalidArgumentException::class, ['hello']],
120 | ['tril', [1, 2, 3],\InvalidArgumentException::class, [0.5]],
121 | ];
122 | }
123 |
124 | /**
125 | * @dataProvider getScenarios
126 | */
127 | public function testScenario($func, $input, $expected, $args = null)
128 | {
129 | $this->staticEquals('\SciPhp\NumPhp', $func, $input, $expected, $args);
130 | }
131 | }
132 |
--------------------------------------------------------------------------------
/test/Unit/Random/RandTest.php:
--------------------------------------------------------------------------------
1 | expectException(\InvalidArgumentException::class);
17 |
18 | $ret = np::random()->rand();
19 | }
20 |
21 | /**
22 | * Test that a call with tuple arguments returns a NdArray with
23 | * good shape and that all values are float between 0 and 1.
24 | */
25 | public function test_shaped_with_tuple()
26 | {
27 | $m = np::random()->rand(2, 2);
28 | $this->assertInstanceOf(NdArray::class, $m);
29 | $this->assertEquals([2, 2], $m->shape);
30 |
31 | foreach ($m->ravel()->data as $value) {
32 | $this->assertIsFloat($value);
33 | $this->assertGreaterThanOrEqual(0, $value);
34 | $this->assertLessThanOrEqual(1, $value);
35 | }
36 | }
37 |
38 | /**
39 | * Test that a call with array arguments returns a NdArray with
40 | * good shape and that all values are float between 0 and 1.
41 | */
42 | public function test_shaped_with_array()
43 | {
44 | $m = np::random()->rand([3, 3, 3]);
45 | $this->assertInstanceOf(NdArray::class, $m);
46 | $this->assertEquals([3, 3, 3], $m->shape);
47 |
48 | foreach ($m->ravel()->data as $value) {
49 | $this->assertIsFloat($value);
50 | $this->assertGreaterThanOrEqual(0, $value);
51 | $this->assertLessThanOrEqual(1, $value);
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/test/Unit/Random/RandintTest.php:
--------------------------------------------------------------------------------
1 | [ -5, null, null],
19 | 'single value - low must be > max' => [ 15, 12, null],
20 |
21 | '1-dim range - low only must be positive' => [ -10, null, 5],
22 | '1-dim range - bad range' => [ -10, null, -5],
23 | '1-dim range - bad array shape' => [ -5, -6, [-5]],
24 | ];
25 | }
26 |
27 | /**
28 | * Test calls that throw an exception
29 | *
30 | * @dataProvider getExceptionScenarios
31 | */
32 | public function test_exception_scenarios($low, $high, $size): void
33 | {
34 | $this->expectException(\InvalidArgumentException::class);
35 |
36 | $ret = np::random()->randint($low, $high, $size);
37 | }
38 |
39 | /**
40 | * Provide valid scenarios
41 | */
42 | public function getScenarios(): array
43 | {
44 | return [
45 | // low, high, size, expected between[min, max], expected shape
46 | 'single value - low only' => [ 5, null, null, [0, 4], null],
47 | 'single value - low and max' => [ 5, 12, null, [5, 11], null],
48 | 'single value - low negative and max' => [ -5, 6, null, [-5, 5], null],
49 |
50 | '1-dim range - low only' => [ 10, null, 5, [0, 9], [5]],
51 | '1-dim range - low and max' => [ -5, 6, 10, [-5, 5], [10]],
52 |
53 | '2-dim range - low only' => [ 10, null, [5, 5], [0, 9], [5, 5]],
54 | '2-dim range - low and max' => [ -5, 6, [10, 2], [-5, 5], [10, 2]],
55 |
56 | '3-dim range - low only' => [ 10, null, [5, 5, 4], [0, 9], [5, 5, 4]],
57 | '3-dim range - low and max' => [ -5, 6, [10, 2, 2], [-5, 5], [10, 2, 2]],
58 | ];
59 | }
60 |
61 | /**
62 | * @dataProvider getScenarios
63 | */
64 | public function test_good_shape_and_values($low, $high, $size, array $expectedBetween, $expectedShape = null): void
65 | {
66 | $m = np::random()->randint($low, $high, $size);
67 |
68 | // A random int
69 | if (is_null($size)) {
70 | $this->assertIsInt($m);
71 | $this->assertGreaterThanOrEqual($expectedBetween[0], $m);
72 | $this->assertLessThanOrEqual($expectedBetween[1], $m);
73 | // A NdArray
74 | } else {
75 | $this->assertInstanceOf(NdArray::class, $m);
76 | $this->assertEquals($expectedShape, $m->shape);
77 |
78 | foreach ($m->ravel()->data as $value) {
79 | $this->assertIsInt($value);
80 | $this->assertGreaterThanOrEqual($expectedBetween[0], $value);
81 | $this->assertLessThanOrEqual($expectedBetween[1], $value);
82 | }
83 | }
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/test/Unit/Random/RandnTest.php:
--------------------------------------------------------------------------------
1 | randn();
17 | $this->assertIsFloat($ret);
18 | }
19 |
20 | /**
21 | * Test that a call with tuple arguments returns a NdArray with
22 | * good shape and that all values are float
23 | */
24 | public function test_shaped_with_tuple()
25 | {
26 | $m = np::random()->randn(2, 2);
27 | $this->assertInstanceOf(NdArray::class, $m);
28 | $this->assertEquals([2, 2], $m->shape);
29 |
30 | foreach ($m->ravel()->data as $value) {
31 | $this->assertIsFloat($value);
32 | }
33 | }
34 |
35 | /**
36 | * Test that a call with an array argument returns a NdArray with
37 | * good shape and that all values are float
38 | */
39 | public function test_shaped_with_array()
40 | {
41 | $m = np::random()->randn([3, 3, 3]);
42 | $this->assertInstanceOf(NdArray::class, $m);
43 | $this->assertEquals([3, 3, 3], $m->shape);
44 |
45 | foreach ($m->ravel()->data as $value) {
46 | $this->assertIsFloat($value);
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/test/Unit/phpunit.xml.dist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | ../../src
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | ../../test/Unit/
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/test/Unit/phpunit.xml.dist.bak:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | ../../test/Unit/
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 | ../../src
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------