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