예제 #1
0
 public Systeme(Matrice a, Matrice b, int n)
 {
     this.n           = n;
     matriceA         = new Matrice(n, n);
     matriceB         = new Matrice(n, 1);
     matriceA.matrice = a.matrice;
     matriceB.matrice = b.matrice;
 }
예제 #2
0
        public Matrice TrouverXParCramer()
        {
            double detA;

            double[] detN = new double[N];
            Matrice  returnMatrice;

            //On va chercher le déterminant de la matrice
            detA = matriceA.Determinant;

            if (!matriceA.EstReguliere)
            {
                return(null);
            }
            else
            {
                for (int col = 0; col < matriceA.NbCol; col++)
                {
                    Matrice newMat = new Matrice(N, N);

                    for (int i = 0; i < matriceA.NbRow; i++)
                    {
                        for (int j = 0; j < matriceA.NbCol; j++)
                        {
                            if (col != j)
                            {
                                newMat.matrice[i, j] = MatriceA.matrice[i, j];
                            }
                            else
                            {
                                newMat.matrice[i, j] = MatriceB.matrice[i, 0];
                            }
                        }
                    }

                    detN[col] = newMat.Determinant;

                    /*newMat.DisplayMatrice();
                     * Console.WriteLine("Le determinant de la matrice est {0}", detN[col]);*/
                }

                returnMatrice       = new Matrice(N, 1);
                double[,] returnTab = new double[N, 1];
                for (int ctr = 0; ctr < N; ctr++)
                {
                    returnTab[ctr, 0] = detN[ctr] / detA;
                }

                returnMatrice.matrice = returnTab;

                return(returnMatrice);
            }
        }
예제 #3
0
 //Copie une matrice (pour jacobi)
 public void Copier(Matrice mat)
 {
     if (mat.NbRow == nbRow && mat.NbCol == nbCol)
     {
         for (int i = 0; i < mat.NbRow; i++)
         {
             for (int j = 0; j < mat.NbCol; j++)
             {
                 matrice[i, j] = mat.matrice[i, j];
             }
         }
     }
 }
예제 #4
0
        private Matrice DivideByDeterminant(Matrice pMatrice, double determinant)
        {
            Matrice resultMatrice = new Matrice(pMatrice.NbRow, pMatrice.NbCol);

            for (int i = 0; i < pMatrice.NbRow; i++)
            {
                for (int j = 0; j < pMatrice.NbCol; j++)
                {
                    resultMatrice.matrice[i, j] = pMatrice.matrice[i, j] / determinant;
                }
            }

            return(resultMatrice);
        }
예제 #5
0
        //Méthode pour la multiplication par un scalaire
        public Matrice FaireProduitScalaire(double scalaire)
        {
            Matrice resultMatrice = new Matrice(nbRow, nbCol);

            for (int i = 0; i < nbRow; i++)
            {
                for (int j = 0; j < nbCol; j++)
                {
                    resultMatrice.matrice[i, j] = matrice[i, j] * scalaire;
                }
            }

            return(resultMatrice);
        }
예제 #6
0
        //Méthode pour l'addition entre 2 matrice
        public Matrice Additionner(Matrice pMatrice)
        {
            Matrice resultMatrice = new Matrice(nbRow, nbCol);

            for (int i = 0; i < nbRow; i++)
            {
                for (int j = 0; j < nbCol; j++)
                {
                    resultMatrice.matrice[i, j] = matrice[i, j] + pMatrice.matrice[i, j];
                }
            }

            return(resultMatrice);
        }
예제 #7
0
        //Constructeur pour initialiser des matrices de tests au lancement initial du projet
        public Systeme(int test)
        {
            double[,] matrice1, matrice2;
            if (test == 1)
            {
                matrice1 = new double[3, 3] {
                    { 2, 1, 3 }, { 1, -2, 1 }, { 1, 1, -2 }
                };
                matrice2 = new double[3, 1] {
                    { 6 }, { 2 }, { 1 }
                };
                matriceA         = new Matrice(3, 3);
                matriceB         = new Matrice(3, 1);
                matriceA.matrice = matrice1;
                matriceB.matrice = matrice2;
                n = 3;
            }

            if (test == 2)
            {
                matrice1 = new double[3, 3] {
                    { 4, -1, 0 }, { -1, 4, -1 }, { 0, -1, 4 }
                };
                matrice2 = new double[3, 1] {
                    { 100 }, { 100 }, { 100 }
                };
                matriceA         = new Matrice(3, 3);
                matriceB         = new Matrice(3, 1);
                matriceA.matrice = matrice1;
                matriceB.matrice = matrice2;
                n = 3;
            }

            if (test == 3)
            {
                matrice1 = new double[3, 3] {
                    { 0, 0, 0 }, { 4, 0, 0 }, { 0, 2, 0 }
                };
                matrice2 = new double[3, 1] {
                    { 5 }, { 2 }, { 3 }
                };
                matriceA         = new Matrice(3, 3);
                matriceB         = new Matrice(3, 1);
                matriceA.matrice = matrice1;
                matriceB.matrice = matrice2;
                n = 3;
            }
        }
예제 #8
0
        public Matrice TrouverXParJacobi(double epsilon)
        {
            if (!matriceA.EstStrictementDominante)
            {
                return(null);
            }
            else
            {
                double  somme, res;
                Boolean fin;

                Matrice newMat = new Matrice(N, 1);
                Matrice oldMat = new Matrice(N, 1);

                do
                {
                    fin = true;
                    for (int i = 0; i < matriceB.NbRow; i++)
                    {
                        somme = 0;
                        for (int j = 0; j < matriceB.NbRow; j++)
                        {
                            if (j != i)
                            {
                                somme += matriceA.matrice[i, j] * oldMat.matrice[j, 0];
                            }
                        }
                        res = 1 / matriceA.matrice[i, i] * (matriceB.matrice[i, 0] - somme);
                        newMat.matrice[i, 0] = res;
                        if (res - oldMat.matrice[i, 0] > epsilon)
                        {
                            fin = false;
                        }
                    }
                    //Console.WriteLine("Matrice temp = \n" + matriceX); Console.ReadLine();
                    oldMat.Copier(newMat);
                } while (fin == false);

                return(newMat);
            }
        }
예제 #9
0
        public Matrice TrouverXParInversionMatricielle()
        {
            Matrice returnMatrice = new Matrice(N, 1);
            Matrice newMat        = new Matrice(N, N);
            int     useless       = 0;

            //On va chercher le déterminant de la matrice


            if (!matriceA.EstReguliere)
            {
                return(null);
            }
            else
            {
                Matrice[] matriceToMultiply = new Matrice[1];
                matriceToMultiply[0] = matriceB;
                newMat        = matriceA.MatriceInverse;
                returnMatrice = newMat.FaireProduitMatriciel(matriceToMultiply, 1, out useless);

                return(returnMatrice);
            }
        }
예제 #10
0
        private double ComplementAlgebrique(int col, Matrice pMatrice)
        {
            double comp = 0;

            if (pMatrice.NbRow > 3)
            {
                int size = pMatrice.NbRow - 1;
                double[,] newMatrice = new double[size, size];
                Matrice newMat = new Matrice(size, size);
                newMat.matrice = newMatrice;

                int rowInd, colInd, nbDataNewMatrice;
                rowInd = colInd = nbDataNewMatrice = 0;

                for (int i = 0; i < pMatrice.NbRow; i++)
                {
                    for (int j = 0; j < pMatrice.NbCol; j++)
                    {
                        //On élimine la colonne du chiffre dont on calcul le complément
                        if (i != 0 && j != col)
                        {
                            newMatrice[rowInd, colInd] = pMatrice.matrice[i, j];
                            //On passe a une autre ligne de la nouvelle matrice
                            if (colInd == size - 1)
                            {
                                colInd = 0;
                                rowInd++;
                            }
                            //On change de colonne de la nouvelle matrice
                            else
                            {
                                colInd++;
                            }
                        }
                    }
                }

                newMat.matrice = newMatrice;
                bool isSignePos = true;
                for (int j = 0; j < newMat.NbCol; j++)
                {
                    if (isSignePos)
                    {
                        comp      += pMatrice.matrice[0, col] * ComplementAlgebrique(j, newMat);
                        isSignePos = false;
                    }
                    else
                    {
                        comp      -= pMatrice.matrice[0, col] * ComplementAlgebrique(j, newMat);
                        isSignePos = true;
                    }
                }
            }
            else
            {
                double[,] newMatrice = new double[2, 2];
                int rowInd, colInd, nbDataNewMatrice;
                rowInd = colInd = nbDataNewMatrice = 0;
                int size = pMatrice.nbRow - 1;
                for (int i = 0; i < pMatrice.nbRow; i++)
                {
                    for (int j = 0; j < pMatrice.nbCol; j++)
                    {
                        //On élimine la colonne du chiffre dont on calcul le complément
                        if (i != 0 && j != col)
                        {
                            newMatrice[rowInd, colInd] = pMatrice.matrice[i, j];

                            if (colInd == size - 1)
                            {
                                colInd = 0;
                                rowInd++;
                            }
                            //On change de colonne de la nouvelle matrice
                            else
                            {
                                colInd++;
                            }
                        }
                    }
                }
                comp = pMatrice.matrice[0, col] * (newMatrice[0, 0] * newMatrice[1, 1] - newMatrice[0, 1] * newMatrice[1, 0]);
            }

            return(comp);
        }
예제 #11
0
        //Méthode pour la multiplication matricielle
        //Oui je sais que j'aurais du simplifier cette méthode avec une méthode privé et l'appeler avec 2 matrices comme parametre au lieu de me faie un if comme j'ai fait...j'étais fatigué apparament
        public Matrice FaireProduitMatriciel(Matrice[] pMatrice, int nbMatrice, out int nbOperation)
        {
            int     indMat                = 0;
            int     currentMatrice        = 1;
            Matrice resultMatrice         = this; //Ne sera pas utiliser avec cette valeur
            Matrice previousResultMatrice = this; //Ne sera pas utiliser avec cette valeur

            nbOperation = 0;

            int resultNbRow, resultNbCol, indResultRow, indResultCol, previousInd;

            previousInd = resultNbCol = resultNbRow = 0;

            while (currentMatrice <= nbMatrice)
            {
                indResultRow = indResultCol = 0;


                resultNbRow = nbRow;
                resultNbCol = pMatrice[indMat].nbCol;
                if (currentMatrice != 1)
                {
                    previousInd = indMat - 1;
                }

                resultMatrice = new Matrice(resultNbRow, resultNbCol);

                double resultat;

                // On boucle parmis les lignes de la première matrice
                if (currentMatrice == 1)
                {
                    for (int i = 0; i < nbRow; i++)
                    {
                        indResultCol = 0;
                        //Pour chaque ligne de la première matrice, on calcul une somme de produit correspondant a la position dans la matrice résultante à la colonne de la seconde matrice
                        while (indResultCol < resultNbCol)
                        {
                            resultat = 0;
                            //On calcul les produits de chaque position dans ligne
                            for (int j = 0; j < nbCol; j++)
                            {
                                resultat += matrice[i, j] * pMatrice[indMat].matrice[j, indResultCol];
                            }
                            resultMatrice.matrice[indResultRow, indResultCol] = resultat;
                            indResultCol++;
                        }

                        indResultRow++;
                    }
                    //On ajoute au nombre d'opération le nombre d'opération de cette étape
                    nbOperation += nbRow * pMatrice[indMat].NbRow * pMatrice[indMat].NbCol;
                }
                else
                {
                    for (int i = 0; i < previousResultMatrice.NbRow; i++)
                    {
                        indResultCol = 0;
                        //Pour chaque ligne de la matrice résultat, on calcul une somme de produit correspondant a la position dans la matrice résultante à la colonne de la seconde matrice
                        while (indResultCol < resultNbCol)
                        {
                            resultat = 0;
                            //On calcul les produits de chaque position dans ligne
                            for (int j = 0; j < previousResultMatrice.NbCol; j++)
                            {
                                resultat += previousResultMatrice.matrice[i, j] * pMatrice[indMat].matrice[j, indResultCol];
                            }
                            resultMatrice.matrice[indResultRow, indResultCol] = resultat;
                            indResultCol++;
                        }

                        indResultRow++;
                    }
                    //On ajoute au nombre d'opération le nombre d'opération de cette étape
                    nbOperation += previousResultMatrice.NbRow * pMatrice[indMat].NbRow * pMatrice[indMat].NbCol;
                }
                currentMatrice++;
                indMat++;
                previousResultMatrice = (Matrice)resultMatrice.MemberwiseClone();
            }

            //On retourne la matrice finale qui correspond au dernier resultat obtenu
            Matrice finalMatrice = resultMatrice;


            return(finalMatrice);
        }