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

Function square

src/compute-engine/interval/elementary.ts:63–77  ·  view source on GitHub ↗
(x: Interval | IntervalResult)

Source from the content-addressed store, hash-verified

61 * Need to handle sign change at 0 since x^2 is not monotonic.
62 */
63export function square(x: Interval | IntervalResult): IntervalResult {
64 const unwrapped = unwrapOrPropagate(x);
65 if (!Array.isArray(unwrapped)) return unwrapped;
66 const [xVal] = unwrapped;
67 if (xVal.lo >= 0) {
68 // Entirely non-negative: monotonically increasing
69 return ok({ lo: xVal.lo * xVal.lo, hi: xVal.hi * xVal.hi });
70 } else if (xVal.hi <= 0) {
71 // Entirely non-positive: monotonically decreasing, flip bounds
72 return ok({ lo: xVal.hi * xVal.hi, hi: xVal.lo * xVal.lo });
73 } else {
74 // Interval contains 0 - minimum is 0
75 return ok({ lo: 0, hi: Math.max(xVal.lo * xVal.lo, xVal.hi * xVal.hi) });
76 }
77}
78
79/**
80 * Integer power helper for non-negative integer exponents.

Callers 1

Calls 2

unwrapOrPropagateFunction · 0.90
okFunction · 0.90

Tested by

no test coverage detected