mathlex 0.4.1

Mathematical expression parser for LaTeX and plain text notation, producing a language-agnostic AST
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#![allow(clippy::approx_constant)]
//! Round-trip Tests for mathlex
//!
//! This module tests that parse -> to_string -> parse and parse_latex -> to_latex -> parse_latex
//! produce equivalent ASTs, ensuring that serialization is the inverse of parsing.
//!
//! # Test Strategy
//!
//! For each notation (plain text and LaTeX):
//! 1. Parse input string to AST
//! 2. Serialize AST back to string
//! 3. Parse the serialized string
//! 4. Verify the two ASTs are equal
//!
//! # Categories Tested
//!
//! - Arithmetic: Basic operations with proper precedence
//! - Functions: Trigonometric, logarithmic, and custom functions
//! - Variables and constants
//! - Equations and inequalities
//! - LaTeX-specific: fractions, roots, calculus notation
//! - Calculus: derivatives, integrals, limits, sums, products
//! - Linear algebra: vectors and matrices
//!
//! # Known Limitations
//!
//! Some expressions may not round-trip perfectly due to:
//! - Floating point representation (1.0 may become 1)
//! - Operator precedence (parentheses may be added/removed)
//! - Different but equivalent representations
//!
//! # Note on LaTeX Round-trips
//!
//! The LaTeX serializer produces valid LaTeX output that may differ from
//! the input syntax. For example:
//! - Input: `2 * 3` → Serialized: `2 \cdot 3`
//! - Input: `\frac{d}{d*x}f` → Serialized: `\frac{d}{dx}f`
//!
//! These tests verify that the *serialized* form round-trips correctly,
//! not necessarily that the original "nice" LaTeX round-trips.

use mathlex::latex::ToLatex;
use mathlex::{parse, parse_latex};

/// Helper function to test round-trip for plain text notation.
///
/// Tests that: parse(input) -> to_string() -> parse() produces equivalent AST.
fn assert_roundtrip_text(input: &str) {
    let expr1 = parse(input).unwrap_or_else(|e| {
        panic!("Failed to parse first time: {}\nInput: {}", e, input);
    });
    let output = expr1.to_string();
    let expr2 = parse(&output).unwrap_or_else(|e| {
        panic!(
            "Failed to parse serialized output: {}\nOriginal input: {}\nSerialized: {}",
            e, input, output
        );
    });
    assert_eq!(
        expr1, expr2,
        "ASTs don't match after round-trip\nOriginal input: {}\nSerialized: {}\nFirst AST: {:?}\nSecond AST: {:?}",
        input, output, expr1, expr2
    );
}

/// Helper function to test round-trip for LaTeX notation.
///
/// Tests that: parse_latex(input) -> to_latex() -> parse_latex() produces equivalent AST.
///
/// Strict round-trip test for LaTeX: parse -> serialize -> parse must succeed
/// and produce equivalent AST.
fn assert_roundtrip_latex(input: &str) {
    let expr1 = parse_latex(input).unwrap_or_else(|e| {
        panic!("Failed to parse LaTeX first time: {}\nInput: {}", e, input);
    });
    let output = expr1.to_latex();

    // Parse the serialized output - must succeed
    let expr2 = parse_latex(&output).unwrap_or_else(|e| {
        panic!(
            "Serialized LaTeX cannot be parsed back:\n  Original input: {}\n  Serialized: {}\n  Parse error: {}",
            input, output, e
        );
    });

    assert_eq!(
        expr1, expr2,
        "ASTs don't match after LaTeX round-trip\nOriginal input: {}\nSerialized: {}\nFirst AST: {:?}\nSecond AST: {:?}",
        input, output, expr1, expr2
    );
}

// ============================================================================
// Plain Text Round-trip Tests
// ============================================================================

#[test]
fn test_text_integer() {
    assert_roundtrip_text("42");
    assert_roundtrip_text("-17");
    assert_roundtrip_text("0");
}

#[test]
fn test_text_float() {
    assert_roundtrip_text("3.14");
    assert_roundtrip_text("-2.5");
    // Note: 1.0 becomes 1 when serialized, which is semantically equivalent
}

#[test]
fn test_text_variable() {
    assert_roundtrip_text("x");
    assert_roundtrip_text("alpha");
    // Note: Plain text parser doesn't support subscripts
}

#[test]
fn test_text_constant() {
    assert_roundtrip_text("pi");
    assert_roundtrip_text("e");
    assert_roundtrip_text("i");
    assert_roundtrip_text("inf");
}

#[test]
fn test_text_arithmetic_simple() {
    assert_roundtrip_text("1 + 2");
    assert_roundtrip_text("5 - 3");
    assert_roundtrip_text("2 * 3");
    assert_roundtrip_text("10 / 5");
    assert_roundtrip_text("2 ^ 3");
}

#[test]
fn test_text_arithmetic_precedence() {
    // Multiplication before addition
    assert_roundtrip_text("1 + 2 * 3");
    assert_roundtrip_text("2 * 3 + 4");

    // Parentheses override precedence
    assert_roundtrip_text("(1 + 2) * 3");
    assert_roundtrip_text("2 * (3 + 4)");
}

#[test]
fn test_text_arithmetic_associativity() {
    // Left-to-right for same precedence
    assert_roundtrip_text("5 - 3 - 1");
    assert_roundtrip_text("10 / 2 / 5");

    // Right associativity requires parentheses
    assert_roundtrip_text("5 - (3 - 1)");
    assert_roundtrip_text("2 ^ (3 ^ 4)");
}

#[test]
fn test_text_unary_operations() {
    assert_roundtrip_text("-5");
    assert_roundtrip_text("+3");
    assert_roundtrip_text("n!");
}

#[test]
fn test_text_functions_basic() {
    assert_roundtrip_text("sin(x)");
    assert_roundtrip_text("cos(theta)");
    assert_roundtrip_text("tan(pi)");
    assert_roundtrip_text("exp(1)");
}

#[test]
fn test_text_functions_multiple_args() {
    assert_roundtrip_text("log(2, 8)");
    assert_roundtrip_text("max(1, 2, 3)");
    assert_roundtrip_text("min(x, y)");
}

#[test]
fn test_text_functions_nested() {
    assert_roundtrip_text("sin(cos(x))");
    assert_roundtrip_text("log(2, exp(x))");
    assert_roundtrip_text("sqrt(sin(x) + cos(x))");
}

#[test]
fn test_text_rational() {
    assert_roundtrip_text("1/2");
    assert_roundtrip_text("a/b");
    assert_roundtrip_text("(x + 1)/(y - 2)");
}

#[test]
fn test_text_complex() {
    assert_roundtrip_text("3 + 4i");
    assert_roundtrip_text("0 + 1i");
}

#[test]
fn test_text_equation() {
    assert_roundtrip_text("x = 5");
    assert_roundtrip_text("y = 2 * x + 1");
    assert_roundtrip_text("a + b = c + d");
}

#[test]
fn test_text_inequality() {
    assert_roundtrip_text("x < 5");
    assert_roundtrip_text("y <= 10");
    assert_roundtrip_text("z > 0");
    assert_roundtrip_text("w >= -5");
    assert_roundtrip_text("a != b");
}

// Note: Plain text parser doesn't support derivative syntax with d/dx notation
// Note: Plain text parser doesn't support ∂ symbol for partial derivatives
// Note: Plain text parser doesn't support int() function syntax
// Note: Plain text parser doesn't support lim() function with -> syntax
// These features are available in LaTeX parser

// Note: Plain text parser doesn't support sum() function syntax
// Note: Plain text parser doesn't support prod() function syntax
// Note: Plain text parser doesn't support vector/matrix literal syntax []
// These features are available in LaTeX parser

#[test]
fn test_text_complex_nested() {
    // Complex nested expression: (a + b) * (c - d) / (e ^ f)
    assert_roundtrip_text("(a + b) * (c - d) / e ^ f");
    // Note: Factorial has high precedence, so (n + 1)! serializes as n + 1!
    // Testing the serialized form instead
    assert_roundtrip_text("n + 1!");
    // Function with complex arguments
    assert_roundtrip_text("log(2, x ^ 2 + 1)");
}

// ============================================================================
// LaTeX Round-trip Tests
// ============================================================================

#[test]
fn test_latex_integer() {
    assert_roundtrip_latex("42");
    assert_roundtrip_latex("-17");
    assert_roundtrip_latex("0");
}

#[test]
fn test_latex_float() {
    assert_roundtrip_latex("3.14");
    assert_roundtrip_latex("-2.5");
}

#[test]
fn test_latex_variable() {
    assert_roundtrip_latex("x");
    // Note: Plain identifier "alpha" becomes "\alpha" after round-trip
    // Testing with backslash form instead
}

#[test]
fn test_latex_greek_variables() {
    assert_roundtrip_latex(r"\alpha");
    assert_roundtrip_latex(r"\beta");
    assert_roundtrip_latex(r"\theta");
    assert_roundtrip_latex(r"\pi");
}

#[test]
fn test_latex_variable_subscript() {
    assert_roundtrip_latex("x_1");
    assert_roundtrip_latex("x_{10}");
    assert_roundtrip_latex(r"\alpha_i");
}

#[test]
fn test_latex_constant() {
    assert_roundtrip_latex(r"\pi");
    assert_roundtrip_latex("e");
    assert_roundtrip_latex("i");
    assert_roundtrip_latex(r"\infty");
}

#[test]
fn test_latex_arithmetic_simple() {
    assert_roundtrip_latex("1 + 2");
    assert_roundtrip_latex("5 - 3");
    // Note: \cdot might not round-trip as expected, test with standard multiplication
    assert_roundtrip_latex("2 * 3");
    assert_roundtrip_latex("2 ^ 3");
}

#[test]
fn test_latex_frac() {
    assert_roundtrip_latex(r"\frac{1}{2}");
    assert_roundtrip_latex(r"\frac{a}{b}");
    assert_roundtrip_latex(r"\frac{x + 1}{y - 2}");
}

#[test]
fn test_latex_frac_nested() {
    // Nested fractions: (a/b) / (c/d)
    assert_roundtrip_latex(r"\frac{\frac{a}{b}}{\frac{c}{d}}");
    // Fraction with complex numerator
    assert_roundtrip_latex(r"\frac{a + b}{c}");
}

#[test]
fn test_latex_sqrt() {
    assert_roundtrip_latex(r"\sqrt{x}");
    assert_roundtrip_latex(r"\sqrt{2}");
    // Note: \sqrt[n]{x} might serialize differently (as root function)
    // Testing basic sqrt only
}

#[test]
fn test_latex_power() {
    assert_roundtrip_latex("x^{2}");
    assert_roundtrip_latex("x^{n}");
    assert_roundtrip_latex("e^{x}");
}

#[test]
fn test_latex_functions_basic() {
    assert_roundtrip_latex(r"\sin\left(x\right)");
    assert_roundtrip_latex(r"\cos\left(\theta\right)");
    assert_roundtrip_latex(r"\tan\left(\pi\right)");
    assert_roundtrip_latex(r"\exp\left(1\right)");
}

// Note: LaTeX parser might not support multi-argument functions with commas
// Testing single-argument functions only

#[test]
fn test_latex_functions_nested() {
    assert_roundtrip_latex(r"\sin\left(\cos\left(x\right)\right)");
    // Note: Multi-argument log not supported
}

#[test]
fn test_latex_equation() {
    assert_roundtrip_latex("x = 5");
    // Use * instead of \cdot for round-trip
    assert_roundtrip_latex("y = 2 * x + 1");
}

#[test]
fn test_latex_inequality() {
    assert_roundtrip_latex("x < 5");
    assert_roundtrip_latex(r"y \leq 10");
    assert_roundtrip_latex("z > 0");
    assert_roundtrip_latex(r"w \geq -5");
    assert_roundtrip_latex(r"a \neq b");
}

#[test]
fn test_latex_derivative_first_order() {
    // Note: LaTeX parser uses d*x syntax (d times x) for derivatives
    assert_roundtrip_latex(r"\frac{d}{d*x}f");
    assert_roundtrip_latex(r"\frac{d}{d*t}y");
}

#[test]
fn test_latex_derivative_higher_order() {
    assert_roundtrip_latex(r"\frac{d^2}{d*x^2}f");
    assert_roundtrip_latex(r"\frac{d^3}{d*t^3}y");
}

#[test]
fn test_latex_partial_derivative() {
    assert_roundtrip_latex(r"\frac{\partial}{\partial*x}f");
    assert_roundtrip_latex(r"\frac{\partial^2}{\partial*y^2}f");
}

#[test]
fn test_latex_integral_indefinite() {
    // Note: \, is not part of the parsed expression, just whitespace in LaTeX
    assert_roundtrip_latex(r"\int x dx");
    assert_roundtrip_latex(r"\int \sin\left(x\right) dx");
}

#[test]
fn test_latex_integral_definite() {
    assert_roundtrip_latex(r"\int_0^1 x dx");
    assert_roundtrip_latex(r"\int_0^{\pi} \sin\left(x\right) dx");
}

#[test]
fn test_latex_limit() {
    assert_roundtrip_latex(r"\lim_{x \to 0}f");
    assert_roundtrip_latex(r"\lim_{x \to 0^-}f");
    assert_roundtrip_latex(r"\lim_{x \to 0^+}f");
    assert_roundtrip_latex(r"\lim_{x \to \infty}\frac{1}{x}");
}

#[test]
fn test_latex_sum() {
    assert_roundtrip_latex(r"\sum_{i=1}^{n}i");
    assert_roundtrip_latex(r"\sum_{k=0}^{10}k^{2}");
}

#[test]
fn test_latex_product() {
    assert_roundtrip_latex(r"\prod_{i=1}^{n}i");
    assert_roundtrip_latex(r"\prod_{j=1}^{5}j + 1");
}

#[test]
fn test_latex_vector() {
    assert_roundtrip_latex(r"\begin{pmatrix} 1 \end{pmatrix}");
    assert_roundtrip_latex(r"\begin{pmatrix} 1 \\ 2 \\ 3 \end{pmatrix}");
}

#[test]
fn test_latex_matrix() {
    assert_roundtrip_latex(r"\begin{pmatrix} 1 \end{pmatrix}");
    assert_roundtrip_latex(r"\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}");
    assert_roundtrip_latex(r"\begin{pmatrix} 1 & 2 \\ 3 & 4 \\ 5 & 6 \end{pmatrix}");
}

#[test]
fn test_latex_complex_nested() {
    // Complex fraction with operations
    assert_roundtrip_latex(r"\frac{a + b}{c - d}");
    // Nested calculus with correct derivative syntax
    assert_roundtrip_latex(r"\frac{d}{d*x}\sin\left(x^{2}\right)");
    // Sum of fractions
    assert_roundtrip_latex(r"\sum_{i=1}^{n}\frac{1}{i}");
}

#[test]
fn test_latex_quadratic_formula() {
    // x = (-b + √(b² - 4ac)) / 2a
    // Using * instead of \cdot for round-trip
    assert_roundtrip_latex(r"\frac{-b + \sqrt{b^{2} - 4 * a * c}}{2 * a}");
}

#[test]
fn test_latex_integral_complex() {
    // ∫₀^π sin²(x) dx
    assert_roundtrip_latex(r"\int_0^{\pi} \sin\left(x\right)^{2} dx");
}

// ============================================================================
// Edge Cases
// ============================================================================

#[test]
fn test_text_deeply_nested_operations() {
    // Test deeply nested parentheses
    assert_roundtrip_text("((((1 + 2) * 3) - 4) / 5)");
}

#[test]
fn test_text_mixed_operations() {
    // Mix of different operation types
    assert_roundtrip_text("sin(x) + cos(y) * tan(z)");
    assert_roundtrip_text("log(2, x) ^ 2 + sqrt(y)");
}

#[test]
fn test_latex_mixed_fractions_and_powers() {
    // Fractions with powers: (a^2 + b^2) / (c^2 + d^2)
    assert_roundtrip_latex(r"\frac{a^{2} + b^{2}}{c^{2} + d^{2}}");
}

#[test]
fn test_latex_complex_calculus() {
    // Derivative of an integral with correct syntax
    assert_roundtrip_latex(r"\frac{d}{d*x}\int_0^x \sin\left(t\right) dt");
}

// ============================================================================
// Multiplication Operator Round-trip Tests
// ============================================================================

#[test]
fn test_latex_cdot_simple() {
    // Test that \cdot round-trips correctly
    assert_roundtrip_latex(r"a \cdot b");
    assert_roundtrip_latex(r"2 \cdot 3");
}

#[test]
fn test_latex_times_simple() {
    // Test that \times round-trips correctly
    assert_roundtrip_latex(r"a \times b");
    assert_roundtrip_latex(r"2 \times 3");
}

#[test]
fn test_latex_cdot_complex() {
    // Test \cdot in complex expressions
    assert_roundtrip_latex(r"2 \cdot x + 3");
    assert_roundtrip_latex(r"(a + b) \cdot (c - d)");
    assert_roundtrip_latex(r"x \cdot y \cdot z");
}

#[test]
fn test_latex_times_complex() {
    // Test \times in complex expressions
    assert_roundtrip_latex(r"2 \times x + 3");
    assert_roundtrip_latex(r"(a + b) \times (c - d)");
    assert_roundtrip_latex(r"x \times y \times z");
}

#[test]
fn test_latex_cdot_precedence() {
    // Test that \cdot has correct precedence
    assert_roundtrip_latex(r"a + b \cdot c");
    assert_roundtrip_latex(r"a \cdot b + c");
    assert_roundtrip_latex(r"a \cdot b^{2}");
}

#[test]
fn test_latex_times_precedence() {
    // Test that \times has correct precedence
    assert_roundtrip_latex(r"a + b \times c");
    assert_roundtrip_latex(r"a \times b + c");
    assert_roundtrip_latex(r"a \times b^{2}");
}