thales 0.4.2

A comprehensive Computer Algebra System (CAS) library for symbolic mathematics, equation solving, calculus, and linear algebra
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
//! Fourier series expansion module.
//!
//! This module provides Fourier series computation for mathematical expressions.
//! A real Fourier series represents a periodic function as:
//!
//! ```text
//! f(x) ~ a_0/2 + Σ_{n=1}^{N} [a_n*cos(2πnx/L) + b_n*sin(2πnx/L)]
//! ```
//!
//! where L is the period and the Fourier coefficients are:
//!
//! ```text
//! a_n = (2/L) * ∫_{-L/2}^{L/2} f(x)*cos(2πnx/L) dx
//! b_n = (2/L) * ∫_{-L/2}^{L/2} f(x)*sin(2πnx/L) dx
//! ```
//!
//! Coefficients are computed via numerical integration using adaptive Simpson's rule.
//!
//! # Examples
//!
//! ```rust
//! use thales::fourier::{fourier_series, FourierSeries};
//! use thales::ast::Variable;
//! use thales::parser::parse_expression;
//!
//! // Fourier series of cos(x) with period 2π should recover cos(x)
//! let expr = parse_expression("cos(x)").unwrap();
//! let x = Variable::new("x");
//! let series = fourier_series(&expr, &x, 3, None).unwrap();
//! // a_1 ≈ 1, all other coefficients ≈ 0
//! assert!((series.a_coefficients[1] - 1.0).abs() < 1e-6);
//! ```

use crate::ast::{Expression, Function, Variable};
use crate::integration::numerical_integrate;
use std::f64::consts::PI;
use std::fmt;

/// Errors that can occur during Fourier series computation.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum FourierSeriesError {
    /// Integration failed when computing a coefficient.
    IntegrationFailed(String),
    /// The requested number of terms is zero.
    InvalidNumTerms,
    /// The period must be strictly positive.
    InvalidPeriod,
    /// Expression evaluation failed.
    EvaluationFailed(String),
}

impl fmt::Display for FourierSeriesError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            FourierSeriesError::IntegrationFailed(msg) => {
                write!(
                    f,
                    "Integration failed during coefficient computation: {msg}"
                )
            }
            FourierSeriesError::InvalidNumTerms => {
                write!(f, "Number of Fourier terms must be at least 1")
            }
            FourierSeriesError::InvalidPeriod => {
                write!(f, "Period must be strictly positive")
            }
            FourierSeriesError::EvaluationFailed(msg) => {
                write!(f, "Expression evaluation failed: {msg}")
            }
        }
    }
}

impl std::error::Error for FourierSeriesError {}

/// Result type for Fourier series operations.
pub type FourierSeriesResult<T> = Result<T, FourierSeriesError>;

/// A real Fourier series: a_0/2 + Σ [a_n*cos(2πnx/L) + b_n*sin(2πnx/L)].
///
/// Coefficients are indexed from 0: `a_coefficients[n]` is a_n,
/// `b_coefficients[n]` is b_n (b_0 is always 0).
#[derive(Debug, Clone)]
pub struct FourierSeries {
    /// Cosine coefficients: a_n for n = 0, 1, ..., num_terms.
    pub a_coefficients: Vec<f64>,
    /// Sine coefficients: b_n for n = 1, ..., num_terms (index 0 is unused, set to 0.0).
    pub b_coefficients: Vec<f64>,
    /// Period of the series.
    pub period: f64,
    /// Variable of expansion.
    pub variable: Variable,
    /// Number of harmonic terms (N), not counting the constant a_0/2.
    pub num_terms: usize,
}

impl FourierSeries {
    /// Numerically evaluate the truncated Fourier series at a point `x`.
    pub fn evaluate(&self, x: f64) -> f64 {
        let l = self.period;
        let mut sum = self.a_coefficients[0] / 2.0;
        for n in 1..=self.num_terms {
            let angle = 2.0 * PI * (n as f64) * x / l;
            sum += self.a_coefficients[n] * angle.cos();
            sum += self.b_coefficients[n] * angle.sin();
        }
        sum
    }

    /// Convert the series to a human-readable string representation.
    pub fn to_display_string(&self) -> String {
        let var = &self.variable.name;
        let l = self.period;
        let mut parts: Vec<String> = Vec::new();

        let a0 = self.a_coefficients[0];
        if a0.abs() > 1e-10 {
            parts.push(format!("{:.6}/2", a0));
        }

        for n in 1..=self.num_terms {
            let an = self.a_coefficients[n];
            let bn = self.b_coefficients[n];
            let arg = if (l - 2.0 * PI).abs() < 1e-10 {
                if n == 1 {
                    var.clone()
                } else {
                    format!("{n}{var}")
                }
            } else {
                format!("2π·{n}·{var}/{l:.6}")
            };
            if an.abs() > 1e-10 {
                parts.push(format!("{:.6}·cos({arg})", an));
            }
            if bn.abs() > 1e-10 {
                parts.push(format!("{:.6}·sin({arg})", bn));
            }
        }

        if parts.is_empty() {
            "0".to_string()
        } else {
            parts.join(" + ")
        }
    }

    /// Generate a LaTeX representation of the series.
    pub fn to_latex(&self) -> String {
        let var = &self.variable.name;
        let l = self.period;
        let mut parts: Vec<String> = Vec::new();

        let a0 = self.a_coefficients[0];
        if a0.abs() > 1e-10 {
            parts.push(format!("\\frac{{{:.6}}}{{2}}", a0));
        }

        for n in 1..=self.num_terms {
            let an = self.a_coefficients[n];
            let bn = self.b_coefficients[n];
            let arg = if (l - 2.0 * PI).abs() < 1e-10 {
                if n == 1 {
                    var.clone()
                } else {
                    format!("{n}{var}")
                }
            } else {
                format!("\\frac{{2\\pi \\cdot {n} \\cdot {var}}}{{{l:.6}}}")
            };
            if an.abs() > 1e-10 {
                parts.push(format!("{:.6}\\cos({arg})", an));
            }
            if bn.abs() > 1e-10 {
                parts.push(format!("{:.6}\\sin({arg})", bn));
            }
        }

        if parts.is_empty() {
            "0".to_string()
        } else {
            parts.join(" + ")
        }
    }
}

impl fmt::Display for FourierSeries {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_display_string())
    }
}

/// Compute the Fourier series of `expr` with respect to `variable`.
///
/// Computes coefficients numerically using adaptive Simpson's rule integration.
///
/// # Arguments
///
/// * `expr` - The expression f(x) to expand
/// * `variable` - The variable of expansion
/// * `num_terms` - Number of harmonic terms N (produces terms n=1..=N)
/// * `period` - Optional period L (defaults to 2π if `None`)
///
/// # Returns
///
/// A [`FourierSeries`] with numerically computed coefficients, or an error.
///
/// # Examples
///
/// ```rust
/// use thales::fourier::fourier_series;
/// use thales::ast::Variable;
/// use thales::parser::parse_expression;
///
/// let expr = parse_expression("sin(x)").unwrap();
/// let x = Variable::new("x");
/// let series = fourier_series(&expr, &x, 3, None).unwrap();
/// // b_1 ≈ 1, all other coefficients ≈ 0
/// assert!((series.b_coefficients[1] - 1.0).abs() < 1e-6);
/// ```
pub fn fourier_series(
    expr: &Expression,
    variable: &Variable,
    num_terms: usize,
    period: Option<f64>,
) -> FourierSeriesResult<FourierSeries> {
    if num_terms == 0 {
        return Err(FourierSeriesError::InvalidNumTerms);
    }

    let l = period.unwrap_or(2.0 * PI);
    if l <= 0.0 {
        return Err(FourierSeriesError::InvalidPeriod);
    }

    let var_name = variable.name.as_str();
    let half_l = l / 2.0;
    let tolerance = 1e-8;

    // Compute a_0 = (2/L) * ∫_{-L/2}^{L/2} f(x) dx
    let a0 = compute_a0(expr, var_name, half_l, tolerance)?;

    let mut a_coefficients = Vec::with_capacity(num_terms + 1);
    let mut b_coefficients = Vec::with_capacity(num_terms + 1);
    a_coefficients.push(a0);
    b_coefficients.push(0.0); // b_0 is undefined/zero by convention

    for n in 1..=num_terms {
        let an = compute_an(expr, var_name, n, l, half_l, tolerance)?;
        let bn = compute_bn(expr, var_name, n, l, half_l, tolerance)?;
        a_coefficients.push(an);
        b_coefficients.push(bn);
    }

    Ok(FourierSeries {
        a_coefficients,
        b_coefficients,
        period: l,
        variable: variable.clone(),
        num_terms,
    })
}

/// Compute a_0 = (2/L) * ∫_{-L/2}^{L/2} f(x) dx.
fn compute_a0(
    expr: &Expression,
    var_name: &str,
    half_l: f64,
    tolerance: f64,
) -> FourierSeriesResult<f64> {
    let integral = numerical_integrate(expr, var_name, -half_l, half_l, tolerance)
        .map_err(|e| FourierSeriesError::IntegrationFailed(e.to_string()))?;
    Ok(2.0 / (2.0 * half_l) * integral)
}

/// Compute a_n = (2/L) * ∫_{-L/2}^{L/2} f(x)*cos(2πnx/L) dx.
fn compute_an(
    expr: &Expression,
    var_name: &str,
    n: usize,
    l: f64,
    half_l: f64,
    tolerance: f64,
) -> FourierSeriesResult<f64> {
    let cos_factor = build_cos_factor(var_name, n, l);
    let integrand = Expression::Binary(
        crate::ast::BinaryOp::Mul,
        Box::new(expr.clone()),
        Box::new(cos_factor),
    );
    let integral = numerical_integrate(&integrand, var_name, -half_l, half_l, tolerance)
        .map_err(|e| FourierSeriesError::IntegrationFailed(e.to_string()))?;
    Ok(2.0 / l * integral)
}

/// Compute b_n = (2/L) * ∫_{-L/2}^{L/2} f(x)*sin(2πnx/L) dx.
fn compute_bn(
    expr: &Expression,
    var_name: &str,
    n: usize,
    l: f64,
    half_l: f64,
    tolerance: f64,
) -> FourierSeriesResult<f64> {
    let sin_factor = build_sin_factor(var_name, n, l);
    let integrand = Expression::Binary(
        crate::ast::BinaryOp::Mul,
        Box::new(expr.clone()),
        Box::new(sin_factor),
    );
    let integral = numerical_integrate(&integrand, var_name, -half_l, half_l, tolerance)
        .map_err(|e| FourierSeriesError::IntegrationFailed(e.to_string()))?;
    Ok(2.0 / l * integral)
}

/// Build the expression cos(2πnx/L).
fn build_cos_factor(var_name: &str, n: usize, l: f64) -> Expression {
    let arg = build_harmonic_arg(var_name, n, l);
    Expression::Function(Function::Cos, vec![arg])
}

/// Build the expression sin(2πnx/L).
fn build_sin_factor(var_name: &str, n: usize, l: f64) -> Expression {
    let arg = build_harmonic_arg(var_name, n, l);
    Expression::Function(Function::Sin, vec![arg])
}

/// Build the argument 2πnx/L as an Expression.
fn build_harmonic_arg(var_name: &str, n: usize, l: f64) -> Expression {
    let x = Expression::Variable(Variable::new(var_name));
    let coeff = 2.0 * PI * (n as f64) / l;
    Expression::Binary(
        crate::ast::BinaryOp::Mul,
        Box::new(Expression::Float(coeff)),
        Box::new(x),
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ast::Variable;
    use crate::parser::parse_expression;

    const COEFF_TOL: f64 = 1e-4;

    #[test]
    fn test_cos_x_fourier_series() {
        // cos(x) on [-π, π] with period 2π should give a_1 = 1, all others ≈ 0
        let expr = parse_expression("cos(x)").unwrap();
        let x = Variable::new("x");
        let series = fourier_series(&expr, &x, 5, None).unwrap();

        assert_eq!(series.num_terms, 5);
        assert!(
            (series.a_coefficients[0]).abs() < COEFF_TOL,
            "a_0 should be 0"
        );
        assert!(
            (series.a_coefficients[1] - 1.0).abs() < COEFF_TOL,
            "a_1 should be 1, got {}",
            series.a_coefficients[1]
        );
        for n in 2..=5 {
            assert!(
                series.a_coefficients[n].abs() < COEFF_TOL,
                "a_{n} should be 0, got {}",
                series.a_coefficients[n]
            );
            assert!(
                series.b_coefficients[n].abs() < COEFF_TOL,
                "b_{n} should be 0, got {}",
                series.b_coefficients[n]
            );
        }
        assert!(
            (series.b_coefficients[1]).abs() < COEFF_TOL,
            "b_1 should be 0"
        );
    }

    #[test]
    fn test_sin_x_fourier_series() {
        // sin(x) on [-π, π] with period 2π should give b_1 = 1, all others ≈ 0
        let expr = parse_expression("sin(x)").unwrap();
        let x = Variable::new("x");
        let series = fourier_series(&expr, &x, 5, None).unwrap();

        assert!(
            (series.a_coefficients[0]).abs() < COEFF_TOL,
            "a_0 should be 0"
        );
        assert!(
            (series.b_coefficients[1] - 1.0).abs() < COEFF_TOL,
            "b_1 should be 1, got {}",
            series.b_coefficients[1]
        );
        assert!(
            (series.a_coefficients[1]).abs() < COEFF_TOL,
            "a_1 should be 0"
        );
        for n in 2..=5 {
            assert!(
                series.a_coefficients[n].abs() < COEFF_TOL,
                "a_{n} should be 0"
            );
            assert!(
                series.b_coefficients[n].abs() < COEFF_TOL,
                "b_{n} should be 0"
            );
        }
    }

    #[test]
    fn test_constant_fourier_series() {
        // f(x) = 1 on [-π, π] should give a_0 = 2, all others ≈ 0
        let expr = parse_expression("1").unwrap();
        let x = Variable::new("x");
        let series = fourier_series(&expr, &x, 3, None).unwrap();

        assert!(
            (series.a_coefficients[0] - 2.0).abs() < COEFF_TOL,
            "a_0 should be 2 (so a_0/2 = 1), got {}",
            series.a_coefficients[0]
        );
        for n in 1..=3 {
            assert!(
                series.a_coefficients[n].abs() < COEFF_TOL,
                "a_{n} should be 0"
            );
            assert!(
                series.b_coefficients[n].abs() < COEFF_TOL,
                "b_{n} should be 0"
            );
        }
    }

    #[test]
    fn test_cos2x_fourier_series() {
        // cos(2x) on [-π, π] should give a_2 = 1, all others ≈ 0
        let expr = parse_expression("cos(2 * x)").unwrap();
        let x = Variable::new("x");

        let series = fourier_series(&expr, &x, 5, None).unwrap();

        assert!(
            (series.a_coefficients[2] - 1.0).abs() < COEFF_TOL,
            "a_2 should be 1, got {}",
            series.a_coefficients[2]
        );
        assert!(
            (series.a_coefficients[1]).abs() < COEFF_TOL,
            "a_1 should be 0"
        );
        assert!(
            (series.a_coefficients[0]).abs() < COEFF_TOL,
            "a_0 should be 0"
        );
    }

    #[test]
    fn test_evaluate_recovers_cos_x() {
        // The series evaluation at several points should match cos(x)
        let expr = parse_expression("cos(x)").unwrap();
        let x = Variable::new("x");
        let series = fourier_series(&expr, &x, 3, None).unwrap();

        for &pt in &[0.0_f64, 0.5, 1.0, 1.5, -1.0] {
            let approx = series.evaluate(pt);
            let exact = pt.cos();
            assert!(
                (approx - exact).abs() < 1e-3,
                "At x={pt}, series={approx}, exact={exact}"
            );
        }
    }

    #[test]
    fn test_custom_period() {
        // f(x) = cos(π x) on period L=2 ([-1, 1]) should give a_1 = 1
        let expr = parse_expression("cos(3.14159265358979 * x)").unwrap();
        let x = Variable::new("x");
        let series = fourier_series(&expr, &x, 3, Some(2.0)).unwrap();

        assert_eq!(series.period, 2.0);
        assert!(
            (series.a_coefficients[1] - 1.0).abs() < 1e-3,
            "a_1 should be ~1, got {}",
            series.a_coefficients[1]
        );
    }

    #[test]
    fn test_zero_num_terms_error() {
        let expr = parse_expression("sin(x)").unwrap();
        let x = Variable::new("x");
        let result = fourier_series(&expr, &x, 0, None);
        assert!(matches!(result, Err(FourierSeriesError::InvalidNumTerms)));
    }

    #[test]
    fn test_invalid_period_error() {
        let expr = parse_expression("sin(x)").unwrap();
        let x = Variable::new("x");
        let result = fourier_series(&expr, &x, 3, Some(-1.0));
        assert!(matches!(result, Err(FourierSeriesError::InvalidPeriod)));
    }

    #[test]
    fn test_to_latex_produces_string() {
        let expr = parse_expression("cos(x)").unwrap();
        let x = Variable::new("x");
        let series = fourier_series(&expr, &x, 2, None).unwrap();
        let latex = series.to_latex();
        assert!(!latex.is_empty());
        assert!(latex.contains("cos"));
    }

    #[test]
    fn test_display_produces_string() {
        let expr = parse_expression("sin(x)").unwrap();
        let x = Variable::new("x");
        let series = fourier_series(&expr, &x, 2, None).unwrap();
        let s = format!("{series}");
        assert!(!s.is_empty());
    }
}