mirror of
https://github.com/SinTan1729/matrix-basic.git
synced 2024-12-25 21:48:35 -06:00
new: Added implementation for Neg
This commit is contained in:
parent
a0a7ae9076
commit
450b00469c
2 changed files with 21 additions and 12 deletions
31
src/lib.rs
31
src/lib.rs
|
@ -13,7 +13,7 @@ use num::{
|
|||
};
|
||||
use std::{
|
||||
fmt::{self, Debug, Display, Formatter},
|
||||
ops::{Add, Div, Mul, Sub},
|
||||
ops::{Add, Div, Mul, Neg, Sub},
|
||||
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> {
|
||||
// TODO: Implement a faster algorithm.
|
||||
type Output = Self;
|
||||
fn mul(self, other: Self) -> Self {
|
||||
fn mul(self, other: Self) -> Self::Output {
|
||||
let width = self.width();
|
||||
if width != other.height() {
|
||||
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> {
|
||||
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() {
|
||||
let mut out = self.entries.clone();
|
||||
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;
|
||||
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() {
|
||||
let mut out = self.entries.clone();
|
||||
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 }
|
||||
self + -other
|
||||
} else {
|
||||
panic!("Both matrices must be of same dimensions.");
|
||||
}
|
||||
|
|
|
@ -14,8 +14,10 @@ fn add_sub_test() {
|
|||
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 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 - b, d);
|
||||
assert_eq!(-c, e);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Reference in a new issue