mirror of
https://github.com/SinTan1729/matrix-basic.git
synced 2024-12-26 05:48:36 -06:00
new: Added mul_scalar method
This commit is contained in:
parent
56222e04f1
commit
79461f7ad7
2 changed files with 49 additions and 27 deletions
28
src/lib.rs
28
src/lib.rs
|
@ -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 {
|
||||||
|
|
|
@ -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]
|
||||||
|
|
Loading…
Reference in a new issue