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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
use core::ops::{Index, IndexMut, Neg, Add, Sub, Mul, Div};
use core::ops::{AddAssign, SubAssign, MulAssign, DivAssign};
use std::{fmt, fs::File, io::Write, cmp::Ordering};
use rand::Rng;

pub use crate::traits::{Number, Signed, Zero, One};
pub use crate::complex::Complex;

pub struct Vector<T> {
    vec: Vec<T>,
    size: usize,
}

pub type Vec64 = Vector<f64>;

impl<T> Vector<T> {
    /// Create a new vector of unspecified size
    #[inline]
    pub const fn empty() -> Self {
        let vec = Vec::<T>::new();
        let size = 0;
        Vector { vec, size }
    }

    /// Create a vector from an std::vec::Vec
    #[inline]
    pub fn create( vec: Vec<T> ) -> Self {
        let size = vec.len();
        Vector { vec, size }
    }

    /// Return the size of the vector 
    #[inline]
    pub fn size(&self) -> usize {
        self.size
    }

    /// Remove all of the element from the vector
    #[inline]
    pub fn clear(&mut self) {
        self.vec.clear();
        self.size = 0;
    }

    /// Swap elements two elements in the vector 
    #[inline]
    pub fn swap(&mut self, i: usize, j: usize) {
        self.vec.swap( i, j );
    }

    /// Push a new element onto the end of the vector 
    #[inline]
    pub fn push(&mut self, elem: T ) {
        self.vec.push( elem );
        self.size += 1;
    }

    /// Insert a new element into the Vector at a specified position 
    #[inline]
    pub fn insert(&mut self, pos: usize, new_elem: T ) {
        self.vec.insert( pos, new_elem );
        self.size += 1;
    }

    /// Remove the last element from the vector and return it
    #[inline]
    pub fn pop(&mut self) -> T {
        let result = self.vec.pop();
        self.size -= 1;
        result.unwrap()
    }
}

impl<T: std::cmp::PartialEq> Vector<T> {
    /// Return the index of the first element in the Vector equal to a given value 
    #[inline]
    pub fn find(&self, value: T ) -> usize {
        let index = self.vec.iter().position( |x| *x == value );
        match index {
            None => panic!( "Entry not found in Vector." ),
            Some(index) => return index,
        }
    }
}

impl<T: Clone> Vector<T> {
    /// Create a new vector of specified size
    #[inline]
    pub fn new( size: usize, elem: T ) -> Self {
        let vec = vec![ elem; size ];
        Vector { vec, size }
    }

    /// Assign a value to every element in the vector
    #[inline]
    pub fn assign(&mut self, elem: T ) {
        for i in 0..self.size {
            self.vec[i] = elem.clone();
        }
    }
}

impl<T: Clone + Number> Vector<T> {
    /// Create a vector of zeros 
    #[inline]
    pub fn zeros( size: usize ) -> Self {
        let vec = vec![ T::zero(); size ];
        Vector{ vec, size }
    }

    /// Create a vector of ones
    #[inline]
    pub fn ones( size: usize ) -> Self {
        let vec = vec![ T::one(); size ];
        Vector{ vec, size }
    }
}

impl<T: Clone> Clone for Vector<T> {
    /// Clone the vector
    #[inline]
    fn clone(&self) -> Self {
        Self::create( self.vec.clone() )
    }
}

impl<T> Index<usize> for Vector<T> {
    type Output = T;
    /// Indexing operator [] (read only)
    #[inline]
    fn index<'a>(&'a self, index: usize ) -> &'a T {
        &self.vec[ index ]
    }
}

impl<T> IndexMut<usize> for Vector<T> {
    /// Indexing operator [] (read/write)
    #[inline]
    fn index_mut(&mut self, index: usize ) -> &mut T {
        &mut self.vec[ index ] 
    }
}

impl<T: Clone + Neg<Output = T>> Neg for Vector<T> {
    type Output = Self;
    /// Return the unary negation ( unary - ) of each element
    #[inline]
    fn neg(self) -> Self::Output {
        let mut result = self.vec.clone();
        for i in 0..result.len() {
            result[i] = -result[i].clone();
        }
        Self::Output::create( result )
    }
}

impl<T: Clone + Number> Add<Vector<T>> for Vector<T> {
    type Output = Self;
    /// Add the elements of two vectors together ( binary + )
    #[inline]
    fn add(self, plus: Self) -> Self::Output {
        if self.size != plus.size { panic!( "Vector sizes do not agree (+)." ); }
        let mut result = Vec::new();
        for i in 0..self.size {
            result.push( self.vec[i].clone() + plus.vec[i].clone() );
        }
        Self::Output::create( result )
    }
}

impl<T: Clone + Number> Sub<Vector<T>> for Vector<T> {
    type Output = Self;
    /// Subtract the elements of one vector from another ( binary - )
    #[inline]
    fn sub(self, minus: Self) -> Self::Output {
        if self.size != minus.size { panic!( "Vector sizes do not agree (-)." ); }
        let mut result = Vec::new();
        for i in 0..self.size {
            result.push( self.vec[i].clone() - minus.vec[i].clone() );
        }
        Self::Output::create( result )
    }
}

impl<T: Clone + Number> Mul<T> for Vector<T> {
    type Output = Self;
    /// Multiply a vector by a scalar (vector * scalar)
    #[inline]
    fn mul(self, scalar: T) -> Self::Output {
        let mut result = Vec::new();
        for i in 0..self.size {
            result.push( self.vec[i].clone() * scalar.clone() );
        }
        Self::Output::create( result )
    }
}

impl Mul<Vector<f64>> for f64 {
    type Output = Vector<f64>;
    /// Allow multiplication on the left by f64 (f64 * vector)
    #[inline]
    fn mul(self, vector: Vector<f64>) -> Self::Output {
        let mut result = Vec::new();
        for i in 0..vector.size {
            result.push( self.clone() * vector.vec[i].clone() );
        }
        Self::Output::create( result )
    }
}

impl<T: Clone + Number> Div<T> for Vector<T> {
    type Output = Self;
    /// Divide a vector by a scalar (vector / scalar)
    fn div(self, scalar: T) -> Self::Output {
        let mut result = Vec::new();
        for i in 0..self.size {
            result.push( self.vec[i].clone() / scalar.clone() );
        }
        Self::Output::create( result )
    }
}

impl<T: Clone + Number> AddAssign for Vector<T> {
    /// Add a vector to a mutable vector and assign the result ( += )
    fn add_assign(&mut self, rhs: Self) {
        if self.size != rhs.size { panic!( "Vector sizes do not agree (+=)." ); }
        for i in 0..self.size {
            self.vec[i] += rhs.vec[i].clone();
        }
    }
}

impl<T: Clone + Number> SubAssign for Vector<T> {
    /// Substract a vector from a mutable vector and assign the result ( -= )
    fn sub_assign(&mut self, rhs: Self) {
        if self.size != rhs.size { panic!( "Vector sizes do not agree (-=)." ); }
        for i in 0..self.size {
            self.vec[i] -= rhs.vec[i].clone();
        }
    }
}

impl<T: Clone + Number> AddAssign<T> for Vector<T> {
    /// Add the same value to every element in a mutable vector 
    fn add_assign(&mut self, rhs: T) {
        for i in 0..self.size {
            self.vec[i] += rhs.clone();
        }
    }
} 

impl<T: Clone + Number> SubAssign<T> for Vector<T> {
    /// Subtract the same value from every element in a mutable vector 
    fn sub_assign(&mut self, rhs: T) {
        for i in 0..self.size {
            self.vec[i] -= rhs.clone();
        }
    }
} 

impl<T: Clone + Number> MulAssign<T> for Vector<T> {
    /// Multiply every element in a mutable vector by a scalar 
    fn mul_assign(&mut self, rhs: T) {
        for i in 0..self.size {
            self.vec[i] *= rhs.clone();
        }
    }
} 

impl<T: Clone + Number> DivAssign<T> for Vector<T> {
    /// Divide every element in a mutable vector by a scalar 
    fn div_assign(&mut self, rhs: T) {
        for i in 0..self.size {
            self.vec[i] /= rhs.clone();
        }
    }
} 

impl<T: std::default::Default> Vector<T> {
    /// Resize the vector 
    #[inline]
    pub fn resize(&mut self, new_size: usize ) {
        self.vec.resize_with( new_size, Default::default );
        self.size = new_size;
    }
}

impl<T: Clone + Number> Vector<T> {
    /// Return the dot product of two vectors v.dot(w)
    #[inline]
    pub fn dot(&self, w: Vector<T>) -> T {
        if self.size != w.size { panic!( "Vector sizes do not agree dot()." ); }
        let mut result: T = T::zero();
        for i in 0..self.size {
            result += self.vec[i].clone() * w.vec[i].clone();
        }
        result
    }

    /// Return the sum of all the elements in the vector
    #[inline]
    pub fn sum(&self) -> T {
        self.sum_slice( 0, self.size - 1 )
    }

    /// Return the sum of the elements in the vector from index start to end 
    #[inline]
    pub fn sum_slice(&self, start: usize, end: usize) -> T {
        if start > end { panic!( "Vector sum: start > end." ); }
        if self.size <= start { panic!( "Vector range error." ); }
        if self.size <= end { panic!( "Vector range error." ); }
        let mut result: T = T::zero();
        for i in start..=end {
            result += self.vec[i].clone();
        }
        result
    }

    /// Return the product of all the elements in the vector
    #[inline]
    pub fn product(&self) -> T {
        self.product_slice( 0, self.size - 1 )
    }

    /// Return the product of the elements in the vector from index start to end
    #[inline]
    pub fn product_slice(&self, start: usize, end: usize) -> T {
        if start > end { panic!( "Vector sum: start > end." ); }
        if self.size <= start { panic!( "Vector range error." ); }
        if self.size <= end { panic!( "Vector range error." ); }
        let mut result: T = self.vec[start].clone();
        for i in start+1..=end {
            result *= self.vec[i].clone();
        }
        result
    }
}

impl<T: fmt::Display> Vector<T> {
    /// Print the vector to a file
    #[inline]
    pub fn output(&self, filename: &str) {
        let mut f = File::create(filename).expect("Unable to create file");
        for i in 0..self.size {                                                                                                                                                                  
            writeln!(f, "{}", self.vec[i]).unwrap();                                                                                                                            
        }
    }
}

impl<T: Clone + Signed> Vector<T> {
    /// Return a vector containing the absolute values of the elements 
    #[inline]
    pub fn abs(&self) -> Self {
        let size = self.size;
        let mut vec = vec![ T::zero(); size ];
        for i in 0..size {
            vec[i] = self.vec[i].abs();
        }
        Vector { vec, size }
    }

    /// Return the L1 norm: sum of absolute values 
    #[inline]
    pub fn norm_1(&self) -> T {
        let mut result = T::zero();
        for i in 0..self.size {
            result += self.vec[i].abs();
        }
        result
    }
}

impl<T: Clone + Signed> Vector<Complex::<T>> {
    /// Return a vector containing the complex conjugate of the elements
    #[inline]
    pub fn conj(&self) -> Self {
        let size = self.size;
        let mut vec = vec![ Complex::<T>::zero(); size ];
        for i in 0..size {
            vec[i] = self.vec[i].clone().conj();
        }
        Vector { vec, size }
    }
}

impl<T: Clone + Number> Vector<Complex::<T>> {
    /// Return a vector containing the real parts of a vector of complex numbers
    #[inline]
    pub fn real(&self) -> Vector<T> {
        let size = self.size;
        let mut vec = vec![ T::zero(); size ];
        for i in 0..size {
            vec[i] = self.vec[i].clone().real;
        }
        Vector { vec, size }
    } 
}

impl Vector<f64> {
    /// Create a linearly spaced vector of f64 with n elements with lower limit a
    /// and upper limit b
    #[inline]
    pub fn linspace( a: f64, b: f64, size: usize ) -> Self {
        let mut vec = vec![ 0.0; size ];
        let h: f64 = ( b - a ) / ((size as f64) - 1.0);
        for i in 0..size {
            vec[i] = a + h * (i as f64);
        }
        Vector{ vec, size }
    }

    /// Create a nonuniform vector of f64 with n elements with lower limit a,
    /// upper limit b and exponent p (p=1 -> linear)
    #[inline]
    pub fn powspace( a: f64, b: f64, size: usize, p: f64 ) -> Self {
        let mut vec = vec![ 0.0; size ];
        for i in 0..size {
            vec[i] = a + (b - a) * libm::pow( (i as f64) / ((size as f64) - 1.0), p );
        }
        Vector{ vec, size }
    }

    /// Return the L2 norm: square root of the sum of the squares
    #[inline]
    pub fn norm_2(&self) -> f64 {
        let mut result = 0.0;
        for i in 0..self.size {
            result += libm::pow( self.vec[i].abs(), 2.0 );
        }
        libm::sqrt( result )
    }

    /// Return the Lp norm: p-th root of the sum of the absolute values 
    /// raised to the power p
    #[inline]
    pub fn norm_p(&self, p: f64 ) -> f64 {
        let mut result = 0.0;
        for i in 0..self.size {
            result += libm::pow( self.vec[i].abs(), p );
        }
        libm::pow( result, 1.0/p )
    }

    /// Return the Inf norm: largest absolute value element (p -> infinity)
    #[inline]
    pub fn norm_inf(&self) -> f64 {
        let mut result = self.vec[0].abs();
        for i in 1..self.size {
            if result < self.vec[i].abs() {
                result = self.vec[i].abs();
            }
        }
        result
    }

    // Create a vector containing n random elements between 0 and 1
    #[inline]
    pub fn random( size: usize ) -> Self {
        let mut vec = vec![ 0.0; size ];
        let mut rng = rand::thread_rng();
        for i in 0..size {
            vec[i] = rng.gen::<f64>()
        }
        Vector{ vec, size }
    }
}

impl<T> fmt::Debug for Vector<T> where
    T: fmt::Debug
{
    /// Format the output [ v_0, v_1, ... ]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self.vec )
    }
}

impl<T> fmt::Display for Vector<T> where
    T: fmt::Debug
{
    /// Format the output [ v_0, v_1, ... ]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self.vec )
    }
} 

impl<T: Ord + PartialEq + PartialOrd> Vector<T>
{
    /// Sort the Vector of elements
    pub fn sort(&mut self) {
        self.vec.sort_unstable();
    }
}

impl<T: PartialEq + PartialOrd> Vector<T>
{
    /// Sort the Vector of elements using a comparison function
    pub fn sort_by<F>(&mut self, compare: F )
    where
        F: FnMut(&T, &T) -> Ordering,
    {
        self.vec.sort_unstable_by( compare );
    }
}