universal-physics-tensor 0.4.5
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/LICENSE +21 -0
- package/README.md +377 -0
- package/dist/bridges/equations/be-11-decoherence-master.d.ts +83 -0
- package/dist/bridges/equations/be-11-decoherence-master.d.ts.map +1 -0
- package/dist/bridges/equations/be-11-decoherence-master.js +116 -0
- package/dist/bridges/equations/be-11-decoherence-master.js.map +1 -0
- package/dist/bridges/equations/be-12-coherence-length.d.ts +80 -0
- package/dist/bridges/equations/be-12-coherence-length.d.ts.map +1 -0
- package/dist/bridges/equations/be-12-coherence-length.js +128 -0
- package/dist/bridges/equations/be-12-coherence-length.js.map +1 -0
- package/dist/bridges/equations/be-13-einstein-trace.d.ts +89 -0
- package/dist/bridges/equations/be-13-einstein-trace.d.ts.map +1 -0
- package/dist/bridges/equations/be-13-einstein-trace.js +143 -0
- package/dist/bridges/equations/be-13-einstein-trace.js.map +1 -0
- package/dist/bridges/equations/be-14-ryu-takayanagi.d.ts +67 -0
- package/dist/bridges/equations/be-14-ryu-takayanagi.d.ts.map +1 -0
- package/dist/bridges/equations/be-14-ryu-takayanagi.js +112 -0
- package/dist/bridges/equations/be-14-ryu-takayanagi.js.map +1 -0
- package/dist/bridges/equations/be-15-emergence.d.ts +164 -0
- package/dist/bridges/equations/be-15-emergence.d.ts.map +1 -0
- package/dist/bridges/equations/be-15-emergence.js +204 -0
- package/dist/bridges/equations/be-15-emergence.js.map +1 -0
- package/dist/bridges/equations/be-16-landauer.d.ts +180 -0
- package/dist/bridges/equations/be-16-landauer.d.ts.map +1 -0
- package/dist/bridges/equations/be-16-landauer.js +206 -0
- package/dist/bridges/equations/be-16-landauer.js.map +1 -0
- package/dist/bridges/equations/be-17-einstein-cartan.d.ts +245 -0
- package/dist/bridges/equations/be-17-einstein-cartan.d.ts.map +1 -0
- package/dist/bridges/equations/be-17-einstein-cartan.js +304 -0
- package/dist/bridges/equations/be-17-einstein-cartan.js.map +1 -0
- package/dist/bridges/equations/be-18-higgs-mass.d.ts +65 -0
- package/dist/bridges/equations/be-18-higgs-mass.d.ts.map +1 -0
- package/dist/bridges/equations/be-18-higgs-mass.js +86 -0
- package/dist/bridges/equations/be-18-higgs-mass.js.map +1 -0
- package/dist/bridges/equations/be-19-quantum-bounce.d.ts +72 -0
- package/dist/bridges/equations/be-19-quantum-bounce.d.ts.map +1 -0
- package/dist/bridges/equations/be-19-quantum-bounce.js +151 -0
- package/dist/bridges/equations/be-19-quantum-bounce.js.map +1 -0
- package/dist/bridges/equations/be-20-vacuum-energy.d.ts +72 -0
- package/dist/bridges/equations/be-20-vacuum-energy.d.ts.map +1 -0
- package/dist/bridges/equations/be-20-vacuum-energy.js +115 -0
- package/dist/bridges/equations/be-20-vacuum-energy.js.map +1 -0
- package/dist/bridges/equations/be-21-kss-bound.d.ts +72 -0
- package/dist/bridges/equations/be-21-kss-bound.d.ts.map +1 -0
- package/dist/bridges/equations/be-21-kss-bound.js +103 -0
- package/dist/bridges/equations/be-21-kss-bound.js.map +1 -0
- package/dist/bridges/equations/be-22-topological-entanglement.d.ts +90 -0
- package/dist/bridges/equations/be-22-topological-entanglement.d.ts.map +1 -0
- package/dist/bridges/equations/be-22-topological-entanglement.js +123 -0
- package/dist/bridges/equations/be-22-topological-entanglement.js.map +1 -0
- package/dist/bridges/equations/be-23-syk-planckian.d.ts +89 -0
- package/dist/bridges/equations/be-23-syk-planckian.d.ts.map +1 -0
- package/dist/bridges/equations/be-23-syk-planckian.js +155 -0
- package/dist/bridges/equations/be-23-syk-planckian.js.map +1 -0
- package/dist/bridges/equations/be-24-foerster-fret.d.ts +81 -0
- package/dist/bridges/equations/be-24-foerster-fret.d.ts.map +1 -0
- package/dist/bridges/equations/be-24-foerster-fret.js +121 -0
- package/dist/bridges/equations/be-24-foerster-fret.js.map +1 -0
- package/dist/bridges/equations/be-25-iit-phi.d.ts +220 -0
- package/dist/bridges/equations/be-25-iit-phi.d.ts.map +1 -0
- package/dist/bridges/equations/be-25-iit-phi.js +259 -0
- package/dist/bridges/equations/be-25-iit-phi.js.map +1 -0
- package/dist/bridges/equations/be-25-orch-or.d.ts +78 -0
- package/dist/bridges/equations/be-25-orch-or.d.ts.map +1 -0
- package/dist/bridges/equations/be-25-orch-or.js +121 -0
- package/dist/bridges/equations/be-25-orch-or.js.map +1 -0
- package/dist/bridges/equations/be-26-dna-tunneling.d.ts +75 -0
- package/dist/bridges/equations/be-26-dna-tunneling.d.ts.map +1 -0
- package/dist/bridges/equations/be-26-dna-tunneling.js +138 -0
- package/dist/bridges/equations/be-26-dna-tunneling.js.map +1 -0
- package/dist/bridges/equations/be-27-effective-temperature.d.ts +81 -0
- package/dist/bridges/equations/be-27-effective-temperature.d.ts.map +1 -0
- package/dist/bridges/equations/be-27-effective-temperature.js +120 -0
- package/dist/bridges/equations/be-27-effective-temperature.js.map +1 -0
- package/dist/bridges/equations/be-28-onsager-entropy-production.d.ts +175 -0
- package/dist/bridges/equations/be-28-onsager-entropy-production.d.ts.map +1 -0
- package/dist/bridges/equations/be-28-onsager-entropy-production.js +203 -0
- package/dist/bridges/equations/be-28-onsager-entropy-production.js.map +1 -0
- package/dist/bridges/equations/be-29-jarzynski.d.ts +86 -0
- package/dist/bridges/equations/be-29-jarzynski.d.ts.map +1 -0
- package/dist/bridges/equations/be-29-jarzynski.js +132 -0
- package/dist/bridges/equations/be-29-jarzynski.js.map +1 -0
- package/dist/bridges/equations/be-30-flm-first-law.d.ts +93 -0
- package/dist/bridges/equations/be-30-flm-first-law.d.ts.map +1 -0
- package/dist/bridges/equations/be-30-flm-first-law.js +109 -0
- package/dist/bridges/equations/be-30-flm-first-law.js.map +1 -0
- package/dist/bridges/equations/be-31-causal-set-bd.d.ts +96 -0
- package/dist/bridges/equations/be-31-causal-set-bd.d.ts.map +1 -0
- package/dist/bridges/equations/be-31-causal-set-bd.js +133 -0
- package/dist/bridges/equations/be-31-causal-set-bd.js.map +1 -0
- package/dist/bridges/equations/be-32-quantum-reference-frame.d.ts +113 -0
- package/dist/bridges/equations/be-32-quantum-reference-frame.d.ts.map +1 -0
- package/dist/bridges/equations/be-32-quantum-reference-frame.js +155 -0
- package/dist/bridges/equations/be-32-quantum-reference-frame.js.map +1 -0
- package/dist/bridges/equations/be-33-hertz-millis.d.ts +77 -0
- package/dist/bridges/equations/be-33-hertz-millis.d.ts.map +1 -0
- package/dist/bridges/equations/be-33-hertz-millis.js +113 -0
- package/dist/bridges/equations/be-33-hertz-millis.js.map +1 -0
- package/dist/bridges/equations/be-34-kibble-zurek.d.ts +76 -0
- package/dist/bridges/equations/be-34-kibble-zurek.d.ts.map +1 -0
- package/dist/bridges/equations/be-34-kibble-zurek.js +139 -0
- package/dist/bridges/equations/be-34-kibble-zurek.js.map +1 -0
- package/dist/bridges/equations/be-35-conformal-bootstrap.d.ts +117 -0
- package/dist/bridges/equations/be-35-conformal-bootstrap.d.ts.map +1 -0
- package/dist/bridges/equations/be-35-conformal-bootstrap.js +167 -0
- package/dist/bridges/equations/be-35-conformal-bootstrap.js.map +1 -0
- package/dist/bridges/equations/be-36-gw-speed-bound.d.ts +77 -0
- package/dist/bridges/equations/be-36-gw-speed-bound.d.ts.map +1 -0
- package/dist/bridges/equations/be-36-gw-speed-bound.js +107 -0
- package/dist/bridges/equations/be-36-gw-speed-bound.js.map +1 -0
- package/dist/bridges/equations/be-37-shapiro-delay.d.ts +260 -0
- package/dist/bridges/equations/be-37-shapiro-delay.d.ts.map +1 -0
- package/dist/bridges/equations/be-37-shapiro-delay.js +429 -0
- package/dist/bridges/equations/be-37-shapiro-delay.js.map +1 -0
- package/dist/bridges/equations/be-38-mond.d.ts +86 -0
- package/dist/bridges/equations/be-38-mond.d.ts.map +1 -0
- package/dist/bridges/equations/be-38-mond.js +122 -0
- package/dist/bridges/equations/be-38-mond.js.map +1 -0
- package/dist/bridges/equations/be-39-asymptotic-safety.d.ts +106 -0
- package/dist/bridges/equations/be-39-asymptotic-safety.d.ts.map +1 -0
- package/dist/bridges/equations/be-39-asymptotic-safety.js +155 -0
- package/dist/bridges/equations/be-39-asymptotic-safety.js.map +1 -0
- package/dist/bridges/equations/be-40-composite-higgs.d.ts +81 -0
- package/dist/bridges/equations/be-40-composite-higgs.d.ts.map +1 -0
- package/dist/bridges/equations/be-40-composite-higgs.js +149 -0
- package/dist/bridges/equations/be-40-composite-higgs.js.map +1 -0
- package/dist/bridges/equations/be-41-swampland.d.ts +67 -0
- package/dist/bridges/equations/be-41-swampland.d.ts.map +1 -0
- package/dist/bridges/equations/be-41-swampland.js +109 -0
- package/dist/bridges/equations/be-41-swampland.js.map +1 -0
- package/dist/bridges/equations/be-42-hawking-temperature.d.ts +67 -0
- package/dist/bridges/equations/be-42-hawking-temperature.d.ts.map +1 -0
- package/dist/bridges/equations/be-42-hawking-temperature.js +109 -0
- package/dist/bridges/equations/be-42-hawking-temperature.js.map +1 -0
- package/dist/bridges/equations/be-43-er-epr.d.ts +73 -0
- package/dist/bridges/equations/be-43-er-epr.d.ts.map +1 -0
- package/dist/bridges/equations/be-43-er-epr.js +114 -0
- package/dist/bridges/equations/be-43-er-epr.js.map +1 -0
- package/dist/bridges/equations/be-44-soft-hair.d.ts +151 -0
- package/dist/bridges/equations/be-44-soft-hair.d.ts.map +1 -0
- package/dist/bridges/equations/be-44-soft-hair.js +185 -0
- package/dist/bridges/equations/be-44-soft-hair.js.map +1 -0
- package/dist/bridges/equations/be-45-tcc.d.ts +116 -0
- package/dist/bridges/equations/be-45-tcc.d.ts.map +1 -0
- package/dist/bridges/equations/be-45-tcc.js +157 -0
- package/dist/bridges/equations/be-45-tcc.js.map +1 -0
- package/dist/bridges/equations/be-46-multiverse-measure.d.ts +163 -0
- package/dist/bridges/equations/be-46-multiverse-measure.d.ts.map +1 -0
- package/dist/bridges/equations/be-46-multiverse-measure.js +198 -0
- package/dist/bridges/equations/be-46-multiverse-measure.js.map +1 -0
- package/dist/bridges/equations/be-47-bbn-dark-sector.d.ts +72 -0
- package/dist/bridges/equations/be-47-bbn-dark-sector.d.ts.map +1 -0
- package/dist/bridges/equations/be-47-bbn-dark-sector.js +121 -0
- package/dist/bridges/equations/be-47-bbn-dark-sector.js.map +1 -0
- package/dist/bridges/equations/be-48-grw-localization.d.ts +84 -0
- package/dist/bridges/equations/be-48-grw-localization.d.ts.map +1 -0
- package/dist/bridges/equations/be-48-grw-localization.js +107 -0
- package/dist/bridges/equations/be-48-grw-localization.js.map +1 -0
- package/dist/bridges/equations/be-49-quantum-darwinism.d.ts +97 -0
- package/dist/bridges/equations/be-49-quantum-darwinism.d.ts.map +1 -0
- package/dist/bridges/equations/be-49-quantum-darwinism.js +129 -0
- package/dist/bridges/equations/be-49-quantum-darwinism.js.map +1 -0
- package/dist/bridges/equations/be-50-wheeler-feynman.d.ts +120 -0
- package/dist/bridges/equations/be-50-wheeler-feynman.d.ts.map +1 -0
- package/dist/bridges/equations/be-50-wheeler-feynman.js +151 -0
- package/dist/bridges/equations/be-50-wheeler-feynman.js.map +1 -0
- package/dist/bridges/gravitational-lensing.d.ts +52 -0
- package/dist/bridges/gravitational-lensing.d.ts.map +1 -0
- package/dist/bridges/gravitational-lensing.js +48 -0
- package/dist/bridges/gravitational-lensing.js.map +1 -0
- package/dist/bridges/index.d.ts +104 -0
- package/dist/bridges/index.d.ts.map +1 -0
- package/dist/bridges/index.js +1663 -0
- package/dist/bridges/index.js.map +1 -0
- package/dist/bridges/perihelion-precession.d.ts +62 -0
- package/dist/bridges/perihelion-precession.d.ts.map +1 -0
- package/dist/bridges/perihelion-precession.js +68 -0
- package/dist/bridges/perihelion-precession.js.map +1 -0
- package/dist/core/tensor.d.ts +135 -0
- package/dist/core/tensor.d.ts.map +1 -0
- package/dist/core/tensor.js +376 -0
- package/dist/core/tensor.js.map +1 -0
- package/dist/core/types.d.ts +131 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +25 -0
- package/dist/core/types.js.map +1 -0
- package/dist/dimensional/algebra.d.ts +34 -0
- package/dist/dimensional/algebra.d.ts.map +1 -0
- package/dist/dimensional/algebra.js +90 -0
- package/dist/dimensional/algebra.js.map +1 -0
- package/dist/dimensional/bridge-check.d.ts +48 -0
- package/dist/dimensional/bridge-check.d.ts.map +1 -0
- package/dist/dimensional/bridge-check.js +137 -0
- package/dist/dimensional/bridge-check.js.map +1 -0
- package/dist/dimensional/connection-validators.d.ts +53 -0
- package/dist/dimensional/connection-validators.d.ts.map +1 -0
- package/dist/dimensional/connection-validators.js +84 -0
- package/dist/dimensional/connection-validators.js.map +1 -0
- package/dist/dimensional/connection.d.ts +40 -0
- package/dist/dimensional/connection.d.ts.map +1 -0
- package/dist/dimensional/connection.js +81 -0
- package/dist/dimensional/connection.js.map +1 -0
- package/dist/dimensional/constants.d.ts +30 -0
- package/dist/dimensional/constants.d.ts.map +1 -0
- package/dist/dimensional/constants.js +31 -0
- package/dist/dimensional/constants.js.map +1 -0
- package/dist/dimensional/errors.d.ts +161 -0
- package/dist/dimensional/errors.d.ts.map +1 -0
- package/dist/dimensional/errors.js +254 -0
- package/dist/dimensional/errors.js.map +1 -0
- package/dist/dimensional/fresh-label.d.ts +26 -0
- package/dist/dimensional/fresh-label.d.ts.map +1 -0
- package/dist/dimensional/fresh-label.js +31 -0
- package/dist/dimensional/fresh-label.js.map +1 -0
- package/dist/dimensional/metric-validators.d.ts +124 -0
- package/dist/dimensional/metric-validators.d.ts.map +1 -0
- package/dist/dimensional/metric-validators.js +141 -0
- package/dist/dimensional/metric-validators.js.map +1 -0
- package/dist/dimensional/metric.d.ts +67 -0
- package/dist/dimensional/metric.d.ts.map +1 -0
- package/dist/dimensional/metric.js +177 -0
- package/dist/dimensional/metric.js.map +1 -0
- package/dist/dimensional/tensor.d.ts +153 -0
- package/dist/dimensional/tensor.d.ts.map +1 -0
- package/dist/dimensional/tensor.js +138 -0
- package/dist/dimensional/tensor.js.map +1 -0
- package/dist/dimensional/types.d.ts +50 -0
- package/dist/dimensional/types.d.ts.map +1 -0
- package/dist/dimensional/types.js +66 -0
- package/dist/dimensional/types.js.map +1 -0
- package/dist/dimensional/validator.d.ts +84 -0
- package/dist/dimensional/validator.d.ts.map +1 -0
- package/dist/dimensional/validator.js +505 -0
- package/dist/dimensional/validator.js.map +1 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +37 -0
- package/dist/index.js.map +1 -0
- package/dist/numerical/be37-covariant-eikonal.d.ts +90 -0
- package/dist/numerical/be37-covariant-eikonal.d.ts.map +1 -0
- package/dist/numerical/be37-covariant-eikonal.js +79 -0
- package/dist/numerical/be37-covariant-eikonal.js.map +1 -0
- package/dist/numerical/connection-lowering-helpers.d.ts +107 -0
- package/dist/numerical/connection-lowering-helpers.d.ts.map +1 -0
- package/dist/numerical/connection-lowering-helpers.js +315 -0
- package/dist/numerical/connection-lowering-helpers.js.map +1 -0
- package/dist/numerical/engine-registry.d.ts +50 -0
- package/dist/numerical/engine-registry.d.ts.map +1 -0
- package/dist/numerical/engine-registry.js +82 -0
- package/dist/numerical/engine-registry.js.map +1 -0
- package/dist/numerical/errors.d.ts +28 -0
- package/dist/numerical/errors.d.ts.map +1 -0
- package/dist/numerical/errors.js +39 -0
- package/dist/numerical/errors.js.map +1 -0
- package/dist/numerical/float64-engine.d.ts +53 -0
- package/dist/numerical/float64-engine.d.ts.map +1 -0
- package/dist/numerical/float64-engine.js +638 -0
- package/dist/numerical/float64-engine.js.map +1 -0
- package/dist/numerical/geodesic-integrator.d.ts +62 -0
- package/dist/numerical/geodesic-integrator.d.ts.map +1 -0
- package/dist/numerical/geodesic-integrator.js +103 -0
- package/dist/numerical/geodesic-integrator.js.map +1 -0
- package/dist/numerical/grid-field.d.ts +24 -0
- package/dist/numerical/grid-field.d.ts.map +1 -0
- package/dist/numerical/grid-field.js +2 -0
- package/dist/numerical/grid-field.js.map +1 -0
- package/dist/numerical/index.d.ts +80 -0
- package/dist/numerical/index.d.ts.map +1 -0
- package/dist/numerical/index.js +75 -0
- package/dist/numerical/index.js.map +1 -0
- package/dist/numerical/lowering.d.ts +48 -0
- package/dist/numerical/lowering.d.ts.map +1 -0
- package/dist/numerical/lowering.js +443 -0
- package/dist/numerical/lowering.js.map +1 -0
- package/dist/numerical/mathts-engine.d.ts +55 -0
- package/dist/numerical/mathts-engine.d.ts.map +1 -0
- package/dist/numerical/mathts-engine.js +164 -0
- package/dist/numerical/mathts-engine.js.map +1 -0
- package/dist/numerical/metric-inverse.d.ts +31 -0
- package/dist/numerical/metric-inverse.d.ts.map +1 -0
- package/dist/numerical/metric-inverse.js +68 -0
- package/dist/numerical/metric-inverse.js.map +1 -0
- package/dist/numerical/null-ray-integrator.d.ts +13 -0
- package/dist/numerical/null-ray-integrator.d.ts.map +1 -0
- package/dist/numerical/null-ray-integrator.js +53 -0
- package/dist/numerical/null-ray-integrator.js.map +1 -0
- package/dist/numerical/pderiv.d.ts +43 -0
- package/dist/numerical/pderiv.d.ts.map +1 -0
- package/dist/numerical/pderiv.js +121 -0
- package/dist/numerical/pderiv.js.map +1 -0
- package/dist/numerical/tensor-engine.d.ts +114 -0
- package/dist/numerical/tensor-engine.d.ts.map +1 -0
- package/dist/numerical/tensor-engine.js +64 -0
- package/dist/numerical/tensor-engine.js.map +1 -0
- package/dist/numerical/types.d.ts +37 -0
- package/dist/numerical/types.d.ts.map +1 -0
- package/dist/numerical/types.js +8 -0
- package/dist/numerical/types.js.map +1 -0
- package/package.json +72 -0
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AST → EngineTensor lowering. Walks a validated ExprNode tree and emits
|
|
3
|
+
* TensorEngine calls. The tensor-product case turns computeContraction()'s
|
|
4
|
+
* contractionPairs into an EinsumSpec (v0.3.5-Design.md §5).
|
|
5
|
+
*
|
|
6
|
+
* v0.3.5 scope: flat tensor-products of contractable operands (the three
|
|
7
|
+
* index-carrying nodes + tensor-partial-derivative) + scalar operands.
|
|
8
|
+
* Nested products, integral/derivative numerical eval are out of scope and
|
|
9
|
+
* throw clearly.
|
|
10
|
+
*
|
|
11
|
+
* @module numerical/lowering
|
|
12
|
+
*/
|
|
13
|
+
import { validate } from '../dimensional/validator.js';
|
|
14
|
+
import { computeContraction, validateTensorSymbol } from '../dimensional/tensor.js';
|
|
15
|
+
import { pderivGrid, pderivNumericalFn, pderivSymbolic } from './pderiv.js';
|
|
16
|
+
import { validateMetricTensor, validateKroneckerDelta, validatePartialDerivative, } from '../dimensional/metric-validators.js';
|
|
17
|
+
import { NumericalBackendError } from './errors.js';
|
|
18
|
+
import { zeroTensor, zeroTensorLike, flatToNested, flattenNA, tensorAdd, tensorAddScaled, computeChristoffelTensor, contractChristoffelWithOperand, getMetricDerivFlat, } from './connection-lowering-helpers.js';
|
|
19
|
+
function isContractable(node) {
|
|
20
|
+
return node.kind === 'tensor-symbol'
|
|
21
|
+
|| node.kind === 'metric-tensor'
|
|
22
|
+
|| node.kind === 'kronecker-delta'
|
|
23
|
+
|| node.kind === 'tensor-partial-derivative';
|
|
24
|
+
}
|
|
25
|
+
/** The effective index list of a contractable operand, in the SAME axis
|
|
26
|
+
* order as its lowered EngineTensor. For the three index-carrying nodes
|
|
27
|
+
* that is `node.indices`. For ∂_μ(of) the lowered tensor has shape
|
|
28
|
+
* [...ofShape, N] (Task 10), so the effective indices are `of`'s indices
|
|
29
|
+
* (v0.3.5: `of` is a tensor-symbol or metric-tensor) followed by the wrtIndex. */
|
|
30
|
+
function operandIndices(node) {
|
|
31
|
+
if (node.kind === 'tensor-partial-derivative') {
|
|
32
|
+
const of = node.of;
|
|
33
|
+
if (of.kind !== 'tensor-symbol' && of.kind !== 'metric-tensor') {
|
|
34
|
+
throw new NumericalBackendError(`lowering: a tensor-partial-derivative operand requires a tensor-symbol or `
|
|
35
|
+
+ `metric-tensor 'of' in v0.3.5/v0.4.0 — got '${of.kind}'`);
|
|
36
|
+
}
|
|
37
|
+
return [...of.indices, node.wrtIndex];
|
|
38
|
+
}
|
|
39
|
+
return node.indices;
|
|
40
|
+
}
|
|
41
|
+
function dimensionOf(inputs) {
|
|
42
|
+
return inputs.dimension ?? 4;
|
|
43
|
+
}
|
|
44
|
+
/** Look up a named tensor's concrete value, or throw. */
|
|
45
|
+
function requireValue(name, inputs) {
|
|
46
|
+
const v = inputs.tensors.get(name);
|
|
47
|
+
if (v === undefined) {
|
|
48
|
+
throw new NumericalBackendError(`lowering: no value supplied for "${name}" in inputs.tensors`);
|
|
49
|
+
}
|
|
50
|
+
return v;
|
|
51
|
+
}
|
|
52
|
+
/** Flatten a NestedArray to a plain number[] and check expected size.
|
|
53
|
+
* Delegates to the canonical flattenNA() from connection-lowering-helpers. */
|
|
54
|
+
function flattenNestedArray(data, expectedSize) {
|
|
55
|
+
const out = flattenNA(data);
|
|
56
|
+
if (out.length !== expectedSize) {
|
|
57
|
+
throw new NumericalBackendError(`lowering: flattenNestedArray: got ${out.length} elements, expected ${expectedSize}`);
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Build the EinsumSpec for a flat tensor-product.
|
|
63
|
+
*
|
|
64
|
+
* CRITICAL (finding #1 of the v0.3.5 adversarial review): this function does
|
|
65
|
+
* NOT decide which indices contract. `computeContraction()` — the v0.2.0
|
|
66
|
+
* symbolic-layer authority, which is variance-aware and applies the
|
|
67
|
+
* implicit-identity-metric rule — already classified every label as
|
|
68
|
+
* contracted or free. buildEinsumSpec only maps those already-classified
|
|
69
|
+
* labels to their (operand, axis) sites. There is exactly one
|
|
70
|
+
* contraction-decision implementation in the codebase.
|
|
71
|
+
*
|
|
72
|
+
* @internal — cross-module/test use only; not part of the consumer surface.
|
|
73
|
+
*/
|
|
74
|
+
export function buildEinsumSpec(operands, contractionPairs, freeIndices) {
|
|
75
|
+
// Map every label to its (operand, axis) sites — via operandIndices() so a
|
|
76
|
+
// tensor-partial-derivative operand contributes its [...of.indices, wrtIndex]
|
|
77
|
+
// effective axes.
|
|
78
|
+
const sites = new Map();
|
|
79
|
+
operands.forEach((op, opIndex) => {
|
|
80
|
+
operandIndices(op).forEach((idx, axis) => {
|
|
81
|
+
const list = sites.get(idx.label) ?? [];
|
|
82
|
+
list.push([opIndex, axis]);
|
|
83
|
+
sites.set(idx.label, list);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
// Contractions: computeContraction told us exactly which labels contract.
|
|
87
|
+
const contractions = [];
|
|
88
|
+
for (const { label } of contractionPairs) {
|
|
89
|
+
const locs = sites.get(label);
|
|
90
|
+
if (!locs || locs.length !== 2) {
|
|
91
|
+
throw new NumericalBackendError(`buildEinsumSpec: contracted label "${label}" must occur at exactly 2 `
|
|
92
|
+
+ `operand sites, found ${locs?.length ?? 0}`);
|
|
93
|
+
}
|
|
94
|
+
contractions.push({ pair: [locs[0], locs[1]] });
|
|
95
|
+
}
|
|
96
|
+
// Free axes in computeContraction's freeIndices order — the symbolic
|
|
97
|
+
// layer's canonical ordering, so the numerical result's axis order
|
|
98
|
+
// matches what the rest of UPT expects.
|
|
99
|
+
const free = [];
|
|
100
|
+
for (const label of freeIndices.keys()) {
|
|
101
|
+
const locs = sites.get(label);
|
|
102
|
+
if (!locs || locs.length !== 1) {
|
|
103
|
+
throw new NumericalBackendError(`buildEinsumSpec: free label "${label}" must occur at exactly 1 `
|
|
104
|
+
+ `operand site, found ${locs?.length ?? 0}`);
|
|
105
|
+
}
|
|
106
|
+
free.push({ operand: locs[0][0], axis: locs[0][1] });
|
|
107
|
+
}
|
|
108
|
+
return { contractions, free };
|
|
109
|
+
}
|
|
110
|
+
/** Lower a contractable operand to a concrete EngineTensor. */
|
|
111
|
+
function lowerContractable(node, inputs, engine) {
|
|
112
|
+
const N = dimensionOf(inputs);
|
|
113
|
+
if (node.kind === 'kronecker-delta')
|
|
114
|
+
return engine.identity(N);
|
|
115
|
+
if (node.kind === 'tensor-partial-derivative') {
|
|
116
|
+
// Delegate to lowerNode, which handles all three numericalForm paths.
|
|
117
|
+
return lowerNode(node, inputs, engine);
|
|
118
|
+
}
|
|
119
|
+
// tensor-symbol / metric-tensor: shape is N per index.
|
|
120
|
+
const shape = node.indices.map(() => N);
|
|
121
|
+
return engine.fromNested(requireValue(node.name, inputs), shape);
|
|
122
|
+
}
|
|
123
|
+
/** Lower a validated ExprNode to an EngineTensor.
|
|
124
|
+
* @internal — cross-module/test use only; not part of the consumer surface. */
|
|
125
|
+
export function lowerNode(node, inputs, engine) {
|
|
126
|
+
switch (node.kind) {
|
|
127
|
+
case 'symbol':
|
|
128
|
+
return engine.fromNested(requireValue(node.name, inputs), []);
|
|
129
|
+
case 'tensor-symbol':
|
|
130
|
+
case 'metric-tensor':
|
|
131
|
+
case 'kronecker-delta':
|
|
132
|
+
return lowerContractable(node, inputs, engine);
|
|
133
|
+
case 'op': {
|
|
134
|
+
if (node.op === '+' || node.op === '-') {
|
|
135
|
+
if (node.args.length === 0)
|
|
136
|
+
return engine.fromNested(0, []);
|
|
137
|
+
let acc = lowerNode(node.args[0], inputs, engine);
|
|
138
|
+
for (let i = 1; i < node.args.length; i++) {
|
|
139
|
+
const next = lowerNode(node.args[i], inputs, engine);
|
|
140
|
+
acc = node.op === '+' ? engine.add(acc, next) : engine.sub(acc, next);
|
|
141
|
+
}
|
|
142
|
+
return acc;
|
|
143
|
+
}
|
|
144
|
+
// '*' / '/' / '^' are scalar-only. Guard arity so an unvalidated AST
|
|
145
|
+
// surfaces a clean NumericalBackendError instead of a raw TypeError
|
|
146
|
+
// (zero-operand '/') or a silent NaN (wrong-arity '^'). The '*' case
|
|
147
|
+
// with zero args is already fine — reduce(..., 1) returns 1.
|
|
148
|
+
if (node.op === '/' && node.args.length === 0) {
|
|
149
|
+
throw new NumericalBackendError("lowering: op '/' requires at least one operand");
|
|
150
|
+
}
|
|
151
|
+
if (node.op === '^' && node.args.length !== 2) {
|
|
152
|
+
throw new NumericalBackendError(`lowering: op '^' requires exactly 2 operands (base, exponent), got ${node.args.length}`);
|
|
153
|
+
}
|
|
154
|
+
// '*' / '/' / '^' are scalar-only (the validator rejects tensor
|
|
155
|
+
// operands). Lower each to rank-0, do the arithmetic in JS, lift back.
|
|
156
|
+
const scalars = node.args.map((a) => {
|
|
157
|
+
const t = lowerNode(a, inputs, engine);
|
|
158
|
+
if (t.shape.length !== 0) {
|
|
159
|
+
throw new NumericalBackendError(`lowering: op '${node.op}' got a rank-${t.shape.length} operand — scalar ops require rank-0`);
|
|
160
|
+
}
|
|
161
|
+
return engine.toNested(t);
|
|
162
|
+
});
|
|
163
|
+
let value;
|
|
164
|
+
if (node.op === '*')
|
|
165
|
+
value = scalars.reduce((a, b) => a * b, 1);
|
|
166
|
+
else if (node.op === '/')
|
|
167
|
+
value = scalars.reduce((a, b) => a / b);
|
|
168
|
+
else
|
|
169
|
+
value = Math.pow(scalars[0], scalars[1]); // '^'
|
|
170
|
+
return engine.fromNested(value, []);
|
|
171
|
+
}
|
|
172
|
+
case 'tensor-product': {
|
|
173
|
+
for (const arg of node.args) {
|
|
174
|
+
if (arg.kind === 'tensor-product') {
|
|
175
|
+
throw new NumericalBackendError('lowering: nested tensor-product numerical evaluation is not supported in v0.3.5 — '
|
|
176
|
+
+ 'flatten the contraction into a single product');
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
// Contractable operands (tensor-symbol / metric-tensor / kronecker-delta
|
|
180
|
+
// / tensor-partial-derivative) participate in the einsum; everything
|
|
181
|
+
// else must be a rank-0 scalar factor.
|
|
182
|
+
const operands = node.args.filter(isContractable);
|
|
183
|
+
const scalarArgs = node.args.filter((a) => !isContractable(a));
|
|
184
|
+
// computeContraction is the single authority on WHICH indices contract
|
|
185
|
+
// (variance-aware, implicit-metric rule — finding #1). buildEinsumSpec
|
|
186
|
+
// only maps the labels it classified to (operand, axis) sites. The
|
|
187
|
+
// recursive validateContractionChild resolves tensor-partial-derivative
|
|
188
|
+
// operands via validatePartialDerivative.
|
|
189
|
+
function validateContractionChild(child) {
|
|
190
|
+
if (child.kind === 'tensor-symbol')
|
|
191
|
+
return validateTensorSymbol(child);
|
|
192
|
+
if (child.kind === 'metric-tensor')
|
|
193
|
+
return validateMetricTensor(child);
|
|
194
|
+
if (child.kind === 'kronecker-delta')
|
|
195
|
+
return validateKroneckerDelta(child);
|
|
196
|
+
if (child.kind === 'tensor-partial-derivative') {
|
|
197
|
+
const r = validatePartialDerivative(child, (g) => validateContractionChild(g));
|
|
198
|
+
return { dim: r.dim, freeIndices: r.freeIndices };
|
|
199
|
+
}
|
|
200
|
+
throw new NumericalBackendError(`lowering: unexpected operand '${child.kind}' in tensor-product einsum`);
|
|
201
|
+
}
|
|
202
|
+
const { contractionPairs, freeIndices } = computeContraction(operands, validateContractionChild);
|
|
203
|
+
const spec = buildEinsumSpec(operands, contractionPairs, freeIndices);
|
|
204
|
+
const operandTensors = operands.map((n) => lowerContractable(n, inputs, engine));
|
|
205
|
+
let result = engine.einsum(spec, ...operandTensors);
|
|
206
|
+
// Scalar operands multiply the whole contraction.
|
|
207
|
+
for (const s of scalarArgs) {
|
|
208
|
+
const st = lowerNode(s, inputs, engine);
|
|
209
|
+
if (st.shape.length !== 0) {
|
|
210
|
+
throw new NumericalBackendError('lowering: non-scalar non-contractable operand in tensor-product');
|
|
211
|
+
}
|
|
212
|
+
result = engine.scale(result, engine.toNested(st));
|
|
213
|
+
}
|
|
214
|
+
return result;
|
|
215
|
+
}
|
|
216
|
+
case 'tensor-partial-derivative': {
|
|
217
|
+
// v0.3.5/v0.4.0 scope: `of` is a tensor-symbol or metric-tensor.
|
|
218
|
+
// ∂_μ(of) adds the wrtIndex as a trailing axis — the result shape is
|
|
219
|
+
// [...ofShape, N], NOT ofShape. (For BE-37, `of` = the scalar S is
|
|
220
|
+
// rank-0, so ∂_μ S is the rank-1 wave covector k_μ, shape [N].)
|
|
221
|
+
const of = node.of;
|
|
222
|
+
// v0.4.0 extension: metric-tensor pderiv dispatch.
|
|
223
|
+
if (of.kind === 'metric-tensor') {
|
|
224
|
+
const mNode = of;
|
|
225
|
+
const strategy = mNode.derivativeStrategy ?? 'computed';
|
|
226
|
+
const N = dimensionOf(inputs);
|
|
227
|
+
const coordLabel = node.wrtIndex.label;
|
|
228
|
+
const ofShape = mNode.indices.map(() => N);
|
|
229
|
+
const resultShape = [...ofShape, N];
|
|
230
|
+
if (strategy === 'zero') {
|
|
231
|
+
// ∂g = 0 everywhere (constant/flat metric).
|
|
232
|
+
return zeroTensor(resultShape, engine);
|
|
233
|
+
}
|
|
234
|
+
if (strategy === 'supplied') {
|
|
235
|
+
// Look up the N slices ∂_mu g for mu=0..N-1 and stack them as the
|
|
236
|
+
// trailing axis. Key format: `${metricName}/${coordLabel}_${mu}`.
|
|
237
|
+
// Result shape: [...ofShape, N] = [N, N, N] for a rank-2 metric.
|
|
238
|
+
// Build as a flat array then convert to nested for engine.fromNested().
|
|
239
|
+
const size = resultShape.reduce((a, b) => a * b, 1);
|
|
240
|
+
const flat = new Array(size).fill(0);
|
|
241
|
+
// ofShape = [N, N], resultShape = [N, N, N]
|
|
242
|
+
// flat[i*N*N + j*N + mu] = (∂_mu g)[i][j]
|
|
243
|
+
for (let mu = 0; mu < N; mu++) {
|
|
244
|
+
const key = `${mNode.name}/${coordLabel}_${mu}`;
|
|
245
|
+
const slice = inputs.metricDerivatives?.get(key);
|
|
246
|
+
if (slice === undefined) {
|
|
247
|
+
throw new NumericalBackendError(`lowering: metric-tensor pderiv with strategy='supplied': ` +
|
|
248
|
+
`no metricDerivatives entry for "${key}"`);
|
|
249
|
+
}
|
|
250
|
+
// Flatten the slice (shape [N,N]) and write into flat at stride N (last axis)
|
|
251
|
+
const flatSlice = flattenNestedArray(slice, N * N);
|
|
252
|
+
for (let ij = 0; ij < N * N; ij++) {
|
|
253
|
+
flat[ij * N + mu] = flatSlice[ij];
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return engine.fromNested(flatToNested(flat, resultShape), resultShape);
|
|
257
|
+
}
|
|
258
|
+
// strategy === 'computed': treat as zero for constant-tensor metrics
|
|
259
|
+
// (the raw metric supplied via inputs.tensors has no coordinate dependence).
|
|
260
|
+
return zeroTensor(resultShape, engine);
|
|
261
|
+
}
|
|
262
|
+
if (of.kind !== 'tensor-symbol') {
|
|
263
|
+
throw new NumericalBackendError(`lowering: tensor-partial-derivative numerical eval requires a tensor-symbol `
|
|
264
|
+
+ `or metric-tensor 'of' operand in v0.3.5/v0.4.0 — got '${of.kind}'`);
|
|
265
|
+
}
|
|
266
|
+
const sym = of;
|
|
267
|
+
const form = sym.numericalForm ?? 'symbolic';
|
|
268
|
+
const coordLabel = node.wrtIndex.label;
|
|
269
|
+
const N = dimensionOf(inputs);
|
|
270
|
+
const ofShape = sym.indices.map(() => N);
|
|
271
|
+
const resultShape = [...ofShape, N];
|
|
272
|
+
if (form === 'symbolic') {
|
|
273
|
+
// pderivSymbolic returns the caller-supplied full ∂_μ(of) tensor,
|
|
274
|
+
// which must already be of shape [...ofShape, N].
|
|
275
|
+
const d = pderivSymbolic(sym.name, coordLabel, inputs.derivatives ?? new Map());
|
|
276
|
+
return engine.fromNested(d, resultShape);
|
|
277
|
+
}
|
|
278
|
+
if (form === 'numerical-fn') {
|
|
279
|
+
// v0.3.5: 'numerical-fn' lowering is scoped to a rank-0 `of` (scalar
|
|
280
|
+
// field). ∂_μ ranges over all N coordinate axes — stack the N
|
|
281
|
+
// single-axis derivatives into the rank-1 result. Higher-rank fields
|
|
282
|
+
// under 'numerical-fn' are a v0.4.0 concern.
|
|
283
|
+
if (ofShape.length !== 0) {
|
|
284
|
+
throw new NumericalBackendError(`lowering: 'numerical-fn' pderiv lowering supports a rank-0 'of' in v0.3.5; `
|
|
285
|
+
+ `"${sym.name}" is rank ${ofShape.length}`);
|
|
286
|
+
}
|
|
287
|
+
const fn = inputs.fields?.get(sym.name);
|
|
288
|
+
if (!fn) {
|
|
289
|
+
throw new NumericalBackendError(`lowering: 'numerical-fn' tensor-symbol "${sym.name}" has no field fn in inputs.fields`);
|
|
290
|
+
}
|
|
291
|
+
const coordValues = inputs.coords ? [...inputs.coords.values()] : [];
|
|
292
|
+
const components = [];
|
|
293
|
+
for (let axis = 0; axis < N; axis++) {
|
|
294
|
+
components.push(pderivNumericalFn(fn, coordValues, axis));
|
|
295
|
+
}
|
|
296
|
+
return engine.fromNested(components, [N]);
|
|
297
|
+
}
|
|
298
|
+
// form === 'grid': the GridField is the field sampled over space, and
|
|
299
|
+
// pderivGrid returns the derivative field sampled on that same grid —
|
|
300
|
+
// result shape is grid.shape. This is a distinct semantic from
|
|
301
|
+
// symbolic/numerical-fn (a sampled derivative field, not a single
|
|
302
|
+
// tensor), kept as the v0.5.0 BSSN forward-compat path. v0.3.5 has no
|
|
303
|
+
// release test driving 'grid' through lowering; pderivGrid itself is
|
|
304
|
+
// unit-tested in Task 10's pderiv.test.ts.
|
|
305
|
+
const grid = inputs.grids?.get(sym.name);
|
|
306
|
+
if (!grid) {
|
|
307
|
+
throw new NumericalBackendError(`lowering: 'grid' tensor-symbol "${sym.name}" has no GridField in inputs.grids`);
|
|
308
|
+
}
|
|
309
|
+
const gridAxis = inputs.coords ? [...inputs.coords.keys()].indexOf(coordLabel) : 0;
|
|
310
|
+
const flat = pderivGrid(grid, gridAxis < 0 ? 0 : gridAxis);
|
|
311
|
+
return engine.fromNested(flat.length === 1 ? flat[0] : flat, grid.shape);
|
|
312
|
+
}
|
|
313
|
+
case 'covariant-derivative': {
|
|
314
|
+
// S2(a) fix: `of.freeIndices` does NOT exist on the raw ExprNode.
|
|
315
|
+
// Re-validate the `of` subtree to obtain its free-index structure.
|
|
316
|
+
const covNode = node;
|
|
317
|
+
const ofExpr = covNode.of;
|
|
318
|
+
const ofValidation = validate(ofExpr);
|
|
319
|
+
// Build ordered list of free indices: [{label, variance}].
|
|
320
|
+
// Iterate of.indices (NOT validation.freeIndices Map) — declaration order IS
|
|
321
|
+
// the axis layout. For tensor-symbol and metric-tensor, of.indices is the
|
|
322
|
+
// canonical axis order; the freeIndices Map insertion order is also
|
|
323
|
+
// of.indices order (validateTensorSymbol iterates node.indices), but relying
|
|
324
|
+
// on that is an undocumented invariant. Iterating of.indices directly makes
|
|
325
|
+
// the axis ordering guarantee explicit and safe for future of-kinds.
|
|
326
|
+
const ofFreeIndices = [];
|
|
327
|
+
const ofIndices = ofExpr.indices;
|
|
328
|
+
if (ofIndices) {
|
|
329
|
+
// tensor-symbol / metric-tensor: iterate the declared indices in order.
|
|
330
|
+
// Each index is either free (present in ofValidation.freeIndices) or
|
|
331
|
+
// contracted (absent — skip). In practice, of.indices for a simple
|
|
332
|
+
// tensor-symbol or metric-tensor has no contracted indices, but we
|
|
333
|
+
// guard with the Map lookup for safety.
|
|
334
|
+
for (const idx of ofIndices) {
|
|
335
|
+
const counts = ofValidation.freeIndices.get(idx.label);
|
|
336
|
+
if (counts === undefined)
|
|
337
|
+
continue; // contracted — not a free axis
|
|
338
|
+
ofFreeIndices.push({
|
|
339
|
+
label: idx.label,
|
|
340
|
+
variance: idx.variance,
|
|
341
|
+
pos: ofFreeIndices.length,
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
// Fallback for future of-kinds without .indices (e.g. tensor-product).
|
|
347
|
+
// Map iteration order is insertion order — a best-effort axis ordering.
|
|
348
|
+
let axisPos = 0;
|
|
349
|
+
for (const [label, counts] of ofValidation.freeIndices) {
|
|
350
|
+
for (let i = 0; i < counts.upper; i++) {
|
|
351
|
+
ofFreeIndices.push({ label, variance: 'upper', pos: axisPos++ });
|
|
352
|
+
}
|
|
353
|
+
for (let i = 0; i < counts.lower; i++) {
|
|
354
|
+
ofFreeIndices.push({ label, variance: 'lower', pos: axisPos++ });
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
// Lower the operand tensor.
|
|
359
|
+
const ofTensor = lowerNode(ofExpr, inputs, engine);
|
|
360
|
+
const N = dimensionOf(inputs);
|
|
361
|
+
const strategy = covNode.gLower.derivativeStrategy ?? 'computed';
|
|
362
|
+
// S2(b): strategy='zero' → flat space, Γ=0, ∇_μ T = ∂_μ T.
|
|
363
|
+
// For constant tensors (like a flat metric), ∂_μ T = 0, so result is all zeros.
|
|
364
|
+
// We return a zero tensor of shape [...ofShape, N] (wrt axis appended last).
|
|
365
|
+
if (strategy === 'zero') {
|
|
366
|
+
const outShape = [...ofTensor.shape, N];
|
|
367
|
+
return zeroTensor(outShape, engine);
|
|
368
|
+
}
|
|
369
|
+
// v0.4.0 CRITICAL FIX (Finding #1): 'computed' on a raw-tensor metric means
|
|
370
|
+
// constant metric → Γ = 0 → covariant-derivative = partial derivative only.
|
|
371
|
+
// v0.5.0 will replace this with coordinate-grid finite-difference.
|
|
372
|
+
// This early return MUST come before the Christoffel construction below,
|
|
373
|
+
// because getMetricDerivFlat only accepts 'zero' | 'supplied'; if 'computed'
|
|
374
|
+
// fell through, the type cast was silently wrong at runtime and the function
|
|
375
|
+
// threw NumericalBackendError for every coordinate.
|
|
376
|
+
//
|
|
377
|
+
// Compute partial first so we can return it directly.
|
|
378
|
+
// (partial is also needed by the 'supplied' path below, so we compute it
|
|
379
|
+
// unconditionally and use the early return only for 'computed'.)
|
|
380
|
+
let partial;
|
|
381
|
+
if (ofExpr.kind === 'tensor-symbol' || ofExpr.kind === 'metric-tensor') {
|
|
382
|
+
const pdNode = {
|
|
383
|
+
kind: 'tensor-partial-derivative',
|
|
384
|
+
of: ofExpr,
|
|
385
|
+
wrt: covNode.wrt,
|
|
386
|
+
wrtIndex: covNode.wrtIndex,
|
|
387
|
+
};
|
|
388
|
+
partial = lowerNode(pdNode, inputs, engine);
|
|
389
|
+
}
|
|
390
|
+
else {
|
|
391
|
+
// Scalar or other: partial derivative is zero.
|
|
392
|
+
const outShape = [...ofTensor.shape, N];
|
|
393
|
+
partial = zeroTensor(outShape, engine);
|
|
394
|
+
}
|
|
395
|
+
// v0.4.0 spec: 'computed' on a raw-tensor metric = constant metric → Γ = 0.
|
|
396
|
+
// The covariant-derivative reduces to the ordinary partial derivative.
|
|
397
|
+
// v0.5.0 will add coordinate-grid finite-difference here.
|
|
398
|
+
if (strategy === 'computed') {
|
|
399
|
+
return partial;
|
|
400
|
+
}
|
|
401
|
+
// S2(c) + S2(d): Build Christoffel Γ^α_{μν} from metric data and apply
|
|
402
|
+
// the sign rule to all free indices of `of`:
|
|
403
|
+
// ∇_μ T^α_β = ∂_μ T^α_β + Γ^α_{μλ} T^λ_β − Γ^λ_{μβ} T^α_λ
|
|
404
|
+
//
|
|
405
|
+
// If `of` has no free indices (scalar), no correction needed.
|
|
406
|
+
if (ofFreeIndices.length === 0) {
|
|
407
|
+
return partial;
|
|
408
|
+
}
|
|
409
|
+
// Get metric and inverse metric data.
|
|
410
|
+
const gLowerNode = covNode.gLower;
|
|
411
|
+
const gInverseNode = covNode.gInverse;
|
|
412
|
+
const gInverseData = flattenNestedArray(requireValue(gInverseNode.name, inputs), N * N);
|
|
413
|
+
// coordLabel for metricDerivatives keys (wrtIndex.label of the covariant derivative)
|
|
414
|
+
const coordLabel = covNode.wrtIndex.label;
|
|
415
|
+
// getMetricDeriv(mu): returns flat [N*N] of ∂_{mu} g
|
|
416
|
+
const getMetricDeriv = (mu) => getMetricDerivFlat(gLowerNode.name, coordLabel, mu, strategy, N, inputs.metricDerivatives);
|
|
417
|
+
// Compute Γ^α_{μν} from metric data.
|
|
418
|
+
const GammaTensor = computeChristoffelTensor(gInverseData, getMetricDeriv, N, engine);
|
|
419
|
+
const GammaFlat = flattenNestedArray(engine.toNested(GammaTensor), N * N * N);
|
|
420
|
+
// Apply the Christoffel correction for each free index of `of`.
|
|
421
|
+
// S2(d) sign rule: upper → +Γ, lower → −Γ.
|
|
422
|
+
const ofFlat = flattenNestedArray(engine.toNested(ofTensor), ofTensor.shape.reduce((a, b) => a * b, 1));
|
|
423
|
+
const ofShapeArr = [...ofTensor.shape];
|
|
424
|
+
let correction = zeroTensorLike(partial, engine);
|
|
425
|
+
for (const freeIdx of ofFreeIndices) {
|
|
426
|
+
const term = contractChristoffelWithOperand(GammaFlat, ofFlat, ofShapeArr, freeIdx.pos, freeIdx.variance, N, engine);
|
|
427
|
+
const sign = freeIdx.variance === 'upper' ? 1 : -1;
|
|
428
|
+
correction = tensorAddScaled(correction, term, sign, engine);
|
|
429
|
+
}
|
|
430
|
+
return tensorAdd(partial, correction, engine);
|
|
431
|
+
}
|
|
432
|
+
case 'integral':
|
|
433
|
+
case 'derivative':
|
|
434
|
+
throw new NumericalBackendError(`lowering: '${node.kind}' is not numerically evaluated in v0.3.5 — `
|
|
435
|
+
+ 'use tensor-partial-derivative for differentiation');
|
|
436
|
+
default: {
|
|
437
|
+
const _exhaustive = node;
|
|
438
|
+
void _exhaustive;
|
|
439
|
+
throw new NumericalBackendError(`lowering: unknown ExprNode.kind ${JSON.stringify(node.kind)}`);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
//# sourceMappingURL=lowering.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lowering.js","sourceRoot":"","sources":["../../src/numerical/lowering.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,EAAE,QAAQ,EAAE,MAAM,6BAA6B,CAAC;AAGvD,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AACpF,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC5E,OAAO,EACL,oBAAoB,EACpB,sBAAsB,EACtB,yBAAyB,GAC1B,MAAM,qCAAqC,CAAC;AAO7C,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EACL,UAAU,EACV,cAAc,EACd,YAAY,EACZ,SAAS,EACT,SAAS,EACT,eAAe,EACf,wBAAwB,EACxB,8BAA8B,EAC9B,kBAAkB,GACnB,MAAM,kCAAkC,CAAC;AAS1C,SAAS,cAAc,CAAC,IAAc;IACpC,OAAO,IAAI,CAAC,IAAI,KAAK,eAAe;WAC/B,IAAI,CAAC,IAAI,KAAK,eAAe;WAC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C,CAAC;YACjD,KAAK,MAAM,OAAO,IAAI,aAAa,EAAE,CAAC;gBACpC,MAAM,IAAI,GAAG,8BAA8B,CACzC,SAAS,EACT,MAAM,EACN,UAAU,EACV,OAAO,CAAC,GAAG,EACX,OAAO,CAAC,QAAQ,EAChB,CAAC,EACD,MAAM,CACP,CAAC;gBACF,MAAM,IAAI,GAAG,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;gBACnD,UAAU,GAAG,eAAe,CAAC,UAAU,EAAE,IAAI,EAAE,IAAc,EAAE,MAAM,CAAC,CAAC;YACzE,CAAC;YAED,OAAO,SAAS,CAAC,OAAO,EAAE,UAAU,EAAE,MAAM,CAAC,CAAC;QAChD,CAAC;QAED,KAAK,UAAU,CAAC;QAChB,KAAK,YAAY;YACf,MAAM,IAAI,qBAAqB,CAC7B,cAAc,IAAI,CAAC,IAAI,6CAA6C;kBAClE,mDAAmD,CACtD,CAAC;QAEJ,OAAO,CAAC,CAAC,CAAC;YACR,MAAM,WAAW,GAAU,IAAI,CAAC;YAChC,KAAK,WAAW,CAAC;YACjB,MAAM,IAAI,qBAAqB,CAC7B,mCAAmC,IAAI,CAAC,SAAS,CAAE,IAA2B,CAAC,IAAI,CAAC,EAAE,CACvF,CAAC;QACJ,CAAC;IACH,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { EngineTensor, TensorEngine, EinsumSpec, ForwardGradResult, ReverseGradResult } from './tensor-engine.js';
|
|
2
|
+
import type { NestedArray } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* `TensorEngine` backed by `@danielsimonjr/mathts-tensor`'s rank-N Tensor —
|
|
5
|
+
* the second v0.3.5 engine; becomes UPT's default in v0.4.0.
|
|
6
|
+
*
|
|
7
|
+
* @public — reachable only via the
|
|
8
|
+
* `universal-physics-tensor/numerical/mathts-engine` exports subpath; requires
|
|
9
|
+
* the `@danielsimonjr/mathts-tensor` optional dependency. Intentionally NOT
|
|
10
|
+
* re-exported from the root barrel.
|
|
11
|
+
*/
|
|
12
|
+
export declare class MathTSEngine implements TensorEngine {
|
|
13
|
+
readonly name = "MathTSEngine";
|
|
14
|
+
fromNested(data: NestedArray, shape: ReadonlyArray<number>): EngineTensor;
|
|
15
|
+
toNested(t: EngineTensor): NestedArray;
|
|
16
|
+
einsum(spec: EinsumSpec, ...operands: EngineTensor[]): EngineTensor;
|
|
17
|
+
matMul(a: EngineTensor, b: EngineTensor): EngineTensor;
|
|
18
|
+
transpose(t: EngineTensor, perm?: ReadonlyArray<number>): EngineTensor;
|
|
19
|
+
reshape(t: EngineTensor, shape: ReadonlyArray<number>): EngineTensor;
|
|
20
|
+
add(a: EngineTensor, b: EngineTensor): EngineTensor;
|
|
21
|
+
sub(a: EngineTensor, b: EngineTensor): EngineTensor;
|
|
22
|
+
mul(a: EngineTensor, b: EngineTensor): EngineTensor;
|
|
23
|
+
scale(t: EngineTensor, k: number): EngineTensor;
|
|
24
|
+
identity(n: number): EngineTensor;
|
|
25
|
+
normInf(t: EngineTensor): number;
|
|
26
|
+
/** Unwrap an EngineTensor to its underlying MathTS Tensor (for the AD boundary). */
|
|
27
|
+
private toMathTSTensor;
|
|
28
|
+
/** Wrap a MathTS Tensor (returned by autograd) back as an EngineTensor. */
|
|
29
|
+
private fromMathTSTensor;
|
|
30
|
+
/**
|
|
31
|
+
* Forward-mode automatic differentiation via a lazy-imported
|
|
32
|
+
* `@danielsimonjr/mathts-autograd`. Throws `EngineCapabilityError` if the
|
|
33
|
+
* optional dependency is absent.
|
|
34
|
+
*
|
|
35
|
+
* S1 fix: `fn` is passed UNCHANGED to `autograd.forwardGrad`. The autograd
|
|
36
|
+
* package wraps `x` as a DualTensor internally; MathTSEngine's arithmetic
|
|
37
|
+
* methods (mul/add/sub/scale) must dispatch DualTensor inputs through to
|
|
38
|
+
* mathts-autograd's dual arithmetic — there must be no fn-wrapping at the
|
|
39
|
+
* boundary, which would strip DualTensor instrumentation and silence the
|
|
40
|
+
* AD trace.
|
|
41
|
+
*/
|
|
42
|
+
forwardGrad(fn: (x: EngineTensor) => EngineTensor, x: EngineTensor): Promise<ForwardGradResult>;
|
|
43
|
+
/**
|
|
44
|
+
* Reverse-mode automatic differentiation via a lazy-imported
|
|
45
|
+
* `@danielsimonjr/mathts-autograd`. Throws `EngineCapabilityError` if the
|
|
46
|
+
* optional dependency is absent.
|
|
47
|
+
*
|
|
48
|
+
* S1 fix: `fn` is passed UNCHANGED (see forwardGrad note above). The
|
|
49
|
+
* autograd package wraps `x` as a TapedTensor; MathTSEngine's op methods
|
|
50
|
+
* must dispatch TapedTensor inputs to mathts-autograd's tape arithmetic via
|
|
51
|
+
* `'tape' in arg` branching.
|
|
52
|
+
*/
|
|
53
|
+
reverseGrad(fn: (x: EngineTensor) => EngineTensor, x: EngineTensor, cotangent?: EngineTensor): Promise<ReverseGradResult>;
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=mathts-engine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mathts-engine.d.ts","sourceRoot":"","sources":["../../src/numerical/mathts-engine.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,UAAU,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACvH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAiB9C;;;;;;;;GAQG;AACH,qBAAa,YAAa,YAAW,YAAY;IAC/C,QAAQ,CAAC,IAAI,kBAAkB;IAE/B,UAAU,CAAC,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE,aAAa,CAAC,MAAM,CAAC,GAAG,YAAY;IAGzE,QAAQ,CAAC,CAAC,EAAE,YAAY,GAAG,WAAW;IAItC,MAAM,CAAC,IAAI,EAAE,UAAU,EAAE,GAAG,QAAQ,EAAE,YAAY,EAAE,GAAG,YA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