1use std::vec::Vec;
2use crate::types::ValueType;
3
4pub trait Vector<'a>
6where Self: Sized + Clone {
7 type Value: 'a + ValueType;
8 type IterVal: Iterator<Item = Self::Value>;
9
10 fn iter(&'a self) -> Self::IterVal;
11
12 fn with_capacity(cap: usize) -> Self;
14
15 fn from_vec(vec: Vec<Self::Value>) -> Self;
17
18 fn new() -> Self {
20 Self::with_capacity(0)
21 }
22
23 fn dim(&self) -> usize;
25
26 fn get(&self, i: usize) -> Self::Value;
28
29 fn get_mut(&mut self, i: usize) -> &mut Self::Value;
32
33 fn add(&'a mut self, rhs: &Self);
34
35 fn sub(&'a mut self, rhs: &Self);
36
37 fn scale(&'a mut self, rhs: Self::Value);
38
39 fn set(&mut self, i: usize, val: Self::Value) {
41 *self.get_mut(i) = val;
42 }
43
44 fn add_to(&mut self, i: usize, val: Self::Value) {
46 *self.get_mut(i) += val;
47 }
48
49 fn inner_prod<V>(&'a self, rhs: &'a V) -> Self::Value
51 where V: Vector<'a, Value = Self::Value> {
52 self.iter().zip(rhs.iter()).map(|(x, y)| x * y).sum()
53 }
54
55 fn norm_squared(&'a self) -> Self::Value {
57 self.iter().map(|x| x * x).sum()
58 }
59
60 fn norm(&'a self) -> f64 {
62 f64::sqrt(self.norm_squared().into())
63 }
64}