public void AfficherVerificationDecomp(Matrice mat, Matrice A, out bool flagSwaps) { Flux.WriteLine("==================================================================================================="); Flux.WriteLine("= Vérification A=L*U ="); Flux.WriteLine("==================================================================================================="); Flux.WriteLine(); Flux.WriteLine("Matrice A :"); foreach (var item in A.Print()) { Flux.WriteLine(item); } ; flagSwaps = false; for (int i = 0; i < mat.Swaps.GetLength(0); i++) { if (mat.Swaps[i, 0] != -1) { flagSwaps = true; } } if (flagSwaps) { Flux.WriteLine("Après permutations."); } Flux.WriteLine(); }
//Inverse la matrice L avec sortie pour l'affichage. public Matrice InverseL(out List <String> display) { if (IsInversible()) { Matrice ident = InitIdentite(NbrLn); Matrice LPrime = new Matrice(NbrLn, NbrCol); double somme = 0; display = new List <string>(); display.Add("Etape 1: "); display.Add("--------"); display.Add(""); //La première ligne est la même que l'identité. for (int j = 0; j < nbrCol; j++) { LPrime[0, j] = ident[0, j]; display.Add(String.Format("x1" + j + " = {0,8:#0.##} = &1" + j, LPrime[0, j])); } //Pour chaque ligne. for (int i = 1; i < NbrLn; i++) { display.Add(""); display.Add("Etape " + (i + 1) + ":"); display.Add("--------"); display.Add(""); //Pour chaque colonne. for (int j = 0; j < NbrCol; j++) { somme = 0; String tmp = ""; //Nombre d'élément de la somme. for (int k = 0; k < i; k++) { tmp += " - m" + (i + 1) + (k + 1) + " * " + "x" + (k + 1) + (j + 1); somme += L[i, k] * LPrime[k, j]; } //Applique la formule. LPrime[i, j] = ident[i, j] - somme; display.Add(String.Format("x" + (i + 1) + (j + 1) + " = {0,8:#0.##} = &" + (i + 1) + (j + 1) + tmp, LPrime[i, j])); } } display.Add(""); display.Add("Matrice L inversé : "); foreach (var item in LPrime.Print()) { display.Add(item); } return(LPrime); } else { throw new Exception("La matrice n'est pas inversible : déterminant = 0"); } }
//Inverse la matrice U avec sortie pour l'affichage. public Matrice InverseU(out List <String> display) { if (IsInversible()) { Matrice ident = InitIdentite(NbrLn); Matrice UPrime = new Matrice(NbrLn, NbrCol); double somme = 0; display = new List <string>(); display.Add("Etape 1: "); display.Add("--------"); display.Add(""); //La dernière ligne. for (int j = 0; j < nbrCol; j++) { UPrime[NbrLn - 1, j] = ident[NbrLn - 1, j] / U[NbrLn - 1, NbrLn - 1]; display.Add(String.Format("x" + NbrLn + (j + 1) + " = {0,8:#0.##} = &" + NbrLn + (j + 1) + "/u" + NbrLn + NbrLn, UPrime[NbrLn - 1, j])); } //Pour chaque ligne. for (int i = NbrLn - 2; i >= 0; i--) { display.Add(""); display.Add("Etape " + (NbrLn - i) + ":"); display.Add("--------"); display.Add(""); //Pour chaque colonne. for (int j = 0; j < NbrCol; j++) { somme = 0; String tmp = ""; //Nombre d'élément de la somme. for (int k = i + 1; k < NbrLn; k++) { tmp += " - u" + (i + 1) + (k + 1) + " * " + "y" + (k + 1) + (j + 1); somme += U[i, k] * UPrime[k, j]; } //Applique la formule. UPrime[i, j] = (ident[i, j] - somme) / U[i, i]; display.Add(String.Format("x" + (i + 1) + (j + 1) + " = {0,8:#0.##} = (&" + (i + 1) + (j + 1) + tmp + ")/u" + (i + 1) + (i + 1), UPrime[i, j])); } } display.Add(""); display.Add("Matrice U inversé : "); foreach (var item in UPrime.Print()) { display.Add(item); } return(UPrime); } else { throw new Exception("La matrice n'est pas inversible : déterminant = 0"); } }
public void AfficherMatrice(Matrice mat) { Flux.WriteLine("==================================================================================================="); Flux.WriteLine("= Matrice à inverser ="); Flux.WriteLine("==================================================================================================="); Flux.WriteLine(); foreach (var item in mat.Print()) { Flux.WriteLine(item); } Flux.WriteLine(); }
public void AfficherInverse(Matrice mat, ref List <String> display) { Flux.WriteLine("==================================================================================================="); Flux.WriteLine("= Résultat de l'inversion ="); Flux.WriteLine("==================================================================================================="); Flux.WriteLine(); foreach (var item in display) { Flux.WriteLine(item); } foreach (var item in mat.Print()) { Flux.WriteLine(item); } }
public void AfficherVerification(Matrice A, Matrice B, Matrice res, bool check) { Flux.WriteLine("==================================================================================================="); Flux.WriteLine("= Vérification A*A(-1) = & ="); Flux.WriteLine("==================================================================================================="); Flux.WriteLine(); foreach (var item in A.Print()) { Flux.WriteLine(item); } Flux.WriteLine(""); Flux.WriteLine(" *"); Flux.WriteLine(""); foreach (var item in B.Print()) { Flux.WriteLine(item); } Flux.WriteLine(""); Flux.WriteLine(" ="); Flux.WriteLine(""); foreach (var item in res.Print()) { Flux.WriteLine(item); } Flux.WriteLine(); if (check) { Flux.WriteLine("L'inversion de la matrice c'est bien déroulée!"); } else { Flux.WriteLine("Erreur : le résultat est faux!"); } }
//Triangularise la matrice par la méthode de Gauss avec une sortie String pour l'affichage. public Matrice Gauss(out int[,] swaps, out double[,] m, out List <String> display) { if (IsSquare()) { Matrice temp = new Matrice(values); double pivot; //Initialise la sortie. display = new List <string>(); swaps = new int[nbrLn - 1, 2]; m = new double[nbrLn, nbrCol]; //Pour chaque étape. (k-1 étapes) for (int k = 0; k < nbrLn - 1; k++) { display.Add("Etape : " + (k + 1)); display.Add("---------"); display.Add(""); pivot = temp[k, k]; //Si le pivot vaut 0 il faut permuter les lignes. if (pivot == 0) { //Récupére les numéros des lignes qui permutent. swaps[k, 0] = k; swaps[k, 1] = k + 1; //Permute les lignes et recalcul le pivot. temp.SwapLn(k, k + 1); pivot = temp[k, k]; if (pivot == 0) { throw new Exception("La matrice n'est pas inversible !"); } } else { //Indique que les lignes n'ont pas été permutés. swaps[k, 0] = -1; swaps[k, 1] = -1; } display.Add(String.Format("Pivot = {0,6:#0.##}", pivot)); //Pour chaque ligne de la matrice. //Calcul du coefficient de l'étape k. for (int i = k + 1; i < nbrLn; i++) { m[i, k] = temp[i, k] / pivot; display.Add(String.Format("{0,5} = {1,6:#0.##}", "m" + (i + 1) + (k + 1), m[i, k])); //Met les 0. for (int j = k; j < nbrLn; j++) { temp[i, j] -= m[i, k] * temp[k, j]; } } display.Add(""); //Affiche le résultat à la fin de l'étape. foreach (var item in temp.Print()) { display.Add(item); } display.Add(""); //Affiche les lignes permutés pour chaque étapes. if (swaps[k, 0] != -1) { display.Add("Lignes permutés : " + (swaps[k, 0] + 1) + " <-> " + (swaps[k, 1] + 1)); display.Add(""); } } return(temp); } else { throw new Exception("La matrice n'est pas carrée : impossible d'utiliser Gauss!"); } }