Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.12% |
16 / 17 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| MatrixTrait | |
94.12% |
16 / 17 |
|
66.67% |
2 / 3 |
6.01 | |
0.00% |
0 / 1 |
| is_square | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| negative | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| transpose | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
2.00 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace SciPhp\NumPhp; |
| 6 | |
| 7 | use SciPhp\NdArray; |
| 8 | use Webmozart\Assert\Assert; |
| 9 | |
| 10 | trait MatrixTrait |
| 11 | { |
| 12 | /** |
| 13 | * Is given matrix a square matrix ? |
| 14 | * |
| 15 | * @param \SciPhp\NdArray|array $m |
| 16 | * @link http://sciphp.org/numphp.is_square Documentation |
| 17 | * @since 0.3.0 |
| 18 | * @api |
| 19 | */ |
| 20 | final public static function is_square($m): bool |
| 21 | { |
| 22 | static::transform($m, true); |
| 23 | |
| 24 | return $m->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 | } |