// Copyright (c) the JPEG XL Project Authors. All rights reserved. // // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file.
fn det2x2(a: f64, b: f64, c: f64, d: f64) -> f64 {
a * d - b * c
}
fn calculate_cofactor(m: &Matrix3x3<f64>, r: usize, c: usize) -> f64 { // Determine the actual row and column indices for the 2x2 submatrix // by excluding the current row 'r' and column 'c'. // Ensure they are taken in ascending order to form the submatrix consistently. letmut sub_rows = [0; 2]; letmut sub_cols = [0; 2];
letmut current_idx = 0; for i in0..3 { if i != r {
sub_rows[current_idx] = i;
current_idx += 1;
}
}
current_idx = 0; for i in0..3 { if i != c {
sub_cols[current_idx] = i;
current_idx += 1;
}
}
let minor_val = det2x2(
m[sub_rows[0]][sub_cols[0]],
m[sub_rows[0]][sub_cols[1]],
m[sub_rows[1]][sub_cols[0]],
m[sub_rows[1]][sub_cols[1]],
);
// Apply the checkerboard pattern sign for the cofactor if (r + c).is_multiple_of(2) {
minor_val
} else {
-minor_val
}
}
/// Calculates the inverse of a 3x3 matrix. pubfn inv_3x3_matrix(m: &Matrix3x3<f64>) -> Result<Matrix3x3<f64>, Error> { let cofactor_matrix: [[f64; 3]; 3] = std::array::from_fn(|r_idx| {
std::array::from_fn(|c_idx| calculate_cofactor(m, r_idx, c_idx))
});
let det = m[0]
.iter()
.zip(cofactor_matrix[0].iter())
.map(|(&m_element, &cof_element)| m_element * cof_element)
.sum::<f64>();
// Check for numerical singularity. const EPSILON: f64 = 1e-12; if det.abs() < EPSILON { return Err(Error::MatrixInversionFailed(det.abs()));
}
let inv_det = 1.0 / det;
let adjugate_matrix: [[f64; 3]; 3] =
std::array::from_fn(|r_idx| std::array::from_fn(|c_idx| cofactor_matrix[c_idx][r_idx]));
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.