Skip to content

Commit

Permalink
refactor(math): use new function aliases, update deps
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Sep 5, 2020
1 parent 0d9a0de commit dd0337f
Show file tree
Hide file tree
Showing 13 changed files with 167 additions and 217 deletions.
9 changes: 5 additions & 4 deletions packages/math/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
"typedoc": "^0.18.0",
"typescript": "^4.0.2"
},
"dependencies": {
"@thi.ng/api": "^6.12.3",
"tslib": "^2.0.1"
},
"files": [
"*.js",
"*.d.ts",
Expand All @@ -65,8 +69,5 @@
"publishConfig": {
"access": "public"
},
"sideEffects": false,
"dependencies": {
"tslib": "^2.0.1"
}
"sideEffects": false
}
3 changes: 2 additions & 1 deletion packages/math/src/abs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FnN2 } from "@thi.ng/api";
import { EPS } from "./api";

export const absDiff = (x: number, y: number) => Math.abs(x - y);
export const absDiff: FnN2 = (x, y) => Math.abs(x - y);

export const sign = (x: number, eps = EPS) => (x > eps ? 1 : x < -eps ? -1 : 0);
31 changes: 16 additions & 15 deletions packages/math/src/angle.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { FnN, FnN2, FnN3 } from "@thi.ng/api";
import { DEG2RAD, HALF_PI, INV_HALF_PI, PI, RAD2DEG, TAU } from "./api";

/**
Expand Down Expand Up @@ -27,11 +28,11 @@ export const cossin = (theta: number, n = 1) => [
*
* @param theta -
*/
export const absTheta = (theta: number) => (
export const absTheta: FnN = (theta) => (
(theta %= TAU), theta < 0 ? TAU + theta : theta
);

export const absInnerAngle = (theta: number) => (
export const absInnerAngle: FnN = (theta) => (
(theta = Math.abs(theta)), theta > PI ? TAU - theta : theta
);

Expand All @@ -42,7 +43,7 @@ export const absInnerAngle = (theta: number) => (
* @param a -
* @param b -
*/
export const angleDist = (a: number, b: number) =>
export const angleDist: FnN2 = (a, b) =>
absInnerAngle(absTheta((b % TAU) - (a % TAU)));

/**
Expand All @@ -51,49 +52,49 @@ export const angleDist = (a: number, b: number) =>
* @param y -
* @param x -
*/
export const atan2Abs = (y: number, x: number) => absTheta(Math.atan2(y, x));
export const atan2Abs: FnN2 = (y, x) => absTheta(Math.atan2(y, x));

/**
* Returns quadrant ID (0-3) of given angle (in radians).
*
* @param theta -
*/
export const quadrant = (theta: number) => (absTheta(theta) * INV_HALF_PI) | 0;
export const quadrant: FnN = (theta) => (absTheta(theta) * INV_HALF_PI) | 0;

/**
* Converts angle to degrees.
*
* @param theta - angle in radians
*/
export const deg = (theta: number) => theta * RAD2DEG;
export const deg: FnN = (theta) => theta * RAD2DEG;

/**
* Converts angle to radians.
*
* @param theta - angle in degrees
*/
export const rad = (theta: number) => theta * DEG2RAD;
export const rad: FnN = (theta) => theta * DEG2RAD;

/**
* Cosecant. Approaches `±Infinity` for `theta` near multiples of π.
*
* @param theta - angle in radians
*/
export const csc = (theta: number) => 1 / Math.sin(theta);
export const csc: FnN = (theta) => 1 / Math.sin(theta);

/**
* Secant. Approaches `±Infinity` for `theta` near π/2 ± nπ
*
* @param theta - angle in radians
*/
export const sec = (theta: number) => 1 / Math.cos(theta);
export const sec: FnN = (theta) => 1 / Math.cos(theta);

/**
* Cotangent. Approaches `±Infinity` for `theta` near multiples of π.
*
* @param theta - angle in radians
*/
export const cot = (theta: number) => 1 / Math.tan(theta);
export const cot: FnN = (theta) => 1 / Math.tan(theta);

/**
* Law of Cosines. Takes length of two sides of a triangle and the inner
Expand All @@ -103,20 +104,20 @@ export const cot = (theta: number) => 1 / Math.tan(theta);
* @param b -
* @param gamma -
*/
export const loc = (a: number, b: number, gamma: number) =>
export const loc: FnN3 = (a, b, gamma) =>
Math.sqrt(a * a + b * b - 2 * a * b * Math.cos(gamma));

/**
* Approximates cos(xπ) for x in [-1,1]
*
* @param x -
*/
export const normCos = (x: number) => {
export const normCos: FnN = (x) => {
const x2 = x * x;
return 1.0 + x2 * (-4 + 2 * x2);
};

const __fastCos = (x: number) => {
const __fastCos: FnN = (x) => {
const x2 = x * x;
return 0.99940307 + x2 * (-0.49558072 + 0.03679168 * x2);
};
Expand All @@ -129,7 +130,7 @@ const __fastCos = (x: number) => {
*
* @param theta - in radians
*/
export const fastCos = (theta: number) => {
export const fastCos: FnN = (theta) => {
theta %= TAU;
theta < 0 && (theta = -theta);
switch ((theta * INV_HALF_PI) | 0) {
Expand All @@ -149,4 +150,4 @@ export const fastCos = (theta: number) => {
*
* @param theta - in radians
*/
export const fastSin = (theta: number) => fastCos(HALF_PI - theta);
export const fastSin: FnN = (theta) => fastCos(HALF_PI - theta);
5 changes: 3 additions & 2 deletions packages/math/src/crossing.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { FnU4 } from "@thi.ng/api";
import { Crossing, EPS } from "./api";
import { eqDelta } from "./eqdelta";

Expand All @@ -17,7 +18,7 @@ import { eqDelta } from "./eqdelta";
* @param b1 -
* @param b2 -
*/
export const isCrossOver = (a1: number, a2: number, b1: number, b2: number) =>
export const isCrossOver: FnU4<number, boolean> = (a1, a2, b1, b2) =>
a1 < b1 && a2 > b2;

/**
Expand All @@ -36,7 +37,7 @@ export const isCrossOver = (a1: number, a2: number, b1: number, b2: number) =>
* @param b1 -
* @param b2 -
*/
export const isCrossUnder = (a1: number, a2: number, b1: number, b2: number) =>
export const isCrossUnder: FnU4<number, boolean> = (a1, a2, b1, b2) =>
a1 > b1 && a2 < b2;

/**
Expand Down
6 changes: 4 additions & 2 deletions packages/math/src/extrema.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { FnU3 } from "@thi.ng/api";

/**
* Returns true if `b` is a local minima, i.e. iff a > b and b < c.
*
* @param a -
* @param b -
* @param c -
*/
export const isMinima = (a: number, b: number, c: number) => a > b && b < c;
export const isMinima: FnU3<number, boolean> = (a, b, c) => a > b && b < c;

/**
* Returns true if `b` is a local maxima, i.e. iff a < b and b > c.
Expand All @@ -14,7 +16,7 @@ export const isMinima = (a: number, b: number, c: number) => a > b && b < c;
* @param b -
* @param c -
*/
export const isMaxima = (a: number, b: number, c: number) => a < b && b > c;
export const isMaxima: FnU3<number, boolean> = (a, b, c) => a < b && b > c;

const index = (
pred: (a: number, b: number, c: number) => boolean,
Expand Down
25 changes: 8 additions & 17 deletions packages/math/src/fit.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { FnN3, FnN5 } from "@thi.ng/api";
import { clamp01, clamp11 } from "./interval";

/**
Expand All @@ -8,25 +9,15 @@ import { clamp01, clamp11 } from "./interval";
* @param a -
* @param b -
*/
export const norm = (x: number, a: number, b: number) =>
b !== a ? (x - a) / (b - a) : 0;
export const norm: FnN3 = (x, a, b) => (b !== a ? (x - a) / (b - a) : 0);

export const fit = (x: number, a: number, b: number, c: number, d: number) =>
c + (d - c) * norm(x, a, b);
export const fit: FnN5 = (x, a, b, c, d) => c + (d - c) * norm(x, a, b);

export const fitClamped = (
x: number,
a: number,
b: number,
c: number,
d: number
) => c + (d - c) * clamp01(norm(x, a, b));
export const fitClamped: FnN5 = (x, a, b, c, d) =>
c + (d - c) * clamp01(norm(x, a, b));

export const fit01 = (x: number, a: number, b: number) =>
a + (b - a) * clamp01(x);
export const fit01: FnN3 = (x, a, b) => a + (b - a) * clamp01(x);

export const fit10 = (x: number, a: number, b: number) =>
b + (a - b) * clamp01(x);
export const fit10: FnN3 = (x, a, b) => b + (a - b) * clamp01(x);

export const fit11 = (x: number, a: number, b: number) =>
a + (b - a) * (0.5 + 0.5 * clamp11(x));
export const fit11: FnN3 = (x, a, b) => a + (b - a) * (0.5 + 0.5 * clamp11(x));
151 changes: 64 additions & 87 deletions packages/math/src/int.ts
Original file line number Diff line number Diff line change
@@ -1,96 +1,73 @@
import type { FnN, FnN2 } from "@thi.ng/api";

const M8 = 0xff;
const M16 = 0xffff;

export const signExtend8 = (a: number) => ((a &= M8), a & 0x80 ? a | ~M8 : a);

export const signExtend16 = (a: number) => (
(a &= M16), a & 0x8000 ? a | ~M16 : a
);
export const signExtend8: FnN = (a) => ((a &= M8), a & 0x80 ? a | ~M8 : a);
export const signExtend16: FnN = (a) => ((a &= M16), a & 0x8000 ? a | ~M16 : a);

export const addi8 = (a: number, b: number) => signExtend8((a | 0) + (b | 0));
export const divi8 = (a: number, b: number) => signExtend8((a | 0) / (b | 0));
export const muli8 = (a: number, b: number) => signExtend8((a | 0) * (b | 0));
export const subi8 = (a: number, b: number) => signExtend8((a | 0) - (b | 0));
export const andi8 = (a: number, b: number) => signExtend8((a | 0) & (b | 0));
export const ori8 = (a: number, b: number) => signExtend8(a | 0 | (b | 0));
export const xori8 = (a: number, b: number) => signExtend8((a | 0) ^ (b | 0));
export const noti8 = (a: number) => signExtend8(~a);
// prettier-ignore
export const lshifti8 = (a: number, b: number) => signExtend8((a | 0) << (b | 0));
// prettier-ignore
export const rshifti8 = (a: number, b: number) => signExtend8((a | 0) >> (b | 0));
export const addi8: FnN2 = (a, b) => signExtend8((a | 0) + (b | 0));
export const divi8: FnN2 = (a, b) => signExtend8((a | 0) / (b | 0));
export const muli8: FnN2 = (a, b) => signExtend8((a | 0) * (b | 0));
export const subi8: FnN2 = (a, b) => signExtend8((a | 0) - (b | 0));
export const andi8: FnN2 = (a, b) => signExtend8((a | 0) & (b | 0));
export const ori8: FnN2 = (a, b) => signExtend8(a | 0 | (b | 0));
export const xori8: FnN2 = (a, b) => signExtend8((a | 0) ^ (b | 0));
export const noti8: FnN = (a) => signExtend8(~a);
export const lshifti8: FnN2 = (a, b) => signExtend8((a | 0) << (b | 0));
export const rshifti8: FnN2 = (a, b) => signExtend8((a | 0) >> (b | 0));

export const addi16 = (a: number, b: number) => signExtend16((a | 0) + (b | 0));
export const divi16 = (a: number, b: number) => signExtend16((a | 0) / (b | 0));
export const muli16 = (a: number, b: number) => signExtend16((a | 0) * (b | 0));
export const subi16 = (a: number, b: number) => signExtend16((a | 0) - (b | 0));
export const andi16 = (a: number, b: number) => signExtend16((a | 0) & (b | 0));
export const ori16 = (a: number, b: number) => signExtend16(a | 0 | (b | 0));
export const xori16 = (a: number, b: number) => signExtend16((a | 0) ^ (b | 0));
export const noti16 = (a: number) => signExtend16(~a);
// prettier-ignore
export const lshifti16 = (a: number, b: number) => signExtend16((a | 0) << (b | 0));
// prettier-ignore
export const rshifti16 = (a: number, b: number) => signExtend16((a | 0) >> (b | 0));
export const addi16: FnN2 = (a, b) => signExtend16((a | 0) + (b | 0));
export const divi16: FnN2 = (a, b) => signExtend16((a | 0) / (b | 0));
export const muli16: FnN2 = (a, b) => signExtend16((a | 0) * (b | 0));
export const subi16: FnN2 = (a, b) => signExtend16((a | 0) - (b | 0));
export const andi16: FnN2 = (a, b) => signExtend16((a | 0) & (b | 0));
export const ori16: FnN2 = (a, b) => signExtend16(a | 0 | (b | 0));
export const xori16: FnN2 = (a, b) => signExtend16((a | 0) ^ (b | 0));
export const noti16: FnN = (a) => signExtend16(~a);
export const lshifti16: FnN2 = (a, b) => signExtend16((a | 0) << (b | 0));
export const rshifti16: FnN2 = (a, b) => signExtend16((a | 0) >> (b | 0));

export const addi32 = (a: number, b: number) => ((a | 0) + (b | 0)) | 0;
export const divi32 = (a: number, b: number) => ((a | 0) / (b | 0)) | 0;
export const muli32 = (a: number, b: number) => ((a | 0) * (b | 0)) | 0;
export const subi32 = (a: number, b: number) => ((a | 0) - (b | 0)) | 0;
export const andi32 = (a: number, b: number) => (a | 0) & (b | 0);
export const ori32 = (a: number, b: number) => a | 0 | (b | 0);
export const xori32 = (a: number, b: number) => (a | 0) ^ (b | 0);
export const lshifti32 = (a: number, b: number) => (a | 0) << (b | 0);
export const rshifti32 = (a: number, b: number) => (a | 0) >> (b | 0);
export const noti32 = (a: number) => ~a;
export const addi32: FnN2 = (a, b) => ((a | 0) + (b | 0)) | 0;
export const divi32: FnN2 = (a, b) => ((a | 0) / (b | 0)) | 0;
export const muli32: FnN2 = (a, b) => ((a | 0) * (b | 0)) | 0;
export const subi32: FnN2 = (a, b) => ((a | 0) - (b | 0)) | 0;
export const andi32: FnN2 = (a, b) => (a | 0) & (b | 0);
export const ori32: FnN2 = (a, b) => a | 0 | (b | 0);
export const xori32: FnN2 = (a, b) => (a | 0) ^ (b | 0);
export const lshifti32: FnN2 = (a, b) => (a | 0) << (b | 0);
export const rshifti32: FnN2 = (a, b) => (a | 0) >> (b | 0);
export const noti32: FnN = (a) => ~a;

// prettier-ignore
export const addu8 = (a: number, b: number) => ((a & M8) + (b & M8)) & M8;
// prettier-ignore
export const divu8 = (a: number, b: number) => ((a & M8) / (b & M8)) & M8;
// prettier-ignore
export const mulu8 = (a: number, b: number) => ((a & M8) * (b & M8)) & M8;
// prettier-ignore
export const subu8 = (a: number, b: number) => ((a & M8) - (b & M8)) & M8;
// prettier-ignore
export const andu8 = (a: number, b: number) => ((a & M8) & (b & M8)) & M8;
// prettier-ignore
export const oru8 = (a: number, b: number) => ((a & M8) | (b & M8)) & M8;
// prettier-ignore
export const xoru8 = (a: number, b: number) => ((a & M8) ^ (b & M8)) & M8;
export const notu8 = (a: number) => ~a & M8;
export const lshiftu8 = (a: number, b: number) => ((a & M8) << (b & M8)) & M8;
export const rshiftu8 = (a: number, b: number) => ((a & M8) >>> (b & M8)) & M8;
export const addu8: FnN2 = (a, b) => ((a & M8) + (b & M8)) & M8;
export const divu8: FnN2 = (a, b) => ((a & M8) / (b & M8)) & M8;
export const mulu8: FnN2 = (a, b) => ((a & M8) * (b & M8)) & M8;
export const subu8: FnN2 = (a, b) => ((a & M8) - (b & M8)) & M8;
export const andu8: FnN2 = (a, b) => a & M8 & (b & M8) & M8;
export const oru8: FnN2 = (a, b) => ((a & M8) | (b & M8)) & M8;
export const xoru8: FnN2 = (a, b) => ((a & M8) ^ (b & M8)) & M8;
export const notu8: FnN = (a) => ~a & M8;
export const lshiftu8: FnN2 = (a, b) => ((a & M8) << (b & M8)) & M8;
export const rshiftu8: FnN2 = (a, b) => ((a & M8) >>> (b & M8)) & M8;

// prettier-ignore
export const addu16 = (a: number, b: number) => ((a & M16) + (b & M16)) & M16;
// prettier-ignore
export const divu16 = (a: number, b: number) => ((a & M16) / (b & M16)) & M16;
// prettier-ignore
export const mulu16 = (a: number, b: number) => ((a & M16) * (b & M16)) & M16;
// prettier-ignore
export const subu16 = (a: number, b: number) => ((a & M16) - (b & M16)) & M16;
// prettier-ignore
export const andu16 = (a: number, b: number) => ((a & M16) & (b & M16)) & M16;
// prettier-ignore
export const oru16 = (a: number, b: number) => ((a & M16) | (b & M16)) & M16;
// prettier-ignore
export const xoru16 = (a: number, b: number) => ((a & M16) ^ (b & M16)) & M16;
export const notu16 = (a: number) => ~a & M16;
// prettier-ignore
export const lshiftu16 = (a: number, b: number) => ((a & M16) << (b & M16)) & M16;
// prettier-ignore
export const rshiftu16 = (a: number, b: number) => ((a & M16) >>> (b & M16)) & M16;
export const addu16: FnN2 = (a, b) => ((a & M16) + (b & M16)) & M16;
export const divu16: FnN2 = (a, b) => ((a & M16) / (b & M16)) & M16;
export const mulu16: FnN2 = (a, b) => ((a & M16) * (b & M16)) & M16;
export const subu16: FnN2 = (a, b) => ((a & M16) - (b & M16)) & M16;
export const andu16: FnN2 = (a, b) => a & M16 & (b & M16) & M16;
export const oru16: FnN2 = (a, b) => ((a & M16) | (b & M16)) & M16;
export const xoru16: FnN2 = (a, b) => ((a & M16) ^ (b & M16)) & M16;
export const notu16: FnN = (a) => ~a & M16;
export const lshiftu16: FnN2 = (a, b) => ((a & M16) << (b & M16)) & M16;
export const rshiftu16: FnN2 = (a, b) => ((a & M16) >>> (b & M16)) & M16;

export const addu32 = (a: number, b: number) => ((a >>> 0) + (b >>> 0)) >>> 0;
export const divu32 = (a: number, b: number) => ((a >>> 0) / (b >>> 0)) >>> 0;
export const mulu32 = (a: number, b: number) => ((a >>> 0) * (b >>> 0)) >>> 0;
export const subu32 = (a: number, b: number) => ((a >>> 0) - (b >>> 0)) >>> 0;
export const andu32 = (a: number, b: number) => ((a >>> 0) & (b >>> 0)) >>> 0;
export const oru32 = (a: number, b: number) => ((a >>> 0) | (b >>> 0)) >>> 0;
export const xoru32 = (a: number, b: number) => ((a >>> 0) ^ (b >>> 0)) >>> 0;
export const notu32 = (a: number) => ~a >>> 0;
// prettier-ignore
export const lshiftu32 = (a: number, b: number) => ((a >>> 0) << (b >>> 0)) >>> 0;
// prettier-ignore
export const rshiftu32 = (a: number, b: number) => ((a >>> 0) >>> (b >>> 0)) >>> 0;
export const addu32: FnN2 = (a, b) => ((a >>> 0) + (b >>> 0)) >>> 0;
export const divu32: FnN2 = (a, b) => ((a >>> 0) / (b >>> 0)) >>> 0;
export const mulu32: FnN2 = (a, b) => ((a >>> 0) * (b >>> 0)) >>> 0;
export const subu32: FnN2 = (a, b) => ((a >>> 0) - (b >>> 0)) >>> 0;
export const andu32: FnN2 = (a, b) => ((a >>> 0) & (b >>> 0)) >>> 0;
export const oru32: FnN2 = (a, b) => ((a >>> 0) | (b >>> 0)) >>> 0;
export const xoru32: FnN2 = (a, b) => ((a >>> 0) ^ (b >>> 0)) >>> 0;
export const notu32: FnN = (a) => ~a >>> 0;
export const lshiftu32: FnN2 = (a, b) => ((a >>> 0) << (b >>> 0)) >>> 0;
export const rshiftu32: FnN2 = (a, b) => ((a >>> 0) >>> (b >>> 0)) >>> 0;
Loading

0 comments on commit dd0337f

Please sign in to comment.