mirror of
https://github.com/SinTan1729/matrix-basic.git
synced 2024-12-26 05:48:36 -06:00
fix: Some grammar and newlines
This commit is contained in:
parent
450b00469c
commit
2c3c2ca80d
2 changed files with 12 additions and 7 deletions
14
src/lib.rs
14
src/lib.rs
|
@ -54,17 +54,17 @@ impl<T: Mul + Add + Sub> Matrix<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the height of a matrix.
|
/// Returns the height of a matrix.
|
||||||
pub fn height(&self) -> usize {
|
pub fn height(&self) -> usize {
|
||||||
self.entries.len()
|
self.entries.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the width of a matrix.
|
/// Returns the width of a matrix.
|
||||||
pub fn width(&self) -> usize {
|
pub fn width(&self) -> usize {
|
||||||
self.entries[0].len()
|
self.entries[0].len()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the transpose of a matrix.
|
/// Returns the transpose of a matrix.
|
||||||
pub fn transpose(&self) -> Self
|
pub fn transpose(&self) -> Self
|
||||||
where
|
where
|
||||||
T: Copy,
|
T: Copy,
|
||||||
|
@ -80,7 +80,7 @@ impl<T: Mul + Add + Sub> Matrix<T> {
|
||||||
Matrix { entries: out }
|
Matrix { entries: out }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return a reference to the rows of a matrix as `&Vec<Vec<T>>`.
|
/// Returns a reference to the rows of a matrix as `&Vec<Vec<T>>`.
|
||||||
pub fn rows(&self) -> &Vec<Vec<T>> {
|
pub fn rows(&self) -> &Vec<Vec<T>> {
|
||||||
&self.entries
|
&self.entries
|
||||||
}
|
}
|
||||||
|
@ -98,7 +98,7 @@ impl<T: Mul + Add + Sub> Matrix<T> {
|
||||||
self.height() == self.width()
|
self.height() == self.width()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return a matrix after removing the provided row and column from it.
|
/// Returns a matrix after removing the provided row and column from it.
|
||||||
/// Note: Row and column numbers are 0-indexed.
|
/// Note: Row and column numbers are 0-indexed.
|
||||||
/// # Example
|
/// # Example
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -127,7 +127,7 @@ impl<T: Mul + Add + Sub> Matrix<T> {
|
||||||
Matrix { entries: out }
|
Matrix { entries: out }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the determinant of a square matrix. This method additionally requires [`Zero`],
|
/// Returns the determinant of a square matrix. This method additionally requires [`Zero`],
|
||||||
/// [`One`] and [`Copy`] traits. Also, we need that the [`Mul`] and [`Add`] operations
|
/// [`One`] and [`Copy`] traits. Also, we need that the [`Mul`] and [`Add`] operations
|
||||||
/// return the same type `T`. This uses basic recursive algorithm using cofactor-minor.
|
/// return the same type `T`. This uses basic recursive algorithm using cofactor-minor.
|
||||||
/// See [`det_in_field`](Self::det_in_field()) for faster determinant calculation in fields.
|
/// See [`det_in_field`](Self::det_in_field()) for faster determinant calculation in fields.
|
||||||
|
@ -169,7 +169,7 @@ impl<T: Mul + Add + Sub> Matrix<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the determinant of a square matrix over a field i.e. needs [`One`] and [`Div`] traits.
|
/// Returns the determinant of a square matrix over a field i.e. needs [`One`] and [`Div`] traits.
|
||||||
/// See [`det`](Self::det()) for determinants in rings.
|
/// See [`det`](Self::det()) for determinants in rings.
|
||||||
/// This method uses row reduction as is much faster.
|
/// This method uses row reduction as is much faster.
|
||||||
/// It'll throw an error if the provided matrix isn't square.
|
/// It'll throw an error if the provided matrix isn't square.
|
||||||
|
|
|
@ -5,6 +5,7 @@ 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 c = Matrix::from(vec![vec![25, 12], vec![56, 27]]).unwrap();
|
||||||
|
|
||||||
assert_eq!(a * b, c);
|
assert_eq!(a * b, c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,6 +16,7 @@ fn add_sub_test() {
|
||||||
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();
|
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);
|
assert_eq!(-c, e);
|
||||||
|
@ -30,6 +32,7 @@ fn det_test() {
|
||||||
vec![1.0, 2.0, 0.0],
|
vec![1.0, 2.0, 0.0],
|
||||||
])
|
])
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(a.det(), Ok(30));
|
assert_eq!(a.det(), Ok(30));
|
||||||
assert_eq!(c.det_in_field(), Ok(-30.0));
|
assert_eq!(c.det_in_field(), Ok(-30.0));
|
||||||
assert!(b.det().is_err());
|
assert!(b.det().is_err());
|
||||||
|
@ -39,6 +42,7 @@ fn det_test() {
|
||||||
fn zero_one_test() {
|
fn zero_one_test() {
|
||||||
let a = Matrix::from(vec![vec![0, 0, 0], vec![0, 0, 0]]).unwrap();
|
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();
|
let b = Matrix::from(vec![vec![1, 0], vec![0, 1]]).unwrap();
|
||||||
|
|
||||||
assert_eq!(Matrix::<i32>::zero(2, 3), a);
|
assert_eq!(Matrix::<i32>::zero(2, 3), a);
|
||||||
assert_eq!(Matrix::<i32>::identity(2), b);
|
assert_eq!(Matrix::<i32>::identity(2), b);
|
||||||
}
|
}
|
||||||
|
@ -49,6 +53,7 @@ fn echelon_test() {
|
||||||
let a = Matrix::from(vec![vec![1.0, 2.0, 3.0], vec![0.0, -2.0, -2.0]]).unwrap();
|
let a = Matrix::from(vec![vec![1.0, 2.0, 3.0], vec![0.0, -2.0, -2.0]]).unwrap();
|
||||||
let b = Matrix::from(vec![vec![1.0, 0.0, 0.0], vec![1.0, -2.0, 0.0]]).unwrap();
|
let b = Matrix::from(vec![vec![1.0, 0.0, 0.0], vec![1.0, -2.0, 0.0]]).unwrap();
|
||||||
let c = Matrix::from(vec![vec![1.0, 2.0, 3.0], vec![0.0, 1.0, 1.0]]).unwrap();
|
let c = Matrix::from(vec![vec![1.0, 2.0, 3.0], vec![0.0, 1.0, 1.0]]).unwrap();
|
||||||
|
|
||||||
assert_eq!(m.row_echelon(), a);
|
assert_eq!(m.row_echelon(), a);
|
||||||
assert_eq!(m.column_echelon(), b);
|
assert_eq!(m.column_echelon(), b);
|
||||||
assert_eq!(m.reduced_row_echelon(), c);
|
assert_eq!(m.reduced_row_echelon(), c);
|
||||||
|
|
Loading…
Reference in a new issue