Skip to content

Commit 0d46ff1

Browse files
committed
Add d3.linspace.
1 parent d091c4b commit 0d46ff1

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ Returns a new interval [*niceStart*, *niceStop*] covering the given interval [*s
652652

653653
<a name="range" href="#range">#</a> d3.<b>range</b>([<i>start</i>, ]<i>stop</i>[, <i>step</i>]) · [Source](https://github.com/d3/d3-array/blob/main/src/range.js), [Examples](https://observablehq.com/@d3/d3-range)
654654

655-
Returns an array containing an arithmetic progression, similar to the Python built-in [range](http://docs.python.org/library/functions.html#range). This method is often used to iterate over a sequence of uniformly-spaced numeric values, such as the indexes of an array or the ticks of a linear scale. (See also [d3.ticks](#ticks) for nicely-rounded values.)
655+
Returns an array containing an arithmetic progression, similar to the Python built-in [range](http://docs.python.org/library/functions.html#range). This method is often used to iterate over a sequence of uniformly-spaced numeric values, such as the indexes of an array or the ticks of a linear scale. (See also [d3.ticks](#ticks) for nicely-rounded values, and [d3.linspace](#linspace) if you prefer to specify the number of values rather than the step.)
656656

657657
If *step* is omitted, it defaults to 1. If *start* is omitted, it defaults to 0. The *stop* value is exclusive; it is not included in the result. If *step* is positive, the last element is the largest *start* + *i* \* *step* less than *stop*; if *step* is negative, the last element is the smallest *start* + *i* \* *step* greater than *stop*. If the returned array would contain an infinite number of values, an empty range is returned.
658658

@@ -671,6 +671,15 @@ d3.range(0, 1, 1 / 49); // BAD: returns 50 elements!
671671
d3.range(49).map(function(d) { return d / 49; }); // GOOD: returns 49 elements.
672672
```
673673

674+
<a name="linspace" href="#linspace">#</a> d3.<b>linspace</b>(<i>start</i>, <i>stop</i>[, <i>n</i>][, <i>endpoint</i>]) · [Source](https://github.com/d3/d3-array/blob/main/src/linspace.js)<!-- , [Examples](https://observablehq.com/@d3/d3-linspace) -->
675+
676+
Returns an array containing an arithmetic progression of *n* values ranging from *start* to *end*, including *end* if *endpoint* is requested. Similar to numpy’s [linspace](https://numpy.org/doc/stable/reference/generated/numpy.linspace.html). Defaults to *n* = 50 and *endpoint* = true.
677+
678+
```js
679+
d3.linspace(0, 1, 101); // [0, 0.01, 0.02, …, 0.99, 1]
680+
d3.linspace(1, 0, 10, false); // [1, 0.9, 0.8, …, 0.1]
681+
```
682+
674683
<a name="transpose" href="#transpose">#</a> d3.<b>transpose</b>(<i>matrix</i>) · [Source](https://github.com/d3/d3-array/blob/main/src/transpose.js), [Examples](https://observablehq.com/@d3/d3-transpose)
675684

676685
Uses the [zip](#zip) operator as a two-dimensional [matrix transpose](http://en.wikipedia.org/wiki/Transpose).

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export {default as quantile, quantileSorted} from "./quantile.js";
3030
export {default as quickselect} from "./quickselect.js";
3131
export {default as range} from "./range.js";
3232
export {default as rank} from "./rank.js";
33+
export {default as linspace} from "./linspace.js";
3334
export {default as least} from "./least.js";
3435
export {default as leastIndex} from "./leastIndex.js";
3536
export {default as greatest} from "./greatest.js";

src/linspace.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default function(start, stop, n = 50, endpoint = true) {
2+
n = Math.floor(n);
3+
start = +start;
4+
stop = (stop - start) / (endpoint ? n - 1 : n);
5+
6+
const range = new Array(n);
7+
for (let i = 0; i < n; ++i) range[i] = start + i * stop;
8+
9+
return range;
10+
}

test/linspace-test.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import assert from "assert";
2+
import {linspace} from "../src/index.js";
3+
4+
it("linspace(start, stop)", () => {
5+
assert.deepEqual(linspace(0, 49), [
6+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
7+
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
8+
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
9+
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
10+
40, 41, 42, 43, 44, 45, 46, 47, 48, 49
11+
]);
12+
});
13+
14+
it("linspace(start, stop, n)", () => {
15+
assert.deepEqual(linspace(2, 3, 5), [2, 2.25, 2.5, 2.75, 3]);
16+
});
17+
18+
it("linspace(start, stop, n, false)", () => {
19+
assert.deepEqual(linspace(2, 3, 5, false), [2, 2.2, 2.4, 2.6, 2.8]);
20+
});
21+
22+
it("linspace(start, stop, n) descending", () => {
23+
assert.deepEqual(linspace(5, 1, 5), [5, 4, 3, 2, 1]);
24+
assert.deepEqual(linspace(5, 0, 5, false), [5, 4, 3, 2, 1]);
25+
});

0 commit comments

Comments
 (0)