mod mul_impl;
mod add_impl;
mod lambda;
mod map;
use crate::vector_impl::Vector;
#[allow(dead_code)]
#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct Matrix<T>
{
rows : usize,
cols : usize,
matrix : Vec<Vec<T>>
}
impl<T> Matrix<T>
where
T: Copy
{
pub fn into_vector(self) -> Vector<T> {
if self.rows == 1 {
let mut params = Vec::with_capacity(self.cols);
for col in &self.matrix[0] {
params.push(*col)
}
return Vector::from(params)
}
else if self.cols == 1 {
let mut params: Vec<T> = Vec::with_capacity(self.rows);
for row in self.matrix {
params.push(row[0])
}
return Vector::from(params);
}
else {
panic!("Cannot convert matrix because neither rows nor columns are 1")
}
}
}
impl<T> Matrix<T> {
pub fn rows(&self) -> usize {
self.rows
}
pub fn cols(&self) -> usize {
self.cols
}
pub fn into_inner(self) -> Vec<Vec<T>> {
self.matrix
}
}
impl<T> From<Vec<Vec<T>>> for Matrix<T> {
fn from(params: Vec<Vec<T>>) -> Self {
let rows = params.len();
let cols = params[0].len();
for row in 1..rows {
if params[row].len() != cols {
panic!("Input 2D Vec does not have same length for all rows")
}
}
Matrix {
rows : params.len(),
cols : params[0].len(),
matrix : params
}
}
}