Used cargo fmt

This commit is contained in:
2025-10-29 15:13:06 -06:00
parent 03f6211e69
commit 1d8accc626

View File

@@ -1,19 +1,39 @@
use num_traits::{Num, NumAssign, Signed, Float};
use core::panic;
use num_traits::{Float, Num, NumAssign, Signed};
use std::fmt::{self, Debug};
use std::ops::Add;
use std::ops::Sub;
use std::ops::Mul;
use std::ops::Sub;
#[derive(PartialEq, Eq, Debug)]
pub struct Matrix<T: Num + NumAssign + Signed + Float + fmt::Display + Copy+PartialEq + Debug + std::iter::Product<T>> {
pub struct Matrix<
T: Num
+ NumAssign
+ Signed
+ Float
+ fmt::Display
+ Copy
+ PartialEq
+ Debug
+ std::iter::Product<T>,
> {
rows: usize,
columns: usize,
data: Vec<T>,
}
impl<T: Num + NumAssign + Signed + Float + fmt::Display + Copy+PartialEq + Debug + std::iter::Product<T>> Matrix<T> {
impl<
T: Num
+ NumAssign
+ Signed
+ Float
+ fmt::Display
+ Copy
+ PartialEq
+ Debug
+ std::iter::Product<T>,
> Matrix<T>
{
pub fn new(rows: usize, columns: usize, default: T) -> Self {
Self {
rows,
@@ -40,7 +60,6 @@ impl<T: Num + NumAssign + Signed + Float + fmt::Display + Copy+PartialEq + Debug
let mut index = 0;
let mut data = Vec::new();
index += self.columns * row;
for i in 0..self.columns {
data.push(self.data[index + i]);
@@ -145,7 +164,6 @@ impl<T: Num + NumAssign + Signed + Float + fmt::Display + Copy+PartialEq + Debug
self.data[column2 + (i * self.columns)] = self.data[column1 + (i * self.columns)];
self.data[column1 + (i * self.columns)] = temp[i];
}
}
pub fn get_determinant(&self) -> T {
@@ -166,13 +184,13 @@ impl<T: Num + NumAssign + Signed + Float + fmt::Display + Copy+PartialEq + Debug
// Assign x to next row
let mut x = i + 1;
while pivot.is_zero() && x < self.rows {
if !trig_matrix.get(x, i).is_zero() {
if !trig_matrix.get(x, i).is_zero() {
trig_matrix.exchange_rows(x, i);
sign = -sign;
pivot = *trig_matrix.get(i, i);
break;
}
x += 1;
}
x += 1;
}
// Restart x for exchanging columns if necessary
x = i + 1;
@@ -197,7 +215,12 @@ impl<T: Num + NumAssign + Signed + Float + fmt::Display + Copy+PartialEq + Debug
while x < trig_matrix.rows {
let m = *trig_matrix.get(x, i) / pivot;
println!("m = {m}");
let new_row = trig_matrix.get_row(x).iter().zip(trig_matrix.get_row(i).iter()).map(|(a, b)| *a - m * *b).collect::<Vec<T>>();
let new_row = trig_matrix
.get_row(x)
.iter()
.zip(trig_matrix.get_row(i).iter())
.map(|(a, b)| *a - m * *b)
.collect::<Vec<T>>();
trig_matrix.set_row(x, new_row);
x += 1;
}
@@ -213,7 +236,18 @@ impl<T: Num + NumAssign + Signed + Float + fmt::Display + Copy+PartialEq + Debug
}
}
impl<T: Num + NumAssign + Signed + Float + fmt::Display + Copy+PartialEq + Debug + std::iter::Product<T>> Add for Matrix<T> {
impl<
T: Num
+ NumAssign
+ Signed
+ Float
+ fmt::Display
+ Copy
+ PartialEq
+ Debug
+ std::iter::Product<T>,
> Add for Matrix<T>
{
type Output = Self;
fn add(self, other: Self) -> Self::Output {
@@ -234,7 +268,18 @@ impl<T: Num + NumAssign + Signed + Float + fmt::Display + Copy+PartialEq + Debug
}
}
impl<T: Num + NumAssign + Signed + Float + fmt::Display + Copy+PartialEq + Debug + std::iter::Product<T>> Sub for Matrix<T> {
impl<
T: Num
+ NumAssign
+ Signed
+ Float
+ fmt::Display
+ Copy
+ PartialEq
+ Debug
+ std::iter::Product<T>,
> Sub for Matrix<T>
{
type Output = Self;
fn sub(self, other: Self) -> Self::Output {
@@ -255,7 +300,18 @@ impl<T: Num + NumAssign + Signed + Float + fmt::Display + Copy+PartialEq + Debug
}
}
impl<T: Num + NumAssign + Signed + Float + fmt::Display + Copy+PartialEq + Debug + std::iter::Product<T>> Mul for Matrix<T> {
impl<
T: Num
+ NumAssign
+ Signed
+ Float
+ fmt::Display
+ Copy
+ PartialEq
+ Debug
+ std::iter::Product<T>,
> Mul for Matrix<T>
{
type Output = Self;
fn mul(self, other: Self) -> Self::Output {
@@ -285,7 +341,18 @@ impl<T: Num + NumAssign + Signed + Float + fmt::Display + Copy+PartialEq + Debug
}
}
impl<T: Num + NumAssign + Signed + Float + fmt::Display + Copy+PartialEq + Debug + std::iter::Product<T>> fmt::Display for Matrix<T> {
impl<
T: Num
+ NumAssign
+ Signed
+ Float
+ fmt::Display
+ Copy
+ PartialEq
+ Debug
+ std::iter::Product<T>,
> fmt::Display for Matrix<T>
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut display = String::new();
let mut index = 0;
@@ -379,7 +446,7 @@ mod tests {
matrix1.set(0, 2, 9.0);
matrix1.set(0, 3, 6.0);
let data = vec![8.0, 7.0 , 9.0, 6.0];
let data = vec![8.0, 7.0, 9.0, 6.0];
assert_eq!(matrix1.get_row(0), data);
}
@@ -406,7 +473,7 @@ mod tests {
let _data = matrix1.get_row(4);
}
#[test]
#[test]
fn get_column_1() {
let mut matrix1 = Matrix::new(4, 3, 0.0);
matrix1.set(0, 0, 8.0);
@@ -414,7 +481,7 @@ mod tests {
matrix1.set(2, 0, 9.0);
matrix1.set(3, 0, 6.0);
let data = vec![8.0, 7.0 , 9.0, 6.0];
let data = vec![8.0, 7.0, 9.0, 6.0];
assert_eq!(matrix1.get_column(0), data);
}