2018-10-22 04:11:27 +08:00
|
|
|
use na::{DefaultAllocator, Real};
|
2018-09-21 01:54:12 +08:00
|
|
|
|
2018-09-23 20:48:45 +08:00
|
|
|
use aliases::TVec;
|
2018-10-22 04:11:27 +08:00
|
|
|
use traits::{Alloc, Dimension};
|
2018-09-21 01:54:12 +08:00
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// The squared distance between two points.
|
2018-09-26 17:39:30 +08:00
|
|
|
///
|
|
|
|
/// # See also:
|
|
|
|
///
|
|
|
|
/// * [`distance`](fn.distance.html)
|
2018-09-23 20:48:45 +08:00
|
|
|
pub fn distance2<N: Real, D: Dimension>(p0: &TVec<N, D>, p1: &TVec<N, D>) -> N
|
2018-10-22 13:00:10 +08:00
|
|
|
where DefaultAllocator: Alloc<N, D> {
|
2018-09-21 01:54:12 +08:00
|
|
|
(p1 - p0).norm_squared()
|
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// The l1-norm of `x - y`.
|
2018-10-08 22:56:58 +08:00
|
|
|
///
|
|
|
|
/// # See also:
|
|
|
|
///
|
|
|
|
/// * [`l1_norm`](fn.l1_norm.html)
|
|
|
|
/// * [`l2_distance`](fn.l2_distance.html)
|
|
|
|
/// * [`l2_norm`](fn.l2_norm.html)
|
2018-09-23 20:48:45 +08:00
|
|
|
pub fn l1_distance<N: Real, D: Dimension>(x: &TVec<N, D>, y: &TVec<N, D>) -> N
|
2018-10-22 13:00:10 +08:00
|
|
|
where DefaultAllocator: Alloc<N, D> {
|
2018-09-23 21:59:24 +08:00
|
|
|
l1_norm(&(y - x))
|
2018-09-21 01:54:12 +08:00
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// The l1-norm of `v`.
|
2018-10-08 22:56:58 +08:00
|
|
|
///
|
|
|
|
/// This is also known as the "Manhattan distance" or "taxicab distance" and
|
|
|
|
/// corresponds to the sum of the absolute values of the components of `v`.
|
|
|
|
///
|
|
|
|
/// # See also:
|
|
|
|
///
|
|
|
|
/// * [`l1_distance`](fn.l1_distance.html)
|
|
|
|
/// * [`l2_distance`](fn.l2_distance.html)
|
|
|
|
/// * [`l2_norm`](fn.l2_norm.html)
|
2018-09-23 20:48:45 +08:00
|
|
|
pub fn l1_norm<N: Real, D: Dimension>(v: &TVec<N, D>) -> N
|
2018-10-22 13:00:10 +08:00
|
|
|
where DefaultAllocator: Alloc<N, D> {
|
2018-09-21 01:54:12 +08:00
|
|
|
::comp_add(&v.abs())
|
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// The l2-norm of `x - y`.
|
2018-10-08 22:56:58 +08:00
|
|
|
///
|
|
|
|
/// This is the same value as returned by [`length2`](fn.length2.html) and
|
|
|
|
/// [`magnitude2`](fn.magnitude2.html).
|
|
|
|
///
|
|
|
|
/// # See also:
|
|
|
|
///
|
|
|
|
/// * [`l1_distance`](fn.l1_distance.html)
|
|
|
|
/// * [`l1_norm`](fn.l1_norm.html)
|
|
|
|
/// * [`l2_norm`](fn.l2_norm.html)
|
|
|
|
/// * [`length`](fn.length.html)
|
|
|
|
/// * [`length2`](fn.length2.html)
|
|
|
|
/// * [`magnitude`](fn.magnitude.html)
|
|
|
|
/// * [`magnitude2`](fn.magnitude2.html)
|
2018-09-23 20:48:45 +08:00
|
|
|
pub fn l2_distance<N: Real, D: Dimension>(x: &TVec<N, D>, y: &TVec<N, D>) -> N
|
2018-10-22 13:00:10 +08:00
|
|
|
where DefaultAllocator: Alloc<N, D> {
|
2018-09-21 01:54:12 +08:00
|
|
|
l2_norm(&(y - x))
|
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// The l2-norm of `v`.
|
2018-10-08 22:56:58 +08:00
|
|
|
///
|
|
|
|
/// This is also known as the Euclidean norm.
|
|
|
|
///
|
|
|
|
/// This is the same value as returned by [`length`](fn.length.html) and
|
|
|
|
/// [`magnitude`](fn.magnitude.html).
|
|
|
|
///
|
|
|
|
/// # See also:
|
|
|
|
///
|
|
|
|
/// * [`l1_distance`](fn.l1_distance.html)
|
|
|
|
/// * [`l1_norm`](fn.l1_norm.html)
|
|
|
|
/// * [`l2_distance`](fn.l2_distance.html)
|
|
|
|
/// * [`length`](fn.length.html)
|
|
|
|
/// * [`length2`](fn.length2.html)
|
|
|
|
/// * [`magnitude`](fn.magnitude.html)
|
|
|
|
/// * [`magnitude2`](fn.magnitude2.html)
|
2018-09-23 20:48:45 +08:00
|
|
|
pub fn l2_norm<N: Real, D: Dimension>(x: &TVec<N, D>) -> N
|
2018-10-22 13:00:10 +08:00
|
|
|
where DefaultAllocator: Alloc<N, D> {
|
2018-09-21 01:54:12 +08:00
|
|
|
x.norm()
|
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// The squared magnitude of `x`.
|
2018-09-26 17:39:30 +08:00
|
|
|
///
|
|
|
|
/// A synonym for [`magnitude2`](fn.magnitude2.html).
|
|
|
|
///
|
|
|
|
/// # See also:
|
|
|
|
///
|
|
|
|
/// * [`distance`](fn.distance.html)
|
|
|
|
/// * [`distance2`](fn.distance2.html)
|
|
|
|
/// * [`length`](fn.length.html)
|
|
|
|
/// * [`magnitude`](fn.magnitude.html)
|
|
|
|
/// * [`magnitude2`](fn.magnitude2.html)
|
2018-09-23 20:48:45 +08:00
|
|
|
pub fn length2<N: Real, D: Dimension>(x: &TVec<N, D>) -> N
|
2018-10-22 13:00:10 +08:00
|
|
|
where DefaultAllocator: Alloc<N, D> {
|
2018-09-21 01:54:12 +08:00
|
|
|
x.norm_squared()
|
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// The squared magnitude of `x`.
|
2018-09-26 17:39:30 +08:00
|
|
|
///
|
|
|
|
/// A wrapper around [`nalgebra::norm_squared`](../nalgebra/fn.norm_squared.html).
|
|
|
|
///
|
|
|
|
/// # See also:
|
|
|
|
///
|
|
|
|
/// * [`distance`](fn.distance.html)
|
|
|
|
/// * [`distance2`](fn.distance2.html)
|
|
|
|
/// * [`length2`](fn.length2.html)
|
|
|
|
/// * [`magnitude`](fn.magnitude.html)
|
|
|
|
/// * [`nalgebra::norm_squared`](../nalgebra/fn.norm_squared.html)
|
2018-09-23 20:48:45 +08:00
|
|
|
pub fn magnitude2<N: Real, D: Dimension>(x: &TVec<N, D>) -> N
|
2018-10-22 13:00:10 +08:00
|
|
|
where DefaultAllocator: Alloc<N, D> {
|
2018-09-22 19:18:59 +08:00
|
|
|
x.norm_squared()
|
|
|
|
}
|
|
|
|
|
2018-09-23 20:48:45 +08:00
|
|
|
//pub fn lxNorm<N: Real, D: Dimension>(x: &TVec<N, D>, y: &TVec<N, D>, unsigned int Depth) -> N
|
2018-09-21 01:54:12 +08:00
|
|
|
// where DefaultAllocator: Alloc<N, D> {
|
|
|
|
// unimplemented!()
|
|
|
|
//}
|
|
|
|
//
|
2018-09-23 20:48:45 +08:00
|
|
|
//pub fn lxNorm<N: Real, D: Dimension>(x: &TVec<N, D>, unsigned int Depth) -> N
|
2018-09-21 01:54:12 +08:00
|
|
|
// where DefaultAllocator: Alloc<N, D> {
|
|
|
|
// unimplemented!()
|
|
|
|
//}
|