mathhook-core 0.2.0

Core mathematical engine for MathHook - expressions, algebra, and solving
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
//! Expression analysis methods
//!
//! This module provides methods for analyzing properties of expressions,
//! including commutativity analysis and variable occurrence counting.

use super::super::Expression;
use crate::core::commutativity::Commutativity;
use crate::core::Symbol;

impl Expression {
    /// Compute commutativity of this expression
    ///
    /// Commutativity is inferred from the symbols and operations:
    /// - Numbers, constants: Commutative
    /// - Symbols: Depends on symbol type (Scalar -> Commutative, Matrix/Operator/Quaternion -> Noncommutative)
    /// - Mul: Noncommutative if ANY factor is noncommutative
    /// - Add, Pow, Function: Depends on subexpressions
    ///
    /// # Examples
    ///
    /// Basic scalar symbols (commutative):
    /// ```
    /// use mathhook_core::core::symbol::Symbol;
    /// use mathhook_core::core::expression::Expression;
    /// use mathhook_core::core::commutativity::Commutativity;
    ///
    /// let x = Symbol::scalar("x");
    /// let y = Symbol::scalar("y");
    /// let expr = Expression::mul(vec![
    ///     Expression::symbol(x.clone()),
    ///     Expression::symbol(y.clone()),
    /// ]);
    /// assert_eq!(expr.commutativity(), Commutativity::Commutative);
    /// ```
    ///
    /// Matrix symbols (noncommutative):
    /// ```
    /// use mathhook_core::core::symbol::Symbol;
    /// use mathhook_core::core::expression::Expression;
    /// use mathhook_core::core::commutativity::Commutativity;
    ///
    /// let a = Symbol::matrix("A");
    /// let b = Symbol::matrix("B");
    /// let expr = Expression::mul(vec![
    ///     Expression::symbol(a.clone()),
    ///     Expression::symbol(b.clone()),
    /// ]);
    /// assert_eq!(expr.commutativity(), Commutativity::Noncommutative);
    /// ```
    pub fn commutativity(&self) -> Commutativity {
        match self {
            Expression::Symbol(s) => s.commutativity(),
            Expression::Number(_) => Commutativity::Commutative,
            Expression::Constant(_) => Commutativity::Commutative,

            Expression::Add(terms) => {
                Commutativity::combine(terms.iter().map(|t| t.commutativity()))
            }

            Expression::Mul(factors) => {
                Commutativity::combine(factors.iter().map(|f| f.commutativity()))
            }

            Expression::Pow(base, _exp) => base.commutativity(),

            Expression::Function { args, .. } => {
                Commutativity::combine(args.iter().map(|a| a.commutativity()))
            }

            Expression::Set(elements) => {
                Commutativity::combine(elements.iter().map(|e| e.commutativity()))
            }

            Expression::Complex(data) => {
                let real_comm = data.real.commutativity();
                let imag_comm = data.imag.commutativity();
                Commutativity::combine([real_comm, imag_comm])
            }

            Expression::Matrix(_) => Commutativity::Noncommutative,

            Expression::Relation(data) => {
                let left_comm = data.left.commutativity();
                let right_comm = data.right.commutativity();
                Commutativity::combine([left_comm, right_comm])
            }

            Expression::Piecewise(data) => {
                let piece_comms = data
                    .pieces
                    .iter()
                    .flat_map(|(expr, cond)| [expr.commutativity(), cond.commutativity()]);
                let default_comm = data.default.as_ref().map(|e| e.commutativity()).into_iter();
                Commutativity::combine(piece_comms.chain(default_comm))
            }

            Expression::Interval(data) => {
                let start_comm = data.start.commutativity();
                let end_comm = data.end.commutativity();
                Commutativity::combine([start_comm, end_comm])
            }

            Expression::Calculus(data) => match &**data {
                crate::core::expression::CalculusData::Derivative {
                    expression,
                    variable: _,
                    order: _,
                } => expression.commutativity(),
                crate::core::expression::CalculusData::Integral {
                    integrand,
                    variable: _,
                    bounds,
                } => {
                    let integrand_comm = integrand.commutativity();
                    if let Some((lower, upper)) = bounds {
                        Commutativity::combine([
                            integrand_comm,
                            lower.commutativity(),
                            upper.commutativity(),
                        ])
                    } else {
                        integrand_comm
                    }
                }
                crate::core::expression::CalculusData::Limit {
                    expression,
                    variable: _,
                    point,
                    direction: _,
                } => Commutativity::combine([expression.commutativity(), point.commutativity()]),
                crate::core::expression::CalculusData::Sum {
                    expression,
                    variable: _,
                    start,
                    end,
                } => Commutativity::combine([
                    expression.commutativity(),
                    start.commutativity(),
                    end.commutativity(),
                ]),
                crate::core::expression::CalculusData::Product {
                    expression,
                    variable: _,
                    start,
                    end,
                } => Commutativity::combine([
                    expression.commutativity(),
                    start.commutativity(),
                    end.commutativity(),
                ]),
            },

            Expression::MethodCall(data) => {
                let object_comm = data.object.commutativity();
                let args_comm = data.args.iter().map(|a| a.commutativity());
                Commutativity::combine([object_comm].into_iter().chain(args_comm))
            }
        }
    }

    /// Count occurrences of a variable in the expression
    ///
    /// Recursively counts how many times a specific variable symbol appears
    /// in the expression tree. This is useful for:
    /// - Determining if an expression is polynomial in a variable
    /// - Analyzing variable dependencies
    /// - Checking if a variable appears in an equation
    ///
    /// # Arguments
    ///
    /// * `variable` - The symbol to count occurrences of
    ///
    /// # Returns
    ///
    /// The number of times the variable appears in the expression
    ///
    /// # Examples
    ///
    /// Basic counting in simple expressions:
    /// ```
    /// use mathhook_core::{Expression, symbol};
    ///
    /// let x = symbol!(x);
    /// let expr = Expression::mul(vec![
    ///     Expression::integer(2),
    ///     Expression::symbol(x.clone()),
    /// ]);
    /// assert_eq!(expr.count_variable_occurrences(&x), 1);
    /// ```
    ///
    /// Counting multiple occurrences:
    /// ```
    /// use mathhook_core::{Expression, symbol};
    /// use std::sync::Arc;
    ///
    /// let x = symbol!(x);
    /// // x^2 + 2*x + 1 has 2 occurrences of x (in x^2 and in 2*x)
    /// let expr = Expression::Add(Arc::new(vec![
    ///     Expression::pow(Expression::symbol(x.clone()), Expression::integer(2)),
    ///     Expression::mul(vec![Expression::integer(2), Expression::symbol(x.clone())]),
    ///     Expression::integer(1),
    /// ]));
    /// assert_eq!(expr.count_variable_occurrences(&x), 2);
    /// ```
    ///
    /// Counting in power expressions:
    /// ```
    /// use mathhook_core::{Expression, symbol};
    ///
    /// let x = symbol!(x);
    /// // x^x has 2 occurrences (base and exponent)
    /// let expr = Expression::pow(
    ///     Expression::symbol(x.clone()),
    ///     Expression::symbol(x.clone())
    /// );
    /// assert_eq!(expr.count_variable_occurrences(&x), 2);
    /// ```
    ///
    /// Counting in functions:
    /// ```
    /// use mathhook_core::{Expression, symbol};
    ///
    /// let x = symbol!(x);
    /// // sin(x)
    /// let expr = Expression::function("sin", vec![Expression::symbol(x.clone())]);
    /// assert_eq!(expr.count_variable_occurrences(&x), 1);
    ///
    /// // f(x, x, 2) has 2 occurrences
    /// let expr2 = Expression::function("f", vec![
    ///     Expression::symbol(x.clone()),
    ///     Expression::symbol(x.clone()),
    ///     Expression::integer(2),
    /// ]);
    /// assert_eq!(expr2.count_variable_occurrences(&x), 2);
    /// ```
    ///
    /// Zero occurrences when variable is not present:
    /// ```
    /// use mathhook_core::{Expression, symbol};
    ///
    /// let x = symbol!(x);
    /// let y = symbol!(y);
    /// let expr = Expression::symbol(y.clone());
    /// assert_eq!(expr.count_variable_occurrences(&x), 0);
    /// ```
    pub fn count_variable_occurrences(&self, variable: &Symbol) -> usize {
        match self {
            Expression::Symbol(s) if s == variable => 1,
            Expression::Symbol(_) | Expression::Number(_) | Expression::Constant(_) => 0,

            Expression::Add(terms) | Expression::Mul(terms) | Expression::Set(terms) => terms
                .iter()
                .map(|t| t.count_variable_occurrences(variable))
                .sum(),

            Expression::Pow(base, exp) => {
                base.count_variable_occurrences(variable) + exp.count_variable_occurrences(variable)
            }

            Expression::Function { args, .. } => args
                .iter()
                .map(|a| a.count_variable_occurrences(variable))
                .sum(),

            Expression::Complex(data) => {
                data.real.count_variable_occurrences(variable)
                    + data.imag.count_variable_occurrences(variable)
            }

            Expression::Matrix(matrix) => {
                let (rows, cols) = matrix.dimensions();
                let mut count = 0;
                for i in 0..rows {
                    for j in 0..cols {
                        count += matrix
                            .get_element(i, j)
                            .count_variable_occurrences(variable);
                    }
                }
                count
            }

            Expression::Relation(data) => {
                data.left.count_variable_occurrences(variable)
                    + data.right.count_variable_occurrences(variable)
            }

            Expression::Piecewise(data) => {
                let pieces_count: usize = data
                    .pieces
                    .iter()
                    .map(|(expr, cond)| {
                        expr.count_variable_occurrences(variable)
                            + cond.count_variable_occurrences(variable)
                    })
                    .sum();
                let default_count = data
                    .default
                    .as_ref()
                    .map_or(0, |e| e.count_variable_occurrences(variable));
                pieces_count + default_count
            }

            Expression::Interval(data) => {
                data.start.count_variable_occurrences(variable)
                    + data.end.count_variable_occurrences(variable)
            }

            Expression::Calculus(data) => match data.as_ref() {
                crate::core::expression::data_types::CalculusData::Derivative {
                    expression,
                    variable: v,
                    ..
                } => {
                    expression.count_variable_occurrences(variable)
                        + if v == variable { 1 } else { 0 }
                }
                crate::core::expression::data_types::CalculusData::Integral {
                    integrand,
                    variable: v,
                    bounds,
                } => {
                    let integrand_count = integrand.count_variable_occurrences(variable);
                    let var_count = if v == variable { 1 } else { 0 };
                    let bounds_count = bounds.as_ref().map_or(0, |(lower, upper)| {
                        lower.count_variable_occurrences(variable)
                            + upper.count_variable_occurrences(variable)
                    });
                    integrand_count + var_count + bounds_count
                }
                crate::core::expression::data_types::CalculusData::Limit {
                    expression,
                    variable: v,
                    point,
                    ..
                } => {
                    expression.count_variable_occurrences(variable)
                        + if v == variable { 1 } else { 0 }
                        + point.count_variable_occurrences(variable)
                }
                crate::core::expression::data_types::CalculusData::Sum {
                    expression,
                    variable: v,
                    start,
                    end,
                }
                | crate::core::expression::data_types::CalculusData::Product {
                    expression,
                    variable: v,
                    start,
                    end,
                } => {
                    expression.count_variable_occurrences(variable)
                        + if v == variable { 1 } else { 0 }
                        + start.count_variable_occurrences(variable)
                        + end.count_variable_occurrences(variable)
                }
            },

            Expression::MethodCall(data) => {
                data.object.count_variable_occurrences(variable)
                    + data
                        .args
                        .iter()
                        .map(|a| a.count_variable_occurrences(variable))
                        .sum::<usize>()
            }
        }
    }

    pub fn contains_variable(&self, symbol: &Symbol) -> bool {
        self.count_variable_occurrences(symbol) > 0
    }

    /// Check if expression is just the variable itself
    pub fn is_simple_variable(&self, var: &Symbol) -> bool {
        matches!(self, Expression::Symbol(s) if s == var)
    }

    /// Check if this expression is a specific symbol
    ///
    /// Convenience method for pattern matching against a specific symbol.
    /// More readable than inline matches! pattern in complex conditions.
    ///
    /// # Arguments
    ///
    /// * `symbol` - The symbol to check against
    ///
    /// # Returns
    ///
    /// True if this expression is exactly the given symbol
    ///
    /// # Examples
    ///
    /// ```
    /// use mathhook_core::{Expression, symbol};
    ///
    /// let x = symbol!(x);
    /// let y = symbol!(y);
    /// let expr = Expression::symbol(x.clone());
    ///
    /// assert!(expr.is_symbol_matching(&x));
    /// assert!(!expr.is_symbol_matching(&y));
    /// ```
    pub fn is_symbol_matching(&self, symbol: &Symbol) -> bool {
        matches!(self, Expression::Symbol(s) if s == symbol)
    }

    /// Extract the base and exponent from a Pow expression
    ///
    /// Returns Some((base, exp)) if this is a Pow expression, None otherwise.
    /// This is a helper method for pattern matching with the Arc-based structure.
    #[inline]
    pub fn as_pow(&self) -> Option<(&Expression, &Expression)> {
        match self {
            Expression::Pow(base, exp) => Some((base.as_ref(), exp.as_ref())),
            _ => None,
        }
    }

    /// Extract the name and args from a Function expression
    ///
    /// Returns Some((name, args)) if this is a Function expression, None otherwise.
    /// This is a helper method for pattern matching with the Arc-based structure.
    #[inline]
    pub fn as_function(&self) -> Option<(&str, &[Expression])> {
        match self {
            Expression::Function { name, args } => Some((name.as_ref(), args.as_slice())),
            _ => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::expression::data_types::{
        CalculusData, ComplexData, PiecewiseData, RelationData, RelationType,
    };
    use crate::expr;
    use crate::matrices::unified::Matrix;
    use crate::symbol;
    use std::sync::Arc;

    #[test]
    fn test_commutativity_scalar_multiplication() {
        let x = Symbol::scalar("x");
        let y = Symbol::scalar("y");
        let expr = Expression::mul(vec![
            Expression::symbol(x.clone()),
            Expression::symbol(y.clone()),
        ]);
        assert_eq!(expr.commutativity(), Commutativity::Commutative);
    }

    #[test]
    fn test_commutativity_matrix_multiplication() {
        let a = Symbol::matrix("A");
        let b = Symbol::matrix("B");
        let expr = Expression::mul(vec![
            Expression::symbol(a.clone()),
            Expression::symbol(b.clone()),
        ]);
        assert_eq!(expr.commutativity(), Commutativity::Noncommutative);
    }

    #[test]
    fn test_count_in_symbol() {
        let x = symbol!(x);
        let expr = Expression::symbol(x.clone());
        assert_eq!(expr.count_variable_occurrences(&x), 1);

        let y = symbol!(y);
        assert_eq!(expr.count_variable_occurrences(&y), 0);
    }

    #[test]
    fn test_count_in_add() {
        let x = symbol!(x);
        let y = symbol!(y);
        let raw_expr = Expression::Add(Arc::new(vec![
            Expression::symbol(x.clone()),
            Expression::symbol(x.clone()),
            Expression::symbol(y.clone()),
        ]));
        assert_eq!(raw_expr.count_variable_occurrences(&x), 2);
        assert_eq!(raw_expr.count_variable_occurrences(&y), 1);
    }

    #[test]
    fn test_count_in_pow() {
        let x = symbol!(x);
        let expr = Expression::pow(Expression::symbol(x.clone()), expr!(2));
        assert_eq!(expr.count_variable_occurrences(&x), 1);

        let expr2 = Expression::pow(Expression::symbol(x.clone()), Expression::symbol(x.clone()));
        assert_eq!(expr2.count_variable_occurrences(&x), 2);
    }

    #[test]
    fn test_count_in_function() {
        let x = symbol!(x);
        let expr = Expression::function("sin", vec![Expression::symbol(x.clone())]);
        assert_eq!(expr.count_variable_occurrences(&x), 1);

        let expr2 = Expression::function(
            "f",
            vec![
                Expression::symbol(x.clone()),
                Expression::symbol(x.clone()),
                expr!(2),
            ],
        );
        assert_eq!(expr2.count_variable_occurrences(&x), 2);
    }

    #[test]
    fn test_count_in_matrix() {
        let x = symbol!(x);
        let y = symbol!(y);
        let matrix = Matrix::dense(vec![
            vec![Expression::symbol(x.clone()), Expression::symbol(y.clone())],
            vec![Expression::symbol(x.clone()), Expression::integer(1)],
        ]);
        let expr = Expression::Matrix(Arc::new(matrix));
        assert_eq!(expr.count_variable_occurrences(&x), 2);
        assert_eq!(expr.count_variable_occurrences(&y), 1);
    }

    #[test]
    fn test_count_in_complex() {
        let x = symbol!(x);
        let expr = Expression::Complex(Arc::new(ComplexData {
            real: Expression::symbol(x.clone()),
            imag: Expression::mul(vec![Expression::integer(2), Expression::symbol(x.clone())]),
        }));
        assert_eq!(expr.count_variable_occurrences(&x), 2);
    }

    #[test]
    fn test_count_in_relation() {
        let x = symbol!(x);
        let expr = Expression::Relation(Arc::new(RelationData {
            left: Expression::symbol(x.clone()),
            right: Expression::mul(vec![Expression::integer(2), Expression::symbol(x.clone())]),
            relation_type: RelationType::Equal,
        }));
        assert_eq!(expr.count_variable_occurrences(&x), 2);
    }

    #[test]
    fn test_count_in_piecewise() {
        let x = symbol!(x);
        let expr = Expression::Piecewise(Arc::new(PiecewiseData {
            pieces: vec![
                (Expression::symbol(x.clone()), Expression::symbol(x.clone())),
                (Expression::integer(0), Expression::symbol(x.clone())),
            ],
            default: Some(Expression::symbol(x.clone())),
        }));
        assert_eq!(expr.count_variable_occurrences(&x), 4);
    }

    #[test]
    fn test_count_in_integral() {
        let x = symbol!(x);
        let expr = Expression::Calculus(Arc::new(CalculusData::Integral {
            integrand: Expression::pow(Expression::symbol(x.clone()), Expression::integer(2)),
            variable: x.clone(),
            bounds: Some((Expression::integer(0), Expression::symbol(x.clone()))),
        }));
        assert_eq!(expr.count_variable_occurrences(&x), 3);
    }

    #[test]
    fn test_is_symbol_matching() {
        let x = symbol!(x);
        let y = symbol!(y);
        let expr_x = Expression::symbol(x.clone());
        let expr_num = Expression::integer(42);

        assert!(expr_x.is_symbol_matching(&x));
        assert!(!expr_x.is_symbol_matching(&y));
        assert!(!expr_num.is_symbol_matching(&x));
    }

    #[test]
    fn test_as_pow() {
        let x = symbol!(x);
        let pow_expr = Expression::pow(Expression::symbol(x.clone()), Expression::integer(2));

        let (base, exp) = pow_expr.as_pow().expect("should be a Pow");
        assert_eq!(*base, Expression::symbol(x.clone()));
        assert_eq!(*exp, Expression::integer(2));

        let not_pow = Expression::integer(42);
        assert!(not_pow.as_pow().is_none());
    }

    #[test]
    fn test_as_function() {
        let x = symbol!(x);
        let func = Expression::function("sin", vec![Expression::symbol(x.clone())]);

        let (name, args) = func.as_function().expect("should be a Function");
        assert_eq!(name, "sin");
        assert_eq!(args.len(), 1);
        assert_eq!(args[0], Expression::symbol(x.clone()));

        let not_func = Expression::integer(42);
        assert!(not_func.as_function().is_none());
    }
}