[−][src]Trait gramit::vec::Vector
Generic vector operations.
This trait defines vectors very generally; in fact, it does not even constrain implementors to be true vector spaces. In particular, it requires that a scalar type be associated with the implementing vector type, and that scalar multiplication and vector addition are defined, but does not require that the scalar type is a mathematical field.
In most cases, however, implementors will want to define common mathematical operations, at least for scalar multiplication and vector addition.
Associated Types
type Scalar
The scalar type over which this vector type is defined.
This should ideally be a field in the mathematical sense. The real numbers, rational numbers, and complex numbers are common examples.
Notably, the integers are not a field, since only 1 and -1 have multiplicative inverses. Hence, vectors with integer components do not constitute a true vector field. Defining such a vector may be useful for practical purposes, however, and so this type is unconstrained.
Associated Constants
const DIMS: usize
The dimension of this vector type.
For most vector types, this should be the number of components in a vector.
Required methods
fn length(&self) -> Self::Scalar
The length (norm) of this vector.
This function should compute the most reasonable norm for the vector type, ideally the norm
induced by the inner product defined by the dot
function.
Most types will implement a Euclidean space, (e.g. two, three, and four-dimensional real vector spaces,) and so should define this function to compute the Euclidean length (2-norm) of the vector.
More obscure vector types may make better use of different norms for this function (e.g. a vector type with integer components may wish to use Manhattan distance). In such a case, the implementation should be sure to document exactly which norm is computed by this function.
fn dot(&self, other: &Self) -> Self::Scalar
The dot (scalar) product of this vector with another.
Ideally this is a true inner product, whose induced norm is implemented by the length
function.
fn ones() -> Self
Returns a vector of all 1's.
fn zeros() -> Self
Returns a vector of all 0's.
fn unit(&self) -> Self
Returns the normalized (unit-length) version of this vector.
fn scalar_mul(&self, s: &Self::Scalar) -> Self
The scalar multiplication operation.
fn vector_add(&self, v: &Self) -> Self
The vector addition operation.