1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use std::ops::{Index, IndexMut};
use crate::algebra::linear::vector::Vector;
impl<T> Index<usize> for Vector<T> {
type Output = T;
/// Returns the component
///
/// # Panics
///
/// if index is out of bounds
///
/// # Example
///
/// ```
/// use mathru::algebra::linear::vector::Vector;
///
/// let mut a: Vector<f64> = Vector::new_row(vec![1.0, 0.0, 3.0, -2.0]);
///
/// assert_eq!(-2.0, a[3])
/// ```
fn index(&self, index: usize) -> &Self::Output {
let num_rows = self.data.m;
if num_rows == 1 {
debug_assert!(
index < self.data.n,
"index out of bounds: is a 1x{} vector but the index is 0x{}",
self.data.n,
index
);
&self.data[[0, index]]
} else {
debug_assert!(
index < num_rows,
"index out of bounds: is a {}x1 vector but the index is {}x0",
num_rows,
index
);
&self.data[[index, 0]]
}
}
}
impl<T> IndexMut<usize> for Vector<T> {
/// Returns the component
///
/// # Panics
///
/// if index is out of bounds
///
/// # Example
///
/// ```
/// use mathru::algebra::linear::vector::Vector;
///
/// let mut a: Vector<f64> = Vector::new_row(vec![1.0, 0.0, 3.0, -2.0]);
///
/// assert_eq!(-2.0, a[3])
/// ```
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
let num_rows = self.data.m;
if num_rows == 1 {
debug_assert!(
index < self.data.n,
"index out of bounds: is a 1x{} vector but the index is 0x{}",
self.data.n,
index
);
&mut self.data[[0, index]]
} else {
debug_assert!(
index < num_rows,
"index out of bounds: is a {}x1 vector but the index is {}x0",
num_rows,
index
);
&mut self.data[[index, 0]]
}
}
}