示例#1
0
        //Triangularise la matrice par la méthode de Gauss.
        public Matrice Gauss(out int[,] swaps, out double[,] m)
        {
            if (IsSquare())
            {
                Matrice temp = new Matrice(values);
                double  pivot;

                //Initialise la sortie.
                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++)
                {
                    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;
                    }

                    //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;

                        //Met les 0.
                        for (int j = k; j < nbrLn; j++)
                        {
                            temp[i, j] -= m[i, k] * temp[k, j];
                        }
                    }
                }
                return(temp);
            }
            else
            {
                throw new Exception("La matrice n'est pas carrée : impossible d'utiliser Gauss!");
            }
        }
示例#2
0
        //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!");
            }
        }
示例#3
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);
                }
            }
        }