|
| 1 | +import type { Expression, RuleStep } from '../global-types'; |
| 2 | +import { isFunction, isNumber } from '../boxed-expression/type-guards'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Extracts base + integer offset from an expression. |
| 6 | + * - Symbol `n` → { base: n, offset: 0 } |
| 7 | + * - Add(n, 3) → { base: n, offset: 3 } |
| 8 | + * - Add(n, -2) → { base: n, offset: -2 } |
| 9 | + * - Multiply(2, x) → { base: Multiply(2, x), offset: 0 } |
| 10 | + * - Number → null (pure numeric, no symbolic base) |
| 11 | + */ |
| 12 | +export function baseOffset( |
| 13 | + expr: Expression |
| 14 | +): { base: Expression; offset: number } | null { |
| 15 | + if (isNumber(expr)) return null; |
| 16 | + |
| 17 | + if (expr.operator === 'Add' && isFunction(expr) && expr.nops === 2) { |
| 18 | + const [op1, op2] = [expr.op1, expr.op2]; |
| 19 | + if (isNumber(op2) && Number.isInteger(op2.re) && !isNumber(op1)) |
| 20 | + return { base: op1, offset: op2.re }; |
| 21 | + if (isNumber(op1) && Number.isInteger(op1.re) && !isNumber(op2)) |
| 22 | + return { base: op2, offset: op1.re }; |
| 23 | + } |
| 24 | + |
| 25 | + return { base: expr, offset: 0 }; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Simplification rules for Binomial and Choose expressions. |
| 30 | + * |
| 31 | + * Patterns: |
| 32 | + * - C(n, 0) → 1 |
| 33 | + * - C(n, 1) → n |
| 34 | + * - C(n, n) → 1 |
| 35 | + * - C(n, n-1) → n |
| 36 | + */ |
| 37 | +export function simplifyBinomial(x: Expression): RuleStep | undefined { |
| 38 | + if (x.operator !== 'Binomial' && x.operator !== 'Choose') return undefined; |
| 39 | + if (!isFunction(x)) return undefined; |
| 40 | + |
| 41 | + const n = x.op1; |
| 42 | + const k = x.op2; |
| 43 | + if (!n || !k) return undefined; |
| 44 | + |
| 45 | + const ce = x.engine; |
| 46 | + |
| 47 | + // C(n, 0) → 1 |
| 48 | + if (k.is(0)) return { value: ce.One, because: 'C(n,0) -> 1' }; |
| 49 | + |
| 50 | + // C(n, 1) → n |
| 51 | + if (k.is(1)) return { value: n, because: 'C(n,1) -> n' }; |
| 52 | + |
| 53 | + // C(n, n) → 1 |
| 54 | + if (k.isSame(n)) return { value: ce.One, because: 'C(n,n) -> 1' }; |
| 55 | + |
| 56 | + // C(n, n-1) → n (structural check via baseOffset) |
| 57 | + const nBO = baseOffset(n); |
| 58 | + const kBO = baseOffset(k); |
| 59 | + if ( |
| 60 | + nBO && |
| 61 | + kBO && |
| 62 | + nBO.base.isSame(kBO.base) && |
| 63 | + nBO.offset - kBO.offset === 1 |
| 64 | + ) |
| 65 | + return { value: n, because: 'C(n,n-1) -> n' }; |
| 66 | + |
| 67 | + return undefined; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Extracts factorial information from a term in an Add expression. |
| 72 | + * Handles: Factorial(n), Negate(Factorial(n)), Multiply(c, Factorial(n)) |
| 73 | + */ |
| 74 | +function extractFactorialTerm( |
| 75 | + term: Expression |
| 76 | +): { coeff: number; factArg: Expression } | null { |
| 77 | + // Direct: Factorial(n) |
| 78 | + if (term.operator === 'Factorial' && isFunction(term)) |
| 79 | + return { coeff: 1, factArg: term.op1 }; |
| 80 | + |
| 81 | + // Negate(Factorial(n)) |
| 82 | + if ( |
| 83 | + term.operator === 'Negate' && |
| 84 | + isFunction(term) && |
| 85 | + term.op1.operator === 'Factorial' && |
| 86 | + isFunction(term.op1) |
| 87 | + ) |
| 88 | + return { coeff: -1, factArg: term.op1.op1 }; |
| 89 | + |
| 90 | + // Multiply with integer coefficient and Factorial |
| 91 | + if (term.operator === 'Multiply' && isFunction(term)) { |
| 92 | + const ops = term.ops; |
| 93 | + let factIdx = -1; |
| 94 | + for (let i = 0; i < ops.length; i++) { |
| 95 | + if (ops[i].operator === 'Factorial' && isFunction(ops[i])) { |
| 96 | + if (factIdx >= 0) return null; // Multiple factorials |
| 97 | + factIdx = i; |
| 98 | + } |
| 99 | + } |
| 100 | + if (factIdx < 0) return null; |
| 101 | + |
| 102 | + const factOp = ops[factIdx]; |
| 103 | + if (!isFunction(factOp)) return null; |
| 104 | + const factArg = factOp.op1; |
| 105 | + const others = ops.filter((_, i) => i !== factIdx); |
| 106 | + |
| 107 | + if ( |
| 108 | + others.length === 1 && |
| 109 | + isNumber(others[0]) && |
| 110 | + Number.isInteger(others[0].re) |
| 111 | + ) |
| 112 | + return { coeff: others[0].re, factArg }; |
| 113 | + |
| 114 | + return null; |
| 115 | + } |
| 116 | + |
| 117 | + return null; |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Simplification for Add expressions containing factorial terms. |
| 122 | + * Factors out the common (smallest) factorial for symbolic expressions. |
| 123 | + * |
| 124 | + * Examples: |
| 125 | + * - n! - (n-1)! → (n-1)! * (n - 1) |
| 126 | + * - (n+1)! - n! → n! * n |
| 127 | + * - (n+1)! + n! → n! * (n + 2) |
| 128 | + */ |
| 129 | +export function simplifyFactorialAdd(x: Expression): RuleStep | undefined { |
| 130 | + if (x.operator !== 'Add' || !isFunction(x)) return undefined; |
| 131 | + |
| 132 | + const ops = x.ops; |
| 133 | + if (ops.length < 2) return undefined; |
| 134 | + |
| 135 | + // Extract factorial info from each operand |
| 136 | + const factTerms: Array<{ |
| 137 | + coeff: number; |
| 138 | + factArg: Expression; |
| 139 | + index: number; |
| 140 | + }> = []; |
| 141 | + |
| 142 | + for (let i = 0; i < ops.length; i++) { |
| 143 | + const info = extractFactorialTerm(ops[i]); |
| 144 | + if (info) factTerms.push({ ...info, index: i }); |
| 145 | + } |
| 146 | + |
| 147 | + // Need at least 2 factorial terms |
| 148 | + if (factTerms.length < 2) return undefined; |
| 149 | + |
| 150 | + const ce = x.engine; |
| 151 | + |
| 152 | + // Get base+offset for each factorial argument |
| 153 | + const bos = factTerms.map((f) => ({ ...f, bo: baseOffset(f.factArg) })); |
| 154 | + const validBOs = bos.filter((b) => b.bo !== null); |
| 155 | + |
| 156 | + if (validBOs.length < 2) return undefined; |
| 157 | + |
| 158 | + // Check if all share the same symbolic base |
| 159 | + const refBase = validBOs[0].bo!.base; |
| 160 | + if (!validBOs.every((b) => b.bo!.base.isSame(refBase))) return undefined; |
| 161 | + |
| 162 | + // Find the minimum offset (smallest factorial) |
| 163 | + let minOffset = Infinity; |
| 164 | + for (const b of validBOs) { |
| 165 | + if (b.bo!.offset < minOffset) minOffset = b.bo!.offset; |
| 166 | + } |
| 167 | + |
| 168 | + // Find the argument expression for the minimum factorial |
| 169 | + const minFactArg = validBOs.find((v) => v.bo!.offset === minOffset)!.factArg; |
| 170 | + |
| 171 | + // Build the inner terms: for each factorial, compute the partial product |
| 172 | + const innerTerms: Expression[] = []; |
| 173 | + |
| 174 | + for (const b of validBOs) { |
| 175 | + const d = b.bo!.offset - minOffset; |
| 176 | + |
| 177 | + if (d === 0) { |
| 178 | + // This term IS the minimum factorial |
| 179 | + innerTerms.push(ce.number(b.coeff)); |
| 180 | + } else if (d > 0 && d <= 8) { |
| 181 | + // Build product (minArg+1)(minArg+2)...(minArg+d) |
| 182 | + let product: Expression = minFactArg.add(ce.One); |
| 183 | + for (let i = 2; i <= d; i++) { |
| 184 | + product = product.mul(minFactArg.add(ce.number(i))); |
| 185 | + } |
| 186 | + if (b.coeff === 1) { |
| 187 | + innerTerms.push(product); |
| 188 | + } else if (b.coeff === -1) { |
| 189 | + innerTerms.push(product.neg()); |
| 190 | + } else { |
| 191 | + innerTerms.push(ce.number(b.coeff).mul(product)); |
| 192 | + } |
| 193 | + } else { |
| 194 | + // Difference too large or negative (shouldn't happen since we found min) |
| 195 | + return undefined; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + // Sum the inner terms |
| 200 | + const innerSum = |
| 201 | + innerTerms.length === 1 |
| 202 | + ? innerTerms[0] |
| 203 | + : ce.function('Add', innerTerms); |
| 204 | + |
| 205 | + // Result: Factorial(minFactArg) * innerSum |
| 206 | + // Use _fn to avoid canonicalization that might re-distribute the product |
| 207 | + const factorialExpr = ce._fn('Factorial', [minFactArg]); |
| 208 | + const factored = ce._fn('Multiply', [factorialExpr, innerSum]); |
| 209 | + |
| 210 | + // Include any non-factorial terms from the original Add |
| 211 | + const factIndices = new Set(factTerms.map((f) => f.index)); |
| 212 | + const nonFactTerms = ops.filter((_, i) => !factIndices.has(i)); |
| 213 | + |
| 214 | + if (nonFactTerms.length > 0) |
| 215 | + return { |
| 216 | + value: ce._fn('Add', [factored, ...nonFactTerms]), |
| 217 | + because: 'factor common factorial', |
| 218 | + }; |
| 219 | + |
| 220 | + return { value: factored, because: 'factor common factorial' }; |
| 221 | +} |
0 commit comments