diff --git a/src/index.js b/src/index.js index 3420d167..487d3efd 100644 --- a/src/index.js +++ b/src/index.js @@ -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"; diff --git a/src/linspace.js b/src/linspace.js new file mode 100644 index 00000000..09069906 --- /dev/null +++ b/src/linspace.js @@ -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; +} diff --git a/test/linspace-test.js b/test/linspace-test.js new file mode 100644 index 00000000..41492cd0 --- /dev/null +++ b/test/linspace-test.js @@ -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(); +});