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

Function numericCostFunction

src/compute-engine/cost-function.ts:47–63  ·  view source on GitHub ↗

* The Cost Function is used to select the simplest between two expressions: * the one with the lowest cost function. * * It is based on the Mathematica cost function. * * * From https://reference.wolfram.com/language/ref/ComplexityFunction.html * * ``` * SimplifyCount[p_] := * Which[ *

(n: NumericValue | number)

Source from the content-addressed store, hash-verified

45
46function numericCostFunction(n: NumericValue | number): number {
47 if (typeof n === 'number') {
48 if (n === 0) return 1;
49 if (Number.isInteger(n))
50 return (
51 Math.floor(Math.log2(Math.abs(n)) / Math.log2(10)) + (n > 0 ? 1 : 2)
52 );
53 return 2;
54 }
55
56 if (n.isZero) return 1;
57
58 if (n.im !== 0) {
59 // A pure-imaginary exact value carries its radical in `imRadical`, so it
60 // needs the same treatment as the real one below — otherwise `i√17` prices
61 // at 4 while `√17` prices at 7, and D6's fix is only half applied.
62 const imRad = (n as { imRadical?: number }).imRadical;
63 const imExtra =
64 imRad !== undefined && imRad > 1 ? 3 + numericCostFunction(imRad) : 0;
65 return numericCostFunction(n.re) + numericCostFunction(n.im) + imExtra + 1;
66 }

Callers 2

costFunctionFunction · 0.85
leafCountFunction · 0.85

Calls 3

absMethod · 0.65
isIntegerMethod · 0.45
floorMethod · 0.45

Tested by

no test coverage detected