//Inverse la matrice avec l'affichage en sortie. public Matrice Inverse(out List <String> display) { if (IsInversible()) { Matrice inverse; display = new List <string>(); Matrice LPrime = InverseL(); Matrice UPrime = InverseU(); inverse = UPrime.Product(LPrime); //Permute les colonnes si il y a eu des permutation de lignes. for (int i = Swaps.GetLength(0) - 1; i >= 0; i--) { if (Swaps[i, 0] != -1) { inverse.SwapCol(Swaps[i, 0], Swaps[i, 1]); display.Add("Permutation de colonne : " + (Swaps[i, 0] + 1) + " <->" + (Swaps[i, 1] + 1)); } } return(inverse); } else { throw new Exception("La matrice n'est pas inversible : déterminant = 0"); } }
//Inverse la matrice. public void Inverse() { if (IsInversible()) { Matrice LPrime = InverseL(); Matrice UPrime = InverseU(); this.values = (UPrime.Product(LPrime)).values; //Permute les colonnes si il y a eu des permutation de lignes. for (int i = 0; i < Swaps.GetLength(0); i++) { if (Swaps[i, 0] != -1) { SwapCol(Swaps[i, 0], Swaps[i, 1]); } } } else { throw new Exception("La matrice n'est pas inversible : déterminant = 0"); } }
//Effectue les calculs et écrit les résultats. public static void Output(Matrice mat, IOutput output) { List <String> display; bool flagSwaps; if (mat != null) { try { //Affiche l'entête. output.Head(); //Affiche la matrice initiale. output.AfficherMatrice(mat); //Affiche la triangulisation par Gauss après avoir fait la décomposition LU. mat.DecompositionLU(out display); output.AfficherGauss(display); //Affichage des permutations output.AfficherPermutations(mat, out flagSwaps); //Affiche la matrice U output.AfficherMatriceU(mat); //Affiche la matrice L output.AfficherMatriceL(mat); //Affiche la vérification. Matrice A = mat.MatriceL.Product(mat.MatriceU); //On refait les permutation pour vérifier si c'est la même matrice. for (int i = 0; i < mat.Swaps.GetLength(0); i++) { if (mat.Swaps[i, 0] != -1) { A.SwapLn(mat.Swaps[i, 0], mat.Swaps[i, 1]); } } //Affiche la vérification de la décomposition. output.AfficherVerificationDecomp(mat, A, out flagSwaps); if (mat.Equals(A)) { output.WriteLine("Matrices identiques ! "); output.WriteLine("Le determinant de la matrice = " + mat.Determinant); output.WriteLine(""); } else { throw new Exception("Les matrices ne sont pas identiques !"); } //Affiche l'inverse des matrices. bool check; Matrice LPrime = mat.InverseL(out display); output.AfficherLInverse(ref display); /*Test*/ output.WriteLine("Vérification inverse L*L(-1)"); foreach (var item in (mat.MatriceL.Product(LPrime)).Print()) { output.WriteLine(item); } /*Test*/ Matrice UPrime = mat.InverseU(out display); output.AfficherUInverse(ref display); /*Test*/ output.WriteLine("Vérification inverse U*U(-1)"); foreach (var item in (mat.MatriceU.Product(UPrime)).Print()) { output.WriteLine(item); } /*Test*/ //Affiche la matrice inverse. Matrice mInverse = mat.Inverse(out display); output.AfficherInverse(mInverse, ref display); //Vérification Matrice res = mInverse.Product(mat); if (res.Equals(Matrice.InitIdentite(mat.NbrLn))) { check = true; } else { check = false; } output.AfficherVerification(mInverse, mat, res, check); } catch (Exception ex) { throw new Exception(ex.Message); } } }