Skip to content

Commit 6b9d2fa

Browse files
committed
Fixed Vector linspace precision
1 parent 87e4a75 commit 6b9d2fa

File tree

4 files changed

+43
-18
lines changed

4 files changed

+43
-18
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
- Unreleased
2-
- Changed percentile method changed to quantile
1+
- 2.0.5
2+
- Percentile method changed to quantile
3+
- Fixed Vector linspace precision
34

45
- 2.0.4
56
- Individual Arithmetic and Comparison methods now public

ext/include/conversion.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifdef HAVE_CONFIG_H
2+
#include "config.h"
3+
#endif
4+
5+
#include "php.h"
6+
#include "kernel/operators.h"
7+
8+
void hashmap_to_c_array(zval *return_value, zval *values)
9+
{
10+
zval *current;
11+
zend_ulong index;
12+
13+
ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(values), index, current) {
14+
15+
}
16+
}

src/Vector.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -242,26 +242,34 @@ public static function range($start, $end, $interval = 1) : self
242242
}
243243

244244
/**
245-
* Return evenly spaced numbers over a specified interval.
245+
* Return n evenly spaced numbers between minimum and maximum.
246246
*
247-
* @param float $start
248-
* @param float $end
247+
* @param float $min
248+
* @param float $max
249249
* @param int $n
250250
* @throws \InvalidArgumentException
251251
* @return self
252252
*/
253-
public static function linspace(float $start, float $end, int $n) : self
253+
public static function linspace(float $min, float $max, int $n) : self
254254
{
255-
if ($n < 1) {
256-
throw new InvalidArgumentException('The number of elements'
257-
. " must be greater than 0, $n given.");
255+
if ($n < 2) {
256+
throw new InvalidArgumentException('Number of elements'
257+
. " must be greater than 1, $n given.");
258258
}
259259

260-
$range = abs($end - $start);
260+
$k = $n - 1;
261+
262+
$interval = abs($max - $min) / $k;
263+
264+
$a = [$min];
265+
266+
while (count($a) < $k) {
267+
$a[] = end($a) + $interval;
268+
}
261269

262-
$interval = ($range / ($n - 1)) - (EPSILON * $range);
270+
$a[] = $max;
263271

264-
return static::range($start, $end, $interval);
272+
return self::quick($a);
265273
}
266274

267275
/**

tests/VectorTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ public function uniform() : void
158158
*/
159159
public function range() : void
160160
{
161-
$z = Vector::range(5.0, 12.0, 2.);
161+
$z = Vector::range(5.0, 12.0, 2.0);
162162

163-
$expected = [5.0, 7.0, 9.0, 11.];
163+
$expected = [5.0, 7.0, 9.0, 11.0];
164164

165165
$this->assertInstanceOf(Vector::class, $z);
166166
$this->assertEquals($expected, $z->asArray());
@@ -171,15 +171,15 @@ public function range() : void
171171
*/
172172
public function linspace() : void
173173
{
174-
$z = Vector::linspace(-5, 5, 9);
174+
$z = Vector::linspace(-5, 5, 10);
175175

176176
$expected = [
177-
-5.0, -3.7500001000000003, -2.5000002, -1.2500003, -4.0000000023354687E-7,
178-
1.2499994999999995, 2.4999994, 3.749999299999999, 4.9999991999999995,
177+
-5.0, -3.888888888888889, -2.7777777777777777, -1.6666666666666665, -0.5555555555555554,
178+
0.5555555555555558, 1.666666666666667, 2.777777777777778, 3.8888888888888893, 5.0,
179179
];
180180

181181
$this->assertInstanceOf(Vector::class, $z);
182-
$this->assertCount(9, $z);
182+
$this->assertCount(10, $z);
183183
$this->assertEquals($expected, $z->asArray());
184184
}
185185

0 commit comments

Comments
 (0)