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
//! Separation of variables method for PDEs
//!
//! This module implements complete separation of variables including:
//! - Eigenvalue problem solving with boundary conditions
//! - Fourier coefficient computation from initial conditions
//! - Complete series solution assembly
//!
//! # Implementation
//!
//! For standard PDEs like heat and wave equations:
//! 1. Assume product solution: u(x,t) = X(x)T(t)
//! 2. Apply boundary conditions → solve eigenvalue problem for X(x)
//! 3. Solve temporal ODE for T(t)
//! 4. Apply initial conditions → compute Fourier coefficients
//! 5. Assemble infinite series solution

use crate::calculus::pde::common::eigenvalue_problem::solve_sturm_liouville;
use crate::calculus::pde::common::fourier_coefficients::compute_fourier_coefficients;
use crate::calculus::pde::types::{BoundaryCondition, InitialCondition, Pde};
use crate::core::{Expression, Symbol};
use crate::expr;

/// Result of applying separation of variables
#[derive(Debug, Clone, PartialEq)]
pub struct SeparatedSolution {
    /// The separated functions (e.g., X(x), T(t) for u(x,t) = X(x)T(t))
    pub functions: Vec<Expression>,
    /// The separation constants (λ₀, λ₁, ...)
    pub constants: Vec<Expression>,
    /// The general product solution (before applying ICs)
    pub solution: Expression,
    /// Computed eigenvalues from boundary conditions
    pub eigenvalues: Vec<Expression>,
    /// Computed eigenfunctions from boundary conditions
    pub eigenfunctions: Vec<Expression>,
    /// Fourier coefficients from initial conditions
    pub coefficients: Vec<Expression>,
}

/// Applies separation of variables to a PDE with boundary and initial conditions
///
/// This is the complete implementation that:
/// 1. Parses boundary conditions
/// 2. Solves eigenvalue problem
/// 3. Computes Fourier coefficients
/// 4. Assembles complete solution
///
/// # Arguments
///
/// * `pde` - The PDE to solve
/// * `boundary_conditions` - Spatial boundary conditions (must have exactly 2)
/// * `initial_conditions` - Temporal initial conditions
///
/// # Returns
///
/// Complete separated solution with eigenvalues, eigenfunctions, and coefficients
///
/// # Examples
///
/// ```rust
/// use mathhook_core::calculus::pde::separation_of_variables::separate_variables;
/// use mathhook_core::calculus::pde::types::{Pde, BoundaryCondition, InitialCondition};
/// use mathhook_core::{symbol, expr};
///
/// let u = symbol!(u);
/// let x = symbol!(x);
/// let t = symbol!(t);
/// let equation = expr!(u);
/// let pde = Pde::new(equation, u, vec![x.clone(), t]);
///
/// let bc_left = BoundaryCondition::dirichlet_at(x.clone(), expr!(0), expr!(0));
/// let bc_right = BoundaryCondition::dirichlet_at(x, expr!(pi), expr!(0));
/// let bcs = vec![bc_left, bc_right];
///
/// let sin_x = expr!(sin(x));
/// let ic = InitialCondition::value(sin_x);
/// let ics = vec![ic];
///
/// let result = separate_variables(&pde, &bcs, &ics);
/// assert!(result.is_ok());
/// ```
pub fn separate_variables(
    pde: &Pde,
    boundary_conditions: &[BoundaryCondition],
    initial_conditions: &[InitialCondition],
) -> Result<SeparatedSolution, String> {
    let num_vars = pde.independent_vars.len();

    if num_vars < 2 {
        return Err("Separation of variables requires at least 2 independent variables".to_owned());
    }

    let functions = create_separated_functions(&pde.independent_vars);
    let constants = create_separation_constants(num_vars - 1);
    let solution = construct_product_solution(&functions);

    if boundary_conditions.is_empty() {
        return Ok(SeparatedSolution {
            functions,
            constants,
            solution,
            eigenvalues: Vec::new(),
            eigenfunctions: Vec::new(),
            coefficients: Vec::new(),
        });
    }

    if boundary_conditions.len() != 2 {
        return Err(format!(
            "Expected exactly 2 boundary conditions, got {}",
            boundary_conditions.len()
        ));
    }

    let eigenvalue_solution =
        solve_sturm_liouville(&boundary_conditions[0], &boundary_conditions[1], 10)?;

    let coefficients = if initial_conditions.is_empty() {
        Vec::new()
    } else {
        compute_fourier_coefficients(
            &initial_conditions[0],
            &eigenvalue_solution.eigenfunctions,
            &eigenvalue_solution.domain,
            &eigenvalue_solution.variable,
        )?
    };

    Ok(SeparatedSolution {
        functions,
        constants,
        solution,
        eigenvalues: eigenvalue_solution.eigenvalues,
        eigenfunctions: eigenvalue_solution.eigenfunctions,
        coefficients,
    })
}

/// Construct complete series solution from eigenvalues and coefficients
///
/// Builds: u(x,t) = Σₙ cₙ Xₙ(x) Tₙ(t)
///
/// # Arguments
///
/// * `coefficients` - Fourier coefficients cₙ
/// * `spatial_eigenfunctions` - Spatial eigenfunctions Xₙ(x)
/// * `temporal_solutions` - Temporal solutions Tₙ(t)
/// * `num_terms` - Number of terms to include in series
///
/// # Returns
///
/// Series solution expression
pub fn construct_series_solution(
    coefficients: &[Expression],
    spatial_eigenfunctions: &[Expression],
    temporal_solutions: &[Expression],
    num_terms: usize,
) -> Expression {
    let mut terms = Vec::new();

    let max_terms = num_terms.min(coefficients.len());

    for n in 0..max_terms {
        let c_n = &coefficients[n];
        let x_n = &spatial_eigenfunctions[n];
        let t_n = &temporal_solutions[n];

        let term = Expression::mul(vec![c_n.clone(), x_n.clone(), t_n.clone()]);
        terms.push(term);
    }

    if terms.is_empty() {
        return expr!(0);
    }

    Expression::add(terms)
}

/// Create separated functions F(x), G(y), etc. for each variable
fn create_separated_functions(vars: &[Symbol]) -> Vec<Expression> {
    vars.iter()
        .map(|var| Expression::function("F", vec![Expression::symbol(var.clone())]))
        .collect()
}

/// Create separation constants λ₀, λ₁, etc.
fn create_separation_constants(count: usize) -> Vec<Expression> {
    (0..count)
        .map(|i| {
            let lambda = Symbol::new(format!("lambda_{}", i));
            Expression::symbol(lambda)
        })
        .collect()
}

/// Construct product solution u = F(x)G(y)H(t)...
fn construct_product_solution(functions: &[Expression]) -> Expression {
    if functions.is_empty() {
        return expr!(1);
    }

    Expression::mul(functions.to_vec())
}

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

    #[test]
    fn test_separate_variables_basic_no_bc() {
        let u = symbol!(u);
        let x = symbol!(x);
        let t = symbol!(t);
        let equation = expr!(u);
        let pde = Pde::new(equation, u, vec![x, t]);

        let result = separate_variables(&pde, &[], &[]);
        assert!(result.is_ok());

        let solution = result.unwrap();
        assert_eq!(solution.functions.len(), 2);
        assert_eq!(solution.constants.len(), 1);
        assert!(solution.eigenvalues.is_empty());
        assert!(solution.eigenfunctions.is_empty());
    }

    #[test]
    fn test_separate_variables_with_dirichlet_bc() {
        let u = symbol!(u);
        let x = symbol!(x);
        let t = symbol!(t);
        let equation = expr!(u);
        let pde = Pde::new(equation, u, vec![x.clone(), t]);

        let bc_left = BoundaryCondition::dirichlet_at(x.clone(), expr!(0), expr!(0));
        let bc_right = BoundaryCondition::dirichlet_at(x, expr!(pi), expr!(0));
        let bcs = vec![bc_left, bc_right];

        let result = separate_variables(&pde, &bcs, &[]);
        assert!(result.is_ok());

        let solution = result.unwrap();
        assert_eq!(solution.eigenvalues.len(), 10);
        assert_eq!(solution.eigenfunctions.len(), 10);
    }

    #[test]
    fn test_separate_variables_with_neumann_bc() {
        let u = symbol!(u);
        let x = symbol!(x);
        let t = symbol!(t);
        let equation = expr!(u);
        let pde = Pde::new(equation, u, vec![x.clone(), t]);

        let bc_left = BoundaryCondition::neumann_at(x.clone(), expr!(0), expr!(0));
        let bc_right = BoundaryCondition::neumann_at(x, expr!(pi), expr!(0));
        let bcs = vec![bc_left, bc_right];

        let result = separate_variables(&pde, &bcs, &[]);
        assert!(result.is_ok());

        let solution = result.unwrap();
        assert_eq!(solution.eigenvalues.len(), 10);
        assert_eq!(solution.eigenfunctions.len(), 10);
    }

    #[test]
    fn test_separate_variables_with_ic() {
        let u = symbol!(u);
        let x = symbol!(x);
        let t = symbol!(t);
        let equation = expr!(u);
        let pde = Pde::new(equation, u, vec![x.clone(), t]);

        let bc_left = BoundaryCondition::dirichlet_at(x.clone(), expr!(0), expr!(0));
        let bc_right = BoundaryCondition::dirichlet_at(x.clone(), expr!(pi), expr!(0));
        let bcs = vec![bc_left, bc_right];

        let sin_x = Expression::function("sin", vec![Expression::symbol(x)]);
        let ic = InitialCondition::value(sin_x);
        let ics = vec![ic];

        let result = separate_variables(&pde, &bcs, &ics);
        assert!(result.is_ok());

        let solution = result.unwrap();
        assert_eq!(solution.coefficients.len(), 10);
    }

    #[test]
    fn test_separate_variables_insufficient_bcs() {
        let u = symbol!(u);
        let x = symbol!(x);
        let t = symbol!(t);
        let equation = expr!(u);
        let pde = Pde::new(equation, u, vec![x.clone(), t]);

        let bc = BoundaryCondition::dirichlet_at(x, expr!(0), expr!(0));
        let bcs = vec![bc];

        let result = separate_variables(&pde, &bcs, &[]);
        assert!(result.is_err());
    }

    #[test]
    fn test_separate_variables_three_vars() {
        let u = symbol!(u);
        let x = symbol!(x);
        let y = symbol!(y);
        let t = symbol!(t);
        let equation = expr!(u);
        let pde = Pde::new(equation, u, vec![x, y, t]);

        let result = separate_variables(&pde, &[], &[]);
        assert!(result.is_ok());

        let solution = result.unwrap();
        assert_eq!(solution.functions.len(), 3);
        assert_eq!(solution.constants.len(), 2);
    }

    #[test]
    fn test_separate_variables_insufficient_vars() {
        let u = symbol!(u);
        let x = symbol!(x);
        let equation = expr!(u);
        let pde = Pde::new(equation, u, vec![x]);

        let result = separate_variables(&pde, &[], &[]);
        assert!(result.is_err());
    }

    #[test]
    fn test_create_separated_functions() {
        let x = symbol!(x);
        let t = symbol!(t);
        let vars = vec![x, t];

        let functions = create_separated_functions(&vars);
        assert_eq!(functions.len(), 2);
    }

    #[test]
    fn test_create_separation_constants() {
        let constants = create_separation_constants(2);
        assert_eq!(constants.len(), 2);
    }

    #[test]
    fn test_construct_product_solution_empty() {
        let solution = construct_product_solution(&[]);
        assert_eq!(solution, expr!(1));
    }

    #[test]
    fn test_construct_product_solution_single() {
        let x = symbol!(x);
        let f = Expression::function("F", vec![Expression::symbol(x)]);
        let solution = construct_product_solution(from_ref(&f));
        assert_eq!(solution, f);
    }

    #[test]
    fn test_construct_series_solution_single_term() {
        let x = symbol!(x);
        let _t = symbol!(t);

        let coefficients = vec![expr!(1)];
        let spatial = vec![Expression::function("sin", vec![Expression::symbol(x)])];
        let temporal = vec![Expression::function("exp", vec![expr!(-t)])];

        let solution = construct_series_solution(&coefficients, &spatial, &temporal, 1);

        assert!(matches!(solution, Expression::Mul(_)));
    }

    #[test]
    fn test_construct_series_solution_multiple_terms() {
        let x = symbol!(x);
        let t = symbol!(t);

        let coefficients = vec![expr!(1), expr!(2)];
        let spatial = vec![
            Expression::function("sin", vec![Expression::symbol(x.clone())]),
            Expression::function(
                "sin",
                vec![Expression::mul(vec![
                    Expression::integer(2),
                    Expression::symbol(x),
                ])],
            ),
        ];
        let temporal = vec![
            Expression::function(
                "exp",
                vec![Expression::mul(vec![
                    Expression::integer(-1),
                    Expression::symbol(t.clone()),
                ])],
            ),
            Expression::function(
                "exp",
                vec![Expression::mul(vec![
                    Expression::integer(-4),
                    Expression::symbol(t),
                ])],
            ),
        ];

        let solution = construct_series_solution(&coefficients, &spatial, &temporal, 2);

        assert!(matches!(solution, Expression::Add(_)));
    }

    #[test]
    fn test_construct_series_solution_empty() {
        let solution = construct_series_solution(&[], &[], &[], 0);
        assert_eq!(solution, expr!(0));
    }

    #[test]
    fn test_separated_solution_clone() {
        let u = symbol!(u);
        let x = symbol!(x);
        let t = symbol!(t);
        let equation = expr!(u);
        let pde = Pde::new(equation, u, vec![x, t]);

        let result = separate_variables(&pde, &[], &[]);
        assert!(result.is_ok());

        let solution = result.unwrap();
        let _cloned = solution.clone();
    }
}