non square matrix
This commit is contained in:
parent
3e07dac628
commit
0c5d7340ed
@ -11,14 +11,15 @@ macro_rules! int {
|
|||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct Matrix {
|
pub struct Matrix {
|
||||||
pub n: usize,
|
pub n: usize, //rows
|
||||||
|
pub m: usize, // columns
|
||||||
values: Vec<Element>,
|
values: Vec<Element>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Matrix {
|
impl Matrix {
|
||||||
pub fn new(n: usize, values: Vec<Element>) -> Option<Self> {
|
pub fn new(n: usize, m: usize, values: Vec<Element>) -> Option<Self> {
|
||||||
if n.pow(2) == values.len() {
|
if n*m == values.len() {
|
||||||
Some(Matrix { n, values })
|
Some(Matrix { n, m, values })
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
@ -40,19 +41,25 @@ impl Matrix {
|
|||||||
values.extend(row);
|
values.extend(row);
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix::new(n, values)
|
Matrix::new(n, n, values)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Index<(usize, usize)> for Matrix {
|
impl Index<(usize, usize)> for Matrix {
|
||||||
type Output = Element;
|
type Output = Element;
|
||||||
fn index(&self, index: (usize, usize)) -> &Self::Output {
|
fn index(&self, index: (usize, usize)) -> &Self::Output {
|
||||||
|
if index.0>=self.m || index.1 >= self.n {
|
||||||
|
panic!();
|
||||||
|
}
|
||||||
&self.values[(self.n * index.0) + index.1]
|
&self.values[(self.n * index.0) + index.1]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IndexMut<(usize, usize)> for Matrix {
|
impl IndexMut<(usize, usize)> for Matrix {
|
||||||
fn index_mut(&mut self, index: (usize, usize)) -> &mut Self::Output {
|
fn index_mut(&mut self, index: (usize, usize)) -> &mut Self::Output {
|
||||||
|
if index.0>=self.m || index.1 >= self.n {
|
||||||
|
panic!();
|
||||||
|
}
|
||||||
&mut self.values[(self.n * index.0) + index.1]
|
&mut self.values[(self.n * index.0) + index.1]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -65,22 +72,27 @@ mod tests {
|
|||||||
assert_eq!(
|
assert_eq!(
|
||||||
Matrix {
|
Matrix {
|
||||||
n: 2,
|
n: 2,
|
||||||
|
m: 2,
|
||||||
values: vec![int!(1), int!(2), int!(3), int!(4)],
|
values: vec![int!(1), int!(2), int!(3), int!(4)],
|
||||||
},
|
},
|
||||||
Matrix::new(2, vec![int!(1), int!(2), int!(3), int!(4)]).unwrap()
|
Matrix::new(2, 2, vec![int!(1), int!(2), int!(3), int!(4)]).unwrap()
|
||||||
);
|
);
|
||||||
assert!(Matrix::new(3, vec![int!(1), int!(2)]).is_none());
|
assert!(Matrix::new(2, 2, vec![int!(1), int!(2)]).is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn indexes() {
|
fn indexes() {
|
||||||
let mut m = Matrix::new(2, vec![int!(1), int!(2), int!(3), int!(4)]).unwrap();
|
let mut m = Matrix::new(2, 2, vec![int!(1), int!(2), int!(3), int!(4)]).unwrap();
|
||||||
|
|
||||||
assert_eq!(m[(0, 0)], int!(1));
|
assert_eq!(m[(0, 0)], int!(1));
|
||||||
assert_eq!(m[(1, 0)], int!(3));
|
assert_eq!(m[(1, 0)], int!(3));
|
||||||
|
|
||||||
m[(1, 0)] = int!(5);
|
m[(1, 0)] = int!(5);
|
||||||
assert_eq!(m[(1, 0)], int!(5));
|
assert_eq!(m[(1, 0)], int!(5));
|
||||||
|
|
||||||
|
let m2 = Matrix::new(3, 2, vec![int!(1), int!(2), int!(3), int!(4), int!(5), int!(6)]).unwrap();
|
||||||
|
assert_eq!(m2[(0, 2)], int!(3));
|
||||||
|
assert_eq!(m2[(1, 0)], int!(4));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user