Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.44% |
17 / 18 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| NumPhp | |
94.44% |
17 / 18 |
|
75.00% |
3 / 4 |
8.01 | |
0.00% |
0 / 1 |
| ar | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| parseArgs | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| transform | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| allNumeric | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
1.01 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace SciPhp; |
| 6 | |
| 7 | use SciPhp\Exception\Message; |
| 8 | use SciPhp\NumPhp\Decorator; |
| 9 | use Webmozart\Assert\Assert; |
| 10 | |
| 11 | /** |
| 12 | * Entry point for np calls. |
| 13 | * |
| 14 | * @link http://sciphp.org/ref.numphp |
| 15 | */ |
| 16 | final class NumPhp extends Decorator |
| 17 | { |
| 18 | /** |
| 19 | * Construct a n-dimensional array |
| 20 | * |
| 21 | * @param array $data |
| 22 | * @link http://sciphp.org/numphp.ar Documentation |
| 23 | * @api |
| 24 | */ |
| 25 | public static function ar(array $data, ?string $identifier = null): NdArray |
| 26 | { |
| 27 | return new NdArray($data, $identifier); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Parse args as a tuple or an array |
| 32 | * |
| 33 | * @param array|array<array> $args |
| 34 | * @api |
| 35 | */ |
| 36 | public static function parseArgs(array $args): array |
| 37 | { |
| 38 | if (isset($args[0]) && \is_array($args[0])) { |
| 39 | Assert::oneOf( |
| 40 | self::ar($args[0])->ndim, |
| 41 | [0, 1], |
| 42 | Message::ARG_NOT_ARRAY_TUPLE |
| 43 | ); |
| 44 | |
| 45 | Assert::allNumeric($args[0]); |
| 46 | |
| 47 | return $args[0]; |
| 48 | } |
| 49 | |
| 50 | Assert::allNumeric($args); |
| 51 | |
| 52 | return $args; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Transform a PHP array in a NdArray |
| 57 | * |
| 58 | * @param array|NdArray $m |
| 59 | */ |
| 60 | public static function transform(&$m, bool $required = false): void |
| 61 | { |
| 62 | if (\is_array($m)) { |
| 63 | $m = self::ar($m); |
| 64 | } |
| 65 | |
| 66 | if ($required) { |
| 67 | Assert::isInstanceof($m, NdArray::class); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Check that all values are numeric |
| 73 | * |
| 74 | * @api |
| 75 | */ |
| 76 | public static function allNumeric(): bool |
| 77 | { |
| 78 | return ! count( |
| 79 | array_filter( |
| 80 | func_get_args(), |
| 81 | static function ($value): bool { |
| 82 | return ! is_numeric($value); |
| 83 | } |
| 84 | ) |
| 85 | ); |
| 86 | } |
| 87 | } |