Code Coverage for /src/SciPhp/NumPhp/VanderTrait.php

 
Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.12% covered (success)
94.12%
16 / 17
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
VanderTrait
94.12% covered (success)
94.12%
16 / 17
50.00% covered (danger)
50.00%
1 / 2
3.00
0.00% covered (danger)
0.00%
0 / 1
 vander
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
2.00
 itemVander
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
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 VanderTrait
11 {
12     /**
13      * Generate a Vandermonde matrix.
14      *
15      * @param    \SciPhp\NdArray|array $matrix A 1-dim array.
16      * @param    int $num Number of columns for the output.
17      * @return \SciPhp\NdArray A Vandermonde matrix
18      * @throws \InvalidArgumentException
19      * @link http://sciphp.org/numphp.vander Documentation
20      * @api
21      */
22     final public static function vander($matrix, $num = null): NdArray
23     {
24         static::transform($matrix, true);
25
26         $num = is_null($num)
27             ? count($matrix->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 }