<commit_before>use linalg::vector::Vector;

pub trait Kernel {
    fn kernel(&self, x1: &[f64], x2: &[f64]) -> f64;
}

pub struct SquaredExp {
    ls: f64,
    ampl: f64,
}

impl SquaredExp {
    pub fn new(ls: f64, ampl: f64) -> SquaredExp {
        SquaredExp {
            ls: ls,
            ampl: ampl,
        }
    }
}

impl Default for SquaredExp {
    fn default() -> SquaredExp {
        SquaredExp {
            ls: 1f64,
            ampl: 1f64,
        }
    }
}

impl Kernel for SquaredExp {
    fn kernel(&self, x1: &[f64], x2: &[f64]) -> f64 {
        assert_eq!(x1.len(), x2.len());

        let v1 = Vector::new(x1.to_vec());
        let v2 = Vector::new(x2.to_vec());

        let x = -(&v1 - &v2).dot(&(v1 - v2)) / (2f64 * self.ls * self.ls);
        (self.ampl * x.exp())
    }
}<commit_msg>Docs and exampls<commit_after>use linalg::vector::Vector;

/// The Kernel trait
///
/// Requires a function mapping two vectors to a scalar.
pub trait Kernel {
    fn kernel(&self, x1: &[f64], x2: &[f64]) -> f64;
}

/// Squared exponential kernel
///
/// Equivalently a gaussian function.
///
/// The kernel function is given by:
///
/// f(x1,x2) = A _exp_(-||x1-x2||^2 / 2(l^2))
///
/// Where A is the amplitude and l the length scale.
pub struct SquaredExp {
    ls: f64,
    ampl: f64,
}

impl SquaredExp {

    /// Construct a new squared exponential kernel.
    ///
    /// # Examples
    ///
    /// ```
    /// use rusty_machine::learning::toolkit::SquaredExp;
    ///
    /// let ker = SquaredExp::new(2f64, 1f64);
    /// ```
    pub fn new(ls: f64, ampl: f64) -> SquaredExp {
        SquaredExp {
            ls: ls,
            ampl: ampl,
        }
    }
}

impl Default for SquaredExp {

    /// Constructs the default Squared Exp kernel.
    ///
    /// The default settings are:
    /// - length scale = 1
    /// - amplitude = 1
    ///
    /// # Examples
    ///
    /// ```
    /// use rusty_machine::learning::toolkit::SquaredExp;
    ///
    /// let ker = SquaredExp::default();
    /// ```
    fn default() -> SquaredExp {
        SquaredExp {
            ls: 1f64,
            ampl: 1f64,
        }
    }
}

impl Kernel for SquaredExp {
    /// The squared exponential kernel function.
    fn kernel(&self, x1: &[f64], x2: &[f64]) -> f64 {
        assert_eq!(x1.len(), x2.len());

        let v1 = Vector::new(x1.to_vec());
        let v2 = Vector::new(x2.to_vec());

        let x = -(&v1 - &v2).dot(&(v1 - v2)) / (2f64 * self.ls * self.ls);
        (self.ampl * x.exp())
    }
}
{'fraction_non_alphanumeric': 0.181058495821727, 'fraction_numerical': 0.04019100676482292, 'mean_word_length': 1.756578947368421, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '815110', 'n_tokens_mistral': 993, 'n_tokens_neox': 916, 'n_words': 228}