Struct matrix::vector::Vector

source ·
pub struct Vector<K: Scalar> {
    pub data: Vec<K>,
}
Expand description

A struct representing a mathematical vector that is generic over type K.

The type K must implement the Scalar trait, which ensures that it supports basic arithmetic operations like addition, subtraction, multiplication, and division.

Fields§

§data: Vec<K>

Implementations§

source§

impl<K: Scalar> Vector<K>

source

pub fn new(data: Vec<K>) -> Self

Creates a new Vector<K> from a vector of K values.

§Parameters
  • data: A Vec<K> representing the elements of the vector.
§Returns
  • A new Vector<K> initialized with the provided data.
§Panics
  • This function does not panic.
§Example
use matrix::Vector;

let vec = Vector::new(vec![1.0, 2.0, 3.0]);
source

pub fn size(&self) -> usize

Returns the number of elements in the Vector.

§Returns
  • The size of the Vector<K>.
§Panics
  • This function does not panic.
§Example
use matrix::Vector;

let vec = Vector::new(vec![1.0, 2.0, 3.0]);
assert_eq!(vec.size(), 3);
source

pub fn print(&self)

Prints the contents of the Vector to standard output.

§Returns
  • No return value.
§Panics
  • This function does not panic.
§Example
use matrix::Vector;

let vec = Vector::new(vec![1.0, 2.0, 3.0]);
vec.print(); // Outputs: [1.0, 2.0, 3.0]
source

pub fn reshape(&self, rows: usize, cols: usize) -> Matrix<K>

Reshapes the Vector into a matrix with specified dimensions.

§Parameters
  • rows: The number of rows in the resulting matrix.
  • cols: The number of columns in the resulting matrix.
§Returns
  • A Matrix<K> derived from the Vector<K>.
§Panics
  • Panics if the size of the Vector does not match the requested dimensions (rows * cols must equal self.size()).
§Example
use matrix::Vector;

let vec = Vector::new(vec![1.0, 2.0, 3.0, 4.0]);
let mat = vec.reshape(2, 2);
assert_eq!(mat.size(), (2, 2));
source

pub fn linear_combination(u: &[Vector<K>], coefs: &[K]) -> Vector<K>

Computes the linear combination of a set of vectors using Fused Multiply-Add (FMA).

§Parameters
  • u: A slice of Vectors of type K to be combined.
  • coefs: A slice of coefficients of type K, corresponding to the vectors in u.
§Returns
  • A Vector<K> containing the result of the linear combination.
§Panics
  • Panics if the length of u and coefs do not match, or if the vectors in u are not the same size.
§Example
use matrix::Vector;

let u = vec![Vector::from([1.0, 2.0]), Vector::from([3.0, 4.0])];
let coefs = vec![0.5, 0.5];
let result = Vector::linear_combination(&u, &coefs);
source

pub fn dot(&self, v: &Vector<K>) -> K

Computes the dot product with another Vector<K>.

§Parameters
  • v: A reference to another Vector<K>.
§Returns
  • The result of the dot product of type K.
§Panics
  • Panics if the vectors are not of the same size.
§Example
use matrix::Vector;

let vec1 = Vector::new(vec![42.0, 4.2]);
let vec2 = Vector::new(vec![-42.0, 4.2]);
assert_eq!(vec1.dot(&vec2), -1746.36);
source

pub fn norm_1(&self) -> f32

Computes the 1-norm (Manhattan norm) of the vector.

Formula

Manhattan norm

§Returns
  • The computed 1-norm as type K.
§Panics
  • This function does not panic.
§Example
use matrix::Vector;

let vec = Vector::new(vec![3, -4, 5]);
assert_eq!(vec.norm_1(), 12.0);
source

pub fn norm(&self) -> f32

Computes the 2-norm (Euclidean norm) of the vector.

Formula

Euclidean norm

§Returns
  • The computed 2-norm as an f32.
§Panics
  • This function does not panic.
§Example
use matrix::Vector;

let vec = Vector::new(vec![3.0, -4.0, 5.0]);
assert_eq!(vec.norm(), (3.0_f32.powi(2) + 4.0_f32.powi(2) + 5.0_f32.powi(2)).sqrt());
source

pub fn norm_inf(&self) -> f32

Computes the ∞-norm (supremum or maximum norm) of the vector.

Formula

Supremum/maximum norm

§Returns
  • The computed ∞-norm as an f32.
§Panics
  • This function does not panic.
§Example
use matrix::Vector;

let vec = Vector::new(vec![3.0, -4.0, 5.0]);
assert_eq!(vec.norm_inf(), 5.0);
source

pub fn add(&mut self, v: &Vector<K>)

Adds another Vector<K> to the calling Vector<K>.

§Parameters
  • v: A reference to the other Vector<K> to add.
§Returns
  • No return value; the calling vector is modified in place.
§Panics
  • Panics if the vectors are not of the same size.
§Example
use matrix::Vector;

let mut vec1 = Vector::new(vec![42.0, 4.2]);
let vec2 = Vector::new(vec![-42.0, 4.2]);
vec1.add(&vec2);
assert_eq!(vec1.data, vec![0.0, 8.4]);
source

pub fn sub(&mut self, v: &Vector<K>)

Subtracts another Vector<K> from the calling Vector<K>.

§Parameters
  • v: A reference to the other Vector<K> to subtract.
§Returns
  • No return value; the calling vector is modified in place.
§Panics
  • Panics if the vectors are not of the same size.
§Example
use matrix::Vector;

let mut vec1 = Vector::new(vec![42.0, 4.2]);
let vec2 = Vector::new(vec![-42.0, 4.2]);
vec1.sub(&vec2);
assert_eq!(vec1.data, vec![84.0, 0.0]);
source

pub fn scl(&mut self, a: K)

Scales the calling Vector<K> by a factor a.

§Parameters
  • a: The scaling factor to multiply each element of the vector.
§Returns
  • No return value; the calling vector is modified in place.
§Example
use matrix::Vector;

let mut vec1 = Vector::new(vec![42.0, 4.2]);
vec1.scl(2.0);
assert_eq!(vec1.data, vec![84.0, 8.4]);
source

pub fn angle_cos(u: &Vector<K>, v: &Vector<K>) -> f32

Calculates the cosine of the angle between two Vectors, u and v.

This function computes the cosine of the angle θ using the formula:

cos(θ) = (u ⋅ v) / (‖u‖ * ‖v‖)

where u ⋅ v is the dot product, and ‖u‖ and ‖v‖ are the magnitudes of the vectors.

The returned cosine value is between -1.0 and 1.0:

  • 1.0 indicates the vectors are parallel and pointing in the same direction.
  • -1.0 indicates they are parallel but pointing in opposite directions.
  • 0.0 indicates the vectors are perpendicular.
§Parameters
  • u - The first Vector.
  • v - The second Vector.
§Returns

A f32 value between -1.0 and 1.0 representing the cosine of the angle between u and v.

§Panics

This function will panic if the vectors are not the same size or if either has zero magnitude.

§Example
use matrix::Vector;

let vec1 = Vector::new(vec![0, 1]);
let vec2 = Vector::new(vec![0, -1]);
assert_eq!(Vector::angle_cos(&vec1, &vec2), -1.0);
source

pub fn cross_product(u: &Vector<K>, v: &Vector<K>) -> Vector<K>

Computes the cross product of two 3-dimensional Vectors, u and v.

§Description

This function calculates the cross product of two 3D vectors using the formula:

u × v =
[ u_y * v_z - u_z * v_y,
  u_z * v_x - u_x * v_z,
  u_x * v_y - u_y * v_x ]

where:

  • u_x, u_y, and u_z are the components of vector u,
  • v_x, v_y, and v_z are the components of vector v.

The cross product of two vectors results in a third vector that is perpendicular to both u and v, following the right-hand rule. The resulting vector lies in the plane perpendicular to both input vectors.

§Parameters
  • u - The first 3-dimensional Vector.
  • v - The second 3-dimensional Vector.
§Returns

Returns a Vector<K> that represents the cross product of u and v. The result is perpendicular to both u and v.

§Panics

This function will panic if either u or v is not of size 3, as the cross product is only defined in 3D space.

§Example
use matrix::Vector;

let vec1 = Vector::new(vec![1.0, 0.0, 0.0]);
let vec2 = Vector::new(vec![0.0, 1.0, 0.0]);
assert_eq!(Vector::cross_product(&vec1, &vec2), Vector::new(vec![0.0, 0.0, 1.0]));

Trait Implementations§

source§

impl<K: Scalar> AddAssign for Vector<K>

source§

fn add_assign(&mut self, other: Self)

Performs the += operation. Read more
source§

impl<K: Clone + Scalar> Clone for Vector<K>

source§

fn clone(&self) -> Vector<K>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<K: Debug + Scalar> Debug for Vector<K>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<K: Scalar + Display> Display for Vector<K>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the Vector<K> for display.

§Parameters
  • f: A mutable reference to a fmt::Formatter for formatting.
§Returns
  • fmt::Result: Indicates success or failure of the formatting.
§Panics
  • This function does not panic.
§Example
use matrix::Vector;

let v = Vector::from([2., 3.]);
println!("{}", v); // Outputs: [2.0, 3.0]
source§

impl<K: Scalar, const N: usize> From<[K; N]> for Vector<K>

source§

fn from(array: [K; N]) -> Self

Converts an array of type [K; N] into a Vector<K>.

§Parameters
  • array: An array of type [K; N] to convert.
§Returns
  • A Vector<K> initialized with the elements of the input array.
§Panics
  • This function does not panic.
§Example
use matrix::Vector;

let v = Vector::from([2.0, 3.0]);
println!("{}", v); // Outputs: [2.0, 3.0]
source§

impl<K: Scalar> MulAssign<K> for Vector<K>

source§

fn mul_assign(&mut self, scalar: K)

Performs the *= operation. Read more
source§

impl<K: Scalar> PartialEq for Vector<K>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<K: Scalar> SubAssign for Vector<K>

source§

fn sub_assign(&mut self, other: Self)

Performs the -= operation. Read more

Auto Trait Implementations§

§

impl<K> Freeze for Vector<K>

§

impl<K> RefUnwindSafe for Vector<K>
where K: RefUnwindSafe,

§

impl<K> Send for Vector<K>
where K: Send,

§

impl<K> Sync for Vector<K>
where K: Sync,

§

impl<K> Unpin for Vector<K>
where K: Unpin,

§

impl<K> UnwindSafe for Vector<K>
where K: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.