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>
impl<K: Scalar> Vector<K>
sourcepub fn reshape(&self, rows: usize, cols: usize) -> Matrix<K>
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 theVector<K>.
§Panics
- Panics if the size of the
Vectordoes not match the requested dimensions (rows * colsmust equalself.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));sourcepub fn linear_combination(u: &[Vector<K>], coefs: &[K]) -> Vector<K>
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 ofVectors of typeKto be combined.coefs: A slice of coefficients of typeK, corresponding to the vectors inu.
§Returns
- A
Vector<K>containing the result of the linear combination.
§Panics
- Panics if the length of
uandcoefsdo not match, or if the vectors inuare 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);sourcepub fn dot(&self, v: &Vector<K>) -> K
pub fn dot(&self, v: &Vector<K>) -> K
Computes the dot product with another Vector<K>.
§Parameters
v: A reference to anotherVector<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);sourcepub fn add(&mut self, v: &Vector<K>)
pub fn add(&mut self, v: &Vector<K>)
Adds another Vector<K> to the calling Vector<K>.
§Parameters
v: A reference to the otherVector<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]);sourcepub fn sub(&mut self, v: &Vector<K>)
pub fn sub(&mut self, v: &Vector<K>)
Subtracts another Vector<K> from the calling Vector<K>.
§Parameters
v: A reference to the otherVector<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]);sourcepub fn scl(&mut self, a: K)
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]);sourcepub fn angle_cos(u: &Vector<K>, v: &Vector<K>) -> f32
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 firstVector.v- The secondVector.
§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);sourcepub fn cross_product(u: &Vector<K>, v: &Vector<K>) -> Vector<K>
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, andu_zare the components of vectoru,v_x,v_y, andv_zare the components of vectorv.
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-dimensionalVector.v- The second 3-dimensionalVector.
§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>
impl<K: Scalar> AddAssign for Vector<K>
source§fn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
+= operation. Read moresource§impl<K: Scalar + Display> Display for Vector<K>
impl<K: Scalar + Display> Display for Vector<K>
source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Formats the Vector<K> for display.
§Parameters
f: A mutable reference to afmt::Formatterfor 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>
impl<K: Scalar, const N: usize> From<[K; N]> for Vector<K>
source§fn from(array: [K; N]) -> Self
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>
impl<K: Scalar> MulAssign<K> for Vector<K>
source§fn mul_assign(&mut self, scalar: K)
fn mul_assign(&mut self, scalar: K)
*= operation. Read moreAuto 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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)