Skip to content

Commit

Permalink
Add d3.linspace.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jun 11, 2020
1 parent a286cfa commit cf59fff
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export {default as permute} from "./permute.js";
export {default as quantile, quantileSorted} from "./quantile.js";
export {default as quickselect} from "./quickselect.js";
export {default as range} from "./range.js";
export {default as linspace} from "./linspace.js";
export {default as least} from "./least.js";
export {default as leastIndex} from "./leastIndex.js";
export {default as greatest} from "./greatest.js";
Expand Down
12 changes: 12 additions & 0 deletions src/linspace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default function(start, stop, n = 50, endpoint = true) {
n = Math.floor(n), start = +start, stop = (stop - start) / (endpoint ? n - 1 : n);

var i = -1,
range = new Array(n);

while (++i < n) {
range[i] = start + i * stop;
}

return range;
}
23 changes: 23 additions & 0 deletions test/linspace-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var tape = require("tape"),
arrays = require("../");

tape.only("linspace(start, stop)", function(test) {
test.deepEqual(arrays.linspace(0, 49), [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49
]);
test.end();
});

tape("linspace(start, stop, n)", function(test) {
test.deepEqual(arrays.linspace(2, 3, 5), [2, 2.25, 2.5, 2.75, 3]);
test.end();
});

tape("linspace(start, stop, n, false)", function(test) {
test.deepEqual(arrays.linspace(2, 3, 5, false), [2, 2.2, 2.4, 2.6, 2.8]);
test.end();
});

0 comments on commit cf59fff

Please sign in to comment.