deno.land / x / simplestatistic@v7.7.1 / src / root_mean_square.js

root_mean_square.js
نووسراو ببینە
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
/** * The Root Mean Square (RMS) is * a mean function used as a measure of the magnitude of a set * of numbers, regardless of their sign. * This is the square root of the mean of the squares of the * input numbers. * This runs in `O(n)`, linear time, with respect to the length of the array. * * @param {Array<number>} x a sample of one or more data points * @returns {number} root mean square * @throws {Error} if x is empty * @example * rootMeanSquare([-1, 1, -1, 1]); // => 1 */function rootMeanSquare(x) { if (x.length === 0) { throw new Error("rootMeanSquare requires at least one data point"); }
let sumOfSquares = 0; for (let i = 0; i < x.length; i++) { sumOfSquares += Math.pow(x[i], 2); }
return Math.sqrt(sumOfSquares / x.length);}
export default rootMeanSquare;
simplestatistic

Version Info

Tagged at
2 years ago