* 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)
| 45 | |
| 46 | function 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 | } |
no test coverage detected