mathhook_core/core/
expression.rs

1//! Expression type and core functionality
2
3pub mod classification;
4pub mod constructors;
5pub mod conversion;
6pub mod data_types;
7pub mod display;
8pub mod eval_numeric;
9pub mod evaluation;
10pub mod matrix_methods;
11pub mod methods;
12pub mod operations;
13pub mod operators;
14pub mod smart_display;
15
16pub use classification::ExpressionClass;
17
18pub use crate::matrices::unified::Matrix;
19pub use data_types::*;
20
21use crate::core::{MathConstant, Number, Symbol};
22use serde::{Deserialize, Serialize};
23
24/// Memory-optimized Expression enum (target: 32 bytes)
25///
26/// Hot-path variants (frequently used) are kept inline for performance.
27/// Cold-path variants (less common) are boxed to maintain small enum size.
28#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
29pub enum Expression {
30    Number(Number),
31    Symbol(Symbol),
32    Add(Box<Vec<Expression>>),
33    Mul(Box<Vec<Expression>>),
34    Pow(Box<Expression>, Box<Expression>),
35    Function {
36        name: String,
37        args: Box<Vec<Expression>>,
38    },
39    Constant(MathConstant),
40    Set(Box<Vec<Expression>>),
41    Complex(Box<ComplexData>),
42    Matrix(Box<Matrix>),
43    Relation(Box<RelationData>),
44    Piecewise(Box<PiecewiseData>),
45    Interval(Box<IntervalData>),
46    Calculus(Box<CalculusData>),
47    MethodCall(Box<MethodCallData>),
48}
49
50#[cfg(test)]
51mod size_tests {
52    use super::*;
53
54    #[test]
55    fn test_expression_size_constraint() {
56        assert_eq!(
57            std::mem::size_of::<Expression>(),
58            32,
59            "Expression size constraint violated! Expected 32 bytes, got {} bytes. \
60             This breaks cache-line optimization.",
61            std::mem::size_of::<Expression>()
62        );
63    }
64}