use crate::error::MatrixError;
use crate::matrix::Matrix;
use crate::number::Zero;
pub fn points<T, R>(
mut f: impl FnMut(usize, usize) -> (T, R),
row: usize,
col: usize,
) -> Vec<(T, R)> {
let mut ps = Vec::with_capacity(row * col);
for r in 1..=row {
for c in 1..=col {
ps.push(f(r, c))
}
}
ps
}
pub fn horizontal_concat<T, const ROW: usize, const COL: usize, const RCOL: usize>(
mat: &Matrix<T, ROW, COL>,
rhs: &Matrix<T, ROW, RCOL>,
) -> Result<Matrix<T, ROW, { COL + RCOL }>, MatrixError>
where
T: Clone + Default,
{
let mut hmat = Matrix::zeros()?;
for r in 1..=ROW {
for c1 in 1..=COL {
hmat.set_element(r, c1, mat.get_element(r, c1)?.to_owned())?;
}
for c2 in 1..=RCOL {
hmat.set_element(r, COL + c2, rhs.get_element(r, c2)?.to_owned())?;
}
}
Ok(hmat)
}
pub fn vertical_concat<T, const ROW: usize, const COL: usize, const RROW: usize>(
mat: &Matrix<T, ROW, COL>,
rhs: &Matrix<T, RROW, COL>,
) -> Result<Matrix<T, { ROW + RROW }, COL>, MatrixError>
where
T: Clone + Default,
{
Matrix::create([&mat.inner[..], &rhs.inner[..]].concat())
}
pub fn solve_linear_equations() {
todo!()
}
pub fn is_sqaure_matrix<T, const ROW: usize, const COL: usize>(_: &Matrix<T, ROW, COL>) -> bool {
ROW == COL
}
pub fn is_upper_triangle_matrix<T, const ROW: usize, const COL: usize>(
m: &Matrix<T, ROW, COL>,
) -> Result<bool, MatrixError>
where
T: Zero,
{
Ok(points(|r, c| (r, c), ROW, COL)
.iter()
.filter(|(r, c)| r > c)
.all(|(r, c)| {
m.get_element(r.to_owned(), c.to_owned())
.unwrap_or(&T::default())
.to_owned()
.is_zero()
}))
}