[][src]Trait rstats::Vectors

pub trait Vectors {
    fn dotp(&self, v: &[f64]) -> f64;
fn vsub(&self, v: &[f64]) -> Vec<f64>;
fn vadd(&self, v: &[f64]) -> Vec<f64>;
fn vmag(&self) -> f64;
fn vdist(&self, v: &[f64]) -> f64;
fn smult(&self, s: f64) -> Vec<f64>;
fn vunit(&self) -> Vec<f64>;
fn arcentroid(&self, d: usize) -> Vec<f64>;
fn medoid(&self, d: usize) -> Result<(f64, usize)>;
fn distsum(&self, d: usize, v: &[f64]) -> f64;
fn nextpoint(
        &self,
        d: usize,
        eps: f64,
        v: &[f64]
    ) -> Result<(bool, Vec<f64>)>;
fn betterpoint(
        &self,
        d: usize,
        eps: f64,
        v: &[f64]
    ) -> Result<(bool, Vec<f64>)>;
fn firstpoint(&self, d: usize, indx: usize, v: &[f64]) -> Result<Vec<f64>>;
fn gmedian(&self, d: usize, eps: f64) -> Result<(f64, Vec<f64>)>;
fn nmedian(&self, d: usize, eps: f64) -> Result<(f64, Vec<f64>)>; }

Implementing basic vector algebra and safe geometric median.

Required methods

fn dotp(&self, v: &[f64]) -> f64

fn vsub(&self, v: &[f64]) -> Vec<f64>

fn vadd(&self, v: &[f64]) -> Vec<f64>

fn vmag(&self) -> f64

fn vdist(&self, v: &[f64]) -> f64

fn smult(&self, s: f64) -> Vec<f64>

fn vunit(&self) -> Vec<f64>

fn arcentroid(&self, d: usize) -> Vec<f64>

fn medoid(&self, d: usize) -> Result<(f64, usize)>

fn distsum(&self, d: usize, v: &[f64]) -> f64

fn nextpoint(&self, d: usize, eps: f64, v: &[f64]) -> Result<(bool, Vec<f64>)>

fn betterpoint(&self, d: usize, eps: f64, v: &[f64]) -> Result<(bool, Vec<f64>)>

fn firstpoint(&self, d: usize, indx: usize, v: &[f64]) -> Result<Vec<f64>>

fn gmedian(&self, d: usize, eps: f64) -> Result<(f64, Vec<f64>)>

fn nmedian(&self, d: usize, eps: f64) -> Result<(f64, Vec<f64>)>

Loading content...

Implementations on Foreign Types

impl<'_> Vectors for &'_ [f64][src]

fn smult(&self, s: f64) -> Vec<f64>[src]

Scalar multiplication of a vector, creates new vec

fn dotp(&self, v: &[f64]) -> f64[src]

Scalar product of two f64 slices.
Must be of the same length - no error checking for speed

fn vsub(&self, v: &[f64]) -> Vec<f64>[src]

Vector subtraction, creates a new Vec result

fn vadd(&self, v: &[f64]) -> Vec<f64>[src]

Vector addition, creates a new Vec result

fn vdist(&self, v: &[f64]) -> f64[src]

Euclidian distance between two n dimensional points (vectors).
Slightly faster than vsub followed by vmag, as both are done in one loop

fn vmag(&self) -> f64[src]

Vector magnitude

fn vunit(&self) -> Vec<f64>[src]

Unit vector - creates a new one

fn arcentroid(&self, d: usize) -> Vec<f64>[src]

Centroid = multidimensional arithmetic mean

Example

use rstats::{Vectors,genvec};
let mut pts = genvec(15,15,255,30);
let centre = pts.as_slice().arcentroid(15);
let dist = pts.as_slice().distsum(15,&centre);
assert_eq!(dist, 4.14556218326653_f64);

fn medoid(&self, d: usize) -> Result<(f64, usize)>[src]

Medoid is a point in n-dimensional set of points with the least sum of distances to all others.
This method returns an index to the start of medoid within a flat vector of d-dimensional points.
d is the number of dimensions = length of the slices. Set of points (slices) is held as one flat buff:&[f64].
This is faster than vec of vecs but users have to handle the indices.
Note: medoid computes each distance twice but it is probably faster than memoizing and looking them up,
unless the dimensionality is somewhat large.

Example

use rstats::{Vectors,genvec};
let mut pts = genvec(15,15,255,30);
let (dist,indx) = pts.as_slice().medoid(15).unwrap();
assert_eq!(dist,4.812334638782327_f64);

fn distsum(&self, d: usize, v: &[f64]) -> f64[src]

The sum of distances of all points contained in &self to given point v.
v which minimises this objective function is the Geometric Median.

fn betterpoint(&self, d: usize, eps: f64, v: &[f64]) -> Result<(bool, Vec<f64>)>[src]

Weiszfeld's formula for one iteration step in finding geometric median.
It has known problems with choosing the starting point and may fail to converge.
However, nmedian below solves those problems.

fn firstpoint(&self, d: usize, indx: usize, v: &[f64]) -> Result<Vec<f64>>[src]

My innovative step that guarantees convergence.

fn gmedian(&self, d: usize, eps: f64) -> Result<(f64, Vec<f64>)>[src]

Geometric Median is the point that minimises the sum of distances to a given set of points. This is the original Weiszfeld's algorithm for comparison. It has problems with convergence and/or division by zero when the estimate runs too close to one of the existing points in the set. See test/tests.rs, where test gmedian panics, whereas nmedian finds the correct result.

Example

use rstats::{Vectors,genvec};
let mut pts = genvec(15,15,255,30);
let (ds,gm) = pts.as_slice().gmedian(15, 1e-5).unwrap();
assert_eq!(ds,4.126465898732421_f64);

fn nmedian(&self, d: usize, eps: f64) -> Result<(f64, Vec<f64>)>[src]

Geometric Median is the point that minimises the sum of distances to a given set of points.
This is an improved algorithm. It dodges any points in the set which cause problems to Weiszfeld. It runs slightly faster on easy datasets and, most importantly, has at least the same convergence even on the difficult cases. Eps controls the desired accuracy.

Example

use rstats::{Vectors,genvec};
let mut pts = genvec(15,15,255,30);
let (ds,gm) = pts.as_slice().nmedian(15, 1e-5).unwrap();
assert_eq!(ds,4.126465898745778_f64);
Loading content...

Implementors

Loading content...