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};
23use std::sync::Arc;
24
25/// Memory-optimized Expression enum (target: 32 bytes)
26///
27/// Uses Arc for O(1) clone performance - cloning is just an atomic increment.
28/// Hot-path variants (frequently used) are kept inline for performance.
29/// Cold-path variants (less common) use Arc to maintain small enum size.
30#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
31pub enum Expression {
32    Number(Number),
33    Symbol(Symbol),
34    Add(Arc<Vec<Expression>>),
35    Mul(Arc<Vec<Expression>>),
36    Pow(Arc<Expression>, Arc<Expression>),
37    Function {
38        name: Arc<str>,
39        args: Arc<Vec<Expression>>,
40    },
41    Constant(MathConstant),
42    Set(Arc<Vec<Expression>>),
43    Complex(Arc<ComplexData>),
44    Matrix(Arc<Matrix>),
45    Relation(Arc<RelationData>),
46    Piecewise(Arc<PiecewiseData>),
47    Interval(Arc<IntervalData>),
48    Calculus(Arc<CalculusData>),
49    MethodCall(Arc<MethodCallData>),
50}
51
52#[cfg(test)]
53mod size_tests {
54    use super::*;
55
56    #[test]
57    fn test_expression_size_constraint() {
58        assert_eq!(
59            std::mem::size_of::<Expression>(),
60            32,
61            "Expression size constraint violated! Expected 32 bytes, got {} bytes. \
62             This breaks cache-line optimization.",
63            std::mem::size_of::<Expression>()
64        );
65    }
66}