Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
21 / 21 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| ShapeTrait | |
100.00% |
21 / 21 |
|
100.00% |
4 / 4 |
8 | |
100.00% |
1 / 1 |
| ravel | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| resize | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| reshape | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| getShape | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace SciPhp\NdArray; |
| 6 | |
| 7 | use RecursiveArrayIterator; |
| 8 | use RecursiveIteratorIterator; |
| 9 | use SciPhp\NdArray; |
| 10 | use SciPhp\NumPhp as np; |
| 11 | use Webmozart\Assert\Assert; |
| 12 | |
| 13 | /** |
| 14 | * Shape methods for NdArray |
| 15 | */ |
| 16 | trait ShapeTrait |
| 17 | { |
| 18 | /** |
| 19 | * Flattens data array |
| 20 | * |
| 21 | * @link http://sciphp.org/ndarray.ravel Documentation |
| 22 | * @api |
| 23 | */ |
| 24 | final public function ravel(): NdArray |
| 25 | { |
| 26 | array_walk_recursive( |
| 27 | $this->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 | } |