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

Function collect

src/compute-engine/boxed-expression/solve.ts:1485–1514  ·  view source on GitHub ↗
(node: Expression)

Source from the content-addressed store, hash-verified

1483
1484 // Get the argument inside the sqrt
1485 if (!isFunction(sqrtTerm)) return expr;
1486 const sqrtArg = sqrtTerm.op1;
1487 if (!sqrtArg) return expr;
1488
1489 // Check if the sqrt argument contains the variable
1490 if (!sqrtArg.has(variable)) return expr;
1491
1492 // Collect all non-sqrt terms: these form g(x) where √f(x) + g(x) = 0
1493 // So √f(x) = -g(x), and we square to get f(x) = g(x)²
1494 const nonSqrtTerms = ops.filter((_, i) => i !== sqrtIndex);
1495
1496 if (nonSqrtTerms.length === 0) return expr;
1497
1498 // g(x) = -(sum of non-sqrt terms), since √f(x) + g(x) = 0 means √f(x) = -g(x)
1499 let gExpr: Expression;
1500 if (nonSqrtTerms.length === 1) {
1501 gExpr = nonSqrtTerms[0].neg();
1502 } else {
1503 gExpr = ce.function('Add', nonSqrtTerms).neg();
1504 }
1505
1506 // Check that g(x) is linear or polynomial in the variable
1507 // (We want to avoid cases where g(x) itself contains sqrt terms)
1508 if (gExpr.has('Sqrt')) return expr;
1509
1510 // Transform: √f(x) = -g(x) becomes f(x) = g(x)²
1511 // So the new equation is: f(x) - g(x)² = 0
1512 const gSquared = gExpr.mul(gExpr);
1513 const transformed = sqrtArg.sub(gSquared).simplify();
1514
1515 return transformed;
1516}
1517

Callers 1

Calls 6

isSymbolFunction · 0.90
isFunctionFunction · 0.90
asSmallIntegerFunction · 0.90
rationalExponentFunction · 0.85
addMethod · 0.65
hasMethod · 0.65

Tested by

no test coverage detected