symplex 0.2.0

Exact symbolic mathematics for Rust: calculus, summation, solving, linear algebra, transforms, compile-time dimensional analysis, and Rust/C code generation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Validation tests for the 1^∞ / ∞^0 limit heuristic in `limit.rs`.
//!
//! Bug 3: The `b^e → exp(e·(b-1))` heuristic fires unconditionally for any
//! `Pow(base, exponent)` where the exponent depends on `var`, but the
//! approximation `ln(b) ≈ b - 1` is only valid when `b → 1`. When the base
//! does NOT tend to 1 (e.g. `x^(1/x)`, `2^(1/x)`), the heuristic produces
//! garbage.
//!
//! These tests exercise every edge case from the mathematical analysis to
//! establish the full scope of the bug and validate any proposed fix.

use symplex::prelude::*;

// ═══════════════════════════════════════════════════════════════════════════
// Group 1: Cases where base → 1 (heuristic SHOULD apply)
// ═══════════════════════════════════════════════════════════════════════════

/// Classic: lim(x→∞) (1 + 1/x)^x = e
#[test]
fn heuristic_valid_1_plus_1_over_x_to_the_x() {
    let ctx = Context::new();
    let x = ctx.symbol("x");
    // (1 + 1/x)^x
    let base = &x.powi(-1) + 1;
    let expr = base.pow(&x);
    let result = expr.limit(&x, &ctx.infinity());
    let val = result.eval_f64().expect("limit should evaluate to f64");
    let expected = std::f64::consts::E;
    assert!(
        (val - expected).abs() < 1e-6,
        "lim (1+1/x)^x should be e ≈ {expected}, got {val}"
    );
}

/// lim(x→∞) (1 - 1/x)^x = 1/e
#[test]
fn heuristic_valid_1_minus_1_over_x_to_the_x() {
    let ctx = Context::new();
    let x = ctx.symbol("x");
    // (1 - 1/x)^x
    let base = 1 - &x.powi(-1);
    let expr = base.pow(&x);
    let result = expr.limit(&x, &ctx.infinity());
    let val = result.eval_f64().expect("limit should evaluate to f64");
    let expected = 1.0 / std::f64::consts::E;
    assert!(
        (val - expected).abs() < 1e-6,
        "lim (1-1/x)^x should be 1/e ≈ {expected}, got {val}"
    );
}

/// lim(x→∞) (1 + 2/x)^(3x) = exp(6)
/// Generalised form with constants a=2, b=3 → exp(ab)=exp(6).
#[test]
fn heuristic_valid_1_plus_a_over_x_to_bx() {
    let ctx = Context::new();
    let x = ctx.symbol("x");
    // (1 + 2/x)^(3x)
    let base = 1 + &(2 / &x);
    let exponent = &x * 3;
    let expr = base.pow(&exponent);
    let result = expr.limit(&x, &ctx.infinity());
    let val = result.eval_f64().expect("limit should evaluate to f64");
    let expected = (6.0_f64).exp(); // e^6 ≈ 403.43
    assert!(
        (val - expected).abs() / expected < 1e-6,
        "lim (1+2/x)^(3x) should be exp(6) ≈ {expected}, got {val}"
    );
}

/// lim(x→∞) (1 + 1/x²)^x = exp(0) = 1
/// Base → 1, heuristic gives lim x·(1/x²) = lim 1/x = 0, so exp(0) = 1.
#[test]
fn heuristic_valid_1_plus_1_over_x2_to_the_x() {
    let ctx = Context::new();
    let x = ctx.symbol("x");
    // (1 + 1/x²)^x
    let base = 1 + &x.powi(-2);
    let expr = base.pow(&x);
    let result = expr.limit(&x, &ctx.infinity());
    let val = result.eval_f64().expect("limit should evaluate to f64");
    assert!(
        (val - 1.0).abs() < 1e-6,
        "lim (1+1/x²)^x should be 1, got {val}"
    );
}

/// lim(x→∞) (1 + 1/x)^(x²) = ∞
/// Base → 1, but the inner product x²·(1/x) = x → ∞, so exp(∞) = ∞.
/// The heuristic should detect the divergence and fall through to Gruntz.
#[test]
fn heuristic_divergent_1_plus_1_over_x_to_the_x2() {
    let ctx = Context::new();
    let x = ctx.symbol("x");
    // (1 + 1/x)^(x²)
    let base = 1 + &x.powi(-1);
    let exponent = x.powi(2);
    let expr = base.pow(&exponent);
    let result = expr.limit(&x, &ctx.infinity());
    let s = format!("{result}");
    assert!(
        s.contains("") || s.contains("oo"),
        "lim (1+1/x)^(x²) should be ∞, got {s}"
    );
}

/// lim(x→∞) (1 + 3/x)^x = e^3
#[test]
fn heuristic_valid_1_plus_3_over_x_to_the_x() {
    let ctx = Context::new();
    let x = ctx.symbol("x");
    let base = 1 + &(3 / &x);
    let expr = base.pow(&x);
    let result = expr.limit(&x, &ctx.infinity());
    let val = result.eval_f64().expect("limit should evaluate to f64");
    let expected = (3.0_f64).exp();
    assert!(
        (val - expected).abs() / expected < 1e-6,
        "lim (1+3/x)^x should be exp(3) ≈ {expected}, got {val}"
    );
}

// ═══════════════════════════════════════════════════════════════════════════
// Group 2: Cases where base → ∞ or base → constant ≠ 1 (heuristic MUST NOT
//          apply — these are the bug triggers)
// ═══════════════════════════════════════════════════════════════════════════

/// BUG CASE: lim(x→∞) x^(1/x) = 1
/// ∞^0 indeterminate form.  base = x → ∞, NOT 1.
/// The heuristic would compute exp((1/x)·(x - 1)) = exp((x-1)/x) → exp(1) = e.
/// That is WRONG; the correct answer is 1.
#[test]
fn bug_inf_pow_0_x_to_the_1_over_x() {
    let ctx = Context::new();
    let x = ctx.symbol("x");
    // x^(1/x)
    let expr = x.pow(&x.powi(-1));
    let result = expr.limit(&x, &ctx.infinity());
    let val = result.eval_f64().expect("limit should evaluate to f64");
    assert!(
        (val - 1.0).abs() < 1e-6,
        "lim x^(1/x) should be 1, got {val}"
    );
}

/// BUG CASE: lim(x→∞) 2^(1/x) = 1
/// Constant base = 2 ≠ 1, exponent → 0.
/// The heuristic would compute exp((1/x)·(2 - 1)) = exp(1/x) → exp(0) = 1.
/// In this case, the heuristic gives the right answer accidentally, but only
/// because (b-1) = 1 is a constant. We still want to verify correctness.
#[test]
fn const_base_2_to_the_1_over_x() {
    let ctx = Context::new();
    let x = ctx.symbol("x");
    // 2^(1/x)
    let expr = ctx.int(2).pow(&x.powi(-1));
    let result = expr.limit(&x, &ctx.infinity());
    let val = result.eval_f64().expect("limit should evaluate to f64");
    assert!(
        (val - 1.0).abs() < 1e-6,
        "lim 2^(1/x) should be 1, got {val}"
    );
}

/// BUG CASE: lim(x→∞) x^(1/x²) = 1
/// ∞^0 form. base = x → ∞.
/// Correct: exp(ln(x)/x²) → exp(0) = 1 (since ln(x)/x² → 0).
/// Heuristic gives: exp((1/x²)·(x - 1)) = exp((x-1)/x²) → exp(0) = 1.
/// Accidentally correct, but via wrong reasoning.
#[test]
fn inf_pow_0_x_to_the_1_over_x2() {
    let ctx = Context::new();
    let x = ctx.symbol("x");
    // x^(1/x²)
    let expr = x.pow(&x.powi(-2));
    let result = expr.limit(&x, &ctx.infinity());
    let val = result.eval_f64().expect("limit should evaluate to f64");
    assert!(
        (val - 1.0).abs() < 1e-6,
        "lim x^(1/x²) should be 1, got {val}"
    );
}

/// BUG CASE: lim(x→∞) (x²)^(1/x) = 1
/// ∞^0 form. base = x² → ∞.
/// Correct: exp(2·ln(x)/x) → exp(0) = 1.
/// Heuristic gives: exp((1/x)·(x² - 1)) = exp((x² - 1)/x) → exp(∞) = ∞. WRONG.
#[test]
fn bug_inf_pow_0_x2_to_the_1_over_x() {
    let ctx = Context::new();
    let x = ctx.symbol("x");
    // (x²)^(1/x)
    let expr = x.powi(2).pow(&x.powi(-1));
    let result = expr.limit(&x, &ctx.infinity());
    let val = result.eval_f64().expect("limit should evaluate to f64");
    assert!(
        (val - 1.0).abs() < 1e-6,
        "lim (x²)^(1/x) should be 1, got {val}"
    );
}

/// BUG CASE: lim(x→∞) (exp(x))^(1/x) = e^1 = e?  No!
/// Actually exp(x)^(1/x) = exp(x/x) = exp(1) = e ... wait, let's be careful.
/// exp(x)^(1/x) = exp(x · (1/x)) = exp(1) = e. That's correct.
/// But the heuristic computes exp((1/x)·(exp(x) - 1)). Since exp(x) → ∞,
/// exp(x)-1 → ∞, so (1/x)·(exp(x)-1) → ∞, and exp(∞) = ∞. WRONG.
#[test]
fn bug_exp_x_to_the_1_over_x() {
    let ctx = Context::new();
    let x = ctx.symbol("x");
    // exp(x)^(1/x) = e
    let expr = x.exp().pow(&x.powi(-1));
    let result = expr.limit(&x, &ctx.infinity());
    let val = result.eval_f64().expect("limit should evaluate to f64");
    let expected = std::f64::consts::E;
    assert!(
        (val - expected).abs() < 1e-6,
        "lim exp(x)^(1/x) should be e ≈ {expected}, got {val}"
    );
}

/// BUG CASE: lim(x→∞) (1+x)^(1/x) = 1
/// base = 1+x → ∞, NOT 1.
/// Correct: exp(ln(1+x)/x) → exp(0) = 1 (since ln(1+x)/x → 0).
/// Heuristic gives: exp((1/x)·((1+x) - 1)) = exp(x/x) = exp(1) = e. WRONG.
#[test]
fn bug_1_plus_x_to_the_1_over_x() {
    let ctx = Context::new();
    let x = ctx.symbol("x");
    // (1 + x)^(1/x)
    let base = &x + 1;
    let expr = base.pow(&x.powi(-1));
    let result = expr.limit(&x, &ctx.infinity());
    let val = result.eval_f64().expect("limit should evaluate to f64");
    assert!(
        (val - 1.0).abs() < 1e-6,
        "lim (1+x)^(1/x) should be 1, got {val}"
    );
}

// ═══════════════════════════════════════════════════════════════════════════
// Group 3: Cases where base → 0 (heuristic also invalid)
// ═══════════════════════════════════════════════════════════════════════════

/// lim(x→∞) (1/x)^(1/x) = 1
/// 0^0 form. base = 1/x → 0.
/// Correct: exp(ln(1/x)/x) = exp(-ln(x)/x) → exp(0) = 1.
/// Heuristic gives: exp((1/x)·(1/x - 1)) = exp((1/x² - 1/x)) → exp(0) = 1.
/// Accidentally correct since both terms → 0, but reasoning is wrong.
#[test]
fn zero_pow_zero_1_over_x_to_the_1_over_x() {
    let ctx = Context::new();
    let x = ctx.symbol("x");
    // (1/x)^(1/x)
    let expr = x.powi(-1).pow(&x.powi(-1));
    let result = expr.limit(&x, &ctx.infinity());
    let val = result.eval_f64().expect("limit should evaluate to f64");
    assert!(
        (val - 1.0).abs() < 1e-6,
        "lim (1/x)^(1/x) should be 1, got {val}"
    );
}

// ═══════════════════════════════════════════════════════════════════════════
// Group 4: Exponent does NOT depend on var (heuristic should not fire at all)
// ═══════════════════════════════════════════════════════════════════════════

/// lim(x→∞) x^2 — exponent is constant, heuristic guard `contains(exp, var)`
/// should prevent it from firing.
#[test]
fn constant_exponent_x_squared() {
    let ctx = Context::new();
    let x = ctx.symbol("x");
    let result = x.powi(2).limit(&x, &ctx.infinity());
    let s = format!("{result}");
    assert!(
        s.contains("") || s.contains("oo"),
        "lim x^2 at ∞ should be ∞, got {s}"
    );
}

// ═══════════════════════════════════════════════════════════════════════════
// Group 5: The exp(e·ln(b)) alternative — would Gruntz handle these
// if we rewrote b^e → exp(e·ln(b)) instead of using the heuristic?
// ═══════════════════════════════════════════════════════════════════════════

/// Verify that Gruntz can handle exp(x·ln(1+1/x)) as x→∞.
/// This is the exact rewrite of (1+1/x)^x = exp(x·ln(1+1/x)).
/// The limit should be e.
///
/// This tests whether the exp(e·ln(b)) alternative would work for the
/// classic 1^∞ case — i.e., whether Gruntz can handle it WITHOUT the heuristic.
#[test]
fn gruntz_handles_exp_x_ln_1_plus_1_over_x() {
    let ctx = Context::new();
    let x = ctx.symbol("x");
    // exp(x · ln(1 + 1/x))
    let inner = &x * (1 + &x.powi(-1)).ln();
    let expr = inner.exp();
    let result = expr.limit(&x, &ctx.infinity());
    let val = result.eval_f64().expect("limit should evaluate to f64");
    let expected = std::f64::consts::E;
    assert!(
        (val - expected).abs() < 1e-6,
        "lim exp(x·ln(1+1/x)) should be e ≈ {expected}, got {val}"
    );
}

/// Verify Gruntz handles exp(ln(x)/x) as x→∞ (the exact rewrite of x^(1/x)).
/// Should give exp(0) = 1.
#[test]
fn gruntz_handles_exp_ln_x_over_x() {
    let ctx = Context::new();
    let x = ctx.symbol("x");
    // exp(ln(x)/x)
    let inner = &x.ln() / &x;
    let expr = inner.exp();
    let result = expr.limit(&x, &ctx.infinity());
    let val = result.eval_f64().expect("limit should evaluate to f64");
    assert!(
        (val - 1.0).abs() < 1e-6,
        "lim exp(ln(x)/x) should be 1, got {val}"
    );
}

/// Verify Gruntz handles exp(2·ln(x)/x) as x→∞ (exact rewrite of (x²)^(1/x)).
/// Should give exp(0) = 1.
#[test]
fn gruntz_handles_exp_2_ln_x_over_x() {
    let ctx = Context::new();
    let x = ctx.symbol("x");
    // exp(2·ln(x)/x)
    let inner = &(&x.ln() * 2) / &x;
    let expr = inner.exp();
    let result = expr.limit(&x, &ctx.infinity());
    let val = result.eval_f64().expect("limit should evaluate to f64");
    assert!(
        (val - 1.0).abs() < 1e-6,
        "lim exp(2·ln(x)/x) should be 1, got {val}"
    );
}

/// Verify Gruntz handles exp(ln(1+1/x)·x²) as x→∞.
/// This is the exact rewrite of (1+1/x)^(x²).
/// ln(1+1/x) ≈ 1/x - 1/(2x²) + ..., so x²·ln(1+1/x) ≈ x - 1/2 + ... → ∞.
/// Result should be ∞.
#[test]
fn gruntz_handles_exp_x2_ln_1_plus_1_over_x() {
    let ctx = Context::new();
    let x = ctx.symbol("x");
    // exp(x² · ln(1 + 1/x))
    let inner = &x.powi(2) * (1 + &x.powi(-1)).ln();
    let expr = inner.exp();
    let result = expr.limit(&x, &ctx.infinity());
    let s = format!("{result}");
    assert!(
        s.contains("") || s.contains("oo"),
        "lim exp(x²·ln(1+1/x)) should be ∞, got {s}"
    );
}

// ═══════════════════════════════════════════════════════════════════════════
// Group 6: Negative-infinity limits (ensure the heuristic guard works there too)
// ═══════════════════════════════════════════════════════════════════════════

/// lim(x→-∞) (1 + 1/x)^x = e
/// Same identity, but approaching from -∞.
#[test]
fn heuristic_valid_1_plus_1_over_x_to_x_neg_inf() {
    let ctx = Context::new();
    let x = ctx.symbol("x");
    let base = 1 + &x.powi(-1);
    let expr = base.pow(&x);
    let neg_inf = -ctx.infinity();
    let result = expr.limit(&x, &neg_inf);
    let val = result.eval_f64().expect("limit should evaluate to f64");
    let expected = std::f64::consts::E;
    assert!(
        (val - expected).abs() < 1e-6,
        "lim(x→-∞) (1+1/x)^x should be e ≈ {expected}, got {val}"
    );
}

// ═══════════════════════════════════════════════════════════════════════════
// Group 7: Additional sanity checks on power limits
// ═══════════════════════════════════════════════════════════════════════════

/// lim(x→∞) (1 + 5/(3x))^x = exp(5/3)
#[test]
fn heuristic_valid_rational_coefficient() {
    let ctx = Context::new();
    let x = ctx.symbol("x");
    // (1 + 5/(3x))^x
    let base = 1 + &(&ctx.int(5) / &(&ctx.int(3) * &x));
    let expr = base.pow(&x);
    let result = expr.limit(&x, &ctx.infinity());
    let val = result.eval_f64().expect("limit should evaluate to f64");
    let expected = (5.0_f64 / 3.0).exp();
    assert!(
        (val - expected).abs() / expected < 1e-4,
        "lim (1+5/(3x))^x should be exp(5/3) ≈ {expected}, got {val}"
    );
}

/// lim(x→∞) (1 + 1/x)^(2x) = e²
#[test]
fn heuristic_valid_1_plus_1_over_x_to_2x() {
    let ctx = Context::new();
    let x = ctx.symbol("x");
    let base = 1 + &x.powi(-1);
    let exponent = &x * 2;
    let expr = base.pow(&exponent);
    let result = expr.limit(&x, &ctx.infinity());
    let val = result.eval_f64().expect("limit should evaluate to f64");
    let expected = std::f64::consts::E.powi(2);
    assert!(
        (val - expected).abs() / expected < 1e-6,
        "lim (1+1/x)^(2x) should be e² ≈ {expected}, got {val}"
    );
}