diff --git a/packages/dual-algebra/package.json b/packages/dual-algebra/package.json index 4692824e4e..b3a56abfff 100644 --- a/packages/dual-algebra/package.json +++ b/packages/dual-algebra/package.json @@ -33,6 +33,7 @@ "devDependencies": { "@istanbuljs/nyc-config-typescript": "^1.0.1", "@microsoft/api-extractor": "^7.9.11", + "@thi.ng/api": "^6.12.3", "@types/mocha": "^8.0.3", "@types/node": "^14.6.1", "mocha": "^8.1.2", @@ -41,9 +42,6 @@ "typedoc": "^0.18.0", "typescript": "^4.0.2" }, - "dependencies": { - "@thi.ng/api": "^6.12.3" - }, "files": [ "*.js", "*.d.ts", diff --git a/packages/dual-algebra/src/api.ts b/packages/dual-algebra/src/api.ts index 9290b89d3b..c31ffbefe3 100644 --- a/packages/dual-algebra/src/api.ts +++ b/packages/dual-algebra/src/api.ts @@ -1,9 +1,26 @@ -import type { Fn, Fn2, Fn3, Fn4, NumericArray } from "@thi.ng/api"; +import type { + Fn, + Fn2, + FnU2, + FnU3, + FnU4, + FnU5, + FnU6, + NumericArray, +} from "@thi.ng/api"; export type Dual = NumericArray; export type Op1 = Fn; + export type Op1N = Fn2; -export type Op2 = Fn2; -export type Op3 = Fn3; -export type Op4 = Fn4; + +export type Op2 = FnU2; + +export type Op3 = FnU3; + +export type Op4 = FnU4; + +export type Op5 = FnU5; + +export type Op6 = FnU6; diff --git a/packages/dual-algebra/src/ops.ts b/packages/dual-algebra/src/ops.ts index 0fa457127d..4a6bcb6f20 100644 --- a/packages/dual-algebra/src/ops.ts +++ b/packages/dual-algebra/src/ops.ts @@ -1,4 +1,4 @@ -import { Op1, Op1N, Op2, Op3, Op4 } from "./api"; +import type { Op1, Op1N, Op2, Op3, Op4 } from "./api"; /** * Creates a multivariate dual number. @@ -8,7 +8,7 @@ import { Op1, Op1N, Op2, Op3, Op4 } from "./api"; * @param i - variable index (0 < i <= n) */ export const dual = (real: number, n = 1, i = 0) => { - const out = new Array(n + 1).fill(0); + const out = new Array(n + 1).fill(0, 1); out[0] = real; i > 0 && (out[i] = 1); return out; @@ -77,7 +77,7 @@ export const mul = defOp( const ar = a[0]; const br = b[0]; const out = [ar * br]; - for (let i = 1, n = a.length; i < n; i++) { + for (let i = a.length; --i >= 1; ) { out[i] = ar * b[i] + a[i] * br; } return out; @@ -91,7 +91,7 @@ export const div = defOp( const br = b[0]; const ibr = 1 / (br * br); const out = [ar / br]; - for (let i = 1, n = a.length; i < n; i++) { + for (let i = a.length; --i >= 1; ) { out[i] = (a[i] * br - ar * b[i]) * ibr; } return out; @@ -103,7 +103,7 @@ export const abs = defOp( (a) => { const s = Math.sign(a[0]); const out = [Math.abs(a[0])]; - for (let i = 1, n = a.length; i < n; i++) { + for (let i = a.length; --i >= 1; ) { out[i] = s * a[i]; } return out; @@ -119,7 +119,7 @@ export const sqrt = defOp( const s = Math.sqrt(a[0]); const si = 0.5 / s; const out = [s]; - for (let i = 1, n = a.length; i < n; i++) { + for (let i = a.length; --i >= 1; ) { out[i] = si * a[i]; } return out; @@ -134,7 +134,7 @@ export const exp = defOp( (a) => { const ar = Math.exp(a[0]); const out = [ar]; - for (let i = 1, n = a.length; i < n; i++) { + for (let i = a.length; --i >= 1; ) { out[i] = ar * a[i]; } return out; @@ -147,7 +147,7 @@ export const log = defOp( const ar = Math.log(a[0]); const iar = 1 / ar; const out = [ar]; - for (let i = 1, n = a.length; i < n; i++) { + for (let i = a.length; --i >= 1; ) { out[i] = iar * a[i]; } return out; @@ -159,7 +159,7 @@ export const pow = defOp( (a, k) => { const f = k * a[0] ** (k - 1); const out = [a[0] ** k]; - for (let i = 1, n = a.length; i < n; i++) { + for (let i = a.length; --i >= 1; ) { out[i] = f * a[i]; } return out; @@ -171,7 +171,7 @@ export const sin = defOp( (a) => { const c = Math.cos(a[0]); const out = [Math.sin(a[0])]; - for (let i = 1, n = a.length; i < n; i++) { + for (let i = a.length; --i >= 1; ) { out[i] = c * a[i]; } return out; @@ -183,7 +183,7 @@ export const cos = defOp( (a) => { const s = -Math.sin(a[0]); const out = [Math.cos(a[0])]; - for (let i = 1, n = a.length; i < n; i++) { + for (let i = a.length; --i >= 1; ) { out[i] = s * a[i]; } return out; @@ -199,7 +199,7 @@ export const tan = defOp( const c = Math.cos(a[0]); const ic = 1 / (c * c); const out = [Math.tan(a[0])]; - for (let i = 1, n = a.length; i < n; i++) { + for (let i = a.length; --i >= 1; ) { out[i] = ic * a[i]; } return out; @@ -212,7 +212,7 @@ export const atan = defOp( const ar = a[0]; const iar = 1 / (1 + ar * ar); const out = [Math.atan(ar)]; - for (let i = 1, n = a.length; i < n; i++) { + for (let i = a.length; --i >= 1; ) { out[i] = iar * a[i]; } return out; diff --git a/packages/dual-algebra/src/poly.ts b/packages/dual-algebra/src/poly.ts index 4055d87aad..bb6a6ea5ad 100644 --- a/packages/dual-algebra/src/poly.ts +++ b/packages/dual-algebra/src/poly.ts @@ -1,8 +1,9 @@ -import { Dual, Op4 } from "./api"; +import type { FnU4, FnU5, FnU6 } from "@thi.ng/api"; +import type { Dual, Op4, Op5 } from "./api"; import { add, mul } from "./ops"; /** - * Computes: `ax^2 + bx + c` + * Computes: `ax^2 + bx + c`. All args must have same size/arity. * * @param x * @param a @@ -21,11 +22,11 @@ export const quadratic: Op4 = (x, a, b, c) => * @param b * @param c */ -export const quadraticS = (x: number, a: number, b: number, c: number) => +export const quadraticS: FnU4 = (x, a, b, c) => quadratic([x, 1], [a, 0], [b, 0], [c, 0]); /** - * Computes: `ax^3 + bx^2 + cx + d` + * Computes: `ax^3 + bx^2 + cx + d`. All args must have same size/arity. * * @param x * @param a @@ -33,7 +34,7 @@ export const quadraticS = (x: number, a: number, b: number, c: number) => * @param c * @param d */ -export const cubic = (x: Dual, a: Dual, b: Dual, c: Dual, d: Dual) => { +export const cubic: Op5 = (x, a, b, c, d) => { const x2 = mul(x, x); return add(add(add(mul(a, mul(x2, x)), mul(b, x2)), mul(c, x)), d); }; @@ -48,11 +49,11 @@ export const cubic = (x: Dual, a: Dual, b: Dual, c: Dual, d: Dual) => { * @param c * @param d */ -export const cubicS = (x: number, a: number, b: number, c: number, d: number) => +export const cubicS: FnU5 = (x, a, b, c, d) => cubic([x, 1], [a, 0], [b, 0], [c, 0], [d, 0]); /** - * Computes: `ax^4 + bx^3 + cx^2 + dx + e` + * Computes: `ax^4 + bx^3 + cx^2 + dx + e`. All args must have same size/arity. * * @param x * @param a @@ -88,11 +89,5 @@ export const quartic = ( * @param d * @param e */ -export const quarticS = ( - x: number, - a: number, - b: number, - c: number, - d: number, - e: number -) => quartic([x, 1], [a, 0], [b, 0], [c, 0], [d, 0], [e, 0]); +export const quarticS: FnU6 = (x, a, b, c, d, e) => + quartic([x, 1], [a, 0], [b, 0], [c, 0], [d, 0], [e, 0]);