Skip to content

Commit

Permalink
refactor(dual-algebra): update deps, imports, use new Fn types
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Sep 5, 2020
1 parent 683b4e9 commit 2438054
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 35 deletions.
4 changes: 1 addition & 3 deletions packages/dual-algebra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -41,9 +42,6 @@
"typedoc": "^0.18.0",
"typescript": "^4.0.2"
},
"dependencies": {
"@thi.ng/api": "^6.12.3"
},
"files": [
"*.js",
"*.d.ts",
Expand Down
25 changes: 21 additions & 4 deletions packages/dual-algebra/src/api.ts
Original file line number Diff line number Diff line change
@@ -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<Dual, Dual>;

export type Op1N = Fn2<Dual, number, Dual>;
export type Op2 = Fn2<Dual, Dual, Dual>;
export type Op3 = Fn3<Dual, Dual, Dual, Dual>;
export type Op4 = Fn4<Dual, Dual, Dual, Dual, Dual>;

export type Op2 = FnU2<Dual>;

export type Op3 = FnU3<Dual>;

export type Op4 = FnU4<Dual>;

export type Op5 = FnU5<Dual>;

export type Op6 = FnU6<Dual>;
26 changes: 13 additions & 13 deletions packages/dual-algebra/src/ops.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -77,7 +77,7 @@ export const mul = defOp<Op2>(
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;
Expand All @@ -91,7 +91,7 @@ export const div = defOp<Op2>(
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;
Expand All @@ -103,7 +103,7 @@ export const abs = defOp<Op1>(
(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;
Expand All @@ -119,7 +119,7 @@ export const sqrt = defOp<Op1>(
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;
Expand All @@ -134,7 +134,7 @@ export const exp = defOp<Op1>(
(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;
Expand All @@ -147,7 +147,7 @@ export const log = defOp<Op1>(
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;
Expand All @@ -159,7 +159,7 @@ export const pow = defOp<Op1N>(
(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;
Expand All @@ -171,7 +171,7 @@ export const sin = defOp<Op1>(
(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;
Expand All @@ -183,7 +183,7 @@ export const cos = defOp<Op1>(
(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;
Expand All @@ -199,7 +199,7 @@ export const tan = defOp<Op1>(
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;
Expand All @@ -212,7 +212,7 @@ export const atan = defOp<Op1>(
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;
Expand Down
25 changes: 10 additions & 15 deletions packages/dual-algebra/src/poly.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,19 +22,19 @@ 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<number, Dual> = (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
* @param b
* @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);
};
Expand All @@ -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<number, Dual> = (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
Expand Down Expand Up @@ -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<number, Dual> = (x, a, b, c, d, e) =>
quartic([x, 1], [a, 0], [b, 0], [c, 0], [d, 0], [e, 0]);

0 comments on commit 2438054

Please sign in to comment.