KiThe 0.3.0

A numerical suite for chemical kinetics and thermodynamics, combustion, heat and mass transfer,chemical engeneering. Work in progress. Advices and contributions will be appreciated
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
//! # Savitzky-Golay Filter Implementation
//!
//! This module implements the Savitzky-Golay filter, a digital filter that can be applied to
//! a set of digital data points for the purpose of smoothing the data and computing derivatives.
//!
//! ## Mathematical Background
//!
//! The Savitzky-Golay filter is based on local polynomial regression. For each point in the data,
//! a polynomial of degree `poly_order` is fitted to a window of `window_length` points centered
//! on that point. The filtered value is then the value of this polynomial at the center point.
//!
//! ### Mathematical Formulation
//!
//! Given a data series y[i], the filter computes:
//! ```text
//! y_filtered[i] = Σ(j=-n to n) c[j] * y[i+j]
//! ```
//! where:
//! - `c[j]` are the Savitzky-Golay coefficients
//! - `n = (window_length - 1) / 2`
//! - The coefficients are computed by solving a least-squares problem
//!
//! ### Coefficient Calculation
//!
//! The coefficients are found by solving the linear system:
//! ```text
//! A * c = y
//! ```
//! where:
//! - `A` is a Vandermonde matrix with A[i,j] = x[j]^i
//! - `x` are the relative positions within the window
//! - `y` is a unit vector with 1 at position `deriv` (for derivative calculation)
//!
//! ## Algorithmic Implementation
//!
//! 1. **Coefficient Computation**: Solve least-squares problem to find filter coefficients
//! 2. **Convolution**: Apply coefficients to data via convolution
//! 3. **Edge Handling**: Use polynomial fitting for boundary points where full window unavailable
//!
//! ## Applications in Chemical Kinetics
//!
//! - Smoothing noisy experimental data (TGA, DSC)
//! - Computing derivatives for kinetic analysis
//! - Preprocessing data for reaction rate calculations

use lstsq::lstsq;
use nalgebra::{DMatrix, DVector, SVD};

/// Computes factorial of a number recursively.
///
/// # Mathematical Background
/// n! = n × (n-1) × ... × 2 × 1, with 0! = 1 by definition
///
/// # Arguments
/// * `num` - Non-negative integer to compute factorial for
///
/// # Returns
/// Factorial of the input number
fn factorial(num: usize) -> usize {
    match num {
        0 => 1,
        1 => 1,
        _ => factorial(num - 1) * num,
    }
}

/// Computes Savitzky-Golay filter coefficients.
///
/// # Mathematical Background
/// Solves the least-squares problem A*c = y where:
/// - A is a Vandermonde matrix with A[i,j] = x[j]^i
/// - x are relative positions: [-n, -n+1, ..., 0, ..., n-1, n]
/// - y is unit vector with y[deriv] = deriv! (factorial of derivative order)
///
/// # Algorithmic Steps
/// 1. Create position vector centered at window middle
/// 2. Build Vandermonde matrix for polynomial basis
/// 3. Set up right-hand side for derivative calculation
/// 4. Solve linear system using least-squares
///
/// # Arguments
/// * `window` - Window length (must be odd and > poly_order)
/// * `poly_order` - Polynomial degree for local fitting
/// * `deriv` - Derivative order (0 for smoothing, 1 for first derivative, etc.)
///
/// # Returns
/// Vector of filter coefficients or error message
fn SG_coeffs(window: usize, poly_order: usize, deriv: usize) -> Result<DVector<f64>, String> {
    if poly_order >= window {
        return Err("poly_order must be less than window.".to_string());
    }

    let (halflen, rem) = (window / 2, window % 2);

    let pos = match rem {
        0 => halflen as f64 - 0.5,
        _ => halflen as f64,
    };

    if deriv > poly_order {
        return Ok(DVector::from_element(window, 0.0));
    }

    let x = DVector::from_fn(window, |i, _| pos - i as f64);
    let order = DVector::from_fn(poly_order + 1, |i, _| i);
    let mat_a = DMatrix::from_fn(poly_order + 1, window, |i, j| x[j].powf(order[i] as f64));

    let mut y = DVector::from_element(poly_order + 1, 0.0);
    y[deriv] = factorial(deriv) as f64;

    let epsilon = 1e-14;
    let results = lstsq(&mat_a, &y, epsilon)?;
    let solution = results.solution;

    return Ok(solution);
}

/// Computes derivative coefficients of a polynomial.
///
/// # Mathematical Background
/// For polynomial P(x) = a₀ + a₁x + a₂x² + ... + aₙxⁿ
/// The derivative P'(x) = a₁ + 2a₂x + 3a₃x² + ... + naₙx^(n-1)
///
/// # Arguments
/// * `coeffs` - Polynomial coefficients [a₀, a₁, a₂, ...]
///
/// # Returns
/// Derivative coefficients [a₁, 2a₂, 3a₃, ...]
fn poly_derivative(coeffs: &[f64]) -> Vec<f64> {
    coeffs[1..]
        .iter()
        .enumerate()
        .map(|(i, c)| c * (i + 1) as f64)
        .collect()
}

/// Evaluates polynomial at given points using Horner's method.
///
/// # Mathematical Background
/// Evaluates P(x) = a₀ + a₁x + a₂x² + ... + aₙxⁿ efficiently
///
/// # Arguments
/// * `poly` - Polynomial coefficients [a₀, a₁, a₂, ...]
/// * `values` - Points at which to evaluate polynomial
///
/// # Returns
/// Vector of polynomial values at input points
fn polyval(poly: &[f64], values: &[f64]) -> Vec<f64> {
    values
        .iter()
        .map(|v| {
            poly.iter()
                .enumerate()
                .fold(0.0, |y, (i, c)| y + c * v.powf(i as f64))
        })
        .collect()
}

/// Fits polynomial to edge region and interpolates boundary points.
///
/// # Algorithmic Purpose
/// Handles boundary conditions where full Savitzky-Golay window cannot be applied.
/// Uses polynomial fitting to extrapolate smoothed values at data edges.
///
/// # Arguments
/// * `x` - Original data vector
/// * `window_start` - Start index of fitting window
/// * `window_stop` - End index of fitting window
/// * `interp_start` - Start index of interpolation region
/// * `interp_stop` - End index of interpolation region
/// * `poly_order` - Polynomial degree for fitting
/// * `deriv` - Derivative order
/// * `y` - Mutable reference to output vector
fn fit_edge(
    x: &DVector<f64>,
    window_start: usize,
    window_stop: usize,
    interp_start: usize,
    interp_stop: usize,
    poly_order: usize,
    deriv: usize,
    y: &mut Vec<f64>,
) -> Result<(), String> {
    let x_edge: Vec<f64> = x.as_slice()[window_start..window_stop].to_vec();
    let y_edge: Vec<f64> = (0..window_stop - window_start).map(|i| i as f64).collect();
    let mut poly_coeffs = polyfit(&y_edge, &x_edge, poly_order)?;

    let mut deriv = deriv;
    while deriv > 0 {
        poly_coeffs = poly_derivative(&poly_coeffs);
        deriv -= 1;
    }

    let i: Vec<f64> = (0..interp_stop - interp_start)
        .map(|i| (interp_start - window_start + i) as f64)
        .collect();
    let values = polyval(&poly_coeffs, &i);
    y.splice(interp_start..interp_stop, values);
    Ok(())
}

/// Applies polynomial fitting to both edges of the data.
///
/// # Algorithmic Implementation
/// Calls fit_edge for both left and right boundaries to handle edge effects
/// in Savitzky-Golay filtering.
///
/// # Arguments
/// * `x` - Original data vector
/// * `window` - Window length
/// * `poly_order` - Polynomial degree
/// * `deriv` - Derivative order
/// * `y` - Mutable reference to output vector
fn fit_edges_polyfit(
    x: &DVector<f64>,
    window: usize,
    poly_order: usize,
    deriv: usize,
    y: &mut Vec<f64>,
) -> Result<(), String> {
    let halflen = window / 2;
    fit_edge(x, 0, window, 0, halflen, poly_order, deriv, y)?;
    let n = x.len();
    fit_edge(x, n - window, n, n - halflen, n, poly_order, deriv, y)?;

    Ok(())
}

/// Input parameters for Savitzky-Golay filter.
///
/// # Fields
/// * `data` - Input data slice to be filtered
/// * `window` - Window length (must be odd)
/// * `poly_order` - Polynomial order for local fitting
/// * `derivative` - Derivative order (0 for smoothing)
#[derive(Clone, Debug)]
pub struct SGInput<'a> {
    pub data: &'a [f64],
    pub window: usize,
    pub poly_order: usize,
    pub derivative: usize,
}

/// Applies Savitzky-Golay filter to input data.
///
/// # Mathematical Process
/// 1. Computes filter coefficients via least-squares
/// 2. Applies convolution with coefficients
/// 3. Handles boundary effects with polynomial fitting
///
/// # Algorithmic Steps
/// 1. Validate input parameters
/// 2. Compute Savitzky-Golay coefficients
/// 3. Perform convolution operation
/// 4. Trim convolution artifacts
/// 5. Apply edge correction using polynomial fitting
///
/// # Arguments
/// * `input` - SGInput struct containing data and parameters
///
/// # Returns
/// Filtered data vector or error message
///
/// # Example
/// ```rust
/// use savgol::{SGInput, SG_filter};
/// let data = [1.0, 2.0, 3.0, 4.0, 5.0];
/// let input = SGInput { data: &data, window: 3, poly_order: 1, derivative: 0 };
/// let filtered = SG_filter(&input).unwrap();
/// ```
pub fn SG_filter(input: &SGInput) -> Result<Vec<f64>, String> {
    if input.window > input.data.len() {
        return Err("window must be less than or equal to the size of the input data".to_string());
    }

    if input.window % 2 == 0 {
        // TODO: figure out how scipy implementation handles the convolution
        // in this case
        return Err("window must be odd".to_string());
    }

    let coeffs = SG_coeffs(input.window, input.poly_order, input.derivative)?;

    let x = DVector::from_vec(input.data.to_vec());

    let y = x.convolve_full(coeffs);

    // trim extra length gained during convolution to mimic scipy convolve1d
    // with mode="constant"
    let padding = y.len() - x.len();
    let padding = padding / 2;
    let y = y.as_slice();
    let mut y = y[padding..y.len().saturating_sub(padding)].to_vec();

    fit_edges_polyfit(&x, input.window, input.poly_order, input.derivative, &mut y)?;
    return Ok(y);
}

/// Fits polynomial to data using least-squares method.
///
/// # Mathematical Background
/// Solves the overdetermined system A*c = b where:
/// - A is Vandermonde matrix: A[i,j] = x[i]^j
/// - c are polynomial coefficients to find
/// - b are the y-values
///
/// Uses SVD decomposition for numerical stability.
///
/// # Arguments
/// * `x_values` - Independent variable values
/// * `y_values` - Dependent variable values
/// * `polynomial_degree` - Degree of polynomial to fit
///
/// # Returns
/// Polynomial coefficients [a₀, a₁, a₂, ...] or error
pub fn polyfit(
    x_values: &[f64],
    y_values: &[f64],
    polynomial_degree: usize,
) -> Result<Vec<f64>, &'static str> {
    let number_of_columns = polynomial_degree + 1;
    let number_of_rows = x_values.len();
    let mut a = DMatrix::zeros(number_of_rows, number_of_columns);

    for (row, &x) in x_values.iter().enumerate() {
        // First column is always 1
        a[(row, 0)] = 1.0f64;

        for col in 1..number_of_columns {
            a[(row, col)] = x.powf(col as f64);
        }
    }

    let b = DVector::from_row_slice(y_values);

    let decomp = SVD::new(a, true, true);

    match decomp.solve(&b, 1e-18f64) {
        Ok(mat) => Ok(mat.data.into()),
        Err(error) => Err(error),
    }
}

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

    #[test]
    fn test_coeffs_basic() {
        // Test basic coefficient calculation for window=5, poly_order=2, deriv=0
        let c = SG_coeffs(5, 2, 0).unwrap();
        let expected = DVector::from_vec(vec![
            -0.08571428571428572,
            0.34285714285714286,
            0.4857142857142857,
            0.34285714285714286,
            -0.08571428571428572,
        ]);
        assert_eq!(c.len(), expected.len());
        for (a, e) in c.iter().zip(expected.iter()) {
            assert_relative_eq!(a, e, max_relative = 1e-9);
        }
    }

    #[test]
    fn test_coeffs_derivative() {
        // Test first derivative coefficients
        let c = SG_coeffs(5, 2, 1).unwrap();
        let expected = DVector::from_vec(vec![0.2, 0.1, 0.0, -0.1, -0.2]);
        assert_eq!(c.len(), expected.len());
        for (a, e) in c.iter().zip(expected.iter()) {
            assert_relative_eq!(a, e, max_relative = 1e-9);
        }
    }

    #[test]
    fn test_coeffs_symmetry() {
        // Coefficients should be symmetric for even derivative orders
        let c = SG_coeffs(7, 3, 0).unwrap();
        let n = c.len();
        for i in 0..n / 2 {
            assert_relative_eq!(c[i], c[n - 1 - i], max_relative = 1e-12);
        }
    }

    #[test]
    fn test_coeffs_error_cases() {
        // Test error conditions
        assert!(SG_coeffs(3, 3, 0).is_err()); // poly_order >= window
        assert!(SG_coeffs(5, 2, 3).is_ok()); // deriv > poly_order should return zeros

        let c = SG_coeffs(5, 2, 3).unwrap();
        for coeff in c.iter() {
            assert_relative_eq!(*coeff, 0.0, max_relative = 1e-12);
        }
    }

    #[test]
    fn test_filter_on_known_series() {
        // Test filtering on known noisy data
        let input = [2.0, 2.0, 5.0, 2.0, 1.0, 0.0, 1.0, 4.0, 9.0];
        let y = SG_filter(&SGInput {
            data: &input,
            window: 5,
            poly_order: 2,
            derivative: 0,
        })
        .unwrap();
        let expected = vec![
            1.65714285714,
            3.02857143,
            3.54285714,
            2.85714286,
            0.65714286,
            0.17142857,
            1.0,
            4.05,
            7.97142857,
        ];
        assert_eq!(y.len(), expected.len());
        for (a, e) in y.iter().zip(expected.iter()) {
            assert_relative_eq!(a, e, max_relative = 1e-1);
        }
    }

    #[test]
    fn test_filter_derivative() {
        // Test first derivative calculation
        let input = [1.0, 4.0, 9.0, 16.0, 25.0]; // x^2 values
        let y = SG_filter(&SGInput {
            data: &input,
            window: 5,
            poly_order: 2,
            derivative: 1,
        })
        .unwrap();
        // First derivative of x^2 is 2x, so expect approximately [2, 4, 6, 8, 10]
        let expected = vec![2.0, 4.0, 6.0, 8.0, 10.0];
        assert_eq!(y.len(), expected.len());
        for (a, e) in y.iter().zip(expected.iter()) {
            assert_relative_eq!(a, e, max_relative = 0.1);
        }
    }

    #[test]
    fn test_filter_constant_data() {
        // Constant data should remain constant after smoothing
        let input = [5.0; 10];
        let y = SG_filter(&SGInput {
            data: &input,
            window: 5,
            poly_order: 2,
            derivative: 0,
        })
        .unwrap();
        for val in y.iter() {
            assert_relative_eq!(*val, 5.0, max_relative = 1e-10);
        }
    }

    #[test]
    fn test_filter_error_cases() {
        let input = [1.0, 2.0, 3.0];

        // Window too large
        assert!(
            SG_filter(&SGInput {
                data: &input,
                window: 5,
                poly_order: 2,
                derivative: 0
            })
            .is_err()
        );

        // Even window
        assert!(
            SG_filter(&SGInput {
                data: &input,
                window: 4,
                poly_order: 2,
                derivative: 0
            })
            .is_err()
        );
    }

    #[test]
    fn test_filter_linear_function() {
        // y = 3x - 2, should largely pass-through within interior; edges are fit by polyfit
        let data: Vec<f64> = (0..100).map(|i| (3 * i - 2) as f64).collect();
        let y = SG_filter(&SGInput {
            data: &data,
            window: 51,
            poly_order: 5,
            derivative: 0,
        })
        .unwrap();
        assert_eq!(y.len(), data.len());
        // Check some interior points equal the true line exactly for poly order >= 1
        for x in 30..70 {
            let expected = 3.0 * x as f64 - 2.0;
            assert_relative_eq!(y[x], expected, max_relative = 1e-9);
        }
    }

    #[test]
    fn test_polyfit_basic() {
        // Test polynomial fitting on known quadratic
        let x = vec![0.0, 1.0, 2.0, 3.0, 4.0];
        let y = vec![0.0, 1.0, 4.0, 9.0, 16.0]; // x^2
        let coeffs = polyfit(&x, &y, 2).unwrap();

        // Should get coefficients [0, 0, 1] for x^2
        assert_relative_eq!(coeffs[0].abs(), 0.0, epsilon = 1e-10);
        assert_relative_eq!(coeffs[1].abs(), 0.0, epsilon = 1e-10);
        assert_relative_eq!(coeffs[2].abs(), 1.0, epsilon = 1e-10);
    }

    #[test]
    fn test_factorial() {
        assert_eq!(factorial(0), 1);
        assert_eq!(factorial(1), 1);
        assert_eq!(factorial(2), 2);
        assert_eq!(factorial(3), 6);
        assert_eq!(factorial(4), 24);
        assert_eq!(factorial(5), 120);
    }

    #[test]
    fn test_poly_derivative() {
        // Test derivative of x^3 + 2x^2 + 3x + 4
        let coeffs = vec![4.0, 3.0, 2.0, 1.0]; // [constant, x, x^2, x^3]
        let deriv_coeffs = poly_derivative(&coeffs);
        let expected = vec![3.0, 4.0, 3.0]; // [3, 4x, 3x^2]

        assert_eq!(deriv_coeffs.len(), expected.len());
        for (a, e) in deriv_coeffs.iter().zip(expected.iter()) {
            assert_relative_eq!(a, e, max_relative = 1e-12);
        }
    }

    #[test]
    fn test_polyval() {
        // Test polynomial evaluation
        let coeffs = vec![1.0, 2.0, 3.0]; // 1 + 2x + 3x^2
        let x_vals = vec![0.0, 1.0, 2.0];
        let y_vals = polyval(&coeffs, &x_vals);
        let expected = vec![1.0, 6.0, 17.0]; // [1, 1+2+3, 1+4+12]

        assert_eq!(y_vals.len(), expected.len());
        for (a, e) in y_vals.iter().zip(expected.iter()) {
            assert_relative_eq!(a, e, max_relative = 1e-12);
        }
    }
}