Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
21 / 21 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| AttributeTrait | |
100.00% |
21 / 21 |
|
100.00% |
3 / 3 |
11 | |
100.00% |
1 / 1 |
| __set | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| __get | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
6 | |||
| getSize | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getShape | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace SciPhp\NdArray; |
| 6 | |
| 7 | use SciPhp\Exception\InvalidAttributeException; |
| 8 | use SciPhp\NumPhp as np; |
| 9 | |
| 10 | /** |
| 11 | * Attribute methods for NdArray |
| 12 | */ |
| 13 | trait AttributeTrait |
| 14 | { |
| 15 | /** |
| 16 | * @var array |
| 17 | */ |
| 18 | protected $data = []; |
| 19 | |
| 20 | /** |
| 21 | * Attribute setter |
| 22 | * |
| 23 | * @param mixed $value |
| 24 | */ |
| 25 | final public function __set(string $name, $value): void |
| 26 | { |
| 27 | switch ($name) { |
| 28 | case 'shape': |
| 29 | $this->__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 | } |