MCPcopy Create free account
hub / github.com/cortex-js/compute-engine / harmonize

Function harmonize

src/compute-engine/boxed-expression/solve.ts:2546–2569  ·  view source on GitHub ↗

Transform expr into one or more equivalent expressions (with the same * root set) that are easier to solve. * * The unknown in `expr` is the literal `_x` symbol; the substitution * pre-binds the `_x` wildcard to it so that the harmonization rule patterns * (which use `_x` for the unknown) match

(expr: Expression)

Source from the content-addressed store, hash-verified

2544}
2545
2546/** Substitute the fresh symbol `u` for the generator `g` throughout `node`.
2547 *
2548 * Besides exact structural matches (`g → u`), an *exponential* generator
2549 * `g = bᵉ⁰` (constant base `b`) also absorbs every integer power sharing that
2550 * base: `bᵏ·ᵉ⁰ → uᵏ` (so `e^{2x}` becomes `u²` when `u = e^x`). This is done
2551 * directly — materializing `(eˣ)²` would be canonicalized straight back to
2552 * `e^{2x}`, undoing the substitution. */
2553function substituteGenerator(
2554 node: Expression,
2555 g: Expression,
2556 u: Expression
2557): Expression {
2558 const ce = node.engine;
2559 if (node.isSame(g)) return u;
2560 if (
2561 isFunction(g, 'Power') &&
2562 isFunction(node, 'Power') &&
2563 node.op1.isSame(g.op1)
2564 ) {
2565 const k = asExactSmallInteger(node.op2.div(g.op2).simplify());
2566 if (k !== null && k >= 1) return ce.function('Power', [u, ce.number(k)]);
2567 }
2568 if (isFunction(node))
2569 return ce.function(
2570 node.operator,
2571 node.ops!.map((o) => substituteGenerator(o, g, u))
2572 );

Callers 1

findUnivariateRootsFunction · 0.85

Calls 4

matchAnyRulesFunction · 0.90
getRuleSetMethod · 0.65
symbolMethod · 0.65
isSameMethod · 0.65

Tested by

no test coverage detected