Refactor
This commit is contained in:
parent
ee18f22c9d
commit
be964d166e
15
src/lib.rs
15
src/lib.rs
@ -14,6 +14,7 @@
|
|||||||
// * Reuse path Vec in step5
|
// * Reuse path Vec in step5
|
||||||
// * Cleanup
|
// * Cleanup
|
||||||
// * More test cases
|
// * More test cases
|
||||||
|
// * Non-square matrices
|
||||||
|
|
||||||
extern crate bit_vec;
|
extern crate bit_vec;
|
||||||
|
|
||||||
@ -38,12 +39,14 @@ pub struct WeightMatrix<T: WeightNum> {
|
|||||||
|
|
||||||
impl<T: WeightNum> WeightMatrix<T> {
|
impl<T: WeightNum> WeightMatrix<T> {
|
||||||
pub fn from_row_vec(n: usize, data: Vec<T>) -> WeightMatrix<T> {
|
pub fn from_row_vec(n: usize, data: Vec<T>) -> WeightMatrix<T> {
|
||||||
|
assert!(n > 0);
|
||||||
WeightMatrix{c: SquareMatrix::from_row_vec(n, data)}
|
WeightMatrix{c: SquareMatrix::from_row_vec(n, data)}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn n(&self) -> usize { self.c.n() }
|
fn n(&self) -> usize { self.c.n() }
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
fn is_element_zero(&self, pos: (usize, usize)) -> bool {
|
fn is_element_zero(&self, pos: (usize, usize)) -> bool {
|
||||||
self.c[pos] == T::zero()
|
self.c[pos] == T::zero()
|
||||||
}
|
}
|
||||||
@ -59,23 +62,17 @@ impl<T: WeightNum> WeightMatrix<T> {
|
|||||||
|
|
||||||
// Subtract `val` from every element in row `row`.
|
// Subtract `val` from every element in row `row`.
|
||||||
fn sub_row(&mut self, row: usize, val: T) {
|
fn sub_row(&mut self, row: usize, val: T) {
|
||||||
for col in 0 .. self.n() {
|
self.c.map_row(row, |cur| cur - val);
|
||||||
self.c[(row, col)] = self.c[(row, col)] - val;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subtract `val` from every element in column `col`.
|
// Subtract `val` from every element in column `col`.
|
||||||
fn sub_col(&mut self, col: usize, val: T) {
|
fn sub_col(&mut self, col: usize, val: T) {
|
||||||
for row in 0 .. self.n() {
|
self.c.map_col(col, |cur| cur - val);
|
||||||
self.c[(row, col)] = self.c[(row, col)] - val;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add `val` to every element in row `row`.
|
// Add `val` to every element in row `row`.
|
||||||
fn add_row(&mut self, row: usize, val: T) {
|
fn add_row(&mut self, row: usize, val: T) {
|
||||||
for col in 0 .. self.n() {
|
self.c.map_row(row, |cur| cur + val);
|
||||||
self.c[(row, col)] = self.c[(row, col)] - val;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Find the first uncovered element with value 0 `find_a_zero`
|
/// Find the first uncovered element with value 0 `find_a_zero`
|
||||||
|
@ -34,12 +34,30 @@ impl<T> IndexMut<(usize, usize)> for SquareMatrix<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> SquareMatrix<T> {
|
impl<T: Copy> SquareMatrix<T> {
|
||||||
pub fn from_row_vec(n: usize, data: Vec<T>) -> SquareMatrix<T> {
|
pub fn from_row_vec(n: usize, data: Vec<T>) -> SquareMatrix<T> {
|
||||||
|
assert!(n > 0);
|
||||||
assert!(data.len() == n*n);
|
assert!(data.len() == n*n);
|
||||||
SquareMatrix {n: n, data: data}
|
SquareMatrix {n: n, data: data}
|
||||||
}
|
}
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn n(&self) -> usize { self.n }
|
pub fn n(&self) -> usize { self.n }
|
||||||
pub fn into_vec(self) -> Vec<T> { self.data }
|
pub fn into_vec(self) -> Vec<T> { self.data }
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn map_row<F: Fn(T) -> T>(&mut self, row: usize, f: F) {
|
||||||
|
for col in 0..self.n {
|
||||||
|
let n = f(self[(row, col)]);
|
||||||
|
self[(row, col)] = n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn map_col<F: Fn(T) -> T>(&mut self, col: usize, f: F) {
|
||||||
|
for row in 0..self.n {
|
||||||
|
let n = f(self[(row, col)]);
|
||||||
|
self[(row, col)] = n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user