Giter VIP home page Giter VIP logo

csaps-rs's Introduction

csaps

Build status 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

use ndarray::{array, Array1};
use csaps::CubicSmoothingSpline;


fn main() {
    let x = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0];
    let y = vec![2.3, 3.5, 3.3, 1.2, 4.5, 6.2, 5.6, 7.2, 1.5];
    
    let spline = CubicSmoothingSpline::new(&x, &y)
        .make()
        .unwrap();
    
    let xi = Array1::linspace(1., 9., 30);
    let yi = spline.evaluate(&xi).unwrap();
    
    println!("{}", xi);
    println!("{}", yi);
}

Multivariate data smoothing with weights and specified smoothing parameter

use ndarray::{array, Array1};
use csaps::CubicSmoothingSpline;


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

2-d grid (surface) data smoothing

use ndarray::array;
use csaps::GridCubicSmoothingSpline;


fn main() {
    let x0 = array![1.0, 2.0, 3.0, 4.0];
    let x1 = array![1.0, 2.0, 3.0, 4.0];
    let x = vec![x0.view(), x1.view()];
    
    let y = array![
        [0.5, 1.2, 3.4, 2.5],
        [1.5, 2.2, 4.4, 3.5],
        [2.5, 3.2, 5.4, 4.5],
        [3.5, 4.2, 6.4, 5.5],
    ];
    
    let yi = GridCubicSmoothingSpline::new(&x, &y)
     .with_smooth_fill(0.5)
     .make().unwrap()
     .evaluate(&x).unwrap();
    
    println!("xi: {:?}", xi);
    println!("yi: {}", yi);
}

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

csaps-rs's People

Contributors

espdev avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.