@danielsimonjr/mathts-functions 0.69.2 → 0.70.1
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/README.md +66 -1
- package/dist/algebra/decomposition/lup.d.ts.map +1 -1
- package/dist/algebra/decomposition/qr.d.ts.map +1 -1
- package/dist/algebra/simplify/util.d.ts.map +1 -1
- package/dist/algebra/simplifyConstant.d.ts +3 -4
- package/dist/algebra/simplifyConstant.d.ts.map +1 -1
- package/dist/algebra/solver/lsolve.d.ts.map +1 -1
- package/dist/algebra/solver/usolve.d.ts.map +1 -1
- package/dist/arithmetic/ceil.d.ts.map +1 -1
- package/dist/arithmetic/fix.d.ts.map +1 -1
- package/dist/arithmetic/floor.d.ts.map +1 -1
- package/dist/arithmetic/norm.d.ts.map +1 -1
- package/dist/arithmetic/round.d.ts.map +1 -1
- package/dist/arithmetic/sign.d.ts +2 -2
- package/dist/arithmetic/sign.d.ts.map +1 -1
- package/dist/arithmetic/snap-to-round.d.ts +36 -0
- package/dist/arithmetic/snap-to-round.d.ts.map +1 -0
- package/dist/arithmetic/xgcd.d.ts +6 -6
- package/dist/arithmetic/xgcd.d.ts.map +1 -1
- package/dist/cas/layer3.d.ts.map +1 -1
- package/dist/cas/rational-integrate.d.ts.map +1 -1
- package/dist/cas/render-coeff-times.d.ts +7 -0
- package/dist/cas/render-coeff-times.d.ts.map +1 -0
- package/dist/cas-integration.d.ts.map +1 -1
- package/dist/config-api.d.ts +47 -5
- package/dist/config-api.d.ts.map +1 -1
- package/dist/descriptive-stats.d.ts +4 -0
- package/dist/descriptive-stats.d.ts.map +1 -1
- package/dist/factories/index.d.ts +3 -0
- package/dist/factories/index.d.ts.map +1 -1
- package/dist/factories/scope.d.ts +8 -0
- package/dist/factories/scope.d.ts.map +1 -1
- package/dist/fit-scalar.d.ts +49 -0
- package/dist/fit-scalar.d.ts.map +1 -0
- package/dist/front-export.d.ts +81 -0
- package/dist/front-export.d.ts.map +1 -0
- package/dist/geometry/alpha-shape.d.ts.map +1 -1
- package/dist/geometry/circumcircle.d.ts +16 -0
- package/dist/geometry/circumcircle.d.ts.map +1 -0
- package/dist/geometry/geometry-extra.d.ts.map +1 -1
- package/dist/geometry/voronoi.d.ts.map +1 -1
- package/dist/gpu/elementwise-gpu.d.ts +2 -0
- package/dist/gpu/elementwise-gpu.d.ts.map +1 -1
- package/dist/index.d.ts +19 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +24998 -25385
- package/dist/matrix/column.d.ts.map +1 -1
- package/dist/matrix/complex-to-interleaved.d.ts +7 -0
- package/dist/matrix/complex-to-interleaved.d.ts.map +1 -0
- package/dist/matrix/fft.d.ts.map +1 -1
- package/dist/matrix/from-vectors.d.ts +45 -0
- package/dist/matrix/from-vectors.d.ts.map +1 -0
- package/dist/matrix/ifft.d.ts.map +1 -1
- package/dist/matrix/mapSlices.d.ts.map +1 -1
- package/dist/matrix/matrixFromColumns.d.ts.map +1 -1
- package/dist/matrix/matrixFromRows.d.ts.map +1 -1
- package/dist/matrix/native-accel.d.ts.map +1 -1
- package/dist/matrix/partitionSelect.d.ts +1 -0
- package/dist/matrix/partitionSelect.d.ts.map +1 -1
- package/dist/matrix/row.d.ts.map +1 -1
- package/dist/matrix/slice-2d.d.ts +16 -0
- package/dist/matrix/slice-2d.d.ts.map +1 -0
- package/dist/matrix/sort.d.ts +1 -1
- package/dist/matrix/sort.d.ts.map +1 -1
- package/dist/matrix/sparse-csc-tail.d.ts +21 -0
- package/dist/matrix/sparse-csc-tail.d.ts.map +1 -0
- package/dist/ml/dbscan-knn.d.ts +6 -0
- package/dist/ml/dbscan-knn.d.ts.map +1 -1
- package/dist/ml/ols.d.ts.map +1 -1
- package/dist/ml/regularized-regression.d.ts.map +1 -1
- package/dist/numbertheory/extra.d.ts.map +1 -1
- package/dist/numeric/bfgs.d.ts.map +1 -1
- package/dist/numeric/control-equations.d.ts +0 -47
- package/dist/numeric/control-equations.d.ts.map +1 -1
- package/dist/numeric/eigsh.d.ts.map +1 -1
- package/dist/numeric/fsolve.d.ts.map +1 -1
- package/dist/numeric/hermite.d.ts +22 -0
- package/dist/numeric/hermite.d.ts.map +1 -0
- package/dist/numeric/krylov.d.ts.map +1 -1
- package/dist/numeric/numeric-jacobian.d.ts +65 -7
- package/dist/numeric/numeric-jacobian.d.ts.map +1 -1
- package/dist/numeric/open-root-finders.d.ts.map +1 -1
- package/dist/numeric/solveDAE.d.ts.map +1 -1
- package/dist/numeric/solveDDE.d.ts.map +1 -1
- package/dist/numeric/solveODE.d.ts.map +1 -1
- package/dist/numeric/svds.d.ts.map +1 -1
- package/dist/numeric-equivalence.d.ts +110 -0
- package/dist/numeric-equivalence.d.ts.map +1 -0
- package/dist/numeric-extra.d.ts.map +1 -1
- package/dist/optimization-extra.d.ts +33 -10
- package/dist/optimization-extra.d.ts.map +1 -1
- package/dist/pareto-front.d.ts +165 -0
- package/dist/pareto-front.d.ts.map +1 -0
- package/dist/plain/number/probability.d.ts +9 -3
- package/dist/plain/number/probability.d.ts.map +1 -1
- package/dist/probability/bigint-binomial.d.ts +34 -0
- package/dist/probability/bigint-binomial.d.ts.map +1 -0
- package/dist/probability/combinations.d.ts.map +1 -1
- package/dist/probability/combinationsWithRep.d.ts.map +1 -1
- package/dist/scalar-batch-parallel.d.ts +71 -0
- package/dist/scalar-batch-parallel.d.ts.map +1 -0
- package/dist/scalar-batch-task-CZd6LPg9.js +2587 -0
- package/dist/scalar-batch-task.d.ts +40 -0
- package/dist/scalar-batch-task.d.ts.map +1 -0
- package/dist/scalar-batch.worker.js +34 -0
- package/dist/scalar-compile.d.ts +227 -0
- package/dist/scalar-compile.d.ts.map +1 -0
- package/dist/scalar-dimension.d.ts +54 -0
- package/dist/scalar-dimension.d.ts.map +1 -0
- package/dist/scalar-eval.d.ts +7 -0
- package/dist/scalar-eval.d.ts.map +1 -1
- package/dist/scalar-mutate.d.ts +70 -0
- package/dist/scalar-mutate.d.ts.map +1 -0
- package/dist/scalar-node-bridge.d.ts +30 -0
- package/dist/scalar-node-bridge.d.ts.map +1 -0
- package/dist/scalar-search.d.ts +176 -0
- package/dist/scalar-search.d.ts.map +1 -0
- package/dist/set/flatten-pair.d.ts +16 -0
- package/dist/set/flatten-pair.d.ts.map +1 -0
- package/dist/set/setSymDifference.d.ts.map +1 -1
- package/dist/set/setUnion.d.ts.map +1 -1
- package/dist/signal-filter-extra.d.ts.map +1 -1
- package/dist/special/niche.d.ts.map +1 -1
- package/dist/statistics/extreme.d.ts +23 -0
- package/dist/statistics/extreme.d.ts.map +1 -0
- package/dist/statistics/max.d.ts.map +1 -1
- package/dist/statistics/median.d.ts +1 -1
- package/dist/statistics/median.d.ts.map +1 -1
- package/dist/statistics/min.d.ts.map +1 -1
- package/dist/statistics/prod.d.ts.map +1 -1
- package/dist/stats/inference-extra2.d.ts.map +1 -1
- package/dist/string/print.d.ts.map +1 -1
- package/dist/type/local/Decimal.d.ts +0 -3
- package/dist/type/local/Decimal.d.ts.map +1 -1
- package/dist/type/matrix/ImmutableDenseMatrix.d.ts +10 -0
- package/dist/type/matrix/ImmutableDenseMatrix.d.ts.map +1 -1
- package/dist/type/matrix/MatrixIndex.d.ts +22 -1
- package/dist/type/matrix/MatrixIndex.d.ts.map +1 -1
- package/dist/type/matrix/utils/matAlgo13xDD.d.ts.map +1 -1
- package/dist/type/matrix/utils/matAlgo14xDs.d.ts.map +1 -1
- package/dist/type/matrix/utils/matrixAlgorithmSuite.d.ts.map +1 -1
- package/dist/typed/aggregate-restart.d.ts +14 -0
- package/dist/typed/aggregate-restart.d.ts.map +1 -0
- package/dist/typed/algebra.d.ts +4 -0
- package/dist/typed/algebra.d.ts.map +1 -1
- package/dist/typed/arithmetic.d.ts.map +1 -1
- package/dist/typed/cas.d.ts +2 -1
- package/dist/typed/cas.d.ts.map +1 -1
- package/dist/typed/centrality-restarts.d.ts +13 -0
- package/dist/typed/centrality-restarts.d.ts.map +1 -0
- package/dist/typed/combinatorics.d.ts.map +1 -1
- package/dist/typed/dist-objects.d.ts.map +1 -1
- package/dist/typed/distributions.d.ts +8 -13
- package/dist/typed/distributions.d.ts.map +1 -1
- package/dist/typed/euclid.d.ts +6 -0
- package/dist/typed/euclid.d.ts.map +1 -0
- package/dist/typed/factorization/finite-field.d.ts.map +1 -1
- package/dist/typed/factorization/index.d.ts.map +1 -1
- package/dist/typed/factorization/integer-poly.d.ts +7 -0
- package/dist/typed/factorization/integer-poly.d.ts.map +1 -1
- package/dist/typed/factorization/kronecker-factor.d.ts.map +1 -1
- package/dist/typed/factorization/render-univariate.d.ts +11 -0
- package/dist/typed/factorization/render-univariate.d.ts.map +1 -0
- package/dist/typed/factorization/zassenhaus.d.ts +9 -0
- package/dist/typed/factorization/zassenhaus.d.ts.map +1 -1
- package/dist/typed/fused.d.ts.map +1 -1
- package/dist/typed/geometry.d.ts +2 -2
- package/dist/typed/geometry.d.ts.map +1 -1
- package/dist/typed/graph.d.ts +2 -7
- package/dist/typed/graph.d.ts.map +1 -1
- package/dist/typed/hypothesis.d.ts.map +1 -1
- package/dist/typed/index.d.ts +1 -0
- package/dist/typed/index.d.ts.map +1 -1
- package/dist/typed/interpolation.d.ts +6 -7
- package/dist/typed/interpolation.d.ts.map +1 -1
- package/dist/typed/matrix-ops.d.ts.map +1 -1
- package/dist/typed/normal-equations-solve.d.ts +8 -0
- package/dist/typed/normal-equations-solve.d.ts.map +1 -0
- package/dist/typed/numeric.d.ts +5 -1
- package/dist/typed/numeric.d.ts.map +1 -1
- package/dist/typed/parallel-map.d.ts +11 -0
- package/dist/typed/parallel-map.d.ts.map +1 -1
- package/dist/typed/polynomial-ideal.d.ts +6 -0
- package/dist/typed/polynomial-ideal.d.ts.map +1 -1
- package/dist/typed/set.d.ts.map +1 -1
- package/dist/typed/signal.d.ts +6 -6
- package/dist/typed/signal.d.ts.map +1 -1
- package/dist/typed/special.d.ts +22 -26
- package/dist/typed/special.d.ts.map +1 -1
- package/dist/typed/statistics.d.ts +19 -2
- package/dist/typed/statistics.d.ts.map +1 -1
- package/dist/typed/string-derivative.d.ts +19 -0
- package/dist/typed/string-derivative.d.ts.map +1 -0
- package/dist/typed/string.d.ts +3 -3
- package/dist/typed/string.d.ts.map +1 -1
- package/dist/typed/unit.d.ts.map +1 -1
- package/dist/utils/isPrime.d.ts +15 -0
- package/dist/utils/isPrime.d.ts.map +1 -1
- package/dist/utils/print.d.ts +5 -0
- package/dist/utils/print.d.ts.map +1 -1
- package/dist/utils/typed-impl.d.ts +11 -0
- package/dist/utils/typed-impl.d.ts.map +1 -0
- package/dist/wasm/WasmLoader.d.ts +3 -21
- package/dist/wasm/WasmLoader.d.ts.map +1 -1
- package/dist/wasm/bridges/common.d.ts +18 -2
- package/dist/wasm/bridges/common.d.ts.map +1 -1
- package/dist/wasm/mathts-as.wasm +0 -0
- package/dist/wasm/poly/wasm-bridge.d.ts +11 -0
- package/dist/wasm/poly/wasm-bridge.d.ts.map +1 -1
- package/dist/wasm/sort/wasm-bridge.d.ts +0 -10
- package/dist/wasm/sort/wasm-bridge.d.ts.map +1 -1
- package/dist/wasm/special/scalars.d.ts +49 -0
- package/dist/wasm/special/scalars.d.ts.map +1 -1
- package/dist/wasm/special/wasm-bridge.d.ts +11 -12
- package/dist/wasm/special/wasm-bridge.d.ts.map +1 -1
- package/dist/wasm/wasm-manifest.json +1 -1
- package/package.json +8 -8
- package/types/index.d.ts +1 -1
- package/dist/wasm/integrity.d.ts +0 -16
- package/dist/wasm/integrity.d.ts.map +0 -1
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The unit of work that crosses to a scalar batch worker (M14), and the function that
|
|
3
|
+
* does it. The worker entry and the tests both call {@link runScalarBatchTask}, so the
|
|
4
|
+
* message a worker receives is evaluated by one body, on or off the main thread.
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
*/
|
|
8
|
+
import { type buildTape } from './scalar-compile.js';
|
|
9
|
+
/** Prefix of every error the tape evaluator throws for a batch task. */
|
|
10
|
+
export declare const SCALAR_BATCH_MESSAGE_PREFIX = "compileScalar:";
|
|
11
|
+
type Tape = ReturnType<typeof buildTape>['tape'];
|
|
12
|
+
/** One compiled tree and the parameter values it is evaluated with. */
|
|
13
|
+
export interface ScalarBatchTaskItem {
|
|
14
|
+
/** Position of the tree in the caller's list; results come back under it. */
|
|
15
|
+
readonly candidateIndex: number;
|
|
16
|
+
readonly tape: Tape;
|
|
17
|
+
readonly rootReg: number;
|
|
18
|
+
readonly nRegs: number;
|
|
19
|
+
readonly params: readonly number[];
|
|
20
|
+
readonly parameters: readonly string[];
|
|
21
|
+
}
|
|
22
|
+
/** Several trees over the same columns; this is the message payload. */
|
|
23
|
+
export interface ScalarBatchTask {
|
|
24
|
+
readonly items: readonly ScalarBatchTaskItem[];
|
|
25
|
+
readonly columns: readonly Float64Array[];
|
|
26
|
+
readonly variables: readonly string[];
|
|
27
|
+
}
|
|
28
|
+
export interface ScalarBatchTaskRow {
|
|
29
|
+
readonly candidateIndex: number;
|
|
30
|
+
readonly values: Float64Array;
|
|
31
|
+
readonly valid: Uint8Array;
|
|
32
|
+
readonly allValid: boolean;
|
|
33
|
+
}
|
|
34
|
+
export interface ScalarBatchTaskResult {
|
|
35
|
+
readonly rows: readonly ScalarBatchTaskRow[];
|
|
36
|
+
}
|
|
37
|
+
/** Evaluates every tree of `task` over its columns, in order. */
|
|
38
|
+
export declare function runScalarBatchTask(task: ScalarBatchTask): ScalarBatchTaskResult;
|
|
39
|
+
export {};
|
|
40
|
+
//# sourceMappingURL=scalar-batch-task.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scalar-batch-task.d.ts","sourceRoot":"","sources":["../src/scalar-batch-task.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAsB,KAAK,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAEzE,wEAAwE;AACxE,eAAO,MAAM,2BAA2B,mBAAmB,CAAC;AAE5D,KAAK,IAAI,GAAG,UAAU,CAAC,OAAO,SAAS,CAAC,CAAC,MAAM,CAAC,CAAC;AAEjD,uEAAuE;AACvE,MAAM,WAAW,mBAAmB;IAClC,6EAA6E;IAC7E,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC;IACpB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC;IACnC,QAAQ,CAAC,UAAU,EAAE,SAAS,MAAM,EAAE,CAAC;CACxC;AAED,wEAAwE;AACxE,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,KAAK,EAAE,SAAS,mBAAmB,EAAE,CAAC;IAC/C,QAAQ,CAAC,OAAO,EAAE,SAAS,YAAY,EAAE,CAAC;IAC1C,QAAQ,CAAC,SAAS,EAAE,SAAS,MAAM,EAAE,CAAC;CACvC;AAED,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,MAAM,EAAE,YAAY,CAAC;IAC9B,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC;IAC3B,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;CAC5B;AAED,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,IAAI,EAAE,SAAS,kBAAkB,EAAE,CAAC;CAC9C;AAED,iEAAiE;AACjE,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,eAAe,GAAG,qBAAqB,CAqB/E"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { n as runScalarBatchTask } from "./scalar-batch-task-CZd6LPg9.js";
|
|
2
|
+
//#region src/scalar-batch.worker.ts
|
|
3
|
+
const isNode = typeof process !== "undefined" && process.versions?.node !== void 0;
|
|
4
|
+
let nodeParentPort = null;
|
|
5
|
+
function sendMessage(message) {
|
|
6
|
+
if (isNode) nodeParentPort?.postMessage(message);
|
|
7
|
+
else postMessage(message);
|
|
8
|
+
}
|
|
9
|
+
function handleMessage(message) {
|
|
10
|
+
try {
|
|
11
|
+
const result = runScalarBatchTask(message.data);
|
|
12
|
+
sendMessage({
|
|
13
|
+
id: message.id,
|
|
14
|
+
type: "result",
|
|
15
|
+
data: result
|
|
16
|
+
});
|
|
17
|
+
} catch (error) {
|
|
18
|
+
sendMessage({
|
|
19
|
+
id: message.id,
|
|
20
|
+
type: "error",
|
|
21
|
+
error: error instanceof Error ? error.message : String(error)
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (isNode) import("worker_threads").then(({ parentPort }) => {
|
|
26
|
+
if (!parentPort) throw new Error("scalar-batch.worker: missing parentPort");
|
|
27
|
+
nodeParentPort = parentPort;
|
|
28
|
+
parentPort.on("message", handleMessage);
|
|
29
|
+
});
|
|
30
|
+
else addEventListener("message", (event) => {
|
|
31
|
+
handleMessage(event.data);
|
|
32
|
+
});
|
|
33
|
+
//#endregion
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Batch evaluation of {@link ScalarNode} trees over `Float64Array` columns.
|
|
3
|
+
*
|
|
4
|
+
* {@link compileScalar} builds a register tape once; {@link CompiledScalar.evaluate}
|
|
5
|
+
* runs every row with a per-row validity mask aligned with {@link evaluateScalar}.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
import type { LoweredScalarFn, ScalarNode, ScalarOperator } from '@danielsimonjr/mathts-expression';
|
|
10
|
+
type TapeInstr = {
|
|
11
|
+
readonly tag: 'const';
|
|
12
|
+
readonly dest: number;
|
|
13
|
+
readonly value: number;
|
|
14
|
+
} | {
|
|
15
|
+
readonly tag: 'column';
|
|
16
|
+
readonly dest: number;
|
|
17
|
+
readonly index: number;
|
|
18
|
+
} | {
|
|
19
|
+
readonly tag: 'param';
|
|
20
|
+
readonly dest: number;
|
|
21
|
+
readonly index: number;
|
|
22
|
+
} | {
|
|
23
|
+
readonly tag: 'mov';
|
|
24
|
+
readonly dest: number;
|
|
25
|
+
readonly src: number;
|
|
26
|
+
} | {
|
|
27
|
+
readonly tag: 'bin';
|
|
28
|
+
readonly dest: number;
|
|
29
|
+
readonly op: ScalarOperator;
|
|
30
|
+
readonly left: number;
|
|
31
|
+
readonly right: number;
|
|
32
|
+
} | {
|
|
33
|
+
readonly tag: 'unary';
|
|
34
|
+
readonly dest: number;
|
|
35
|
+
readonly fn: LoweredScalarFn;
|
|
36
|
+
readonly arg: number;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Bindings for {@link compileScalar}.
|
|
40
|
+
*
|
|
41
|
+
* `variables` is the column order. `parameters` are free constants supplied
|
|
42
|
+
* to each `evaluate` call (still `symbol` nodes in the tree).
|
|
43
|
+
* `constants` are fixed numbers, for example `{ pi: Math.PI }`.
|
|
44
|
+
*
|
|
45
|
+
* A name may occur in only one of those three. Symbol lookup then follows
|
|
46
|
+
* `evaluateScalar`: parameter, else column, else constant, else a numeric
|
|
47
|
+
* literal spelling (`'2'`, `'-1'`, `'1e-3'`), else a compile-time
|
|
48
|
+
* {@link ScalarEvalError}. `pi` and `e` are not literals.
|
|
49
|
+
*
|
|
50
|
+
* The node must already be a `ScalarNode`. A formula string, a
|
|
51
|
+
* `transcendental` node, or an `abs` node is rejected; normalize with
|
|
52
|
+
* `scalar.from` first.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* import { compileScalar, scalar } from '@danielsimonjr/mathts-functions';
|
|
57
|
+
* const node = scalar.op('*', [scalar.symbol('x'), scalar.symbol('x')]);
|
|
58
|
+
* compileScalar(node, { variables: ['x'], parameters: ['a'] }).parameters; // ['a']
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export interface CompileScalarOptions {
|
|
62
|
+
readonly variables: readonly string[];
|
|
63
|
+
readonly parameters?: readonly string[];
|
|
64
|
+
readonly constants?: Readonly<Record<string, number>>;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* One batch. `values[i]` equals `evaluateScalar` for that row when `valid[i]`
|
|
68
|
+
* is 1, including a stored `NaN`, `±Infinity`, or `-0` on a bare number or
|
|
69
|
+
* symbol. `values[i]` is `NaN` when `valid[i]` is 0, which is exactly when
|
|
70
|
+
* `evaluateScalar` throws. `allValid` is true when every `valid` byte is 1,
|
|
71
|
+
* including when there are zero rows.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* import { compileScalar, scalar } from '@danielsimonjr/mathts-functions';
|
|
76
|
+
* const compiled = compileScalar(scalar.call('ln', scalar.symbol('x')), { variables: ['x'] });
|
|
77
|
+
* const { values, valid } = compiled.evaluate([new Float64Array([1, -1])]);
|
|
78
|
+
* [...valid]; // [1, 0]
|
|
79
|
+
* values[1]; // NaN
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export interface ScalarBatchResult {
|
|
83
|
+
readonly values: Float64Array;
|
|
84
|
+
readonly valid: Uint8Array;
|
|
85
|
+
readonly allValid: boolean;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* A compiled scalar. `variables` and `parameters` are copies made at compile time.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```ts
|
|
92
|
+
* import { compileScalar, scalar } from '@danielsimonjr/mathts-functions';
|
|
93
|
+
* const compiled = compileScalar(
|
|
94
|
+
* scalar.op('*', [scalar.symbol('x'), scalar.symbol('x')]),
|
|
95
|
+
* { variables: ['x'] }
|
|
96
|
+
* );
|
|
97
|
+
* [...compiled.evaluate([new Float64Array([2, 3])]).values]; // [4, 9]
|
|
98
|
+
* compiled.evaluateRow([2]); // 4
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
export interface CompiledScalar {
|
|
102
|
+
readonly variables: readonly string[];
|
|
103
|
+
readonly parameters: readonly string[];
|
|
104
|
+
/**
|
|
105
|
+
* Evaluate every row. Does not throw because a row is non-finite.
|
|
106
|
+
* Throws {@link ScalarEvalError} for a column or parameter mismatch, a bad
|
|
107
|
+
* `out` length, or a non-number parameter or row binding that is not a
|
|
108
|
+
* `Float64Array` column.
|
|
109
|
+
* When `out.length ===` the row count, `values` is `out` and no second value
|
|
110
|
+
* buffer is allocated.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```ts
|
|
114
|
+
* import { compileScalar, scalar } from '@danielsimonjr/mathts-functions';
|
|
115
|
+
* const compiled = compileScalar(
|
|
116
|
+
* scalar.op('*', [scalar.symbol('x'), scalar.symbol('x')]),
|
|
117
|
+
* { variables: ['x'] }
|
|
118
|
+
* );
|
|
119
|
+
* [...compiled.evaluate([new Float64Array([2, 3])]).values].join(','); // '4,9'
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
evaluate(columns: readonly Float64Array[], params?: ArrayLike<number>, out?: Float64Array): ScalarBatchResult;
|
|
123
|
+
/**
|
|
124
|
+
* One row, in `variables` order. Returns the same number `evaluateScalar`
|
|
125
|
+
* would, including non-finite bare values. Throws {@link ScalarEvalError}
|
|
126
|
+
* when the row is invalid or the binding is the wrong shape.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```ts
|
|
130
|
+
* import { compileScalar, scalar, ScalarEvalError } from '@danielsimonjr/mathts-functions';
|
|
131
|
+
* const compiled = compileScalar(scalar.call('ln', scalar.symbol('x')), { variables: ['x'] });
|
|
132
|
+
* try {
|
|
133
|
+
* compiled.evaluateRow([-1]);
|
|
134
|
+
* } catch (e) {
|
|
135
|
+
* e instanceof ScalarEvalError; // true — 'compileScalar: row produced a non-finite value.'
|
|
136
|
+
* }
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
evaluateRow(row: ArrayLike<number>, params?: ArrayLike<number>): number;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Batch primal values and exact parameter gradients of a compiled scalar.
|
|
143
|
+
* `grad[r * nParams + p]` is ∂f/∂`parameters[p]` at row `r`. Uses the same
|
|
144
|
+
* register tape as {@link compileScalar} with one forward lane per parameter.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```ts
|
|
148
|
+
* import { compileScalarGradient, scalar } from '@danielsimonjr/mathts-functions';
|
|
149
|
+
* const node = scalar.op('+', [
|
|
150
|
+
* scalar.op('*', [scalar.symbol('a'), scalar.symbol('x')]),
|
|
151
|
+
* scalar.symbol('b'),
|
|
152
|
+
* ]);
|
|
153
|
+
* const g = compileScalarGradient(node, { variables: ['x'], parameters: ['a', 'b'] });
|
|
154
|
+
* const { values, grad } = g.evaluate([new Float64Array([3])], [2, -1]);
|
|
155
|
+
* values[0]; // 5
|
|
156
|
+
* [...grad]; // 3, 1 — ∂/∂a = x, ∂/∂b = 1
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
export interface CompiledScalarGradient {
|
|
160
|
+
readonly variables: readonly string[];
|
|
161
|
+
readonly parameters: readonly string[];
|
|
162
|
+
/**
|
|
163
|
+
* Evaluate every row. Invalid rows store `NaN` in `values` and every
|
|
164
|
+
* gradient entry for that row. The mask matches {@link compileScalar} except
|
|
165
|
+
* when a consumed `^` partial is not finite (a live child lane of `0^0`, or
|
|
166
|
+
* `ln` of a negative base with a live exponent): those rows are extra-invalid.
|
|
167
|
+
* Throws {@link ScalarEvalError} for binding mismatches or bad `out` lengths.
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```ts
|
|
171
|
+
* import { compileScalarGradient, scalar } from '@danielsimonjr/mathts-functions';
|
|
172
|
+
* const node = scalar.op('+', [
|
|
173
|
+
* scalar.op('*', [scalar.symbol('a'), scalar.symbol('x')]),
|
|
174
|
+
* scalar.symbol('b'),
|
|
175
|
+
* ]);
|
|
176
|
+
* const compiled = compileScalarGradient(node, {
|
|
177
|
+
* variables: ['x'],
|
|
178
|
+
* parameters: ['a', 'b'],
|
|
179
|
+
* });
|
|
180
|
+
* const outV = new Float64Array(1);
|
|
181
|
+
* const outG = new Float64Array(2);
|
|
182
|
+
* compiled.evaluate([new Float64Array([3])], [2, -1], { values: outV, grad: outG });
|
|
183
|
+
* outV[0]; // 5
|
|
184
|
+
* [...outG]; // 3, 1
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
evaluate(columns: readonly Float64Array[], params: ArrayLike<number>, out?: {
|
|
188
|
+
values?: Float64Array;
|
|
189
|
+
grad?: Float64Array;
|
|
190
|
+
}): ScalarBatchResult & {
|
|
191
|
+
readonly grad: Float64Array;
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Run a compiled register tape over every row. Shared by {@link compileScalar} and the
|
|
196
|
+
* scalar batch worker (M14); not re-exported from the package barrel.
|
|
197
|
+
*/
|
|
198
|
+
export declare function evaluateScalarTape(tape: readonly TapeInstr[], rootReg: number, nRegs: number, columns: readonly Float64Array[], variables: readonly string[], params: ArrayLike<number>, parameters: readonly string[], messagePrefix: string, out?: Float64Array, scratch?: Float64Array): ScalarBatchResult;
|
|
199
|
+
export declare function buildTape(node: ScalarNode, options: CompileScalarOptions, messagePrefix: string): {
|
|
200
|
+
tape: readonly TapeInstr[];
|
|
201
|
+
rootReg: number;
|
|
202
|
+
nRegs: number;
|
|
203
|
+
variables: string[];
|
|
204
|
+
parameters: string[];
|
|
205
|
+
};
|
|
206
|
+
export declare function compileScalar(node: ScalarNode, options: CompileScalarOptions): CompiledScalar;
|
|
207
|
+
/**
|
|
208
|
+
* Compile `node` once for batch primal values and exact parameter gradients.
|
|
209
|
+
* Shares the M3 register tape; adds one forward lane per parameter via
|
|
210
|
+
* {@link DUAL_UNARY_RULES} and explicit `^` partials. Not reentrant.
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* ```ts
|
|
214
|
+
* import { compileScalarGradient, scalar } from '@danielsimonjr/mathts-functions';
|
|
215
|
+
* const node = scalar.op('+', [
|
|
216
|
+
* scalar.op('*', [scalar.symbol('a'), scalar.symbol('x')]),
|
|
217
|
+
* scalar.symbol('b'),
|
|
218
|
+
* ]);
|
|
219
|
+
* const g = compileScalarGradient(node, { variables: ['x'], parameters: ['a', 'b'] });
|
|
220
|
+
* const { values, grad } = g.evaluate([new Float64Array([3])], [2, -1]);
|
|
221
|
+
* values[0]; // 5
|
|
222
|
+
* [...grad]; // 3, 1
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
225
|
+
export declare function compileScalarGradient(node: ScalarNode, options: CompileScalarOptions): CompiledScalarGradient;
|
|
226
|
+
export {};
|
|
227
|
+
//# sourceMappingURL=scalar-compile.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scalar-compile.d.ts","sourceRoot":"","sources":["../src/scalar-compile.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,KAAK,EAAE,eAAe,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,kCAAkC,CAAC;AAiBpG,KAAK,SAAS,GACV;IAAE,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACxE;IAAE,QAAQ,CAAC,GAAG,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACzE;IAAE,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACxE;IAAE,QAAQ,CAAC,GAAG,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,G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AAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,UAAU,EAAE,MAAM,EAAE,CAAC;CACtB,CAwBA;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,oBAAoB,GAAG,cAAc,CAyD7F;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,qBAAqB,CACnC,IAAI,EAAE,UAAU,EAChB,OAAO,EAAE,oBAAoB,GAC5B,sBAAsB,CAmExB"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SI dimension inference and parameter-dimension solving over {@link ScalarNode}.
|
|
3
|
+
*
|
|
4
|
+
* {@link inferScalarDimension} walks a scalar tree with rational exponents on the
|
|
5
|
+
* seven base dimensions. {@link solveParameterDimensions} extends that walk with
|
|
6
|
+
* linear forms and {@link rationalNullspace}.
|
|
7
|
+
*
|
|
8
|
+
* @packageDocumentation
|
|
9
|
+
*/
|
|
10
|
+
import type { ScalarNode } from '@danielsimonjr/mathts-expression';
|
|
11
|
+
import { type SiDimensionVector } from '@danielsimonjr/mathts-core';
|
|
12
|
+
/** SI dimension vector with exact {@link Fraction} exponents. */
|
|
13
|
+
export type RationalDimension = SiDimensionVector;
|
|
14
|
+
/** Result of {@link inferScalarDimension}. */
|
|
15
|
+
export type DimensionInference = {
|
|
16
|
+
readonly ok: true;
|
|
17
|
+
readonly dimension: RationalDimension;
|
|
18
|
+
} | {
|
|
19
|
+
readonly ok: false;
|
|
20
|
+
readonly violation: 'add-mismatch' | 'transcendental-arg-not-dimensionless' | 'non-literal-exponent-on-dimensional-base' | 'unknown-symbol';
|
|
21
|
+
readonly path: readonly number[];
|
|
22
|
+
};
|
|
23
|
+
/** Result of {@link solveParameterDimensions}. */
|
|
24
|
+
export type ParameterDimensionResult = {
|
|
25
|
+
readonly solved: Readonly<Record<string, RationalDimension>>;
|
|
26
|
+
} | {
|
|
27
|
+
readonly underdetermined: readonly string[];
|
|
28
|
+
} | {
|
|
29
|
+
readonly inconsistent: true;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Infer the SI dimension of a scalar tree, or report a structural violation.
|
|
33
|
+
*/
|
|
34
|
+
export declare function inferScalarDimension(node: ScalarNode, dims: Readonly<Record<string, RationalDimension>>): DimensionInference;
|
|
35
|
+
/**
|
|
36
|
+
* Solve unknown parameter dimensions so the tree matches a target dimension.
|
|
37
|
+
*
|
|
38
|
+
* One walk of the tree collects, for each SI component, the equations that the
|
|
39
|
+
* parameters must satisfy: both sides of a `+` or `-` agree, the argument of a
|
|
40
|
+
* transcendental call and a non-literal exponent are dimensionless, and the root
|
|
41
|
+
* equals `target`. `x + c` with a length `x` therefore solves `c` as a length,
|
|
42
|
+
* and `sin(c * t)` solves `c` as one over `t`. The seven component systems are
|
|
43
|
+
* solved exactly with {@link rationalNullspace}.
|
|
44
|
+
*
|
|
45
|
+
* The result is `solved` (every parameter has one value), `underdetermined` (the
|
|
46
|
+
* named parameters are not fixed by the equations, or one sits in an exponent,
|
|
47
|
+
* which this solver does not treat as linear), or `inconsistent` (no values can
|
|
48
|
+
* satisfy the tree, including a symbol that is neither in `dims` nor in
|
|
49
|
+
* `parameters`).
|
|
50
|
+
*/
|
|
51
|
+
export declare function solveParameterDimensions(node: ScalarNode, dims: Readonly<Record<string, RationalDimension>>, target: RationalDimension, parameters: readonly string[], options?: {
|
|
52
|
+
readonly dimensionlessOnly?: boolean;
|
|
53
|
+
}): ParameterDimensionResult;
|
|
54
|
+
//# sourceMappingURL=scalar-dimension.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scalar-dimension.d.ts","sourceRoot":"","sources":["../src/scalar-dimension.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kCAAkC,CAAC;AACnE,OAAO,EAAiC,KAAK,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAInG,i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/mathts-expression").ScalarBu
|
|
|
27
27
|
export declare class ScalarEvalError extends Error {
|
|
28
28
|
constructor(message: string);
|
|
29
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Decimal spellings both evaluators bind as symbol names (`'2'`, `'1e-3'`).
|
|
32
|
+
* Hex, blank, `Infinity`, `NaN`, and underscores do not match. A match is
|
|
33
|
+
* bound only when `Number(name)` is finite (`'1e309'` matches and is rejected).
|
|
34
|
+
* Internal: not re-exported from `index.ts`.
|
|
35
|
+
*/
|
|
36
|
+
export declare const NUMERIC_LITERAL: RegExp;
|
|
30
37
|
/**
|
|
31
38
|
* Evaluate `node` with MathTS scalar functions.
|
|
32
39
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scalar-eval.d.ts","sourceRoot":"","sources":["../src/scalar-eval.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AA0BH,gFAAgF;AAChF,MAAM,MAAM,WAAW,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AAE3D,wFAAwF;AACxF,eAAO,MAAM,MAAM,0DAAwB,CAAC;AAE5C,4CAA4C;AAC5C,qBAAa,eAAgB,SAAQ,KAAK;IACxC,YAAY,OAAO,EAAE,MAAM,EAG1B;CACF;
|
|
1
|
+
{"version":3,"file":"scalar-eval.d.ts","sourceRoot":"","sources":["../src/scalar-eval.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AA0BH,gFAAgF;AAChF,MAAM,MAAM,WAAW,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AAE3D,wFAAwF;AACxF,eAAO,MAAM,MAAM,0DAAwB,CAAC;AAE5C,4CAA4C;AAC5C,qBAAa,eAAgB,SAAQ,KAAK;IACxC,YAAY,OAAO,EAAE,MAAM,EAG1B;CACF;AAgBD;;;;;GAKG;AACH,eAAO,MAAM,eAAe,QAAgD,CAAC;AAuF7E;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,GAAE,WAAgB,GAAG,MAAM,CAU7E"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mutation and crossover operators over {@link ScalarNode} trees.
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
import { type ScalarGrammar, type ScalarNode } from '@danielsimonjr/mathts-expression';
|
|
7
|
+
import type { SeededRng } from '@danielsimonjr/mathts-core';
|
|
8
|
+
/** Probability of negating a perturbed constant when mutating a numeric leaf. */
|
|
9
|
+
export declare const MUTATE_CONSTANT_NEGATE_PROBABILITY = 0.00743;
|
|
10
|
+
/** Default mutation weights (optimize/backsolve omitted). Draw order. */
|
|
11
|
+
export declare const DEFAULT_MUTATION_WEIGHTS: {
|
|
12
|
+
readonly rotateTree: 4.26;
|
|
13
|
+
readonly addNode: 2.47;
|
|
14
|
+
readonly deleteNode: 0.87;
|
|
15
|
+
readonly mutateOperator: 0.293;
|
|
16
|
+
readonly identity: 0.273;
|
|
17
|
+
readonly swapOperands: 0.198;
|
|
18
|
+
readonly mutateFeature: 0.1;
|
|
19
|
+
readonly mutateConstant: 0.0346;
|
|
20
|
+
readonly insertNode: 0.0112;
|
|
21
|
+
readonly simplifyConstants: 0.00209;
|
|
22
|
+
readonly randomizeTree: 0.000502;
|
|
23
|
+
};
|
|
24
|
+
export type MutationWeightKey = keyof typeof DEFAULT_MUTATION_WEIGHTS;
|
|
25
|
+
/** Context for mutation operators (no hidden defaults). */
|
|
26
|
+
export interface ScalarMutationContext {
|
|
27
|
+
readonly grammar: ScalarGrammar;
|
|
28
|
+
readonly perturbationFactor: number;
|
|
29
|
+
readonly temperature: number;
|
|
30
|
+
}
|
|
31
|
+
export type ScalarMutationOperator = (node: ScalarNode, rng: SeededRng, ctx: ScalarMutationContext) => ScalarNode | null;
|
|
32
|
+
/**
|
|
33
|
+
* Returns the input unchanged. Does not consume `rng`. The weight key is `identity`;
|
|
34
|
+
* the export carries a longer name because
|
|
35
|
+
* `@danielsimonjr/mathts-functions` already exports the matrix `identity`.
|
|
36
|
+
*/
|
|
37
|
+
export declare function identityMutation(node: ScalarNode, _rng?: SeededRng, _ctx?: ScalarMutationContext): ScalarNode | null;
|
|
38
|
+
/** Perturb one numeric leaf; `null` when none or grammar rejects. */
|
|
39
|
+
export declare function mutateConstant(node: ScalarNode, rng: SeededRng, ctx: ScalarMutationContext): ScalarNode | null;
|
|
40
|
+
/** Replace one operator or call name; `null` on failure. */
|
|
41
|
+
export declare function mutateOperator(node: ScalarNode, rng: SeededRng, ctx: ScalarMutationContext): ScalarNode | null;
|
|
42
|
+
/** Replace one symbol leaf with another allowed name. */
|
|
43
|
+
export declare function mutateFeature(node: ScalarNode, rng: SeededRng, ctx: ScalarMutationContext): ScalarNode | null;
|
|
44
|
+
/** Swap the two operands of one binary operator. */
|
|
45
|
+
export declare function swapOperands(node: ScalarNode, rng: SeededRng, ctx: ScalarMutationContext): ScalarNode | null;
|
|
46
|
+
/** Replace a subtree with a binary op or unary call over it and a new leaf. */
|
|
47
|
+
export declare function addNode(node: ScalarNode, rng: SeededRng, ctx: ScalarMutationContext): ScalarNode | null;
|
|
48
|
+
/**
|
|
49
|
+
* Insert a binary operator between a parent (an operator or a call) and one of
|
|
50
|
+
* its children. A single-node tree has no parent edge and returns `null`.
|
|
51
|
+
*/
|
|
52
|
+
export declare function insertNode(node: ScalarNode, rng: SeededRng, ctx: ScalarMutationContext): ScalarNode | null;
|
|
53
|
+
/**
|
|
54
|
+
* Replace a binary operator with one of its children. A tree that is a single
|
|
55
|
+
* node has no operator to delete and returns `null`.
|
|
56
|
+
*/
|
|
57
|
+
export declare function deleteNode(node: ScalarNode, rng: SeededRng, ctx: ScalarMutationContext): ScalarNode | null;
|
|
58
|
+
/** One left or right rotation at a nested binary node. */
|
|
59
|
+
export declare function rotateTree(node: ScalarNode, rng: SeededRng, ctx: ScalarMutationContext): ScalarNode | null;
|
|
60
|
+
/** Replace the tree with a random grammar-constrained tree. */
|
|
61
|
+
export declare function randomizeTree(node: ScalarNode, rng: SeededRng, ctx: ScalarMutationContext): ScalarNode | null;
|
|
62
|
+
/** Fold numeric-only subtrees via {@link evaluateScalar}. */
|
|
63
|
+
export declare function simplifyConstants(node: ScalarNode): ScalarNode;
|
|
64
|
+
/** Subtree crossover between two individuals. */
|
|
65
|
+
export declare function crossoverSubtrees(left: ScalarNode, right: ScalarNode, rng: SeededRng, ctx: ScalarMutationContext): ScalarNode | null;
|
|
66
|
+
/** Draw a mutation operator key from weighted defaults or overrides. */
|
|
67
|
+
export declare function drawMutationOperator(rng: SeededRng, weights?: Readonly<Record<MutationWeightKey, number>>): MutationWeightKey;
|
|
68
|
+
/** Combine mutation operators with {@link DEFAULT_MUTATION_WEIGHTS}. */
|
|
69
|
+
export declare function weightedMutation(weights?: Readonly<Record<MutationWeightKey, number>>): ScalarMutationOperator;
|
|
70
|
+
//# sourceMappingURL=scalar-mutate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scalar-mutate.d.ts","sourceRoot":"","sources":["../src/scalar-mutate.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAKL,KAAK,aAAa,EAClB,KAAK,UAAU,EAChB,MAAM,kCAAkC,CAAC;AAC1C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAC;AAG5D,iFAAiF;AACjF,eAAO,MAAM,kCAAkC,UAAU,CAAC;AAE1D,yEAAyE;AACzE,eAAO,MAAM,wBAAwB;aACnC,UAAU,EAAE,IAAI;aAChB,OAAO,EAAE,IAAI;aACb,UAAU,EAAE,IAAI;aAChB,cAAc,EAAE,KAAK;aACrB,QAAQ,EAAE,KAAK;aACf,YAAY,EAAE,KAAK;aACnB,aAAa,EAAE,GAAG;aAClB,cAAc,EAAE,MAAM;aACtB,UAAU,EA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
|
+
/**
|
|
2
|
+
* Convert between {@link ScalarNode} trees and wired expression {@link MathNode}s.
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
import type { MathNode, ScalarNode, StringOptions } from '@danielsimonjr/mathts-expression';
|
|
7
|
+
import type { SimplifyOptions } from '../types/index.js';
|
|
8
|
+
export type { MathNode, StringOptions, SimplifyOptions };
|
|
9
|
+
type ConstantCtor = new (value: number) => MathNode;
|
|
10
|
+
type SymbolCtor = new (name: string) => MathNode;
|
|
11
|
+
type OperatorCtor = new (op: string, fn: string, args: MathNode[], implicit?: boolean, isPercentage?: boolean) => MathNode;
|
|
12
|
+
type FunctionCtor = new (fn: string, args: MathNode[]) => MathNode;
|
|
13
|
+
export interface ScalarNodeBridgeDeps {
|
|
14
|
+
ConstantNode: ConstantCtor;
|
|
15
|
+
SymbolNode: SymbolCtor;
|
|
16
|
+
OperatorNode: OperatorCtor;
|
|
17
|
+
FunctionNode: FunctionCtor;
|
|
18
|
+
simplify: (node: MathNode, ...rest: unknown[]) => MathNode;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Wire {@link scalarToNode}, {@link nodeToScalar}, {@link simplifyScalar}, and
|
|
22
|
+
* {@link scalarToTex} to the expression node classes and `simplify` from the factory graph.
|
|
23
|
+
*/
|
|
24
|
+
export declare function createScalarNodeBridge(deps: ScalarNodeBridgeDeps): {
|
|
25
|
+
scalarToNode: (node: ScalarNode) => MathNode;
|
|
26
|
+
nodeToScalar: (node: MathNode) => ScalarNode;
|
|
27
|
+
simplifyScalar: (node: ScalarNode, options?: SimplifyOptions) => ScalarNode;
|
|
28
|
+
scalarToTex: (node: ScalarNode, options?: StringOptions) => string;
|
|
29
|
+
};
|
|
30
|
+
//# sourceMappingURL=scalar-node-bridge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scalar-node-bridge.d.ts","sourceRoot":"","sources":["../src/scalar-node-bridge.ts"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