rust_linear_algebra/matrix/
row_echelon.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use super::Matrix;
use crate::num::MinusOne;
use crate::num::One;
use std::fmt::Debug;
use std::ops::{Add, Div, Mul, Sub, SubAssign};

impl<K> Matrix<K>
where
    K: Debug
        + Copy
        + One
        + MinusOne
        + Default
        + PartialEq
        + Add<Output = K>
        + Sub<Output = K>
        + SubAssign<K>
        + Mul<Output = K>
        + Div<Output = K>,
{
    pub fn row_echelon(&self) -> Matrix<K> {
        if self.elements.is_empty() || self.elements[0].is_empty() {
            return self.clone();
        }
        if self.is_row_echelon_form() {
            return self.clone();
        }
        self.gaussian_elimination(None, None)
    }
}

impl<K> Matrix<K>
where
    K: Debug + Copy + Default + PartialEq,
{
    pub fn is_row_echelon_form(&self) -> bool {
        let mut last_pivot_col = None;

        for row in 0..self.rows() {
            let pivot_col = self.elements[row].iter().position(|&x| x != K::default());

            match pivot_col {
                Some(col) => {
                    if let Some(last_col) = last_pivot_col {
                        if col <= last_col {
                            return false;
                        }
                    }
                    last_pivot_col = Some(col);

                    for below_row in row + 1..self.rows() {
                        if self.elements[below_row][col] != K::default() {
                            return false;
                        }
                    }
                }
                None => {
                    for below_row in row + 1..self.rows() {
                        if self.elements[below_row].iter().any(|&x| x != K::default()) {
                            return false;
                        }
                    }
                }
            }
        }

        true
    }
}