All files / lib/utils math_utils.ts

0% Statements 0/10
0% Branches 0/1
0% Functions 0/1
0% Lines 0/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15                             
export const clamp = (v: number, min: number, max: number) => Math.max(min, Math.min(max, v));
 
export const mean = (arr: number[]) =>
  arr.length ? arr.reduce((a, b) => a + b, 0) / arr.length : 0;
 
export const variance = (arr: number[]) => {
  if (!arr.length) return 0;
  const m = mean(arr);
  return mean(arr.map((x) => (x - m) ** 2));
};
 
export const momentum = (delta: number, prevDelta: number) => 0.7 * delta + 0.3 * prevDelta; // smoothing
 
export const sign = (x: number) => (x > 0 ? 1 : x < 0 ? -1 : 0);