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

Function equalOrder

src/compute-engine/boxed-expression/order.ts:125–154  ·  view source on GitHub ↗
(a: Expression, b: Expression)

Source from the content-addressed store, hash-verified

123 if (aLex < bLex) return -1;
124 if (aLex > bLex) return +1;
125 }
126 return order(a, b);
127}
128
129/**
130 * Sort terms of a sum with `addOrder` semantics, computing each term's
131 * degree/lexicographic keys once instead of on every comparison (`addOrder`
132 * re-walks both expressions per comparison — O(n log n) walks per sort).
133 * The comparator chain below returns exactly the values `addOrder` would,
134 * and `Array.sort` is stable, so the result order is identical.
135 */
136export function sortAddTerms(
137 xs: ReadonlyArray<Expression>
138): ReadonlyArray<Expression> {
139 if (xs.length <= 1) return [...xs];
140 const keyed = xs.map((x) => ({
141 x,
142 total: totalDegree(x),
143 max: maxDegree(x),
144 lex: revlex(x),
145 }));
146 keyed.sort((a, b) => {
147 if (a.total !== b.total) return b.total - a.total;
148 if (a.max !== b.max) return b.max - a.max;
149 if (a.lex || b.lex) {
150 if (!a.lex) return +1;
151 if (!b.lex) return -1;
152 if (a.lex < b.lex) return -1;
153 if (a.lex > b.lex) return +1;
154 }
155 return order(a.x, b.x);
156 });
157 return keyed.map((k) => k.x);

Callers

nothing calls this directly

Calls 5

isSymbolFunction · 0.90
isNumberFunction · 0.90
eqRankFunction · 0.85
compareFloatFunction · 0.85
orderFunction · 0.70

Tested by

no test coverage detected