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

Function extractSquareRoot

src/compute-engine/boxed-expression/factor.ts:209–220  ·  view source on GitHub ↗

* Extract the polynomial square root of a term if it is a perfect square, * returning `null` otherwise — crucially never an expression containing `Sqrt`, * `Abs`, `Root`, or a fractional power, so the difference-of-squares / * perfect-square factorings it feeds stay polynomial. * * Examples: x²

(term: Expression)

Source from the content-addressed store, hash-verified

207 if (crossTerm.isSame(positiveCross)) {
208 // Pattern: sqrt1² + 2*sqrt1*sqrt2 + sqrt2² = (sqrt1+sqrt2)²
209 return ce.expr(['Square', sqrt1.add(sqrt2).json]);
210 } else if (crossTerm.isSame(negativeCross)) {
211 // Pattern: sqrt1² - 2*sqrt1*sqrt2 + sqrt2² = (sqrt1-sqrt2)²
212 return ce.expr(['Square', sqrt1.sub(sqrt2).json]);
213 }
214 }
215 }
216
217 return null;
218}
219
220/**
221 * Replace every `Abs(u)` subexpression with its argument `u`, recursively
222 * (including inside powers and products). `|u|` and `u` have the same square,
223 * so for a polynomial factorization we want the bare polynomial representative

Callers 2

factorPerfectSquareFunction · 0.85

Calls 3

perfectSquareRootFunction · 0.85
stripAbsFunction · 0.85
isPolynomialFormFunction · 0.85

Tested by

no test coverage detected