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