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
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use ::std::ops::{Index, IndexMut, Add, Sub, Mul, Neg, Rem, BitAnd, BitOr, BitXor, Not, Shl, Shr};
use ::std::convert::{Into, From};
use ::std::fmt::{Display, Formatter, Result};
use ::std_vec_tools::VecTools;
use ::matrix_class::Matrix;


/// A Vector with generic type items.
/// Can be indexed by `vector[i]`
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Vector<T> {
    data: Vec<T>
}

impl<T> Vector<T> {
    pub fn new(data: Vec<T>) -> Self {
        Self { data }
    }

    pub fn dim(&self) -> usize {
        self.data.len()
    }
    /// Multiplies the vector with a scalar
    pub fn scaled<U, O>(self, scalar: U) -> Vector<O>
        where T: Mul<U, Output=O>, U: Clone
    {
        self.map(|x| x * scalar.clone())
    }

    pub fn zip<U>(self, other: Vector<U>) -> Vector<(T, U)> {
        Vector::new(self.data.zip(other.data))
    }
    /// Applies a function to every element of the vector
    pub fn map<F, U>(self, f: F) -> Vector<U>
        where F: Fn(T) -> U
    {
        Vector::new(self.data.map(f))
    }
}

impl<T> Display for Vector<T>
    where T: Display + Clone
{
    fn fmt(&self, f: &mut Formatter) -> Result {
        let s = self.data.clone().map(|x| format!("{}", x)).join(", ");
        write!(f, "Vector({})", s)
    }
}

/// A Vector can be cast to a matrix and back
impl<T> Into<Matrix<T>> for Vector<T> {
    fn into(self) -> Matrix<T> {
        Matrix::from_vec(self.dim(), 1, self.data)
    }
}

/// A Vector can be cast to a matrix and back
impl<T> From<Matrix<T>> for Vector<T> {
    fn from(mat: Matrix<T>) -> Self {
        let (_, cols, data) = mat.dump();
        assert_eq!(cols, 1);
        Self {
            data
        }
    }
}

impl<T> Index<usize> for Vector<T> {
    type Output = T;

    fn index(&self, i: usize) -> &T {
        &self.data[i]
    }
}

impl<T> IndexMut<usize> for Vector<T> {
    fn index_mut(&mut self, i: usize) -> &mut T {
        &mut self.data[i]
    }
}

/// Adds two vectors element by element
impl<T, U, O> Add<Vector<U>> for Vector<T>
    where T: Add<U, Output=O>
{
    type Output = Vector<O>;

    fn add(self, rhs: Vector<U>) -> Vector<O> {
        self.zip(rhs).map(|(a, b)| a + b)
    }
}

/// Subtracts two vectors element by element
impl<T, U, O> Sub<Vector<U>> for Vector<T>
    where T: Sub<U, Output=O>
{
    type Output = Vector<O>;

    fn sub(self, rhs: Vector<U>) -> Vector<O> {
        self.zip(rhs).map(|(a, b)| a - b)
    }
}

impl<T, O> Neg for Vector<T>
    where T: Neg<Output=O>
{
    type Output = Vector<O>;

    fn neg(self) -> Vector<O> {
        self.map(|x| x.neg())
    }
}

impl<T, U, O> Rem<U> for Vector<T>
    where T: Rem<U, Output=O>, U: Clone
{
    type Output = Vector<O>;

    fn rem(self, rhs: U) -> Vector<O> {
        self.map(|x| x % rhs.clone())
    }
}

impl<T, U, O> BitAnd<U> for Vector<T>
    where T: BitAnd<U, Output=O>, U: Clone
{
    type Output = Vector<O>;

    fn bitand(self, rhs: U) -> Vector<O> {
        self.map(|x| x & rhs.clone())
    }
}

impl<T, U, O> BitOr<U> for Vector<T>
    where T: BitOr<U, Output=O>, U: Clone
{
    type Output = Vector<O>;

    fn bitor(self, rhs: U) -> Vector<O> {
        self.map(|x| x | rhs.clone())
    }
}

impl<T, U, O> BitXor<U> for Vector<T>
    where T: BitXor<U, Output=O>, U: Clone
{
    type Output = Vector<O>;

    fn bitxor(self, rhs: U) -> Vector<O> {
        self.map(|x| x ^ rhs.clone())
    }
}

impl<T, O> Not for Vector<T>
    where T: Not<Output=O>
{
    type Output = Vector<O>;

    fn not(self) -> Vector<O> {
        self.map(|x| x.not())
    }
}

impl<T, U, O> Shl<U> for Vector<T>
    where T: Shl<U, Output=O>, U: Clone
{
    type Output = Vector<O>;

    fn shl(self, rhs: U) -> Vector<O> {
        self.map(|x| x << rhs.clone())
    }
}

impl<T, U, O> Shr<U> for Vector<T>
    where T: Shr<U, Output=O>, U: Clone
{
    type Output = Vector<O>;

    fn shr(self, rhs: U) -> Vector<O> {
        self.map(|x| x >> rhs.clone())
    }
}