Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| NormsTrait | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
| norm | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace SciPhp\LinAlg; |
| 6 | |
| 7 | use SciPhp\NumPhp as np; |
| 8 | |
| 9 | /** |
| 10 | * Norms methods |
| 11 | */ |
| 12 | trait NormsTrait |
| 13 | { |
| 14 | /** |
| 15 | * Matrix or vector norm. |
| 16 | * |
| 17 | * @param \SciPhp\NdArray|array $matrix |
| 18 | * @return float|int |
| 19 | * |
| 20 | * @link http://sciphp.org/linalg.norm |
| 21 | * Documentation for norm() |
| 22 | * |
| 23 | * @since 0.3.0 |
| 24 | * @api |
| 25 | */ |
| 26 | final public function norm($matrix) |
| 27 | { |
| 28 | np::transform($matrix, true); |
| 29 | |
| 30 | if ($matrix->ndim === 0) { |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | return sqrt( |
| 35 | $matrix->power(2)->sum() |
| 36 | ); |
| 37 | } |
| 38 | } |