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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
# AST Reference

Detailed documentation for all `Expression` enum variants in mathlex.

For the type definitions and a concise variant summary see
[`src/ast/expression.rs`](../src/ast/expression.rs) and
[`src/ast/mod.rs`](../src/ast/mod.rs).

---

## Table of Contents

- [Scalar Values]#scalar-values
- [Operations]#operations
- [Calculus]#calculus
- [Integral Variants]#integral-variants
- [Linear Algebra]#linear-algebra
- [Sets and Logic]#sets-and-logic
- [Relations]#relations
- [Tensor and Differential Geometry]#tensor-and-differential-geometry
- [Function Theory]#function-theory
- [Differential Forms]#differential-forms

---

## Scalar Values

### `Integer(i64)`

Represents whole-number literals, both positive and negative.

**Examples:** `42`, `-17`, `0`

---

### `Float(MathFloat)`

Represents decimal number literals. `MathFloat` wraps `OrderedFloat<f64>` to
provide `Hash` and `Eq` implementations. NaN values compare equal to each
other (differs from IEEE 754, required for use in hash collections).

**Examples:** `3.14`, `-2.5`, `1.0e-10`

**Serialization note:** When using JSON (via serde), NaN and Infinity values
serialize to `null`. Use binary formats such as bincode for lossless
serialization of special floats.

---

### `Rational { numerator, denominator }`

Represents a ratio of two arbitrary expressions.

**Important:** Fields are `Expression`, not `i64`. This allows symbolic
rationals such as `x/y` or `(a+b)/(c+d)`, not just numeric fractions.

**Not produced by parsers.** Current parsers represent divisions as
`Binary { op: Div, ... }`. This variant is available for programmatic
construction, typically by symbolic manipulation libraries that want a
simplified rational form.

```rust
use mathlex::ast::Expression;

// Numeric rational: 1/2
let half = Expression::Rational {
    numerator: Box::new(Expression::Integer(1)),
    denominator: Box::new(Expression::Integer(2)),
};

// Symbolic rational: x/y
let symbolic = Expression::Rational {
    numerator: Box::new(Expression::Variable("x".to_string())),
    denominator: Box::new(Expression::Variable("y".to_string())),
};
```

---

### `Complex { real, imaginary }`

Represents a complex number in the form `a + bi`.

**Important:** Fields are `Expression`, not numeric types. This allows
symbolic complex numbers such as `(x+y) + (z+w)i`.

**Not produced by parsers.** Parsers represent complex expressions via
`Binary` operations with the imaginary constant `i`. This variant is for
programmatic construction.

```rust
use mathlex::ast::Expression;

// Numeric: 3 + 4i
let complex = Expression::Complex {
    real: Box::new(Expression::Integer(3)),
    imaginary: Box::new(Expression::Integer(4)),
};

// Pure imaginary: 0 + i
let pure_imaginary = Expression::Complex {
    real: Box::new(Expression::Integer(0)),
    imaginary: Box::new(Expression::Integer(1)),
};
```

---

### `Quaternion { real, i, j, k }`

Represents a quaternion in canonical form `a + bi + cj + dk` using the
standard basis {1, i, j, k}.

**Not produced by parsers.** Available for programmatic construction.

**Quaternion algebra rules:**
- `i² = j² = k² = ijk = -1`
- `ij = k`, `jk = i`, `ki = j`
- `ji = -k`, `kj = -i`, `ik = -j`

```rust
use mathlex::ast::Expression;

// 1 + 2i + 3j + 4k
let quat = Expression::Quaternion {
    real: Box::new(Expression::Integer(1)),
    i: Box::new(Expression::Integer(2)),
    j: Box::new(Expression::Integer(3)),
    k: Box::new(Expression::Integer(4)),
};
```

---

### `Variable(String)`

Represents a symbolic variable name.

**Examples:** `x`, `theta`, `x_1`

Libraries that need additional variable metadata (type, dimension, units)
should maintain a separate metadata map rather than extending this variant:

```rust,ignore
let metadata: HashMap<String, VariableMetadata> = HashMap::new();
for var in expr.find_variables() {
    if let Some(meta) = metadata.get(&var) {
        // use metadata for dimensional analysis, etc.
    }
}
```

---

### `Constant(MathConstant)`

Represents a well-known mathematical constant.

**Values:** π (`Pi`), e (`E`), i (`ImaginaryUnit`), ∞ (`Infinity`),
-∞ (`NegInfinity`)

**Note:** `MathConstant::NegInfinity` is produced by parsers when unary minus
is applied to infinity. Both `-∞` (plain text) and `-\infty` (LaTeX) parse
directly as `Constant(NegInfinity)`.

---

## Operations

### `Binary { op, left, right }`

Represents an infix operation with two operands.

**Operators (`BinaryOp`):** `Add`, `Sub`, `Mul`, `Div`, `Pow`, `Mod`

**Examples:** `x + y`, `2 * π`, `a^b`

---

### `Unary { op, operand }`

Represents a prefix or postfix operation with a single operand.

**Operators (`UnaryOp`):** `Neg` (negation), `Factorial`, `Transpose`

**Examples:** `-x`, `n!`, `A'`

---

### `Function { name, args }`

Represents a named function applied to zero or more argument expressions.

**Examples:** `sin(x)`, `max(a, b, c)`, `f()`

---

## Calculus

### `Derivative { expr, var, order }`

Represents the nth ordinary derivative of an expression with respect to a
single variable.

- `order` must be ≥ 1.
- `var` is the differentiation variable.
- `expr` is the expression being differentiated.

**Notation:**
- First derivative: `d/dx f(x)` or `f'(x)` or `df/dx`
- Second derivative: `d²/dx² f(x)` or `f''(x)`
- nth derivative: `dⁿ/dxⁿ f(x)`

```rust
use mathlex::ast::Expression;

// d/dx(x²)
let first_deriv = Expression::Derivative {
    expr: Box::new(Expression::Binary {
        op: mathlex::ast::BinaryOp::Pow,
        left: Box::new(Expression::Variable("x".to_string())),
        right: Box::new(Expression::Integer(2)),
    }),
    var: "x".to_string(),
    order: 1,
};

// d²/dx²(sin(x))
let second_deriv = Expression::Derivative {
    expr: Box::new(Expression::Function {
        name: "sin".to_string(),
        args: vec![Expression::Variable("x".to_string())],
    }),
    var: "x".to_string(),
    order: 2,
};
```

---

### `PartialDerivative { expr, var, order }`

Represents the nth partial derivative of a multivariable expression with
respect to one variable, holding others constant.

**Notation:**
- `∂f/∂x`, `∂/∂x f(x,y,z)`
- `∂²f/∂x²`, `∂ⁿf/∂xⁿ`

**When to use `PartialDerivative` vs `Derivative`:**
- Use `PartialDerivative` for functions of multiple variables, or when
  emphasising that other variables are held constant.
- Use `Derivative` for single-variable calculus.

```rust
use mathlex::ast::Expression;

// ∂/∂x(x²y)
let partial = Expression::PartialDerivative {
    expr: Box::new(Expression::Binary {
        op: mathlex::ast::BinaryOp::Mul,
        left: Box::new(Expression::Binary {
            op: mathlex::ast::BinaryOp::Pow,
            left: Box::new(Expression::Variable("x".to_string())),
            right: Box::new(Expression::Integer(2)),
        }),
        right: Box::new(Expression::Variable("y".to_string())),
    }),
    var: "x".to_string(),
    order: 1,
};
```

---

## Integral Variants

### `Integral { integrand, var, bounds }`

Represents both definite and indefinite integrals.

- `bounds = None` → indefinite integral `∫ f(x) dx`
- `bounds = Some(IntegralBounds)` → definite integral `∫ₐᵇ f(x) dx`

Bounds can be numeric, symbolic, infinite (`Constant(Infinity)`), or
complex expressions.

```rust
use mathlex::ast::{Expression, IntegralBounds};

// Indefinite integral: ∫ x dx
let indefinite = Expression::Integral {
    integrand: Box::new(Expression::Variable("x".to_string())),
    var: "x".to_string(),
    bounds: None,
};

// Definite integral: ∫₀¹ x² dx
let definite = Expression::Integral {
    integrand: Box::new(Expression::Binary {
        op: mathlex::ast::BinaryOp::Pow,
        left: Box::new(Expression::Variable("x".to_string())),
        right: Box::new(Expression::Integer(2)),
    }),
    var: "x".to_string(),
    bounds: Some(IntegralBounds {
        lower: Box::new(Expression::Integer(0)),
        upper: Box::new(Expression::Integer(1)),
    }),
};
```

---

### `MultipleIntegral { dimension, integrand, bounds, vars }`

Represents double, triple, or higher-dimensional integrals.

- `dimension = 2` → double integral (∬), typically over an area
- `dimension = 3` → triple integral (∭), typically over a volume
- `dimension > 3` → higher-dimensional integrals

```rust,ignore
// ∬_R f(x,y) dy dx
Expression::MultipleIntegral {
    dimension: 2,
    integrand: Box::new(f_expr),
    bounds: None,
    vars: vec!["y".to_string(), "x".to_string()],
}
```

---

### `ClosedIntegral { dimension, integrand, surface, var }`

Represents closed path integrals.

- `dimension = 1` → line integral over a closed curve (∮)
- `dimension = 2` → surface integral over a closed surface (∯)
- `dimension = 3` → volume integral over a closed volume (∰)

```rust,ignore
// ∮_C F · dr
Expression::ClosedIntegral {
    dimension: 1,
    integrand: Box::new(f_dot_dr),
    surface: Some("C".to_string()),
    var: "r".to_string(),
}
```

---

### `Limit { expr, var, to, direction }`

Represents the limit of an expression as a variable approaches a value.

**Direction variants:**
- `Direction::Both` — two-sided limit: `lim_{x→a} f(x)`
- `Direction::Left` — left-hand limit: `lim_{x→a⁻} f(x)` (approach from below)
- `Direction::Right` — right-hand limit: `lim_{x→a⁺} f(x)` (approach from above)

The `to` field can be finite, symbolic, `Constant(Infinity)`, or
`Constant(NegInfinity)`.

```rust
use mathlex::ast::{Expression, Direction, MathConstant};

// lim_{x→0} sin(x)/x
let limit_both = Expression::Limit {
    expr: Box::new(Expression::Binary {
        op: mathlex::ast::BinaryOp::Div,
        left: Box::new(Expression::Function {
            name: "sin".to_string(),
            args: vec![Expression::Variable("x".to_string())],
        }),
        right: Box::new(Expression::Variable("x".to_string())),
    }),
    var: "x".to_string(),
    to: Box::new(Expression::Integer(0)),
    direction: Direction::Both,
};

// lim_{x→∞} 1/x
let limit_infinity = Expression::Limit {
    expr: Box::new(Expression::Binary {
        op: mathlex::ast::BinaryOp::Div,
        left: Box::new(Expression::Integer(1)),
        right: Box::new(Expression::Variable("x".to_string())),
    }),
    var: "x".to_string(),
    to: Box::new(Expression::Constant(MathConstant::Infinity)),
    direction: Direction::Both,
};

// lim_{x→0⁺} 1/x
let limit_right = Expression::Limit {
    expr: Box::new(Expression::Binary {
        op: mathlex::ast::BinaryOp::Div,
        left: Box::new(Expression::Integer(1)),
        right: Box::new(Expression::Variable("x".to_string())),
    }),
    var: "x".to_string(),
    to: Box::new(Expression::Integer(0)),
    direction: Direction::Right,
};
```

---

### `Sum { index, lower, upper, body }`

Represents a summation using sigma notation: `Σ_{index=lower}^{upper} body`.

Evaluates to: `body[index=lower] + body[index=lower+1] + ... + body[index=upper]`

The `index` variable is bound within `body` and takes each integer value from
`lower` to `upper` inclusive.

Bounds can be numeric, symbolic, or infinite.

```rust
use mathlex::ast::Expression;

// Σ_{i=1}^{n} i
let sum = Expression::Sum {
    index: "i".to_string(),
    lower: Box::new(Expression::Integer(1)),
    upper: Box::new(Expression::Variable("n".to_string())),
    body: Box::new(Expression::Variable("i".to_string())),
};

// Σ_{k=1}^{10} k²
let sum_squares = Expression::Sum {
    index: "k".to_string(),
    lower: Box::new(Expression::Integer(1)),
    upper: Box::new(Expression::Integer(10)),
    body: Box::new(Expression::Binary {
        op: mathlex::ast::BinaryOp::Pow,
        left: Box::new(Expression::Variable("k".to_string())),
        right: Box::new(Expression::Integer(2)),
    }),
};
```

---

### `Product { index, lower, upper, body }`

Represents a product using pi notation: `Π_{index=lower}^{upper} body`.

Evaluates to: `body[index=lower] * body[index=lower+1] * ... * body[index=upper]`

```rust
use mathlex::ast::Expression;

// Π_{i=1}^{n} i  (factorial-like)
let factorial = Expression::Product {
    index: "i".to_string(),
    lower: Box::new(Expression::Integer(1)),
    upper: Box::new(Expression::Variable("n".to_string())),
    body: Box::new(Expression::Variable("i".to_string())),
};
```

---

## Linear Algebra

### `Vector(Vec<Expression>)`

An ordered collection of expressions as a mathematical vector.

- Elements can be any expression type.
- Dimension is determined by the number of elements.
- An empty `Vec` represents a zero-dimensional vector.

```rust
use mathlex::ast::Expression;

// [1, 2, 3]
let position = Expression::Vector(vec![
    Expression::Integer(1),
    Expression::Integer(2),
    Expression::Integer(3),
]);

// [x, y, z]
let symbolic = Expression::Vector(vec![
    Expression::Variable("x".to_string()),
    Expression::Variable("y".to_string()),
    Expression::Variable("z".to_string()),
]);
```

---

### `Matrix(Vec<Vec<Expression>>)`

A 2D array of expressions in rows and columns.

- Dimensions are M×N where M is rows and N is columns.
- Parsers always produce rectangular matrices.
- The AST does not enforce uniform row lengths. Use
  `Expression::is_valid_matrix()` or `Expression::matrix_dimensions()` to
  validate manually-constructed matrices.

Special cases:
- Empty: `[]` → 0×0
- Row vector: `[[1, 2, 3]]` → 1×3
- Column vector: `[[1], [2], [3]]` → 3×1
- Scalar: `[[x]]` → 1×1

```rust
use mathlex::ast::Expression;

// 2×2 identity matrix
let identity = Expression::Matrix(vec![
    vec![Expression::Integer(1), Expression::Integer(0)],
    vec![Expression::Integer(0), Expression::Integer(1)],
]);
```

---

### `MarkedVector { name, notation }`

A vector variable with an explicit visual notation style.

**Notations (`VectorNotation`):** `Bold` (`\mathbf{v}`), `Arrow` (`\vec{a}`),
`Hat` (`\hat{n}`), `Underline`, `Plain`

---

### `DotProduct { left, right }`

Dot product (inner product) of two vectors: `u · v` or `\mathbf{a} \cdot \mathbf{b}`.

---

### `CrossProduct { left, right }`

Cross product of two 3D vectors: `u × v` or `\mathbf{a} \times \mathbf{b}`.

---

### `OuterProduct { left, right }`

Outer product (tensor product): `u ⊗ v` or `\mathbf{a} \otimes \mathbf{b}`.

---

### `Gradient { expr }`

Gradient of a scalar field: `∇f`.

Points in the direction of greatest increase of the scalar field.

```rust
use mathlex::ast::Expression;

let grad = Expression::Gradient {
    expr: Box::new(Expression::Variable("f".to_string())),
};
```

---

### `Divergence { field }`

Divergence of a vector field: `∇·F`.

Scalar field measuring the "outflow" of a vector field at each point.

```rust
use mathlex::ast::Expression;

let div = Expression::Divergence {
    field: Box::new(Expression::Variable("F".to_string())),
};
```

---

### `Curl { field }`

Curl of a vector field: `∇×F`.

Vector field measuring the rotation of a vector field at each point.

```rust
use mathlex::ast::Expression;

let curl = Expression::Curl {
    field: Box::new(Expression::Variable("F".to_string())),
};
```

---

### `Laplacian { expr }`

Laplacian of a scalar field: `∇²f` or `Δf`.

Equals the divergence of the gradient: `∇·(∇f)`.

```rust
use mathlex::ast::Expression;

let laplacian = Expression::Laplacian {
    expr: Box::new(Expression::Variable("f".to_string())),
};
```

---

### `Nabla`

The raw nabla/del operator `∇` without an operand. Used when nabla appears
in non-standard combinations or without an immediately following operand.

---

### `Determinant { matrix }`

Determinant of a matrix: `det(A)` or `|A|`.

Returns the signed volume scaling factor of the linear transformation.

```rust
use mathlex::ast::Expression;

let det = Expression::Determinant {
    matrix: Box::new(Expression::Variable("A".to_string())),
};
```

---

### `Trace { matrix }`

Trace of a matrix (sum of diagonal elements): `tr(A)`.

```rust
use mathlex::ast::Expression;

let trace = Expression::Trace {
    matrix: Box::new(Expression::Variable("A".to_string())),
};
```

---

### `Rank { matrix }`

Rank of a matrix (dimension of column space / row space): `rank(A)`.

```rust
use mathlex::ast::Expression;

let rank = Expression::Rank {
    matrix: Box::new(Expression::Variable("A".to_string())),
};
```

---

### `ConjugateTranspose { matrix }`

Conjugate transpose (Hermitian adjoint): `A†`, `A*`, or `A^H`.

Transpose of the complex conjugate. For real matrices, equals the transpose.

```rust
use mathlex::ast::Expression;

let adjoint = Expression::ConjugateTranspose {
    matrix: Box::new(Expression::Variable("A".to_string())),
};
```

---

### `MatrixInverse { matrix }`

Matrix inverse: `A⁻¹`.

The matrix that when multiplied by A gives the identity. Only exists for
square matrices with non-zero determinant.

```rust
use mathlex::ast::Expression;

let inverse = Expression::MatrixInverse {
    matrix: Box::new(Expression::Variable("A".to_string())),
};
```

---

## Sets and Logic

### `NumberSetExpr(NumberSet)`

A standard number set: `ℕ` (Natural), `ℤ` (Integer), `ℚ` (Rational),
`ℝ` (Real), `ℂ` (Complex), `ℍ` (Quaternion).

```rust,ignore
Expression::NumberSetExpr(NumberSet::Real)
```

---

### `SetOperation { op, left, right }`

Binary set operation.

**Operators (`SetOp`):** `Union` (∪), `Intersection` (∩), `Difference` (∖),
`SymmetricDifference` (△), `CartesianProduct` (×)

```rust,ignore
// A ∪ B
Expression::SetOperation {
    op: SetOp::Union,
    left: Box::new(a),
    right: Box::new(b),
}
```

---

### `SetRelationExpr { relation, element, set }`

Set membership or subset relation.

**Relations (`SetRelation`):** `In` (∈), `NotIn` (∉), `Subset` (⊆),
`ProperSubset` (⊊), `Superset` (⊇), `ProperSuperset` (⊋)

```rust,ignore
// x ∈ ℝ
Expression::SetRelationExpr {
    relation: SetRelation::In,
    element: Box::new(Expression::Variable("x".to_string())),
    set: Box::new(Expression::NumberSetExpr(NumberSet::Real)),
}
```

---

### `SetBuilder { variable, domain, predicate }`

Set builder notation: `{x | P(x)}` or `{x ∈ S | P(x)}`.

```rust,ignore
// {x ∈ ℝ | x > 0}
Expression::SetBuilder {
    variable: "x".to_string(),
    domain: Some(Box::new(Expression::NumberSetExpr(NumberSet::Real))),
    predicate: Box::new(x_greater_than_zero),
}
```

---

### `EmptySet`

The empty set: `∅` or `{}`.

---

### `PowerSet { set }`

Power set of S (the set of all subsets): `𝒫(S)`.

```rust,ignore
// 𝒫(A)
Expression::PowerSet {
    set: Box::new(Expression::Variable("A".to_string())),
}
```

---

### `ForAll { variable, domain, body }`

Universal quantifier: `∀x ∈ S, P(x)`.

`domain` is optional; when absent represents `∀x, P(x)`.

---

### `Exists { variable, domain, body, unique }`

Existential quantifier: `∃x ∈ S, P(x)`.

Set `unique = true` for unique existence: `∃!x`.

---

### `Logical { op, operands }`

Logical expression combining operands with a logical operator.

**Operators (`LogicalOp`):** `And` (∧), `Or` (∨), `Not` (¬),
`Implies` (→), `Iff` (↔)

---

## Relations

### `Equation { left, right }`

An equality between two expressions: `x = 5`, `f(x) = x²`.

This type has no identifier field. Libraries that need to track equations
through processing pipelines should wrap it in their own struct:

```rust,ignore
struct TrackedEquation {
    id: String,
    equation: mathlex::Expression,
}
```

---

### `Inequality { op, left, right }`

An inequality comparison: `x < 5`, `y ≥ 0`, `a ≠ b`.

**Operators (`InequalityOp`):** `Lt` (<), `Le` (≤), `Gt` (>), `Ge` (≥),
`Ne` (≠)

---

### `Relation { op, left, right }`

A mathematical relation expressing similarity, equivalence, congruence, or
approximation.

**Operators (`RelationOp`):**
- `Similar` (`~`): `a \sim b`
- `Equivalent` (``): `a \equiv b`
- `Congruent` (``): `a \cong b`
- `Approx` (``): `a \approx b`

```rust
use mathlex::ast::{Expression, RelationOp};

// x ~ y
let similar = Expression::Relation {
    op: RelationOp::Similar,
    left: Box::new(Expression::Variable("x".to_string())),
    right: Box::new(Expression::Variable("y".to_string())),
};

// a ≈ b
let approx = Expression::Relation {
    op: RelationOp::Approx,
    left: Box::new(Expression::Variable("a".to_string())),
    right: Box::new(Expression::Variable("b".to_string())),
};
```

---

## Tensor and Differential Geometry

### `Tensor { name, indices }`

A tensor with upper and/or lower indices, supporting Einstein summation
convention.

**Notation examples:**
- `T^{ij}` — two upper indices
- `T_{ab}` — two lower indices
- `T^i_j` — mixed (one upper, one lower)
- `R^a_{bcd}` — Riemann-like tensor

**Einstein summation convention:** When the same index appears once upper and
once lower in a product, summation is implied: `A^i B_i = Σ_i A^i B_i`.

```rust
use mathlex::ast::{Expression, TensorIndex, IndexType};

// g^{μν}
let metric = Expression::Tensor {
    name: "g".to_string(),
    indices: vec![
        TensorIndex { name: "μ".to_string(), index_type: IndexType::Upper },
        TensorIndex { name: "ν".to_string(), index_type: IndexType::Upper },
    ],
};

// T^i_j
let mixed = Expression::Tensor {
    name: "T".to_string(),
    indices: vec![
        TensorIndex { name: "i".to_string(), index_type: IndexType::Upper },
        TensorIndex { name: "j".to_string(), index_type: IndexType::Lower },
    ],
};
```

---

### `KroneckerDelta { indices }`

The Kronecker delta: `δ^i_j` or `δ_{ij}`.

- `δ^i_j = 1` if `i = j`, `0` otherwise
- `δ^i_j A^j = A^i` (index substitution)
- `δ^i_i = n` (trace in n dimensions)

```rust
use mathlex::ast::{Expression, TensorIndex, IndexType};

// δ^i_j
let delta = Expression::KroneckerDelta {
    indices: vec![
        TensorIndex { name: "i".to_string(), index_type: IndexType::Upper },
        TensorIndex { name: "j".to_string(), index_type: IndexType::Lower },
    ],
};
```

---

### `LeviCivita { indices }`

The Levi-Civita totally antisymmetric symbol: `ε^{ijk}` or `ε_{ijk}`.

**Properties:**
- `ε^{123} = 1` in 3D (even permutation)
- Changes sign under any index swap (antisymmetric)
- `ε^{ijk} = 0` if any two indices are equal

**Common uses:**
- Cross product: `(a × b)^i = ε^{ijk} a_j b_k`
- Determinant: `det(A) = ε^{i₁...iₙ} A_{1i₁}...A_{niₙ}`
- Exterior algebra and differential forms

```rust
use mathlex::ast::{Expression, TensorIndex, IndexType};

// ε^{ijk}
let epsilon = Expression::LeviCivita {
    indices: vec![
        TensorIndex { name: "i".to_string(), index_type: IndexType::Upper },
        TensorIndex { name: "j".to_string(), index_type: IndexType::Upper },
        TensorIndex { name: "k".to_string(), index_type: IndexType::Upper },
    ],
};
```

---

## Function Theory

### `FunctionSignature { name, domain, codomain }`

Function signature/mapping declaration: `f: A → B`.

- LaTeX: `f: A \to B`
- Plain text: `f: A → B`

```rust
use mathlex::ast::{Expression, NumberSet};

// f: ℝ → ℝ
let real_func = Expression::FunctionSignature {
    name: "f".to_string(),
    domain: Box::new(Expression::NumberSetExpr(NumberSet::Real)),
    codomain: Box::new(Expression::NumberSetExpr(NumberSet::Real)),
};
```

---

### `Composition { outer, inner }`

Function composition: `f ∘ g`, where `(f ∘ g)(x) = f(g(x))`.

The `inner` function is applied first, then `outer`.

- LaTeX: `f \circ g`
- Unicode: `f ∘ g`

```rust
use mathlex::ast::Expression;

let composition = Expression::Composition {
    outer: Box::new(Expression::Variable("f".to_string())),
    inner: Box::new(Expression::Variable("g".to_string())),
};
```

---

## Differential Forms

### `Differential { var }`

Differential of a variable: `dx`, `dy`, `dt`.

Represents a differential 1-form, distinct from derivative notation `d/dx`.
Commonly appears as the integration variable in integrals: `∫ f(x) dx`.
In differential geometry, differentials are 1-forms.

```rust
use mathlex::ast::Expression;

let dx = Expression::Differential { var: "x".to_string() };
let dt = Expression::Differential { var: "t".to_string() };
```

---

### `WedgeProduct { left, right }`

Wedge product (exterior product) of two differential forms: `dx ∧ dy`.

**Properties:**
- Anticommutative: `dx ∧ dy = -(dy ∧ dx)`
- Associative: `(dx ∧ dy) ∧ dz = dx ∧ (dy ∧ dz)`
- Wedge with itself is zero: `dx ∧ dx = 0`

**Common uses:** area/volume elements in integration, exterior calculus,
differential geometry.

```rust
use mathlex::ast::Expression;

// dx ∧ dy
let dx = Expression::Differential { var: "x".to_string() };
let dy = Expression::Differential { var: "y".to_string() };
let wedge = Expression::WedgeProduct {
    left: Box::new(dx),
    right: Box::new(dy),
};

// dx ∧ dy ∧ dz (nested)
let dz = Expression::Differential { var: "z".to_string() };
let wedge_3d = Expression::WedgeProduct {
    left: Box::new(wedge),
    right: Box::new(dz),
};
```