@thi.ng/dual-algebra 0.4.45 → 0.4.47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-12-09T19:12:03Z
3
+ - **Last updated**: 2023-12-18T13:41:19Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
package/README.md CHANGED
@@ -138,7 +138,7 @@ For Node.js REPL:
138
138
  const dualAlgebra = await import("@thi.ng/dual-algebra");
139
139
  ```
140
140
 
141
- Package sizes (brotli'd, pre-treeshake): ESM: 990 bytes
141
+ Package sizes (brotli'd, pre-treeshake): ESM: 991 bytes
142
142
 
143
143
  ## Dependencies
144
144
 
package/api.js CHANGED
@@ -1 +0,0 @@
1
- export {};
package/ops.js CHANGED
@@ -1,185 +1,190 @@
1
- /**
2
- * Creates a multivariate dual number.
3
- *
4
- * @param real - real value
5
- * @param n - number of variables (default: 1)
6
- * @param i - variable index (0 < i <= n)
7
- */
8
- export const dual = (real, n = 1, i = 0) => {
9
- const out = new Array(n + 1).fill(0, 1);
10
- out[0] = real;
11
- i > 0 && (out[i] = 1);
12
- return out;
1
+ const dual = (real, n = 1, i = 0) => {
2
+ const out = new Array(n + 1).fill(0, 1);
3
+ out[0] = real;
4
+ i > 0 && (out[i] = 1);
5
+ return out;
13
6
  };
14
- /**
15
- * Creates a 1-dual number of `r`. Syntax sugar for {@link dual}.
16
- *
17
- * @param r -
18
- * @param i -
19
- */
20
- export const $ = (r, i = 0) => [r, i === 1 ? 1 : 0];
21
- /**
22
- * Creates a 2-dual number of `r`. Syntax sugar for {@link dual}.
23
- *
24
- * @param r -
25
- * @param i -
26
- */
27
- export const $2 = (r, i = 0) => dual(r, 2, i);
28
- /**
29
- * Creates a 3-dual number of `r`. Syntax sugar for {@link dual}.
30
- *
31
- * @param r -
32
- * @param i -
33
- */
34
- export const $3 = (r, i = 0) => dual(r, 3, i);
35
- /**
36
- * Creates a 4-dual number of `r`. Syntax sugar for {@link dual}.
37
- *
38
- * @param r -
39
- * @param i -
40
- */
41
- export const $4 = (r, i = 0) => dual(r, 4, i);
42
- export const defOp = (single, multi, dispatch = 0) => (((...xs) => xs[dispatch].length < 3 ? single(...xs) : multi(...xs)));
43
- export const add = defOp((a, b) => [a[0] + b[0], a[1] + b[1]], (a, b) => a.map((x, i) => x + b[i]));
44
- export const sub = defOp((a, b) => [a[0] - b[0], a[1] - b[1]], (a, b) => a.map((x, i) => x - b[i]));
45
- export const neg = defOp((a) => [-a[0], -a[1]], (a) => a.map((x) => (x !== 0 ? -x : 0)));
46
- export const mul = defOp(([ar, ad], [br, bd]) => [ar * br, ar * bd + ad * br], (a, b) => {
7
+ const $ = (r, i = 0) => [r, i === 1 ? 1 : 0];
8
+ const $2 = (r, i = 0) => dual(r, 2, i);
9
+ const $3 = (r, i = 0) => dual(r, 3, i);
10
+ const $4 = (r, i = 0) => dual(r, 4, i);
11
+ const defOp = (single, multi, dispatch = 0) => (...xs) => xs[dispatch].length < 3 ? single(...xs) : multi(...xs);
12
+ const add = defOp(
13
+ (a, b) => [a[0] + b[0], a[1] + b[1]],
14
+ (a, b) => a.map((x, i) => x + b[i])
15
+ );
16
+ const sub = defOp(
17
+ (a, b) => [a[0] - b[0], a[1] - b[1]],
18
+ (a, b) => a.map((x, i) => x - b[i])
19
+ );
20
+ const neg = defOp(
21
+ (a) => [-a[0], -a[1]],
22
+ (a) => a.map((x) => x !== 0 ? -x : 0)
23
+ );
24
+ const mul = defOp(
25
+ ([ar, ad], [br, bd]) => [ar * br, ar * bd + ad * br],
26
+ (a, b) => {
47
27
  const ar = a[0];
48
28
  const br = b[0];
49
29
  const out = [ar * br];
50
- for (let i = a.length; i-- > 1;) {
51
- out[i] = ar * b[i] + a[i] * br;
30
+ for (let i = a.length; i-- > 1; ) {
31
+ out[i] = ar * b[i] + a[i] * br;
52
32
  }
53
33
  return out;
54
- });
55
- export const div = defOp(([ar, ad], [br, bd]) => [ar / br, (ad * br - ar * bd) / (br * br)], (a, b) => {
34
+ }
35
+ );
36
+ const div = defOp(
37
+ ([ar, ad], [br, bd]) => [ar / br, (ad * br - ar * bd) / (br * br)],
38
+ (a, b) => {
56
39
  const ar = a[0];
57
40
  const br = b[0];
58
41
  const ibr = 1 / (br * br);
59
42
  const out = [ar / br];
60
- for (let i = a.length; i-- > 1;) {
61
- out[i] = (a[i] * br - ar * b[i]) * ibr;
43
+ for (let i = a.length; i-- > 1; ) {
44
+ out[i] = (a[i] * br - ar * b[i]) * ibr;
62
45
  }
63
46
  return out;
64
- });
65
- export const abs = defOp(([ar, ad]) => [Math.abs(ar), ad * Math.sign(ar)], (a) => {
47
+ }
48
+ );
49
+ const abs = defOp(
50
+ ([ar, ad]) => [Math.abs(ar), ad * Math.sign(ar)],
51
+ (a) => {
66
52
  const s = Math.sign(a[0]);
67
53
  const out = [Math.abs(a[0])];
68
- for (let i = a.length; i-- > 1;) {
69
- out[i] = s * a[i];
54
+ for (let i = a.length; i-- > 1; ) {
55
+ out[i] = s * a[i];
70
56
  }
71
57
  return out;
72
- });
73
- export const sqrt = defOp((a) => {
58
+ }
59
+ );
60
+ const sqrt = defOp(
61
+ (a) => {
74
62
  const s = Math.sqrt(a[0]);
75
- return [s, (0.5 * a[1]) / s];
76
- }, (a) => {
63
+ return [s, 0.5 * a[1] / s];
64
+ },
65
+ (a) => {
77
66
  const s = Math.sqrt(a[0]);
78
67
  const si = 0.5 / s;
79
68
  const out = [s];
80
- for (let i = a.length; i-- > 1;) {
81
- out[i] = si * a[i];
69
+ for (let i = a.length; i-- > 1; ) {
70
+ out[i] = si * a[i];
82
71
  }
83
72
  return out;
84
- });
85
- export const exp = defOp(([ar, ad]) => {
73
+ }
74
+ );
75
+ const exp = defOp(
76
+ ([ar, ad]) => {
86
77
  ar = Math.exp(ar);
87
78
  return [ar, ad * ar];
88
- }, (a) => {
79
+ },
80
+ (a) => {
89
81
  const ar = Math.exp(a[0]);
90
82
  const out = [ar];
91
- for (let i = a.length; i-- > 1;) {
92
- out[i] = ar * a[i];
83
+ for (let i = a.length; i-- > 1; ) {
84
+ out[i] = ar * a[i];
93
85
  }
94
86
  return out;
95
- });
96
- export const log = defOp(([ar, ad]) => [Math.log(ar), ad / ar], (a) => {
87
+ }
88
+ );
89
+ const log = defOp(
90
+ ([ar, ad]) => [Math.log(ar), ad / ar],
91
+ (a) => {
97
92
  const ar = Math.log(a[0]);
98
93
  const iar = 1 / ar;
99
94
  const out = [ar];
100
- for (let i = a.length; i-- > 1;) {
101
- out[i] = iar * a[i];
95
+ for (let i = a.length; i-- > 1; ) {
96
+ out[i] = iar * a[i];
102
97
  }
103
98
  return out;
104
- });
105
- export const pow = defOp(([ar, ad], k) => [ar ** k, ad * k * ar ** (k - 1)], (a, k) => {
99
+ }
100
+ );
101
+ const pow = defOp(
102
+ ([ar, ad], k) => [ar ** k, ad * k * ar ** (k - 1)],
103
+ (a, k) => {
106
104
  const f = k * a[0] ** (k - 1);
107
105
  const out = [a[0] ** k];
108
- for (let i = a.length; i-- > 1;) {
109
- out[i] = f * a[i];
106
+ for (let i = a.length; i-- > 1; ) {
107
+ out[i] = f * a[i];
110
108
  }
111
109
  return out;
112
- });
113
- export const sin = defOp(([ar, ad]) => [Math.sin(ar), ad * Math.cos(ar)], (a) => {
110
+ }
111
+ );
112
+ const sin = defOp(
113
+ ([ar, ad]) => [Math.sin(ar), ad * Math.cos(ar)],
114
+ (a) => {
114
115
  const c = Math.cos(a[0]);
115
116
  const out = [Math.sin(a[0])];
116
- for (let i = a.length; i-- > 1;) {
117
- out[i] = c * a[i];
117
+ for (let i = a.length; i-- > 1; ) {
118
+ out[i] = c * a[i];
118
119
  }
119
120
  return out;
120
- });
121
- export const cos = defOp(([ar, ad]) => [Math.cos(ar), -ad * Math.sin(ar)], (a) => {
121
+ }
122
+ );
123
+ const cos = defOp(
124
+ ([ar, ad]) => [Math.cos(ar), -ad * Math.sin(ar)],
125
+ (a) => {
122
126
  const s = -Math.sin(a[0]);
123
127
  const out = [Math.cos(a[0])];
124
- for (let i = a.length; i-- > 1;) {
125
- out[i] = s * a[i];
128
+ for (let i = a.length; i-- > 1; ) {
129
+ out[i] = s * a[i];
126
130
  }
127
131
  return out;
128
- });
129
- export const tan = defOp(([ar, ad]) => {
132
+ }
133
+ );
134
+ const tan = defOp(
135
+ ([ar, ad]) => {
130
136
  const c = Math.cos(ar);
131
137
  return [Math.tan(ar), ad / (c * c)];
132
- }, (a) => {
138
+ },
139
+ (a) => {
133
140
  const c = Math.cos(a[0]);
134
141
  const ic = 1 / (c * c);
135
142
  const out = [Math.tan(a[0])];
136
- for (let i = a.length; i-- > 1;) {
137
- out[i] = ic * a[i];
143
+ for (let i = a.length; i-- > 1; ) {
144
+ out[i] = ic * a[i];
138
145
  }
139
146
  return out;
140
- });
141
- export const atan = defOp(([ar, ad]) => [Math.atan(ar), ad / (1 + ar * ar)], (a) => {
147
+ }
148
+ );
149
+ const atan = defOp(
150
+ ([ar, ad]) => [Math.atan(ar), ad / (1 + ar * ar)],
151
+ (a) => {
142
152
  const ar = a[0];
143
153
  const iar = 1 / (1 + ar * ar);
144
154
  const out = [Math.atan(ar)];
145
- for (let i = a.length; i-- > 1;) {
146
- out[i] = iar * a[i];
155
+ for (let i = a.length; i-- > 1; ) {
156
+ out[i] = iar * a[i];
147
157
  }
148
158
  return out;
149
- });
150
- /**
151
- * Linear interpolation for dual numbers: `a + (b - a) * t`
152
- *
153
- * @param a -
154
- * @param b -
155
- * @param t -
156
- */
157
- export const mix = (a, b, t) => add(a, mul(sub(b, a), t));
158
- /**
159
- * Higher order function. Takes a 2-multivariate {@link Op2} and returns new
160
- * function which takes two real numbers `x` and `y` representing variables in
161
- * the given function. When called, converts `x` and `y` first into dual numbers
162
- * and then calls `fn` and returns result.
163
- *
164
- * @remarks
165
- * The result tuple can be interpreted the following:
166
- *
167
- * - index 0 - real value of `fn`
168
- * - index 1 - derivative of first var at `x`
169
- * - index 2 - derivative of second var at `y`
170
- *
171
- * @param fn -
172
- */
173
- export const evalFn2 = (fn) => (x, y) => fn([x, 1, 0], [y, 0, 1]);
174
- /**
175
- * Same as {@link evalFn2}, but 3-multivariate functions.
176
- *
177
- * @param fn -
178
- */
179
- export const evalFn3 = (fn) => (x, y, z) => fn([x, 1, 0, 0], [y, 0, 1, 0], [z, 0, 0, 1]);
180
- /**
181
- * Same as {@link evalFn4}, but 4-multivariate functions.
182
- *
183
- * @param fn -
184
- */
185
- export const evalFn4 = (fn) => (x, y, z, w) => fn([x, 1, 0, 0, 0], [y, 0, 1, 0, 0], [z, 0, 0, 1, 0], [w, 0, 0, 0, 1]);
159
+ }
160
+ );
161
+ const mix = (a, b, t) => add(a, mul(sub(b, a), t));
162
+ const evalFn2 = (fn) => (x, y) => fn([x, 1, 0], [y, 0, 1]);
163
+ const evalFn3 = (fn) => (x, y, z) => fn([x, 1, 0, 0], [y, 0, 1, 0], [z, 0, 0, 1]);
164
+ const evalFn4 = (fn) => (x, y, z, w) => fn([x, 1, 0, 0, 0], [y, 0, 1, 0, 0], [z, 0, 0, 1, 0], [w, 0, 0, 0, 1]);
165
+ export {
166
+ $,
167
+ $2,
168
+ $3,
169
+ $4,
170
+ abs,
171
+ add,
172
+ atan,
173
+ cos,
174
+ defOp,
175
+ div,
176
+ dual,
177
+ evalFn2,
178
+ evalFn3,
179
+ evalFn4,
180
+ exp,
181
+ log,
182
+ mix,
183
+ mul,
184
+ neg,
185
+ pow,
186
+ sin,
187
+ sqrt,
188
+ sub,
189
+ tan
190
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/dual-algebra",
3
- "version": "0.4.45",
3
+ "version": "0.4.47",
4
4
  "description": "Multivariate dual number algebra, automatic differentiation",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -24,7 +24,9 @@
24
24
  "author": "Karsten Schmidt (https://thi.ng)",
25
25
  "license": "Apache-2.0",
26
26
  "scripts": {
27
- "build": "yarn clean && tsc --declaration",
27
+ "build": "yarn build:esbuild && yarn build:decl",
28
+ "build:decl": "tsc --declaration --emitDeclarationOnly",
29
+ "build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
28
30
  "clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
29
31
  "doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
30
32
  "doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
@@ -33,10 +35,11 @@
33
35
  "test": "bun test"
34
36
  },
35
37
  "dependencies": {
36
- "@thi.ng/api": "^8.9.11"
38
+ "@thi.ng/api": "^8.9.13"
37
39
  },
38
40
  "devDependencies": {
39
41
  "@microsoft/api-extractor": "^7.38.3",
42
+ "esbuild": "^0.19.8",
40
43
  "rimraf": "^5.0.5",
41
44
  "tools": "^0.0.1",
42
45
  "typedoc": "^0.25.4",
@@ -59,7 +62,7 @@
59
62
  "access": "public"
60
63
  },
61
64
  "engines": {
62
- "node": ">=12.7"
65
+ "node": ">=18"
63
66
  },
64
67
  "files": [
65
68
  "./*.js",
@@ -89,5 +92,5 @@
89
92
  "status": "alpha",
90
93
  "year": 2020
91
94
  },
92
- "gitHead": "25f2ac8ff795a432a930119661b364d4d93b59a0\n"
95
+ "gitHead": "25a42a81fac8603a1e440a7aa8bc343276211ff4\n"
93
96
  }
package/poly.js CHANGED
@@ -1,71 +1,25 @@
1
1
  import { add, mul } from "./ops.js";
2
- /**
3
- * Computes: `ax^2 + bx + c`. All args must have same size/arity.
4
- *
5
- * @param x -
6
- * @param a -
7
- * @param b -
8
- * @param c -
9
- */
10
- export const quadratic = (x, a, b, c) => add(add(mul(a, mul(x, x)), mul(b, x)), c);
11
- /**
12
- * Same as {@link quadratic}, but for real/scalar inputs. `x` is treated as
13
- * variable `x+1ε`, the rest as `n+0ε`.
14
- *
15
- * @param x -
16
- * @param a -
17
- * @param b -
18
- * @param c -
19
- */
20
- export const quadraticS = (x, a, b, c) => quadratic([x, 1], [a, 0], [b, 0], [c, 0]);
21
- /**
22
- * Computes: `ax^3 + bx^2 + cx + d`. All args must have same size/arity.
23
- *
24
- * @param x -
25
- * @param a -
26
- * @param b -
27
- * @param c -
28
- * @param d -
29
- */
30
- export const cubic = (x, a, b, c, d) => {
31
- const x2 = mul(x, x);
32
- return add(add(add(mul(a, mul(x2, x)), mul(b, x2)), mul(c, x)), d);
2
+ const quadratic = (x, a, b, c) => add(add(mul(a, mul(x, x)), mul(b, x)), c);
3
+ const quadraticS = (x, a, b, c) => quadratic([x, 1], [a, 0], [b, 0], [c, 0]);
4
+ const cubic = (x, a, b, c, d) => {
5
+ const x2 = mul(x, x);
6
+ return add(add(add(mul(a, mul(x2, x)), mul(b, x2)), mul(c, x)), d);
33
7
  };
34
- /**
35
- * Same as {@link cubic}, but for real/scalar inputs. `x` is treated as variable
36
- * `x+1ε`, the rest as `n+0ε`.
37
- *
38
- * @param x -
39
- * @param a -
40
- * @param b -
41
- * @param c -
42
- * @param d -
43
- */
44
- export const cubicS = (x, a, b, c, d) => cubic([x, 1], [a, 0], [b, 0], [c, 0], [d, 0]);
45
- /**
46
- * Computes: `ax^4 + bx^3 + cx^2 + dx + e`. All args must have same size/arity.
47
- *
48
- * @param x -
49
- * @param a -
50
- * @param b -
51
- * @param c -
52
- * @param d -
53
- * @param e -
54
- */
55
- export const quartic = (x, a, b, c, d, e) => {
56
- const x2 = mul(x, x);
57
- const x3 = mul(x2, x);
58
- return add(add(add(add(mul(a, mul(x3, x)), mul(b, x3)), mul(c, x2)), mul(d, x)), e);
8
+ const cubicS = (x, a, b, c, d) => cubic([x, 1], [a, 0], [b, 0], [c, 0], [d, 0]);
9
+ const quartic = (x, a, b, c, d, e) => {
10
+ const x2 = mul(x, x);
11
+ const x3 = mul(x2, x);
12
+ return add(
13
+ add(add(add(mul(a, mul(x3, x)), mul(b, x3)), mul(c, x2)), mul(d, x)),
14
+ e
15
+ );
16
+ };
17
+ const quarticS = (x, a, b, c, d, e) => quartic([x, 1], [a, 0], [b, 0], [c, 0], [d, 0], [e, 0]);
18
+ export {
19
+ cubic,
20
+ cubicS,
21
+ quadratic,
22
+ quadraticS,
23
+ quartic,
24
+ quarticS
59
25
  };
60
- /**
61
- * Same as {@link quartic}, but for real/scalar inputs. `x` is treated as
62
- * variable `x+1ε`, the rest as `n+0ε`.
63
- *
64
- * @param x -
65
- * @param a -
66
- * @param b -
67
- * @param c -
68
- * @param d -
69
- * @param e -
70
- */
71
- export const quarticS = (x, a, b, c, d, e) => quartic([x, 1], [a, 0], [b, 0], [c, 0], [d, 0], [e, 0]);
package/vector.js CHANGED
@@ -1,41 +1,14 @@
1
1
  import { add, div, dual, mul, sub } from "./ops.js";
2
- const defVecOp2 = (op) => (a, b) => a.map((a, i) => op(a, b[i]));
3
- /**
4
- * Dual vector addition. Applies {@link add} in a component-wise manner. Returns
5
- * new (dual) vector.
6
- *
7
- * @param a -
8
- * @param b -
9
- */
10
- export const vadd = defVecOp2(add);
11
- /**
12
- * Dual vector subtraction. Applies {@link sub} in a component-wise manner.
13
- * Returns new (dual) vector.
14
- *
15
- * @param a -
16
- * @param b -
17
- */
18
- export const vsub = defVecOp2(sub);
19
- /**
20
- * Dual vector multiplication. Applies {@link mul} in a component-wise manner.
21
- * Returns new (dual) vector.
22
- *
23
- * @param a -
24
- * @param b -
25
- */
26
- export const vmul = defVecOp2(mul);
27
- /**
28
- * Dual vector division. Applies {@link div} in a component-wise manner.
29
- * Returns new (dual) vector.
30
- *
31
- * @param a -
32
- * @param b -
33
- */
34
- export const vdiv = defVecOp2(div);
35
- /**
36
- * Computes dot product of 2 dual vectors.
37
- *
38
- * @param a -
39
- * @param b -
40
- */
41
- export const dot = (a, b) => a.reduce((acc, a, i) => add(acc, mul(a, b[i])), dual(0, a[0].length));
2
+ const defVecOp2 = (op) => (a, b) => a.map((a2, i) => op(a2, b[i]));
3
+ const vadd = defVecOp2(add);
4
+ const vsub = defVecOp2(sub);
5
+ const vmul = defVecOp2(mul);
6
+ const vdiv = defVecOp2(div);
7
+ const dot = (a, b) => a.reduce((acc, a2, i) => add(acc, mul(a2, b[i])), dual(0, a[0].length));
8
+ export {
9
+ dot,
10
+ vadd,
11
+ vdiv,
12
+ vmul,
13
+ vsub
14
+ };