time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

E_A_Floats.js (2321B)


      1 import isInteger from "lodash/isInteger.js";
      2 import * as E_A from "./E_A.js";
      3 class RangeError extends Error {
      4 }
      5 export const range = (min, max, n) => {
      6     if (!isInteger(n)) {
      7         throw new Error("n must be integer");
      8     }
      9     else if (n < 0) {
     10         throw new RangeError("n must be greater than 0");
     11     }
     12     else if (min > max) {
     13         throw new RangeError("Min value is higher then max value");
     14     }
     15     if (n === 0) {
     16         return [];
     17     }
     18     else if (n === 1) {
     19         return [min];
     20     }
     21     else if (n === 2) {
     22         return [min, max];
     23     }
     24     else if (min === max) {
     25         return new Array(n).fill(min);
     26     }
     27     else {
     28         const diff = (max - min) / (n - 1);
     29         const result = [];
     30         for (let i = 0; i < n; i++) {
     31             result.push(min + i * diff);
     32         }
     33         return result;
     34     }
     35 };
     36 export const upTo = (low, high) => {
     37     return range(low, high, high - low + 1);
     38 };
     39 export const isSorted = (t) => {
     40     if (t.length <= 1) {
     41         return true;
     42     }
     43     for (let i = 0; i < t.length; i++) {
     44         if (t[i] >= t[i + 1]) {
     45             return false;
     46         }
     47     }
     48     return true;
     49 };
     50 export const sum = (t) => {
     51     let sum = 0;
     52     for (const v of t) {
     53         sum += v;
     54     }
     55     return sum;
     56 };
     57 export const product = (t) => {
     58     let prod = 1;
     59     let i = t.length;
     60     while (--i >= 0) {
     61         prod *= t[i];
     62     }
     63     return prod;
     64 };
     65 export const mean = (t) => {
     66     return sum(t) / t.length;
     67 };
     68 export const geomean = (t) => {
     69     return Math.pow(product(t), 1 / t.length);
     70 };
     71 export const sort = (t) => {
     72     return Array.from(new Float64Array(t).sort());
     73 };
     74 export const variance = (xs) => {
     75     const n = xs.length;
     76     const offset = (xs[0] + xs[n - 1]) / 2;
     77     let sum = 0;
     78     let sumsq = 0;
     79     for (let i = 0; i < n; i++) {
     80         const xOffset = xs[i] - offset;
     81         sum += xOffset;
     82         sumsq += xOffset * xOffset;
     83     }
     84     const mean = sum / n;
     85     return sumsq / n - mean * mean;
     86 };
     87 export const stdev = (t) => Math.sqrt(variance(t));
     88 export const cumSum = (t) => {
     89     return E_A.accumulate(t, (a, b) => a + b);
     90 };
     91 export const cumProd = (t) => {
     92     return E_A.accumulate(t, (a, b) => a * b);
     93 };
     94 export const diff = (t) => {
     95     return E_A.pairwise(t, (left, right) => right - left);
     96 };
     97 //# sourceMappingURL=E_A_Floats.js.map