mirror of
https://github.com/SinTan1729/matrix-basic.git
synced 2024-12-25 21:48:35 -06:00
new: Added one and zero methods
This commit is contained in:
parent
c14f13b148
commit
68dd53a8db
2 changed files with 49 additions and 2 deletions
|
@ -29,4 +29,12 @@ mod tests {
|
||||||
assert_eq!(a.det(), Ok(30));
|
assert_eq!(a.det(), Ok(30));
|
||||||
assert!(b.det().is_err());
|
assert!(b.det().is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn zero_one_test() {
|
||||||
|
let a = Matrix::from(vec![vec![0, 0, 0], vec![0, 0, 0]]).unwrap();
|
||||||
|
let b = Matrix::from(vec![vec![1, 0], vec![0, 1]]).unwrap();
|
||||||
|
assert_eq!(Matrix::<i32>::zero(2, 3), a);
|
||||||
|
assert_eq!(Matrix::<i32>::identity(2), b);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
use num::{traits::Zero, Integer};
|
use num::{
|
||||||
|
traits::{One, Zero},
|
||||||
|
Integer,
|
||||||
|
};
|
||||||
use std::{
|
use std::{
|
||||||
fmt::{self, Debug, Display, Formatter},
|
fmt::{self, Debug, Display, Formatter},
|
||||||
ops::{Add, Mul, Sub},
|
ops::{Add, Mul, Sub},
|
||||||
|
@ -10,7 +13,7 @@ pub struct Matrix<T: Mul + Add + Sub> {
|
||||||
entries: Vec<Vec<T>>,
|
entries: Vec<Vec<T>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Mul + Add + Sub + Zero> Matrix<T> {
|
impl<T: Mul + Add + Sub> Matrix<T> {
|
||||||
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();
|
||||||
|
@ -93,6 +96,7 @@ impl<T: Mul + Add + Sub + Zero> Matrix<T> {
|
||||||
T: Copy,
|
T: Copy,
|
||||||
T: Mul<Output = T>,
|
T: Mul<Output = T>,
|
||||||
T: Sub<Output = T>,
|
T: Sub<Output = T>,
|
||||||
|
T: Zero,
|
||||||
{
|
{
|
||||||
if self.is_square() {
|
if self.is_square() {
|
||||||
let out = if self.width() == 1 {
|
let out = if self.width() == 1 {
|
||||||
|
@ -114,6 +118,41 @@ impl<T: Mul + Add + Sub + Zero> Matrix<T> {
|
||||||
Err("Provided matrix isn't square.")
|
Err("Provided matrix isn't square.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn zero(height: usize, width: usize) -> Self
|
||||||
|
where
|
||||||
|
T: Zero,
|
||||||
|
{
|
||||||
|
let mut out = Vec::new();
|
||||||
|
for _ in 0..height {
|
||||||
|
let mut new_row = Vec::new();
|
||||||
|
for _ in 0..width {
|
||||||
|
new_row.push(T::zero());
|
||||||
|
}
|
||||||
|
out.push(new_row);
|
||||||
|
}
|
||||||
|
Matrix { entries: out }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn identity(size: usize) -> Self
|
||||||
|
where
|
||||||
|
T: Zero,
|
||||||
|
T: One,
|
||||||
|
{
|
||||||
|
let mut out = Vec::new();
|
||||||
|
for i in 0..size {
|
||||||
|
let mut new_row = Vec::new();
|
||||||
|
for j in 0..size {
|
||||||
|
if i == j {
|
||||||
|
new_row.push(T::one());
|
||||||
|
} else {
|
||||||
|
new_row.push(T::zero());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out.push(new_row);
|
||||||
|
}
|
||||||
|
Matrix { entries: out }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Debug + Mul + Add + Sub> Display for Matrix<T> {
|
impl<T: Debug + Mul + Add + Sub> Display for Matrix<T> {
|
||||||
|
|
Loading…
Reference in a new issue