项目作者: espdev

项目描述 :
Cubic spline approximation (smoothing) in Rust
高级语言: Rust
项目地址: git://github.com/espdev/csaps-rs.git
创建时间: 2020-02-24T22:43:37Z
项目社区:https://github.com/espdev/csaps-rs

开源协议:MIT License

下载



csaps


Coverage status
crates.io
Docs
License


Cubic spline approximation (smoothing) algorithm written in Rust.

csaps is a crate for univariate, multivariate and n-dimensional grid data approximation using cubic smoothing splines.
The package can be useful in practical engineering tasks for data approximation and smoothing.

Usage

Univariate data auto-smoothing

  1. use ndarray::{array, Array1};
  2. use csaps::CubicSmoothingSpline;
  3. fn main() {
  4. let x = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0];
  5. let y = vec![2.3, 3.5, 3.3, 1.2, 4.5, 6.2, 5.6, 7.2, 1.5];
  6. let spline = CubicSmoothingSpline::new(&x, &y)
  7. .make()
  8. .unwrap();
  9. let xi = Array1::linspace(1., 9., 30);
  10. let yi = spline.evaluate(&xi).unwrap();
  11. println!("{}", xi);
  12. println!("{}", yi);
  13. }

Multivariate data smoothing with weights and specified smoothing parameter

  1. use ndarray::{array, Array1};
  2. use csaps::CubicSmoothingSpline;
  3. fn main() {
  4. let x = array![1., 2., 3., 4.];
  5. let y = array![[1., 2., 3., 4.],
  6. [5., 6., 7., 8.]];
  7. let w = array![1., 0.7, 0.5, 1.];
  8. let spline = CubicSmoothingSpline::new(&x, &y)
  9. .with_weights(&w)
  10. .with_smooth(0.8)
  11. .make()
  12. .unwrap();
  13. let xi = Array1::linspace(1., 4., 10);
  14. let yi = spline.evaluate(&xi).unwrap();
  15. println!("{}", xi);
  16. println!("{}", yi);
  17. }

2-d grid (surface) data smoothing

  1. use ndarray::array;
  2. use csaps::GridCubicSmoothingSpline;
  3. fn main() {
  4. let x0 = array![1.0, 2.0, 3.0, 4.0];
  5. let x1 = array![1.0, 2.0, 3.0, 4.0];
  6. let x = vec![x0.view(), x1.view()];
  7. let y = array![
  8. [0.5, 1.2, 3.4, 2.5],
  9. [1.5, 2.2, 4.4, 3.5],
  10. [2.5, 3.2, 5.4, 4.5],
  11. [3.5, 4.2, 6.4, 5.5],
  12. ];
  13. let yi = GridCubicSmoothingSpline::new(&x, &y)
  14. .with_smooth_fill(0.5)
  15. .make().unwrap()
  16. .evaluate(&x).unwrap();
  17. println!("xi: {:?}", xi);
  18. println!("yi: {}", yi);
  19. }

Performance Issues

Currently, the performance of computation of smoothing splines might be very low for a large data.

The algorithm of sparse matrices mutliplication in sprs crate is not optimized for large diagonal
matrices which causes a poor performance of computation of smoothing splines.
See issue for details.

Algorithms and implementations

The crate implementation is based on ndarray and
sprs crates and has been inspired by Fortran routine SMOOTH from PGS
(originally written by Carl de Boor).

The implementation of the algorithm in other languages:

References

  • C. de Boor, A Practical Guide to Splines, Springer-Verlag, 1978.

License

MIT