///|
priv struct LuDecomposition[T] {
lu : Matrix[T]
permutation : Array[Int32]
swap_count : Int32
singular : Bool
}
///|
fn[T : Compare + Field + Num + Tolerance] Matrix::lu_decompose_internal(
self : Matrix[T],
) -> LuDecomposition[T] {
let lu = self.copy()
let n = lu.row
let data = lu.data
let permutation = Array::makei(n, fn(i) { i })
let mut swap_count = 0
let mut singular = false
let tol = Tolerance::tolerance()
for j in 0.. Num::abs(data[max_row * n + j]) {
max_row = i
}
}
if max_row != j {
lu.swap_rows(j, max_row)
let temp = permutation[j]
permutation[j] = permutation[max_row]
permutation[max_row] = temp
swap_count += 1
}
let pivot = data[j * n + j]
if Num::abs(pivot) < tol {
singular = true
continue
}
let pivot_row_offset = j * n
for i in (j + 1).. T {
if self.singular {
return Zero::zero()
}
let n = self.lu.row
let lu_data = self.lu.data
let mut det = if (self.swap_count & 1) == 0 {
One::one()
} else {
-One::one()
}
for i in 0.. Matrix[T]? {
if self.singular {
return None
}
let n = self.lu.row
let cols = rhs.col
let lu_data = self.lu.data
let rhs_data = rhs.data
let permutation = self.permutation
let tol = Tolerance::tolerance()
let y = Matrix::new(n, cols, Zero::zero())
let y_data = y.data
for i in 0.. Matrix[T]? {
if self.singular {
return None
}
let n = self.lu.row
let lu_data = self.lu.data
let permutation = self.permutation
let tol = Tolerance::tolerance()
let work = Array::make(n, Zero::zero())
let x = Matrix::new(n, n, Zero::zero())
let x_data = x.data
for rhs_col in 0..