From 96903e0266d47727c9be617078169ee946a35497 Mon Sep 17 00:00:00 2001 From: RebeccaHirst <30597180+RebeccaHirst@users.noreply.github.com> Date: Thu, 6 Jan 2022 16:39:08 +0000 Subject: [PATCH] add linspace to util.js --- src/util/Util.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/util/Util.js b/src/util/Util.js index 2e01674..3d93bbf 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -358,6 +358,24 @@ export function shuffle(array, randomNumberGenerator = undefined) return array; } +/** + * linspace + * + * @name module:util.linspace + * @function + * @public + * @param {Object[]} startValue, stopValue, cardinality + * @return {Object[]} an array from startValue to stopValue with cardinality steps + */ +export function linspace(startValue, stopValue, cardinality) { + var arr = []; + var step = (stopValue - startValue) / (cardinality - 1); + for (var i = 0; i < cardinality; i++) { + arr.push(startValue + (step * i)); + } + return arr; +} + /** * Pick a random value from an array, uses `util.shuffle` to shuffle the array and returns the last value. *