new: Added implementation for Neg

This commit is contained in:
Sayantan Santra 2023-05-26 01:18:52 -05:00
parent a0a7ae9076
commit 450b00469c
Signed by: SinTan1729
GPG key ID: EB3E68BFBA25C85F
2 changed files with 21 additions and 12 deletions

View file

@ -13,7 +13,7 @@ use num::{
}; };
use std::{ use std::{
fmt::{self, Debug, Display, Formatter}, fmt::{self, Debug, Display, Formatter},
ops::{Add, Div, Mul, Sub}, ops::{Add, Div, Mul, Neg, Sub},
result::Result, result::Result,
}; };
@ -377,7 +377,7 @@ impl<T: Debug + Mul + Add + Sub> Display for Matrix<T> {
impl<T: Mul<Output = T> + Add + Sub + Copy + Zero> Mul for Matrix<T> { impl<T: Mul<Output = T> + Add + Sub + Copy + Zero> Mul for Matrix<T> {
// TODO: Implement a faster algorithm. // TODO: Implement a faster algorithm.
type Output = Self; type Output = Self;
fn mul(self, other: Self) -> Self { fn mul(self, other: Self) -> Self::Output {
let width = self.width(); let width = self.width();
if width != other.height() { if width != other.height() {
panic!("Row length of first matrix must be same as column length of second matrix."); panic!("Row length of first matrix must be same as column length of second matrix.");
@ -401,7 +401,7 @@ impl<T: Mul<Output = T> + Add + Sub + Copy + Zero> Mul for Matrix<T> {
impl<T: Add<Output = T> + Sub + Mul + Copy + Zero> Add for Matrix<T> { impl<T: Add<Output = T> + Sub + Mul + Copy + Zero> Add for Matrix<T> {
type Output = Self; type Output = Self;
fn add(self, other: Self) -> Self { fn add(self, other: Self) -> Self::Output {
if self.height() == other.height() && self.width() == other.width() { if self.height() == other.height() && self.width() == other.width() {
let mut out = self.entries.clone(); let mut out = self.entries.clone();
for (i, row) in self.rows().iter().enumerate() { for (i, row) in self.rows().iter().enumerate() {
@ -416,17 +416,24 @@ impl<T: Add<Output = T> + Sub + Mul + Copy + Zero> Add for Matrix<T> {
} }
} }
impl<T: Add + Sub<Output = T> + Mul + Copy + Zero> Sub for Matrix<T> { impl<T: Add + Sub<Output = T> + Mul + Copy + Neg<Output = T>> Neg for Matrix<T> {
type Output = Self; type Output = Self;
fn sub(self, other: Self) -> Self { fn neg(self) -> Self::Output {
let mut out = self;
for row in &mut out.entries {
for entry in row {
*entry = -*entry;
}
}
out
}
}
impl<T: Add + Sub<Output = T> + Mul + Copy + Zero + Neg<Output = T>> Sub for Matrix<T> {
type Output = Self;
fn sub(self, other: Self) -> Self::Output {
if self.height() == other.height() && self.width() == other.width() { if self.height() == other.height() && self.width() == other.width() {
let mut out = self.entries.clone(); self + -other
for (i, row) in self.rows().iter().enumerate() {
for (j, entry) in other.rows()[i].iter().enumerate() {
out[i][j] = row[j] - *entry;
}
}
Matrix { entries: out }
} else { } else {
panic!("Both matrices must be of same dimensions."); panic!("Both matrices must be of same dimensions.");
} }

View file

@ -14,8 +14,10 @@ fn add_sub_test() {
let b = Matrix::from(vec![vec![0, 0, 1], vec![2, 1, 3]]).unwrap(); let b = Matrix::from(vec![vec![0, 0, 1], vec![2, 1, 3]]).unwrap();
let c = Matrix::from(vec![vec![1, 2, 4], vec![2, 2, 5]]).unwrap(); let c = Matrix::from(vec![vec![1, 2, 4], vec![2, 2, 5]]).unwrap();
let d = Matrix::from(vec![vec![1, 2, 2], vec![-2, 0, -1]]).unwrap(); let d = Matrix::from(vec![vec![1, 2, 2], vec![-2, 0, -1]]).unwrap();
let e = Matrix::from(vec![vec![-1, -2, -4], vec![-2, -2, -5]]).unwrap();
assert_eq!(a.clone() + b.clone(), c); assert_eq!(a.clone() + b.clone(), c);
assert_eq!(a - b, d); assert_eq!(a - b, d);
assert_eq!(-c, e);
} }
#[test] #[test]