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
//! Symbolic Polynomial Expansion
//!
//! Provides symbolic expansion of orthogonal polynomials to explicit Expression forms
//! using recurrence relations. All implementations are mathematically verified against

use crate::core::Expression;
use crate::simplify::Simplify;

/// Expand Legendre polynomial P_n(x) to explicit symbolic form
///
/// Uses three-term recurrence to build symbolic expression:
/// - P_0(x) = 1
/// - P_1(x) = x
/// - P_{n+1}(x) = [(2n+1)x P_n(x) - n P_{n-1}(x)] / (n+1)
///
/// This implementation builds the polynomial iteratively using the Expression system,
/// applying simplification at each step to maintain manageable expression size.
///
/// # Arguments
///
/// * `n` - Polynomial degree (non-negative integer)
///
/// # Returns
///
/// Expression representing the expanded Legendre polynomial P_n(x)
///
/// # Mathematical Background
///
/// Legendre polynomials are solutions to Legendre's differential equation:
/// (1-x²)y'' - 2xy' + n(n+1)y = 0
///
/// They are orthogonal on [-1, 1] with weight function w(x) = 1.
///
/// # Examples
///
/// ```rust
/// use mathhook_core::functions::polynomials::symbolic::expand_legendre_symbolic;
/// use mathhook_core::core::Expression;
///
/// let p0 = expand_legendre_symbolic(0);
/// let p1 = expand_legendre_symbolic(1);
/// let p2 = expand_legendre_symbolic(2);
/// let p3 = expand_legendre_symbolic(3);
/// ```
#[inline]
#[must_use]
pub fn expand_legendre_symbolic(n: usize) -> Expression {
    if n == 0 {
        return Expression::integer(1);
    }
    if n == 1 {
        return Expression::symbol("x");
    }

    let x = Expression::symbol("x");
    let mut p_prev = Expression::integer(1);
    let mut p_curr = x.clone();

    for i in 1..n {
        let i_i64 = i as i64;

        let alpha_num = 2 * i_i64 + 1;
        let alpha_den = i_i64 + 1;
        let gamma_num = -i_i64;
        let gamma_den = i_i64 + 1;

        let term1 = Expression::mul(vec![
            Expression::rational(alpha_num, alpha_den),
            x.clone(),
            p_curr.clone(),
        ]);

        let term2 = Expression::mul(vec![
            Expression::rational(gamma_num, gamma_den),
            p_prev.clone(),
        ]);

        let p_next = Expression::add(vec![term1, term2]).simplify();

        p_prev = p_curr;
        p_curr = p_next;
    }

    p_curr
}

/// Expand Hermite polynomial H_n(x) to explicit symbolic form
///
/// Uses three-term recurrence to build symbolic expression:
/// - H_0(x) = 1
/// - H_1(x) = 2x
/// - H_{n+1}(x) = 2x H_n(x) - 2n H_{n-1}(x)
///
/// These are the physicist's Hermite polynomials used in quantum mechanics
/// for the harmonic oscillator eigenfunctions.
///
/// # Arguments
///
/// * `n` - Polynomial degree (non-negative integer)
///
/// # Returns
///
/// Expression representing the expanded Hermite polynomial H_n(x)
///
/// # Mathematical Background
///
/// Hermite polynomials are solutions to Hermite's differential equation:
/// y'' - 2xy' + 2ny = 0
///
/// They are orthogonal on (-∞, ∞) with weight function w(x) = e^(-x²).
///
/// # Examples
///
/// ```rust
/// use mathhook_core::functions::polynomials::symbolic::expand_hermite_symbolic;
/// use mathhook_core::core::Expression;
///
/// let h0 = expand_hermite_symbolic(0);
/// let h1 = expand_hermite_symbolic(1);
/// let h2 = expand_hermite_symbolic(2);
/// let h3 = expand_hermite_symbolic(3);
/// ```
#[inline]
#[must_use]
pub fn expand_hermite_symbolic(n: usize) -> Expression {
    if n == 0 {
        return Expression::integer(1);
    }
    if n == 1 {
        return Expression::mul(vec![Expression::integer(2), Expression::symbol("x")]);
    }

    let x = Expression::symbol("x");
    let mut p_prev = Expression::integer(1);
    let mut p_curr = Expression::mul(vec![Expression::integer(2), x.clone()]);

    for i in 1..n {
        let i_i64 = i as i64;

        let term1 = Expression::mul(vec![Expression::integer(2), x.clone(), p_curr.clone()]);

        let term2 = Expression::mul(vec![Expression::integer(-2 * i_i64), p_prev.clone()]);

        let p_next = Expression::add(vec![term1, term2]).simplify();

        p_prev = p_curr;
        p_curr = p_next;
    }

    p_curr
}

/// Expand Laguerre polynomial L_n(x) to explicit symbolic form
///
/// Uses three-term recurrence to build symbolic expression:
/// - L_0(x) = 1
/// - L_1(x) = 1 - x
/// - (n+1)L_{n+1}(x) = (2n+1-x)L_n(x) - nL_{n-1}(x)
///
/// These are the standard Laguerre polynomials (not generalized).
///
/// # Arguments
///
/// * `n` - Polynomial degree (non-negative integer)
///
/// # Returns
///
/// Expression representing the expanded Laguerre polynomial L_n(x)
///
/// # Mathematical Background
///
/// Laguerre polynomials are solutions to Laguerre's differential equation:
/// xy'' + (1-x)y' + ny = 0
///
/// They are orthogonal on [0, ∞) with weight function w(x) = e^(-x).
///
/// # Examples
///
/// ```rust
/// use mathhook_core::functions::polynomials::symbolic::expand_laguerre_symbolic;
/// use mathhook_core::core::Expression;
///
/// let l0 = expand_laguerre_symbolic(0);
/// let l1 = expand_laguerre_symbolic(1);
/// let l2 = expand_laguerre_symbolic(2);
/// let l3 = expand_laguerre_symbolic(3);
/// ```
#[inline]
#[must_use]
pub fn expand_laguerre_symbolic(n: usize) -> Expression {
    if n == 0 {
        return Expression::integer(1);
    }
    if n == 1 {
        return Expression::add(vec![
            Expression::integer(1),
            Expression::mul(vec![Expression::integer(-1), Expression::symbol("x")]),
        ]);
    }

    let x = Expression::symbol("x");
    let mut p_prev = Expression::integer(1);
    let mut p_curr = Expression::add(vec![
        Expression::integer(1),
        Expression::mul(vec![Expression::integer(-1), x.clone()]),
    ]);

    for i in 1..n {
        let i_i64 = i as i64;

        let alpha_num = -1;
        let alpha_den = i_i64 + 1;
        let beta_num = 2 * i_i64 + 1;
        let beta_den = i_i64 + 1;
        let gamma_num = -i_i64;
        let gamma_den = i_i64 + 1;

        let term1 = Expression::mul(vec![
            Expression::rational(alpha_num, alpha_den),
            x.clone(),
            p_curr.clone(),
        ]);

        let term2 = Expression::mul(vec![
            Expression::rational(beta_num, beta_den),
            p_curr.clone(),
        ]);

        let term3 = Expression::mul(vec![
            Expression::rational(gamma_num, gamma_den),
            p_prev.clone(),
        ]);

        let p_next = Expression::add(vec![term1, term2, term3]).simplify();

        p_prev = p_curr;
        p_curr = p_next;
    }

    p_curr
}

/// Expand Chebyshev polynomial of the first kind T_n(x) to explicit symbolic form
///
/// Uses three-term recurrence to build symbolic expression:
/// - T_0(x) = 1
/// - T_1(x) = x
/// - T_{n+1}(x) = 2x T_n(x) - T_{n-1}(x)
///
/// Chebyshev polynomials of the first kind are important in approximation theory
/// and have the explicit form T_n(x) = cos(n arccos(x)) for |x| ≤ 1.
///
/// # Arguments
///
/// * `n` - Polynomial degree (non-negative integer)
///
/// # Returns
///
/// Expression representing the expanded Chebyshev polynomial T_n(x)
///
/// # Mathematical Background
///
/// Chebyshev polynomials of the first kind are solutions to:
/// (1-x²)y'' - xy' + n²y = 0
///
/// They are orthogonal on [-1, 1] with weight function w(x) = 1/√(1-x²).
///
/// # Examples
///
/// ```rust
/// use mathhook_core::functions::polynomials::symbolic::expand_chebyshev_first_symbolic;
/// use mathhook_core::core::Expression;
///
/// let t0 = expand_chebyshev_first_symbolic(0);
/// let t1 = expand_chebyshev_first_symbolic(1);
/// let t2 = expand_chebyshev_first_symbolic(2);
/// let t3 = expand_chebyshev_first_symbolic(3);
/// ```
#[inline]
#[must_use]
pub fn expand_chebyshev_first_symbolic(n: usize) -> Expression {
    if n == 0 {
        return Expression::integer(1);
    }
    if n == 1 {
        return Expression::symbol("x");
    }

    let x = Expression::symbol("x");
    let mut p_prev = Expression::integer(1);
    let mut p_curr = x.clone();

    for _ in 1..n {
        let term1 = Expression::mul(vec![Expression::integer(2), x.clone(), p_curr.clone()]);

        let term2 = Expression::mul(vec![Expression::integer(-1), p_prev.clone()]);

        let p_next = Expression::add(vec![term1, term2]).simplify();

        p_prev = p_curr;
        p_curr = p_next;
    }

    p_curr
}

/// Expand Chebyshev polynomial of the second kind U_n(x) to explicit symbolic form
///
/// Uses three-term recurrence to build symbolic expression:
/// - U_0(x) = 1
/// - U_1(x) = 2x
/// - U_{n+1}(x) = 2x U_n(x) - U_{n-1}(x)
///
/// Chebyshev polynomials of the second kind have the explicit form
/// U_n(x) = sin((n+1) arccos(x)) / sin(arccos(x)) for |x| < 1.
///
/// # Arguments
///
/// * `n` - Polynomial degree (non-negative integer)
///
/// # Returns
///
/// Expression representing the expanded Chebyshev polynomial U_n(x)
///
/// # Mathematical Background
///
/// Chebyshev polynomials of the second kind are orthogonal on [-1, 1]
/// with weight function w(x) = √(1-x²).
///
/// # Examples
///
/// ```rust
/// use mathhook_core::functions::polynomials::symbolic::expand_chebyshev_second_symbolic;
/// use mathhook_core::core::Expression;
///
/// let u0 = expand_chebyshev_second_symbolic(0);
/// let u1 = expand_chebyshev_second_symbolic(1);
/// let u2 = expand_chebyshev_second_symbolic(2);
/// let u3 = expand_chebyshev_second_symbolic(3);
/// ```
#[inline]
#[must_use]
pub fn expand_chebyshev_second_symbolic(n: usize) -> Expression {
    if n == 0 {
        return Expression::integer(1);
    }
    if n == 1 {
        return Expression::mul(vec![Expression::integer(2), Expression::symbol("x")]);
    }

    let x = Expression::symbol("x");
    let mut p_prev = Expression::integer(1);
    let mut p_curr = Expression::mul(vec![Expression::integer(2), x.clone()]);

    for _ in 1..n {
        let term1 = Expression::mul(vec![Expression::integer(2), x.clone(), p_curr.clone()]);

        let term2 = Expression::mul(vec![Expression::integer(-1), p_prev.clone()]);

        let p_next = Expression::add(vec![term1, term2]).simplify();

        p_prev = p_curr;
        p_curr = p_next;
    }

    p_curr
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_legendre_p0_p1() {
        let p0 = expand_legendre_symbolic(0);
        let p1 = expand_legendre_symbolic(1);

        assert_eq!(p0, Expression::integer(1));
        assert_eq!(p1, Expression::symbol("x"));
    }

    #[test]
    fn test_hermite_h0_h1() {
        let h0 = expand_hermite_symbolic(0);
        let h1 = expand_hermite_symbolic(1);

        assert_eq!(h0, Expression::integer(1));
        assert_eq!(
            h1,
            Expression::mul(vec![Expression::integer(2), Expression::symbol("x")])
        );
    }

    #[test]
    fn test_laguerre_l0_l1() {
        let l0 = expand_laguerre_symbolic(0);
        let _l1 = expand_laguerre_symbolic(1);

        assert_eq!(l0, Expression::integer(1));
    }

    #[test]
    fn test_chebyshev_first_t0_t1() {
        let t0 = expand_chebyshev_first_symbolic(0);
        let t1 = expand_chebyshev_first_symbolic(1);

        assert_eq!(t0, Expression::integer(1));
        assert_eq!(t1, Expression::symbol("x"));
    }

    #[test]
    fn test_chebyshev_second_u0_u1() {
        let u0 = expand_chebyshev_second_symbolic(0);
        let u1 = expand_chebyshev_second_symbolic(1);

        assert_eq!(u0, Expression::integer(1));
        assert_eq!(
            u1,
            Expression::mul(vec![Expression::integer(2), Expression::symbol("x")])
        );
    }
}