Socket
Socket
Sign inDemoInstall

@thi.ng/math

Package Overview
Dependencies
Maintainers
1
Versions
160
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@thi.ng/math - npm Package Compare versions

Comparing version 0.2.2 to 1.0.0

lib/index.js

8

abs.js

@@ -1,5 +0,3 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const api_1 = require("./api");
exports.absDiff = (x, y) => Math.abs(x - y);
exports.sign = (x, eps = api_1.EPS) => x > eps ? 1 : x < -eps ? -1 : 0;
import { EPS } from "./api";
export const absDiff = (x, y) => Math.abs(x - y);
export const sign = (x, eps = EPS) => x > eps ? 1 : x < -eps ? -1 : 0;

@@ -1,3 +0,5 @@

export declare const sincos: (theta: number) => number[];
export declare const sincos: (theta: number, n?: number) => number[];
export declare const cossin: (theta: number, n?: number) => number[];
export declare const absTheta: (theta: number) => number;
export declare const absInnerAngle: (x: number) => number;
export declare const angleDist: (a: number, b: number) => number;

@@ -4,0 +6,0 @@ export declare const atan2Abs: (y: number, x: number) => number;

@@ -1,9 +0,9 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const api_1 = require("./api");
exports.sincos = (theta) => [Math.sin(theta), Math.cos(theta)];
exports.absTheta = (theta) => (theta %= api_1.TAU, theta < 0 ? api_1.TAU + theta : theta);
exports.angleDist = (a, b) => (a = exports.absTheta((b % api_1.TAU) - (a % api_1.TAU)), a > api_1.PI ? api_1.TAU - a : a);
exports.atan2Abs = (y, x) => exports.absTheta(Math.atan2(y, x));
exports.quadrant = (theta) => (exports.absTheta(theta) / api_1.HALF_PI) | 0;
import { DEG2RAD, HALF_PI, PI, RAD2DEG, TAU } from "./api";
export const sincos = (theta, n = 1) => [Math.sin(theta) * n, Math.cos(theta) * n];
export const cossin = (theta, n = 1) => [Math.cos(theta) * n, Math.sin(theta) * n];
export const absTheta = (theta) => (theta %= TAU, theta < 0 ? TAU + theta : theta);
export const absInnerAngle = (x) => (x = Math.abs(x), x > PI ? TAU - x : x);
export const angleDist = (a, b) => absInnerAngle(absTheta((b % TAU) - (a % TAU)));
export const atan2Abs = (y, x) => absTheta(Math.atan2(y, x));
export const quadrant = (theta) => (absTheta(theta) / HALF_PI) | 0;
/**

@@ -14,3 +14,3 @@ * Converts angle to degrees.

*/
exports.deg = (x) => x * api_1.RAD2DEG;
export const deg = (x) => x * RAD2DEG;
/**

@@ -21,2 +21,2 @@ * Converts angle to radians.

*/
exports.rad = (x) => x * api_1.DEG2RAD;
export const rad = (x) => x * DEG2RAD;

@@ -11,2 +11,5 @@ export declare const PI: number;

export declare const SQRT3: number;
export declare const THIRD: number;
export declare const TWO_THIRD: number;
export declare const SIXTH: number;
export declare let EPS: number;

@@ -1,13 +0,14 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PI = Math.PI;
exports.TAU = exports.PI * 2;
exports.HALF_PI = exports.PI / 2;
exports.THIRD_PI = exports.PI / 3;
exports.QUARTER_PI = exports.PI / 4;
exports.SIXTH_PI = exports.PI / 6;
exports.DEG2RAD = exports.PI / 180;
exports.RAD2DEG = 180 / exports.PI;
exports.SQRT2 = Math.SQRT2;
exports.SQRT3 = Math.sqrt(3);
exports.EPS = 1e-6;
export const PI = Math.PI;
export const TAU = PI * 2;
export const HALF_PI = PI / 2;
export const THIRD_PI = PI / 3;
export const QUARTER_PI = PI / 4;
export const SIXTH_PI = PI / 6;
export const DEG2RAD = PI / 180;
export const RAD2DEG = 180 / PI;
export const SQRT2 = Math.SQRT2;
export const SQRT3 = Math.sqrt(3);
export const THIRD = 1 / 3;
export const TWO_THIRD = 2 / 3;
export const SIXTH = 1 / 6;
export let EPS = 1e-6;

@@ -6,2 +6,32 @@ # Change Log

# [1.0.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/math@0.2.2...@thi.ng/math@1.0.0) (2019-01-21)
### Build System
* update package build scripts & outputs, imports in ~50 packages ([b54b703](https://github.com/thi-ng/umbrella/commit/b54b703))
### Features
* **math:** add absInnerAngle() ([a78bd87](https://github.com/thi-ng/umbrella/commit/a78bd87))
* **math:** add constants ([8fa05c3](https://github.com/thi-ng/umbrella/commit/8fa05c3))
* **math:** add cossin(), add opt scale arg for sincos() ([0043fb5](https://github.com/thi-ng/umbrella/commit/0043fb5))
* **math:** update eqDelta w/ adaptive eps, rename old => eqDeltaFixed ([5018009](https://github.com/thi-ng/umbrella/commit/5018009))
### BREAKING CHANGES
* enabled multi-outputs (ES6 modules, CJS, UMD)
- build scripts now first build ES6 modules in package root, then call
`scripts/bundle-module` to build minified CJS & UMD bundles in `/lib`
- all imports MUST be updated to only refer to package level
(not individual files anymore). tree shaking in user land will get rid of
all unused imported symbols.
## [0.2.2](https://github.com/thi-ng/umbrella/compare/@thi.ng/math@0.2.1...@thi.ng/math@0.2.2) (2018-12-15)

@@ -8,0 +38,0 @@

/**
* Checks if `|a - b| <= ε`.
* Checks if `|a - b| <= ε` and adapts given epsilon value to the given
* arguments:
*
* ε is factored with the largest absolute value of `a` or `b` (but
* never lesser than the given `eps` value):
*
* `ε = ε * max(1, |a|, |b|)`
*
* @param a left value
* @param b right value
* @param eps epsilon / tolerance
* @param eps epsilon / tolerance, default `1e-6`
*/
export declare const eqDelta: (a: number, b: number, eps?: number) => boolean;
/**
* Similar to `eqDelta()`, but used given `eps` as is.
*
* @param a left value
* @param b right value
* @param eps epsilon / tolerance, default `1e-6`
*/
export declare const eqDeltaFixed: (a: number, b: number, eps?: number) => boolean;

@@ -1,14 +0,25 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const api_1 = require("./api");
import { EPS } from "./api";
const abs = Math.abs;
const max = Math.max;
/**
* Checks if `|a - b| <= ε`.
* Checks if `|a - b| <= ε` and adapts given epsilon value to the given
* arguments:
*
* ε is factored with the largest absolute value of `a` or `b` (but
* never lesser than the given `eps` value):
*
* `ε = ε * max(1, |a|, |b|)`
*
* @param a left value
* @param b right value
* @param eps epsilon / tolerance
* @param eps epsilon / tolerance, default `1e-6`
*/
exports.eqDelta = (a, b, eps = api_1.EPS) => {
const d = a - b;
return (d < 0 ? -d : d) <= eps;
};
export const eqDelta = (a, b, eps = EPS) => abs(a - b) <= eps * max(1, abs(a), abs(b));
/**
* Similar to `eqDelta()`, but used given `eps` as is.
*
* @param a left value
* @param b right value
* @param eps epsilon / tolerance, default `1e-6`
*/
export const eqDeltaFixed = (a, b, eps = EPS) => abs(a - b) <= eps;

@@ -1,4 +0,2 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const interval_1 = require("./interval");
import { clamp01, clamp11 } from "./interval";
/**

@@ -12,7 +10,7 @@ * Returns normalized value of `x` WRT to interval `a .. b`. If `a`

*/
exports.norm = (x, a, b) => b !== a ? (x - a) / (b - a) : 0;
exports.fit = (x, a, b, c, d) => c + (d - c) * exports.norm(x, a, b);
exports.fitClamped = (x, a, b, c, d) => c + (d - c) * interval_1.clamp01(exports.norm(x, a, b));
exports.fit01 = (x, a, b) => a + (b - a) * interval_1.clamp01(x);
exports.fit10 = (x, a, b) => b + (a - b) * interval_1.clamp01(x);
exports.fit11 = (x, a, b) => a + (b - a) * (0.5 + 0.5 * interval_1.clamp11(x));
export const norm = (x, a, b) => b !== a ? (x - a) / (b - a) : 0;
export const fit = (x, a, b, c, d) => c + (d - c) * norm(x, a, b);
export const fitClamped = (x, a, b, c, d) => c + (d - c) * clamp01(norm(x, a, b));
export const fit01 = (x, a, b) => a + (b - a) * clamp01(x);
export const fit10 = (x, a, b) => b + (a - b) * clamp01(x);
export const fit11 = (x, a, b) => a + (b - a) * (0.5 + 0.5 * clamp11(x));

@@ -1,15 +0,10 @@

"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./api"));
__export(require("./abs"));
__export(require("./angle"));
__export(require("./eqdelta"));
__export(require("./fit"));
__export(require("./interval"));
__export(require("./mix"));
__export(require("./prec"));
__export(require("./solve"));
__export(require("./step"));
export * from "./api";
export * from "./abs";
export * from "./angle";
export * from "./eqdelta";
export * from "./fit";
export * from "./interval";
export * from "./mix";
export * from "./prec";
export * from "./solve";
export * from "./step";

@@ -1,3 +0,1 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**

@@ -10,13 +8,13 @@ * Clamps value `x` to given closed interval.

*/
exports.clamp = (x, min, max) => x < min ? min : x > max ? max : x;
exports.clamp01 = (x) => x < 0 ? 0 : x > 1 ? 1 : x;
exports.clamp11 = (x) => x < -1 ? -1 : x > 1 ? 1 : x;
exports.wrap = (x, min, max) => x < min ? x - min + max : x >= max ? x - max + min : x;
exports.wrap01 = (x) => x < 0 ? x + 1 : x >= 1 ? x - 1 : x;
exports.wrap11 = (x) => x < -1 ? x + 2 : x >= 1 ? x - 2 : x;
exports.min2id = (a, b) => a <= b ? 0 : 1;
exports.min3id = (a, b, c) => (a <= b) ?
export const clamp = (x, min, max) => x < min ? min : x > max ? max : x;
export const clamp01 = (x) => x < 0 ? 0 : x > 1 ? 1 : x;
export const clamp11 = (x) => x < -1 ? -1 : x > 1 ? 1 : x;
export const wrap = (x, min, max) => x < min ? x - min + max : x >= max ? x - max + min : x;
export const wrap01 = (x) => x < 0 ? x + 1 : x >= 1 ? x - 1 : x;
export const wrap11 = (x) => x < -1 ? x + 2 : x >= 1 ? x - 2 : x;
export const min2id = (a, b) => a <= b ? 0 : 1;
export const min3id = (a, b, c) => (a <= b) ?
(a <= c ? 0 : 2) :
(b <= c ? 1 : 2);
exports.min4id = (a, b, c, d) => a <= b ?
export const min4id = (a, b, c, d) => a <= b ?
(a <= c ?

@@ -28,7 +26,7 @@ (a <= d ? 0 : 3) :

(c <= d ? 2 : 3));
exports.max2id = (a, b) => a >= b ? 0 : 1;
exports.max3id = (a, b, c) => (a >= b) ?
export const max2id = (a, b) => a >= b ? 0 : 1;
export const max3id = (a, b, c) => (a >= b) ?
(a >= c ? 0 : 2) :
(b >= c ? 1 : 2);
exports.max4id = (a, b, c, d) => a >= b ?
export const max4id = (a, b, c, d) => a >= b ?
(a >= c ?

@@ -47,3 +45,3 @@ (a >= d ? 0 : 3) :

*/
exports.smin = (a, b, k) => exports.smax(a, b, -k);
export const smin = (a, b, k) => smax(a, b, -k);
/**

@@ -60,3 +58,3 @@ * Smooth maximum. Note: Result values will be slightly larger than max

*/
exports.smax = (a, b, k) => {
export const smax = (a, b, k) => {
const ea = Math.exp(a * k);

@@ -74,5 +72,5 @@ const eb = Math.exp(b * k);

*/
exports.sclamp = (x, min, max, k) => exports.smin(exports.smax(x, min, k), max, k);
exports.absMin = (a, b) => Math.abs(a) < Math.abs(b) ? a : b;
exports.absMax = (a, b) => Math.abs(a) > Math.abs(b) ? a : b;
export const sclamp = (x, min, max, k) => smin(smax(x, min, k), max, k);
export const absMin = (a, b) => Math.abs(a) < Math.abs(b) ? a : b;
export const absMax = (a, b) => Math.abs(a) > Math.abs(b) ? a : b;
/**

@@ -84,3 +82,3 @@ * http://www.musicdsp.org/showone.php?id=203

*/
exports.foldback = (e, x) => (x < -e || x > e) ?
export const foldback = (e, x) => (x < -e || x > e) ?
Math.abs(Math.abs((x - e) % (4 * e)) - 2 * e) - e :

@@ -95,3 +93,3 @@ x;

*/
exports.inRange = (x, min, max) => x >= min && x <= max;
export const inRange = (x, min, max) => x >= min && x <= max;
/**

@@ -104,2 +102,2 @@ * Returns true iff `x` is in open interval `(min .. max)`

*/
exports.inOpenRange = (x, min, max) => x > min && x < max;
export const inOpenRange = (x, min, max) => x > min && x < max;

@@ -1,5 +0,3 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const api_1 = require("./api");
exports.mix = (a, b, t) => a + (b - a) * t;
import { PI, HALF_PI } from "./api";
export const mix = (a, b, t) => a + (b - a) * t;
/**

@@ -21,8 +19,8 @@ * ```

*/
exports.mixBilinear = (a, b, c, d, u, v) => exports.mix(exports.mix(a, b, u), exports.mix(c, d, u), v);
exports.mixQuadratic = (a, b, c, t) => {
export const mixBilinear = (a, b, c, d, u, v) => mix(mix(a, b, u), mix(c, d, u), v);
export const mixQuadratic = (a, b, c, t) => {
const s = 1 - t;
return a * s * s + b * 2 * s * t + c * t * t;
};
exports.mixCubic = (a, b, c, d, t) => {
export const mixCubic = (a, b, c, d, t) => {
const t2 = t * t;

@@ -33,3 +31,3 @@ const s = 1 - t;

};
exports.tween = (f, from, to) => (t) => exports.mix(from, to, f(t));
export const tween = (f, from, to) => (t) => mix(from, to, f(t));
/**

@@ -40,11 +38,11 @@ * Circular interpolation: `sqrt(1 - (1 - t)^2)`

*/
exports.circular = (t) => {
export const circular = (t) => {
t = 1 - t;
return Math.sqrt(1 - t * t);
};
exports.cosine = (t) => 1 - (Math.cos(t * api_1.PI) * 0.5 + 0.5);
exports.decimated = (n, t) => Math.floor(t * n) / n;
exports.bounce = (k, amp, t) => {
export const cosine = (t) => 1 - (Math.cos(t * PI) * 0.5 + 0.5);
export const decimated = (n, t) => Math.floor(t * n) / n;
export const bounce = (k, amp, t) => {
const tk = t * k;
return 1 - amp * Math.sin(tk) / tk * Math.cos(t * api_1.HALF_PI);
return 1 - amp * Math.sin(tk) / tk * Math.cos(t * HALF_PI);
};

@@ -61,3 +59,3 @@ /**

*/
exports.ease = (ease, t) => Math.pow(t, ease);
export const ease = (ease, t) => Math.pow(t, ease);
/**

@@ -68,11 +66,11 @@ * HOF impulse generator. Peaks at `t=1/k`

*/
exports.impulse = (k, t) => {
export const impulse = (k, t) => {
const h = k * t;
return h * Math.exp(1 - h);
};
exports.gain = (k, t) => t < 0.5 ?
export const gain = (k, t) => t < 0.5 ?
0.5 * Math.pow(2 * t, k) :
1 - 0.5 * Math.pow(2 - 2 * t, k);
exports.parabola = (k, t) => Math.pow(4.0 * t * (1.0 - t), k);
exports.cubicPulse = (w, c, t) => {
export const parabola = (k, t) => Math.pow(4.0 * t * (1.0 - t), k);
export const cubicPulse = (w, c, t) => {
t = Math.abs(t - c);

@@ -83,5 +81,5 @@ return t > w ?

};
exports.sinc = (k, t) => {
t = api_1.PI * (k * t - 1.0);
export const sinc = (k, t) => {
t = PI * (k * t - 1.0);
return Math.sin(t) / t;
};
{
"name": "@thi.ng/math",
"version": "0.2.2",
"version": "1.0.0",
"description": "Assorted common math functions & utilities",
"main": "./index.js",
"module": "./index.js",
"main": "./lib/index.js",
"umd:main": "./lib/index.umd.js",
"typings": "./index.d.ts",

@@ -15,8 +17,10 @@ "repository": {

"scripts": {
"build": "yarn run clean && tsc --declaration",
"clean": "rm -rf *.js *.d.ts .nyc_output build coverage doc",
"build": "yarn clean && yarn build:es6 && yarn build:bundle",
"build:es6": "tsc --declaration",
"build:bundle": "../../scripts/bundle-module math",
"test": "rimraf build && tsc -p test/tsconfig.json && nyc mocha build/test/*.js",
"clean": "rimraf *.js *.d.ts .nyc_output build coverage doc lib",
"cover": "yarn test && nyc report --reporter=lcov",
"doc": "node_modules/.bin/typedoc --mode modules --out doc src",
"pub": "yarn run build && yarn publish --access public",
"test": "rm -rf build && tsc -p test && nyc mocha build/test/*.js"
"pub": "yarn build && yarn publish --access public"
},

@@ -28,3 +32,3 @@ "devDependencies": {

"nyc": "^13.1.0",
"typedoc": "^0.13.0",
"typedoc": "^0.14.0",
"typescript": "^3.2.2"

@@ -44,3 +48,4 @@ },

},
"gitHead": "159ce8f6b1d2dad1e12f2ba3f4f7b60d1623acee"
"sideEffects": false,
"gitHead": "348e7303b8b4d2749a02dd43e3f78d711242e4fe"
}

@@ -1,4 +0,2 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const api_1 = require("./api");
import { EPS } from "./api";
/**

@@ -10,6 +8,6 @@ * Returns `a - b * floor(a/b)`

*/
exports.fmod = (a, b) => a - b * Math.floor(a / b);
exports.fract = (x) => x - Math.floor(x);
exports.trunc = (x) => x < 0 ? Math.ceil(x) : Math.floor(x);
exports.roundTo = (x, prec = 1) => Math.round(x / prec) * prec;
export const fmod = (a, b) => a - b * Math.floor(a / b);
export const fract = (x) => x - Math.floor(x);
export const trunc = (x) => x < 0 ? Math.ceil(x) : Math.floor(x);
export const roundTo = (x, prec = 1) => Math.round(x / prec) * prec;
/**

@@ -21,4 +19,4 @@ * Only rounds `x` to nearest int if `fract(x)` < `eps` or > `1-eps`.

*/
exports.roundEps = (x, eps = api_1.EPS) => {
const f = exports.fract(x);
export const roundEps = (x, eps = EPS) => {
const f = fract(x);
return f <= eps || f >= 1 - eps ?

@@ -25,0 +23,0 @@ Math.round(x) :

@@ -1,4 +0,2 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const api_1 = require("./api");
import { EPS } from "./api";
/**

@@ -20,5 +18,3 @@ * Produces a new function which computes derivative of the given

*/
exports.derivative = (f, eps = api_1.EPS) => {
return (x) => (f(x + eps) - f(x)) / eps;
};
export const derivative = (f, eps = EPS) => (x) => (f(x + eps) - f(x)) / eps;
/**

@@ -32,3 +28,3 @@ * Computes solution for linear equation: `ax + b = 0`.

*/
exports.solveLinear = (a, b) => -b / a;
export const solveLinear = (a, b) => -b / a;
/**

@@ -48,3 +44,3 @@ * Computes solutions for quadratic equation: `ax^2 + bx + c = 0`.

*/
exports.solveQuadratic = (a, b, c, eps = 1e-9) => {
export const solveQuadratic = (a, b, c, eps = 1e-9) => {
const d = 2 * a;

@@ -72,3 +68,3 @@ let r = b * b - 4 * a * c;

*/
exports.solveCubic = (a, b, c, d, eps = 1e-9) => {
export const solveCubic = (a, b, c, d, eps = 1e-9) => {
const aa = a * a;

@@ -75,0 +71,0 @@ const bb = b * b;

@@ -1,4 +0,2 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const interval_1 = require("./interval");
import { clamp01 } from "./interval";
/**

@@ -11,3 +9,3 @@ * Step/threshold function.

*/
exports.step = (edge, x) => x < edge ? 0 : 1;
export const step = (edge, x) => x < edge ? 0 : 1;
/**

@@ -21,4 +19,4 @@ * GLSL-style smoothStep threshold function.

*/
exports.smoothStep = (edge, edge2, x) => {
x = interval_1.clamp01((x - edge) / (edge2 - edge));
export const smoothStep = (edge, edge2, x) => {
x = clamp01((x - edge) / (edge2 - edge));
return (3 - 2 * x) * x * x;

@@ -33,4 +31,4 @@ };

*/
exports.smootherStep = (edge, edge2, x) => {
x = interval_1.clamp01((x - edge) / (edge2 - edge));
export const smootherStep = (edge, edge2, x) => {
x = clamp01((x - edge) / (edge2 - edge));
return x * x * x * (x * (x * 6 - 15) + 10);

@@ -50,2 +48,2 @@ };

*/
exports.expStep = (k, n, x) => 1 - Math.exp(-k * Math.pow(x, n));
export const expStep = (k, n, x) => 1 - Math.exp(-k * Math.pow(x, n));
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc