(x: Expression)
| 6 | sym, |
| 7 | } from '../boxed-expression/type-guards.js'; |
| 8 | import { |
| 9 | hasInexactLiteral, |
| 10 | matchesRationalPattern, |
| 11 | } from '../boxed-expression/rational-body.js'; |
| 12 | import { asFloat } from '../boxed-expression/float-result.js'; |
| 13 | import { isTransitivelyPure } from '../boxed-expression/transitive-purity.js'; |
| 14 | import { emptyBigOpValue } from './big-op-identity.js'; |
| 15 | import { bigOpClosedFormStep } from './big-op-closed-form.js'; |
| 16 | |
| 17 | /** |
| 18 | * Whether a bound of a `Sum` or `Product` is proved not to be an integer, so |
| 19 | * that a closed form must not substitute it for the index. |
| 20 | * |
| 21 | * The enumeration of the operator rounds a numeric bound down |
| 22 | * (`reduceBigOp()` in `library/utils.ts`), and a closed form does not: with |
| 23 | * `y: real`, `Σ_{k=1}^{y} (1/k − 1/(k+1))` has the closed form |
| 24 | * `1 − 1/(y+1)`, which is about 0.714 at `y = 2.5`, while the sum at |
| 25 | * `y = 2.5` is 2/3. The value then depends on whether the closed form was |
| 26 | * made before or after the substitution. |
| 27 | * |
| 28 | * A bound is proved not to be an integer when it is not an integer |
| 29 | * (`isInteger` is false: `2.5`, `π`), when it holds a symbol declared with a |
| 30 | * type that is not an integer type (`real`, `number`, `complex`), or when |
| 31 | * every symbol in it is declared or constant and its type is not an integer |
| 32 | * type (`n/2` or `n + 1/2` with `n: integer` has the type `rational`, and |
| 33 | * `isInteger` is undefined). A call of a host function whose fixed result |
| 34 | * type is a real type that is not an integer type is handled as such a |
| 35 | * declared symbol (`isNonIntegerCallOfFixedType()`). An undeclared symbol, |
| 36 | * or a symbol whose type was only inferred, is a generic value and does not prevent the closed form, as |
| 37 | * with the other conventions for generic symbols (`x/x → 1`): `u − 1` with |
| 38 | * an undeclared `u` has the type `number`, and keeps the closed form. An |
| 39 | * infinite bound is not an integer, but it is the range of a series, and the |
| 40 | * series rules read it. |
| 41 | */ |
| 42 | export function boundIsNotInteger(bound: Expression): boolean { |
| 43 | if (bound.isInfinity === true) return false; |
| 44 | if (bound.isInteger === true) return false; |
| 45 | if (bound.isInteger === false) return true; |
| 46 | // A bound with a free symbol can have a constant value, and the |
| 47 | // enumeration reads that value (`bigopBoundValue()` in |
| 48 | // `library/utils.ts`): `D(x, x) + 0.5` has `x` free and the value 1.5, so |
| 49 | // `Π_{k=1}^{D(x, x) + 0.5} k` is 1, and its closed form |
| 50 | // `(D(x, x) + 0.5)!` is about 1.329. |
| 51 | const value = constantBoundValue(bound); |
| 52 | if (value !== undefined) |
| 53 | return Number.isFinite(value) && !Number.isInteger(value); |
| 54 | if (holdsDeclaredNonIntegerSymbol(bound)) return true; |
| 55 | return holdsOnlyDeclaredSymbols(bound) && !isIntegerOrInfinityType(bound); |
| 56 | } |
| 57 | |
| 58 | /** The real value of a pure function expression whose value has no free |
| 59 | * symbol, or `undefined`. The bound `D(x, x) + 0.5` has `x` free (the |
| 60 | * derivative is taken at `x`) and the value 1.5. A value with a free symbol |
| 61 | * (`2x` for `D(x^2, x)`) or with an imaginary part that is not zero has no |
| 62 | * real value to read. */ |
| 63 | function constantBoundValue(bound: Expression): number | undefined { |
| 64 | if (!isFunction(bound) || !bound.isPure) return undefined; |
| 65 | const value = bound.evaluate({ numericApproximation: true }); |
nothing calls this directly
no test coverage detected