use std::cmp;
use num_complex::{Complex, Complex64};
#[derive(Debug, PartialEq)]
pub struct Vector {
data: Vec<Complex64>
}
impl Vector {
pub fn new(data: Vec<Complex64>) -> Vector {
Vector { data: data }
}
pub fn from_reals(v: Vec<f64>) -> Vector {
let data: Vec<Complex64> = v.iter().map(|x| Complex::new(*x, 0.)).collect();
Vector::new(data)
}
pub fn to_vec(&self) -> &Vec<Complex64> {
&self.data
}
pub fn len(&self) -> usize {
self.data.len()
}
pub fn at(&self, i: usize) -> Complex64 {
if i < self.data.len() {
self.data[i]
} else {
Complex::new(0., 0.)
}
}
pub fn scale(&self, a: f64) -> Vector {
let data = self.to_vec().iter().map(|x| x*a).collect();
Vector::new(data)
}
pub fn add(&self, v: &Vector) -> Vector {
let size = cmp::max(self.data.len(), v.len());
let mut x: Vec<Complex64> = Vec::with_capacity(size);
for n in 0..size {
x.push(self.at(n) + v.at(n));
}
Vector::new(x)
}
pub fn multiply(&self, v: &Vector) -> Vector {
let size = cmp::min(self.data.len(), v.len());
let mut x: Vec<Complex64> = Vec::with_capacity(size);
for n in 0..size {
x.push(self.at(n) * v.at(n));
}
Vector::new(x)
}
}
#[cfg(test)]
mod tests {
use num_complex::{Complex};
use super::*;
#[test]
fn test_init() {
let v = Vector::from_reals(vec![1., 2., 3., 4.]);
assert!(v == Vector::new(vec![Complex::new(1., 0.),
Complex::new(2., 0.),
Complex::new(3., 0.),
Complex::new(4., 0.)]));
}
#[test]
fn test_scale() {
let v = Vector::from_reals(vec![1., 2., 3., 4.]);
let v1 = v.scale(-2.0);
assert!(v1 == Vector::new(vec![Complex::new(-2., 0.),
Complex::new(-4., 0.),
Complex::new(-6., 0.),
Complex::new(-8., 0.)]));
}
#[test]
fn test_add() {
let x = Vector::new(vec![Complex::new(1., 2.),
Complex::new(2., 4.),
Complex::new(3., 6.),
Complex::new(4., 8.)]);
let y = Vector::from_reals(vec![2., 3., 4.]);
let z = x.add(&y);
assert!(z == Vector::new(vec![Complex::new(3., 2.),
Complex::new(5., 4.),
Complex::new(7., 6.),
Complex::new(4., 8.)]));
}
#[test]
fn test_multiply() {
let x = Vector::new(vec![Complex::new(1., 2.),
Complex::new(2., 4.),
Complex::new(3., 6.),
Complex::new(4., 8.)]);
let y = Vector::new(vec![Complex::new(2., 4.),
Complex::new(3., 6.),
Complex::new(4., 1.)]);
let z = x.multiply(&y);
assert!(z == Vector::new(vec![Complex::new(-6., 8.),
Complex::new(-18., 24.),
Complex::new(6., 27.)]));
}
}