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

 
Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.36% covered (success)
96.36%
53 / 55
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
RangeTrait
96.36% covered (success)
96.36%
53 / 55
66.67% covered (warning)
66.67%
2 / 3
23
0.00% covered (danger)
0.00%
0 / 1
 arange
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
10
 linspace
93.33% covered (success)
93.33%
28 / 30
0.00% covered (danger)
0.00%
0 / 1
12.04
 logspace
100.00% covered (success)
100.00%
3 / 3
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 RangeTrait
11 {
12     /**
13      * Creates a NdArray with a range of values
14      *
15      * @param int|float $start
16      * @param int|float $end
17      * @param int|float $step
18      *
19      * @link http://sciphp.org/numphp.arange
20      *    Documentation for arange()
21      *
22      * @api
23      */
24     final public static function arange($start, $end = null, $step = 1): NdArray
25     {
26         Assert::numeric($start);
27
28         if (is_null($end)) {
29             $end = $start > 0 ? $start : 0;
30             $start = $start > 0 ? 0 : $start;
31         }
32
33         Assert::numeric($end);
34         Assert::notEq($step, 0);
35         Assert::numeric($step);
36
37         if ($end < $start && $step === 1) {
38             $step = -1;
39         }
40
41         if ($start === $end) {
42             return static::ar([]);
43         }
44
45         if ($step < 0) {
46             Assert::greaterThan($start, $end);
47             if ($start + $step < $end) {
48                 return static::ar([$start]);
49             }
50             $end -= $step;
51         } else {
52             Assert::greaterThan($end, $start);
53
54             if ($end < $start + $step) {
55                 return static::ar([$start]);
56             }
57             $end -= $step;
58         }
59
60         return static::ar(
61             range($start, $end, $step)
62         );
63     }
64
65     /**
66      * Creates a n-dim array with a range of values
67      *
68      * @param int|float $start
69      * @param int|float $end
70      * @return \SciPhp\NdArray|array<\NumPhp\NdArray, $retstep>
71      *
72      * @link http://sciphp.org/numphp.linspace
73      *    Documentation for linspace()
74      *
75      * @api
76      */
77     final public static function linspace($start, $end, int $num = 50, bool $endpoint = true, bool $retstep = false)
78     {
79         Assert::numeric($start);
80         Assert::numeric($end);
81         Assert::integer($num);
82         Assert::greaterThanEq($num, 0, '$num must be non-negative. "%s" given.');
83
84         $step = $end - $start;
85
86         if ($num === 0) {
87             return ! $retstep
88                 ? static::ar([])
89                 : [static::ar([]), null];
90         }
91
92         if ($endpoint && $num === 1) {
93             $start = $end;
94         } elseif (! $endpoint && $num === 1) {
95             $end = $start;
96         } elseif ($endpoint) {
97             $step = ($end - $start) / ($num - 1);
98             $end = $start + $num * $step;
99         } elseif (! $endpoint) {
100             $step = ($end - $start) / $num;
101
102             // workaround because sometimes when $step is a float
103             // $start + $num * $step > $stop
104             $end = $start + $num * $step;
105         }
106
107         // range with same number
108         if ($start === $end) {
109             return ! $retstep
110                 ? static::ar(array_fill(0, $num, $start))
111                 : [static::ar(array_fill(0, $num, $start)), $step];
112         }
113
114         return ! $retstep
115             ? static::ar(
116                     array_slice(
117                         range($start, $end, $step), 0, $num
118                     )
119                 )
120             : [ static::ar(
121                         array_slice(
122                             range($start, $end, $step), 0, $num
123                         )
124                 ),
125                 $step
126             ];
127     }
128
129     /**
130      * Creates a NdArray with a range of values
131      *
132      * @param int|float $start
133      * @param int|float $end
134      * @param int $num
135      * @param bool $endpoint
136      * @param float $base
137      *
138      * @link http://sciphp.org/numphp.logspace
139      *    Documentation for logspace()
140      *
141      * @api
142      */
143     final public static function logspace($start, $end, int $num = 50, bool $endpoint = true, float $base = 10): NdArray
144     {
145         $func = static function (&$item) use ($base): void {
146             $item = pow($base, $item);
147         };
148
149         //return $this->copy()->walk_recursive($func);
150         return self::linspace($start, $end, $num, $endpoint)->walk_recursive($func);
151     }
152 }