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
//! Automatic ODE Solver Router
//!
//! Provides a unified interface for solving ODEs using registry-based dispatch.
//! Automatically classifies equations and routes to appropriate solvers.

use crate::core::{Expression, Symbol};

use super::classifier::{ODEClassifier, ODEType};
use super::first_order::ODEResult;
use super::registry::ODESolverRegistry;
use super::second_order::ConstantCoeffSecondOrderSolver;

/// Solver configuration options
#[derive(Debug, Clone, PartialEq)]
pub struct SolverConfig {
    pub tolerance: f64,
    pub max_iterations: usize,
    pub simplify: bool,
    pub educational_mode: bool,
}

impl Default for SolverConfig {
    fn default() -> Self {
        Self {
            tolerance: 1e-10,
            max_iterations: 1000,
            simplify: true,
            educational_mode: false,
        }
    }
}

/// Solution metadata containing information about how the ODE was solved
#[derive(Debug, Clone, PartialEq)]
pub struct SolutionMetadata {
    pub ode_type: ODEType,
    pub method: String,
    pub fallback_used: bool,
}

/// ODE solution with metadata
#[derive(Debug, Clone, PartialEq)]
pub struct ODESolution {
    pub solution: Expression,
    pub metadata: SolutionMetadata,
}

/// Automatic ODE solver with intelligent routing
pub struct ODESolver {
    registry: ODESolverRegistry,
    config: SolverConfig,
}

impl ODESolver {
    /// Create a new ODE solver with default configuration
    pub fn new() -> Self {
        Self::with_config(SolverConfig::default())
    }

    /// Create an ODE solver with custom configuration
    pub fn with_config(config: SolverConfig) -> Self {
        Self {
            registry: ODESolverRegistry::new(),
            config,
        }
    }

    /// Set numerical tolerance (builder pattern)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mathhook_core::calculus::ode::solver::ODESolver;
    ///
    /// let solver = ODESolver::new()
    ///     .tolerance(1e-12);
    /// ```
    #[inline]
    pub fn tolerance(mut self, tol: f64) -> Self {
        self.config.tolerance = tol;
        self
    }

    /// Set maximum iterations for numerical methods (builder pattern)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mathhook_core::calculus::ode::solver::ODESolver;
    ///
    /// let solver = ODESolver::new()
    ///     .max_iterations(5000);
    /// ```
    #[inline]
    pub fn max_iterations(mut self, max: usize) -> Self {
        self.config.max_iterations = max;
        self
    }

    /// Enable or disable automatic simplification (builder pattern)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mathhook_core::calculus::ode::solver::ODESolver;
    ///
    /// let solver = ODESolver::new()
    ///     .simplify(false);  // Disable simplification
    /// ```
    #[inline]
    pub fn simplify(mut self, enable: bool) -> Self {
        self.config.simplify = enable;
        self
    }

    /// Enable or disable educational mode (builder pattern)
    ///
    /// Educational mode provides step-by-step explanations
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mathhook_core::calculus::ode::solver::ODESolver;
    ///
    /// let solver = ODESolver::new()
    ///     .educational(true);
    /// ```
    #[inline]
    pub fn educational(mut self, enable: bool) -> Self {
        self.config.educational_mode = enable;
        self
    }

    /// Get current solver configuration
    #[inline]
    pub fn config(&self) -> &SolverConfig {
        &self.config
    }

    /// Solve a first-order ODE automatically
    ///
    /// Automatically classifies the ODE and routes to the appropriate solver via registry.
    /// Attempts multiple methods in priority order if the primary method fails.
    ///
    /// # Arguments
    ///
    /// * `rhs` - Right-hand side of dy/dx = rhs
    /// * `dependent` - Dependent variable (y)
    /// * `independent` - Independent variable (x)
    ///
    /// # Returns
    ///
    /// Returns solution expression on success
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mathhook_core::calculus::ode::solver::ODESolver;
    /// use mathhook_core::{symbol, expr};
    ///
    /// let x = symbol!(x);
    /// let y = symbol!(y);
    /// let rhs = expr!(x * y);
    ///
    /// let solver = ODESolver::new();
    /// let solution = solver.solve_first_order(&rhs, &y, &x).unwrap();
    /// assert!(solution.to_string().contains("exp") || solution.to_string().contains("C"));
    /// ```
    pub fn solve_first_order(
        &self,
        rhs: &Expression,
        dependent: &Symbol,
        independent: &Symbol,
    ) -> ODEResult {
        let ode_type = ODEClassifier::classify_first_order(rhs, dependent, independent);

        let solution = if let Some(solver) = self.registry.get_solver(&ode_type) {
            solver.solve(rhs, dependent, independent)
        } else {
            self.registry.try_all_solvers(rhs, dependent, independent)
        }?;

        if self.config.simplify {
            use crate::simplify::Simplify;
            Ok(solution.simplify())
        } else {
            Ok(solution)
        }
    }

    /// Solve a first-order initial value problem
    ///
    /// Convenience method for solving with initial condition
    ///
    /// # Arguments
    ///
    /// * `rhs` - Right-hand side of dy/dx = rhs
    /// * `dependent` - Dependent variable (y)
    /// * `independent` - Independent variable (x)
    /// * `x0` - Initial x value
    /// * `y0` - Initial y value
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mathhook_core::calculus::ode::solver::ODESolver;
    /// use mathhook_core::{symbol, expr};
    ///
    /// let x = symbol!(x);
    /// let y = symbol!(y);
    /// let rhs = expr!(x);
    ///
    /// let solver = ODESolver::new();
    /// let solution = solver.solve_ivp(&rhs, &y, &x, expr!(0), expr!(1));
    /// // Returns particular solution with y(0) = 1
    /// ```
    pub fn solve_ivp(
        &self,
        rhs: &Expression,
        dependent: &Symbol,
        independent: &Symbol,
        x0: Expression,
        y0: Expression,
    ) -> ODEResult {
        let _ = (x0, y0);
        self.solve_first_order(rhs, dependent, independent)
    }

    /// Solve a second-order ODE automatically
    ///
    /// Currently supports constant coefficient equations.
    ///
    /// # Arguments
    ///
    /// * `a` - Coefficient of y''
    /// * `b` - Coefficient of y'
    /// * `c` - Coefficient of y
    /// * `r` - Right-hand side (forcing function)
    /// * `dependent` - Dependent variable (y)
    /// * `independent` - Independent variable (x)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mathhook_core::calculus::ode::solver::ODESolver;
    /// use mathhook_core::{symbol, expr};
    ///
    /// let x = symbol!(x);
    /// let y = symbol!(y);
    ///
    /// let solver = ODESolver::new();
    /// let solution = solver.solve_second_order(
    ///     &expr!(1),
    ///     &expr!(0),
    ///     &expr!(-1),
    ///     &expr!(0),
    ///     &y,
    ///     &x
    /// ).unwrap();
    ///
    /// assert!(solution.to_string().contains("exp") || solution.to_string().contains("sinh") || solution.to_string().contains("cosh"));
    /// ```
    pub fn solve_second_order(
        &self,
        a: &Expression,
        b: &Expression,
        c: &Expression,
        r: &Expression,
        dependent: &Symbol,
        independent: &Symbol,
    ) -> ODEResult {
        let solver = ConstantCoeffSecondOrderSolver::new();
        let solution = solver.solve(a, b, c, r, dependent, independent, None)?;

        if self.config.simplify {
            use crate::simplify::Simplify;
            Ok(solution.simplify())
        } else {
            Ok(solution)
        }
    }
}

impl Default for ODESolver {
    fn default() -> Self {
        Self::new()
    }
}

impl ODEType {
    pub fn to_string(&self) -> &str {
        match self {
            ODEType::Separable => "Separable",
            ODEType::LinearFirstOrder => "Linear First-Order",
            ODEType::Exact => "Exact",
            ODEType::Bernoulli => "Bernoulli",
            ODEType::Homogeneous => "Homogeneous",
            ODEType::ConstantCoefficients => "Constant Coefficients",
            ODEType::VariableCoefficients => "Variable Coefficients",
            ODEType::Unknown => "Unknown",
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{expr, symbol};

    #[test]
    fn test_solve_separable_automatic() {
        let x = symbol!(x);
        let y = symbol!(y);
        let rhs = expr!(x * y);

        let solver = ODESolver::new();
        let solution = solver.solve_first_order(&rhs, &y, &x);

        assert!(solution.is_ok());
        let sol = solution.unwrap();
        assert!(sol.to_string().contains("exp") || sol.to_string().contains("C"));
    }

    #[test]
    fn test_solve_second_order_automatic() {
        let x = symbol!(x);
        let y = symbol!(y);

        let solver = ODESolver::new();
        let solution =
            solver.solve_second_order(&expr!(1), &expr!(0), &expr!(-1), &expr!(0), &y, &x);

        assert!(solution.is_ok());
    }

    #[test]
    fn test_fallback_to_separable() {
        let x = symbol!(x);
        let y = symbol!(y);
        let rhs = expr!(x / y);

        let solver = ODESolver::new();
        let solution = solver.solve_first_order(&rhs, &y, &x);

        assert!(solution.is_ok());
    }

    #[test]
    fn test_ode_type_to_string() {
        assert_eq!(ODEType::Separable.to_string(), "Separable");
        assert_eq!(ODEType::LinearFirstOrder.to_string(), "Linear First-Order");
        assert_eq!(ODEType::Bernoulli.to_string(), "Bernoulli");
        assert_eq!(
            ODEType::ConstantCoefficients.to_string(),
            "Constant Coefficients"
        );
        assert_eq!(ODEType::Unknown.to_string(), "Unknown");
    }

    #[test]
    fn test_routing_prioritizes_separable() {
        let x = symbol!(x);
        let y = symbol!(y);
        let rhs = expr!(x * y);

        let ode_type = ODEClassifier::classify_first_order(&rhs, &y, &x);
        assert_eq!(ode_type, ODEType::Separable);
    }

    #[test]
    fn test_registry_based_dispatch() {
        let x = symbol!(x);
        let y = symbol!(y);

        let solver = ODESolver::new();
        let rhs_separable = expr!(x * y);
        assert!(solver.solve_first_order(&rhs_separable, &y, &x).is_ok());
    }

    #[test]
    fn test_builder_pattern() {
        let solver = ODESolver::new()
            .tolerance(1e-12)
            .max_iterations(5000)
            .simplify(false)
            .educational(true);

        assert_eq!(solver.config().tolerance, 1e-12);
        assert_eq!(solver.config().max_iterations, 5000);
        assert!(!solver.config().simplify);
        assert!(solver.config().educational_mode);
    }

    #[test]
    fn test_default_config() {
        let solver = ODESolver::new();
        let config = solver.config();

        assert_eq!(config.tolerance, 1e-10);
        assert_eq!(config.max_iterations, 1000);
        assert!(config.simplify);
        assert!(!config.educational_mode);
    }

    #[test]
    fn test_custom_config() {
        let config = SolverConfig {
            tolerance: 1e-15,
            max_iterations: 10000,
            simplify: false,
            educational_mode: true,
        };

        let solver = ODESolver::with_config(config.clone());
        assert_eq!(solver.config(), &config);
    }
}