|
| 1 | +# Implementation Plan: Assumptions Applied During Simplification |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +When users declare assumptions like `x > 0`, simplification rules should use |
| 6 | +this information. Currently, they don't. |
| 7 | + |
| 8 | +**Current behavior:** |
| 9 | + |
| 10 | +```typescript |
| 11 | +ce.assume(ce.parse('x > 0')); |
| 12 | +ce.parse('\\sqrt{x^2}').simplify().toLatex(); |
| 13 | +// Returns: "|x|" |
| 14 | +// Expected: "x" (since x > 0) |
| 15 | +``` |
| 16 | + |
| 17 | +## Root Cause Analysis |
| 18 | + |
| 19 | +### How Assumptions Are Stored |
| 20 | + |
| 21 | +When `ce.assume(ce.parse('x > 0'))` is called: |
| 22 | + |
| 23 | +1. `assume.ts:assumeInequality()` processes the inequality |
| 24 | +2. The proposition `['Greater', 'x', 0]` is stored in `ce.context.assumptions` |
| 25 | + (a Map) |
| 26 | +3. The symbol `x` is declared with type `'real'` if not already defined |
| 27 | + |
| 28 | +**Location:** `src/compute-engine/assume.ts:272-367` |
| 29 | + |
| 30 | +### How Sign Properties Are Queried |
| 31 | + |
| 32 | +When simplification checks `base.isNonNegative`: |
| 33 | + |
| 34 | +1. `BoxedSymbol.isNonNegative` calls `nonNegativeSign(this.sgn)` (line 591-592) |
| 35 | +2. `BoxedSymbol.sgn` returns `this.value?.sgn` (line 554-556) |
| 36 | +3. **Problem:** This only checks if the symbol has an assigned VALUE, not its |
| 37 | + ASSUMPTIONS |
| 38 | + |
| 39 | +**Location:** `src/compute-engine/boxed-expression/boxed-symbol.ts:554-593` |
| 40 | + |
| 41 | +### The Simplification Rule That Should Work |
| 42 | + |
| 43 | +The rule exists and correctly checks for non-negativity: |
| 44 | + |
| 45 | +```typescript |
| 46 | +// simplify-power.ts:114-116 |
| 47 | +if (exp.is(2) && base.isNonNegative === true) { |
| 48 | + return { value: base, because: 'sqrt(x^2) -> x when x >= 0' }; |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +**The rule is correct.** The problem is that `base.isNonNegative` returns |
| 53 | +`undefined` instead of `true` when there's an assumption but no assigned value. |
| 54 | + |
| 55 | +## Solution Design |
| 56 | + |
| 57 | +### Option A: Enhance BoxedSymbol.sgn (Recommended) |
| 58 | + |
| 59 | +Modify `BoxedSymbol.sgn` to query assumptions when there's no assigned value. |
| 60 | + |
| 61 | +**Pros:** |
| 62 | + |
| 63 | +- Single point of change |
| 64 | +- All sign-dependent properties automatically work |
| 65 | +- Consistent with how other properties work |
| 66 | + |
| 67 | +**Cons:** |
| 68 | + |
| 69 | +- May have performance implications (need to query assumptions) |
| 70 | +- Need to handle compound assumptions carefully |
| 71 | + |
| 72 | +### Option B: Add assumption-aware property getters |
| 73 | + |
| 74 | +Add separate getters like `isNonNegativeWithAssumptions`. |
| 75 | + |
| 76 | +**Pros:** |
| 77 | + |
| 78 | +- Explicit about when assumptions are used |
| 79 | +- No risk of breaking existing behavior |
| 80 | + |
| 81 | +**Cons:** |
| 82 | + |
| 83 | +- Requires updating all simplification rules |
| 84 | +- Code duplication |
| 85 | + |
| 86 | +### Option C: Cache assumption-derived signs in symbol definition |
| 87 | + |
| 88 | +When an assumption is made, update the symbol's definition with sign |
| 89 | +information. |
| 90 | + |
| 91 | +**Pros:** |
| 92 | + |
| 93 | +- Fast lookups (no runtime assumption queries) |
| 94 | +- Clean separation of concerns |
| 95 | + |
| 96 | +**Cons:** |
| 97 | + |
| 98 | +- Need to maintain consistency when assumptions change |
| 99 | +- Complex with compound assumptions |
| 100 | + |
| 101 | +## Recommended Implementation: Option A |
| 102 | + |
| 103 | +### Phase 1: Core Sign Resolution |
| 104 | + |
| 105 | +#### 1.1 Create assumption query helper |
| 106 | + |
| 107 | +**File:** `src/compute-engine/assume.ts` |
| 108 | + |
| 109 | +```typescript |
| 110 | +/** |
| 111 | + * Query assumptions to determine the sign of a symbol. |
| 112 | + * Returns undefined if no relevant assumptions found. |
| 113 | + */ |
| 114 | +export function getSignFromAssumptions( |
| 115 | + ce: IComputeEngine, |
| 116 | + symbol: string |
| 117 | +): Sign | undefined { |
| 118 | + const assumptions = ce.context.assumptions; |
| 119 | + if (!assumptions || assumptions.size === 0) return undefined; |
| 120 | + |
| 121 | + for (const [assumption, _] of assumptions) { |
| 122 | + // Check for direct inequalities involving the symbol |
| 123 | + // x > 0 → 'positive' |
| 124 | + // x >= 0 → 'non-negative' |
| 125 | + // x < 0 → 'negative' |
| 126 | + // x <= 0 → 'non-positive' |
| 127 | + |
| 128 | + const op = assumption.operator; |
| 129 | + if (!op) continue; |
| 130 | + |
| 131 | + // Handle: Greater(x, 0), Less(x, 0), etc. |
| 132 | + if (['Greater', 'GreaterEqual', 'Less', 'LessEqual'].includes(op)) { |
| 133 | + const [lhs, rhs] = assumption.ops ?? []; |
| 134 | + if (!lhs || !rhs) continue; |
| 135 | + |
| 136 | + // Check if this assumption is about our symbol compared to 0 |
| 137 | + if (lhs.symbol === symbol && rhs.is(0)) { |
| 138 | + if (op === 'Greater') return 'positive'; |
| 139 | + if (op === 'GreaterEqual') return 'non-negative'; |
| 140 | + if (op === 'Less') return 'negative'; |
| 141 | + if (op === 'LessEqual') return 'non-positive'; |
| 142 | + } |
| 143 | + |
| 144 | + // Handle reversed form: 0 < x, 0 > x, etc. |
| 145 | + if (rhs.symbol === symbol && lhs.is(0)) { |
| 146 | + if (op === 'Less') return 'positive'; // 0 < x means x > 0 |
| 147 | + if (op === 'LessEqual') return 'non-negative'; |
| 148 | + if (op === 'Greater') return 'negative'; // 0 > x means x < 0 |
| 149 | + if (op === 'GreaterEqual') return 'non-positive'; |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + return undefined; |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +#### 1.2 Update BoxedSymbol.sgn |
| 159 | + |
| 160 | +**File:** `src/compute-engine/boxed-expression/boxed-symbol.ts` |
| 161 | + |
| 162 | +```typescript |
| 163 | +get sgn(): Sign | undefined { |
| 164 | + // First check if there's an assigned value |
| 165 | + if (this.value) return this.value.sgn; |
| 166 | + |
| 167 | + // Then check assumptions |
| 168 | + return getSignFromAssumptions(this.engine, this.name); |
| 169 | +} |
| 170 | +``` |
| 171 | + |
| 172 | +### Phase 2: Handle Compound Expressions |
| 173 | + |
| 174 | +#### 2.1 Ensure BoxedFunction.sgn propagates correctly |
| 175 | + |
| 176 | +The `BoxedFunction.sgn` getter already calls operator-defined `sgn` functions. |
| 177 | +These should work automatically once the symbol signs are correct. |
| 178 | + |
| 179 | +**Verify:** `src/compute-engine/boxed-expression/boxed-function.ts:491-500` |
| 180 | + |
| 181 | +#### 2.2 Handle expressions like `x^2` where x > 0 |
| 182 | + |
| 183 | +The `Power` operator's `sgn` function should already handle this: |
| 184 | + |
| 185 | +- If base > 0, then base^n > 0 for any real n |
| 186 | + |
| 187 | +**Verify:** `src/compute-engine/library/arithmetic.ts` - Power sgn function |
| 188 | + |
| 189 | +### Phase 3: Extended Patterns |
| 190 | + |
| 191 | +#### 3.1 Absolute value simplification |
| 192 | + |
| 193 | +With assumptions working, `|x|` should simplify to `x` when `x >= 0`. |
| 194 | + |
| 195 | +**Verify:** `src/compute-engine/symbolic/simplify-abs.ts:51-54` |
| 196 | + |
| 197 | +```typescript |
| 198 | +// Already exists: |
| 199 | +if (x.isNonNegative === true) { |
| 200 | + return { value: x, because: '|x| -> x when x >= 0' }; |
| 201 | +} |
| 202 | +``` |
| 203 | + |
| 204 | +#### 3.2 Even roots |
| 205 | + |
| 206 | +`√[4]{x^4}` should simplify to `x` when `x >= 0`. |
| 207 | + |
| 208 | +**Verify:** `src/compute-engine/symbolic/simplify-power.ts:36-45` |
| 209 | + |
| 210 | +#### 3.3 Sign-dependent power rules |
| 211 | + |
| 212 | +`(-x)^n` rules should work with assumptions about x. |
| 213 | + |
| 214 | +### Phase 4: Testing |
| 215 | + |
| 216 | +#### 4.1 Enable existing skipped tests |
| 217 | + |
| 218 | +**File:** `test/compute-engine/assumptions.test.ts` |
| 219 | + |
| 220 | +Remove `.skip` from test blocks and verify they pass. |
| 221 | + |
| 222 | +#### 4.2 Add new assumption-based simplification tests |
| 223 | + |
| 224 | +```typescript |
| 225 | +describe('ASSUMPTION-BASED SIMPLIFICATION', () => { |
| 226 | + let ce: ComputeEngine; |
| 227 | + |
| 228 | + beforeEach(() => { |
| 229 | + ce = new ComputeEngine(); |
| 230 | + }); |
| 231 | + |
| 232 | + test('sqrt(x^2) with x > 0', () => { |
| 233 | + ce.assume(ce.parse('x > 0')); |
| 234 | + expect(ce.parse('\\sqrt{x^2}').simplify().latex).toBe('x'); |
| 235 | + }); |
| 236 | + |
| 237 | + test('sqrt(x^2) with x >= 0', () => { |
| 238 | + ce.assume(ce.parse('x \\ge 0')); |
| 239 | + expect(ce.parse('\\sqrt{x^2}').simplify().latex).toBe('x'); |
| 240 | + }); |
| 241 | + |
| 242 | + test('sqrt(x^2) with x < 0', () => { |
| 243 | + ce.assume(ce.parse('x < 0')); |
| 244 | + expect(ce.parse('\\sqrt{x^2}').simplify().latex).toBe('-x'); |
| 245 | + }); |
| 246 | + |
| 247 | + test('|x| with x > 0', () => { |
| 248 | + ce.assume(ce.parse('x > 0')); |
| 249 | + expect(ce.parse('|x|').simplify().latex).toBe('x'); |
| 250 | + }); |
| 251 | + |
| 252 | + test('|x| with x < 0', () => { |
| 253 | + ce.assume(ce.parse('x < 0')); |
| 254 | + expect(ce.parse('|x|').simplify().latex).toBe('-x'); |
| 255 | + }); |
| 256 | + |
| 257 | + test('fourth root of x^4 with x > 0', () => { |
| 258 | + ce.assume(ce.parse('x > 0')); |
| 259 | + expect(ce.parse('\\sqrt[4]{x^4}').simplify().latex).toBe('x'); |
| 260 | + }); |
| 261 | + |
| 262 | + test('assumptions do not leak between sessions', () => { |
| 263 | + ce.assume(ce.parse('x > 0')); |
| 264 | + const ce2 = new ComputeEngine(); |
| 265 | + // x should have unknown sign in ce2 |
| 266 | + expect(ce2.parse('\\sqrt{x^2}').simplify().latex).toBe('|x|'); |
| 267 | + }); |
| 268 | + |
| 269 | + test('compound expression sign propagation', () => { |
| 270 | + ce.assume(ce.parse('x > 0')); |
| 271 | + ce.assume(ce.parse('y > 0')); |
| 272 | + // x * y > 0, so sqrt((xy)^2) = xy |
| 273 | + expect(ce.parse('\\sqrt{(xy)^2}').simplify().latex).toBe('xy'); |
| 274 | + }); |
| 275 | +}); |
| 276 | +``` |
| 277 | + |
| 278 | +## Implementation Order |
| 279 | + |
| 280 | +1. **Phase 1.1:** Create `getSignFromAssumptions()` helper |
| 281 | +2. **Phase 1.2:** Update `BoxedSymbol.sgn` to use the helper |
| 282 | +3. **Phase 4.2:** Add basic tests to verify core functionality works |
| 283 | +4. **Phase 2:** Verify compound expressions work (may need no changes) |
| 284 | +5. **Phase 3:** Verify extended patterns work (may need no changes) |
| 285 | +6. **Phase 4.1:** Enable skipped tests, fix any that fail |
| 286 | + |
| 287 | +## Performance Considerations |
| 288 | + |
| 289 | +- Assumption queries happen during simplification, which can be called |
| 290 | + frequently |
| 291 | +- The `context.assumptions` Map is typically small (< 100 entries) |
| 292 | +- Consider caching sign information if performance becomes an issue |
| 293 | +- May want to add a fast path: if no assumptions exist, skip the query |
| 294 | + |
| 295 | +## Edge Cases to Handle |
| 296 | + |
| 297 | +1. **Multiple assumptions about same symbol:** |
| 298 | + - `x > 0` and `x < 10` → sign is 'positive' |
| 299 | + - Contradictory assumptions should be caught by `assume()` |
| 300 | + |
| 301 | +2. **Assumptions about expressions:** |
| 302 | + - `x + y > 0` doesn't directly tell us sign of x or y |
| 303 | + - For now, only handle direct symbol-to-constant comparisons |
| 304 | + |
| 305 | +3. **Symbolic bounds:** |
| 306 | + - `x > a` where a is another symbol |
| 307 | + - Cannot determine sign without knowing sign of a |
| 308 | + - Return undefined in these cases |
| 309 | + |
| 310 | +4. **Scope considerations:** |
| 311 | + - Assumptions are stored in `ce.context` |
| 312 | + - They should be properly scoped and not leak |
| 313 | + |
| 314 | +## Files to Modify |
| 315 | + |
| 316 | +| File | Changes | |
| 317 | +| ----------------------------------------------------- | ------------------------------ | |
| 318 | +| `src/compute-engine/assume.ts` | Add `getSignFromAssumptions()` | |
| 319 | +| `src/compute-engine/boxed-expression/boxed-symbol.ts` | Update `sgn` getter | |
| 320 | +| `test/compute-engine/assumptions.test.ts` | Enable tests, add new tests | |
| 321 | + |
| 322 | +## Success Criteria |
| 323 | + |
| 324 | +1. `ce.assume(ce.parse('x > 0')); ce.parse('\\sqrt{x^2}').simplify()` returns |
| 325 | + `x` |
| 326 | +2. `ce.assume(ce.parse('x >= 0')); ce.parse('|x|').simplify()` returns `x` |
| 327 | +3. `ce.assume(ce.parse('x < 0')); ce.parse('\\sqrt{x^2}').simplify()` returns |
| 328 | + `-x` |
| 329 | +4. All existing tests continue to pass |
| 330 | +5. Previously skipped assumption tests pass (or are documented as known |
| 331 | + limitations) |
0 commit comments