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

Function piecewise

src/compute-engine/interval/comparison.ts:155–203  ·  view source on GitHub ↗
(
  xOrCond: Interval | IntervalResult | BoolInterval,
  conditionOrTrue:
    | ((x: Interval) => BoolInterval)
    | (() => Interval | IntervalResult),
  trueOrFalse:
    | ((x: Interval) => Interval | IntervalResult)
    | (() => Interval | IntervalResult),
  falseBranch?: (x: Interval) => Interval | IntervalResult
)

Source from the content-addressed store, hash-verified

153 if (!Array.isArray(unwrapped)) return 'maybe';
154 const [aVal, bVal] = unwrapped;
155 if (aVal.lo >= bVal.hi) return 'true';
156 if (aVal.hi < bVal.lo) return 'false';
157 return 'maybe';
158}
159
160/**
161 * Equality comparison for intervals.
162 *
163 * Returns:
164 * - 'true' only if both are point intervals with same value
165 * - 'false' if intervals don't overlap
166 * - 'maybe' if intervals overlap
167 */
168export function equal(
169 a: Interval | IntervalResult,
170 b: Interval | IntervalResult
171): BoolInterval {
172 const unwrapped = unwrapOrPropagate(a, b);
173 if (!Array.isArray(unwrapped)) return 'maybe';
174 const [aVal, bVal] = unwrapped;
175 // Equal only if both are point intervals with same value
176 if (aVal.lo === aVal.hi && bVal.lo === bVal.hi && aVal.lo === bVal.lo)
177 return 'true';
178 // Definitely not equal if intervals don't overlap
179 if (aVal.hi < bVal.lo || bVal.hi < aVal.lo) return 'false';
180 return 'maybe';
181}
182
183/**
184 * Not equal comparison for intervals.
185 */
186export function notEqual(
187 a: Interval | IntervalResult,
188 b: Interval | IntervalResult
189): BoolInterval {
190 const eq = equal(a, b);
191 if (eq === 'true') return 'false';
192 if (eq === 'false') return 'true';
193 return 'maybe';
194}
195
196/**
197 * Logical AND for boolean intervals.
198 */
199export function and(a: BoolInterval, b: BoolInterval): BoolInterval {
200 if (a === 'false' || b === 'false') return 'false';
201 if (a === 'true' && b === 'true') return 'true';
202 return 'maybe';
203}
204
205/**
206 * Logical OR for boolean intervals.

Callers

nothing calls this directly

Calls 3

unionResultsFunction · 0.90
unwrapOrPropagateFunction · 0.90
toResultFunction · 0.85

Tested by

no test coverage detected