new: Added mul_scalar method

This commit is contained in:
Sayantan Santra 2023-05-27 01:41:41 -05:00
parent 56222e04f1
commit 79461f7ad7
Signed by: SinTan1729
GPG key ID: EB3E68BFBA25C85F
2 changed files with 49 additions and 27 deletions

View file

@ -33,11 +33,11 @@ impl<T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy> Matri
/// # Example /// # Example
/// ``` /// ```
/// use matrix_basic::Matrix; /// use matrix_basic::Matrix;
/// let m = Matrix::from(vec![vec![1,2,3], vec![4,5,6]]); /// let m = Matrix::from(vec![vec![1, 2, 3], vec![4, 5, 6]]);
/// ``` /// ```
/// will create the following matrix: /// will create the following matrix:
/// ⌈1,2,3⌉ /// ⌈1, 2, 3⌉
/// ⌊4,5,6⌋ /// ⌊4, 5, 6⌋
pub fn from(entries: Vec<Vec<T>>) -> Result<Matrix<T>, &'static str> { pub fn from(entries: Vec<Vec<T>>) -> Result<Matrix<T>, &'static str> {
let mut equal_rows = true; let mut equal_rows = true;
let row_len = entries[0].len(); let row_len = entries[0].len();
@ -97,9 +97,9 @@ impl<T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy> Matri
/// # Example /// # Example
/// ``` /// ```
/// use matrix_basic::Matrix; /// use matrix_basic::Matrix;
/// let m = Matrix::from(vec![vec![1,2,3], vec![4,5,6]]).unwrap(); /// let m = Matrix::from(vec![vec![1, 2, 3], vec![4, 5, 6]]).unwrap();
/// let n = Matrix::from(vec![vec![5,6]]).unwrap(); /// let n = Matrix::from(vec![vec![5, 6]]).unwrap();
/// assert_eq!(m.submatrix(0,0),n); /// assert_eq!(m.submatrix(0, 0), n);
/// ``` /// ```
pub fn submatrix(&self, row: usize, col: usize) -> Self { pub fn submatrix(&self, row: usize, col: usize) -> Self {
let mut out = Vec::new(); let mut out = Vec::new();
@ -125,8 +125,8 @@ impl<T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy> Matri
/// # Example /// # Example
/// ``` /// ```
/// use matrix_basic::Matrix; /// use matrix_basic::Matrix;
/// let m = Matrix::from(vec![vec![1,2], vec![3,4]]).unwrap(); /// let m = Matrix::from(vec![vec![1, 2], vec![3, 4]]).unwrap();
/// assert_eq!(m.det(),Ok(-2)); /// assert_eq!(m.det(), Ok(-2));
/// ``` /// ```
pub fn det(&self) -> Result<T, &'static str> { pub fn det(&self) -> Result<T, &'static str> {
if self.is_square() { if self.is_square() {
@ -160,8 +160,8 @@ impl<T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy> Matri
/// # Example /// # Example
/// ``` /// ```
/// use matrix_basic::Matrix; /// use matrix_basic::Matrix;
/// let m = Matrix::from(vec![vec![1.0,2.0], vec![3.0,4.0]]).unwrap(); /// let m = Matrix::from(vec![vec![1.0, 2.0], vec![3.0, 4.0]]).unwrap();
/// assert_eq!(m.det(),Ok(-2.0)); /// assert_eq!(m.det(), Ok(-2.0));
/// ``` /// ```
pub fn det_in_field(&self) -> Result<T, &'static str> pub fn det_in_field(&self) -> Result<T, &'static str>
where where
@ -211,9 +211,9 @@ impl<T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy> Matri
/// # Example /// # Example
/// ``` /// ```
/// use matrix_basic::Matrix; /// use matrix_basic::Matrix;
/// let m = Matrix::from(vec![vec![1.0,2.0,3.0], vec![3.0,4.0,5.0]]).unwrap(); /// let m = Matrix::from(vec![vec![1.0, 2.0, 3.0], vec![3.0, 4.0, 5.0]]).unwrap();
/// let n = Matrix::from(vec![vec![1.0,2.0,3.0], vec![0.0,-2.0,-4.0]]).unwrap(); /// let n = Matrix::from(vec![vec![1.0, 2.0, 3.0], vec![0.0, -2.0, -4.0]]).unwrap();
/// assert_eq!(m.row_echelon(),n); /// assert_eq!(m.row_echelon(), n);
/// ``` /// ```
pub fn row_echelon(&self) -> Self pub fn row_echelon(&self) -> Self
where where
@ -269,9 +269,9 @@ impl<T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy> Matri
/// # Example /// # Example
/// ``` /// ```
/// use matrix_basic::Matrix; /// use matrix_basic::Matrix;
/// let m = Matrix::from(vec![vec![1.0,2.0,3.0], vec![3.0,4.0,5.0]]).unwrap(); /// let m = Matrix::from(vec![vec![1.0, 2.0, 3.0], vec![3.0, 4.0, 5.0]]).unwrap();
/// let n = Matrix::from(vec![vec![1.0,2.0,3.0], vec![0.0,1.0,2.0]]).unwrap(); /// let n = Matrix::from(vec![vec![1.0, 2.0, 3.0], vec![0.0, 1.0, 2.0]]).unwrap();
/// assert_eq!(m.reduced_row_echelon(),n); /// assert_eq!(m.reduced_row_echelon(), n);
/// ``` /// ```
pub fn reduced_row_echelon(&self) -> Self pub fn reduced_row_echelon(&self) -> Self
where where
@ -324,8 +324,8 @@ impl<T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy> Matri
/// # Example /// # Example
/// ``` /// ```
/// use matrix_basic::Matrix; /// use matrix_basic::Matrix;
/// let m = Matrix::from(vec![vec![1,2], vec![3,4]]).unwrap(); /// let m = Matrix::from(vec![vec![1, 2], vec![3, 4]]).unwrap();
/// assert_eq!(m.trace(),Ok(5)); /// assert_eq!(m.trace(), Ok(5));
/// ``` /// ```
pub fn trace(self) -> Result<T, &'static str> { pub fn trace(self) -> Result<T, &'static str> {
if self.is_square() { if self.is_square() {
@ -343,10 +343,10 @@ impl<T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy> Matri
/// # Example /// # Example
/// ``` /// ```
/// use matrix_basic::Matrix; /// use matrix_basic::Matrix;
/// let m = Matrix::diagonal_matrix(vec![1,2,3]); /// let m = Matrix::diagonal_matrix(vec![1, 2, 3]);
/// let n = Matrix::from(vec![vec![1,0,0], vec![0,2,0], vec![0,0,3]]).unwrap(); /// let n = Matrix::from(vec![vec![1, 0, 0], vec![0, 2, 0], vec![0, 0, 3]]).unwrap();
/// ///
/// assert_eq!(m,n); /// assert_eq!(m, n);
/// ``` /// ```
pub fn diagonal_matrix(diag: Vec<T>) -> Self { pub fn diagonal_matrix(diag: Vec<T>) -> Self {
let size = diag.len(); let size = diag.len();
@ -357,6 +357,25 @@ impl<T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy> Matri
out out
} }
/// Multiplies all entries of a matrix by a scalar.
/// Note that it modifies the supplied matrix.
/// # Example
/// ```
/// use matrix_basic::Matrix;
/// let mut m = Matrix::from(vec![vec![1, 2, 0], vec![0, 2, 5], vec![0, 0, 3]]).unwrap();
/// let n = Matrix::from(vec![vec![2, 4, 0], vec![0, 4, 10], vec![0, 0, 6]]).unwrap();
/// m.mul_scalar(2);
///
/// assert_eq!(m, n);
/// ```
pub fn mul_scalar(&mut self, scalar: T) {
for row in &mut self.entries {
for entry in row {
*entry = *entry * scalar;
}
}
}
// TODO: Canonical forms, eigenvalues, eigenvectors etc. // TODO: Canonical forms, eigenvalues, eigenvectors etc.
} }
@ -368,7 +387,7 @@ impl<T: Debug + Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Cop
} }
} }
impl<T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy + Copy + Zero> Mul impl<T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy + Zero> Mul
for Matrix<T> for Matrix<T>
{ {
// TODO: Implement a faster algorithm. // TODO: Implement a faster algorithm.
@ -395,7 +414,7 @@ impl<T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy + Copy
} }
} }
impl<T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy + Copy + Zero> Add impl<T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy + Zero> Add
for Matrix<T> for Matrix<T>
{ {
type Output = Self; type Output = Self;
@ -414,9 +433,8 @@ impl<T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy + Copy
} }
} }
impl< impl<T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy + Neg<Output = T>> Neg
T: Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Zero + Copy + Copy + Neg<Output = T>, for Matrix<T>
> Neg for Matrix<T>
{ {
type Output = Self; type Output = Self;
fn neg(self) -> Self::Output { fn neg(self) -> Self::Output {

View file

@ -5,9 +5,13 @@ use crate::Matrix;
fn mul_test() { fn mul_test() {
let a = Matrix::from(vec![vec![1, 2, 4], vec![3, 4, 9]]).unwrap(); let a = Matrix::from(vec![vec![1, 2, 4], vec![3, 4, 9]]).unwrap();
let b = Matrix::from(vec![vec![1, 2], vec![2, 3], vec![5, 1]]).unwrap(); let b = Matrix::from(vec![vec![1, 2], vec![2, 3], vec![5, 1]]).unwrap();
let c = Matrix::from(vec![vec![25, 12], vec![56, 27]]).unwrap(); let mut c = Matrix::from(vec![vec![25, 12], vec![56, 27]]).unwrap();
let d = Matrix::from(vec![vec![75, 36], vec![168, 81]]).unwrap();
assert_eq!(a * b, c); assert_eq!(a * b, c);
c.mul_scalar(3);
assert_eq!(c, d);
} }
#[test] #[test]