use std::ops::{Add, Mul, AddAssign};
#[deny(missing_docs)]
use crate::matrix::Matrix;
#[allow(dead_code)]
#[derive(Debug, PartialEq, Eq)]
pub struct Vector<T>
{
size : usize,
list : Vec<T>
}
#[allow(dead_code)]
impl<T> Vector<T>
where
T: Copy
{
pub fn to_col_matrix(self) -> Matrix<T> {
let mut matrix = vec![];
for param in self.list {
matrix.push(vec![param])
}
Matrix::new(matrix.len(), 1, matrix)
}
pub fn to_row_matrix(self) -> Matrix<T> {
Matrix::new(1, self.list.len(), vec![self.list])
}
pub fn size(&self) -> usize {
self.size
}
pub fn list(&self) -> &Vec<T> {
&self.list
}
pub fn new(size: usize, list: Vec<T>) -> Self {
Vector { size: size, list: list }
}
}
impl<T> From<Vec<T>> for Vector<T>
{
fn from(vec: Vec<T>) -> Self {
Vector {
size : vec.len(),
list : vec
}
}
}
impl<T> Mul for Vector<T>
where
T: Copy + Mul<Output = T> + AddAssign + Default
{
type Output = T;
fn mul(self, rhs: Self) -> Self::Output {
if self.size != rhs.size {
panic!("Cannot find dot product of two differently sized vectors.")
}
let mut product = T::default();
for idx in 0..self.size {
product += self.list[idx] * rhs.list[idx]
}
product
}
}
impl<T> Add for Vector<T>
where
T: Add<Output = T> + Copy
{
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
if self.size != rhs.size {
panic!("Vectors with different sizes cannot be added together.")
}
let mut params = vec![];
for idx in 0..self.size {
params.push(self.list[idx] + rhs.list[idx])
}
Vector::from(params)
}
}