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
185
186
use std::ops::{Add, Deref, Index, IndexMut, Sub, Neg, Mul, Div};
use structure::vector::{VecOps, FPVector};
use PowOps;

/// Smart pointer of Vector
pub struct RedoxVector {
    pub data: Vec<f64>
}

impl RedoxVector {
    pub fn new(n: usize) -> Self {
        RedoxVector {
            data: vec![f64::default(); n]
        }
    }

    pub fn from_vec(v: Vec<f64>) -> Self {
        RedoxVector {
            data: v
        }
    }
}

impl Deref for RedoxVector {
    type Target = Vec<f64>;

    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

impl Index<usize> for RedoxVector {
    type Output = f64;

    fn index(&self, index: usize) -> &Self::Output {
        self.data.index(index)
    }
}

impl IndexMut<usize> for RedoxVector {
    fn index_mut(&mut self, index: usize) -> &mut f64 {
        self.data.index_mut(index)
    }
}

impl Neg for RedoxVector {
    type Output = RedoxVector;

    fn neg(self) -> Self::Output {
        RedoxVector::from_vec(self.data.into_iter().map(|x| -x).collect())
    }
}

impl Add<f64> for RedoxVector {
    type Output = RedoxVector;

    fn add(self, rhs: f64) -> Self::Output {
        RedoxVector::from_vec(self.data.s_add(rhs))
    }
}

impl Sub<f64> for RedoxVector {
    type Output = RedoxVector;

    fn sub(self, rhs: f64) -> Self::Output {
        RedoxVector::from_vec(self.data.s_sub(rhs))
    }
}

impl Mul<f64> for RedoxVector {
    type Output = RedoxVector;

    fn mul(self, rhs: f64) -> Self::Output {
        RedoxVector::from_vec(self.data.s_mul(rhs))
    }
}

impl Div<f64> for RedoxVector {
    type Output = RedoxVector;

    fn div(self, rhs: f64) -> Self::Output {
        RedoxVector::from_vec(self.data.s_div(rhs))
    }
}

impl Add<RedoxVector> for f64 {
    type Output = RedoxVector;

    fn add(self, rhs: RedoxVector) -> Self::Output {
        rhs.add(self)
    }
}

impl Sub<RedoxVector> for f64 {
    type Output = RedoxVector;

    fn sub(self, rhs: RedoxVector) -> Self::Output {
        RedoxVector::from_vec(rhs.data.into_iter().map(|x| self - x).collect())
    }
}

impl Mul<RedoxVector> for f64 {
    type Output = RedoxVector;

    fn mul(self, rhs: RedoxVector) -> Self::Output {
        rhs.mul(self)
    }
}

impl Add<RedoxVector> for RedoxVector {
    type Output = RedoxVector;

    fn add(self, rhs: Self) -> Self::Output {
        RedoxVector::from_vec(self.data.add(&rhs.data))
    }
}

impl<'a, 'b> Add<&'b RedoxVector> for &'a RedoxVector {
    type Output = RedoxVector;

    fn add(self, rhs: &'b RedoxVector) -> Self::Output {
        RedoxVector::from_vec(self.data.add(&rhs.data))
    }
}

impl Sub<RedoxVector> for RedoxVector {
    type Output = RedoxVector;

    fn sub(self, rhs: Self) -> Self::Output {
        RedoxVector::from_vec(self.data.sub(&rhs.data))
    }
}

impl<'a, 'b> Sub<&'b RedoxVector> for &'a RedoxVector {
    type Output = RedoxVector;

    fn sub(self, rhs: &'b RedoxVector) -> Self::Output {
        RedoxVector::from_vec(self.data.sub(&rhs.data))
    }
}

/// Dot product
impl Mul<RedoxVector> for RedoxVector {
    type Output = f64;

    fn mul(self, rhs: Self) -> Self::Output {
        self.data.dot(&rhs.data)
    }
}

impl<'a, 'b> Mul<&'b RedoxVector> for &'a RedoxVector {
    type Output = f64;

    fn mul(self, rhs: &'b RedoxVector) -> Self::Output {
        self.data.dot(&rhs.data)
    }
}

impl PowOps for RedoxVector {
    fn powi(&self, n: i32) -> Self {
        RedoxVector::from_vec(self.data.fmap(|x| x.powi(n)))
    }

    fn powf(&self, f: f64) -> Self {
        RedoxVector::from_vec(self.data.fmap(|x| x.powf(f)))
    }

    fn pow(&self, f: Self) -> Self {
        unimplemented!()
    }

    fn sqrt(&self) -> Self {
        RedoxVector::from_vec(self.data.fmap(|x| x.sqrt()))
    }
}

/// Redox wrap for Vector
pub trait Redoxable {
    fn redox(self) -> RedoxVector;
}

impl Redoxable for Vec<f64> {
    fn redox(self) -> RedoxVector {
        RedoxVector::from_vec(self)
    }
}