Exemplo n.º 1
0
        //méthode du produit matriciel
        public Matrice FaireProduitMatriciel(Matrice matrice)
        {
            //si le nombre de colonnes de A est égal au nombre de lignes de B
            VerifierColonneAcommeLigneB(matrice);
            //if (VerifierColonneAcommeLigneB(matrice))
            //{

            var n = _matrice.GetLength(0);
            var p = matrice.GetLength(1);

            Matrice produitMatriciel = new Matrice(new double[n, p]);

            for (var i = 0; i < n; i++)
            {
                for (var j = 0; j < p; j++)
                {
                    produitMatriciel[i, j] = 0;
                    for (var z = 0; z < n; z++)
                    {
                        produitMatriciel[i, j] += _matrice[i, z] * matrice[z, j];
                    }
                }
            }
            // Console.WriteLine(produitMatriciel.AfficheMatrice());
            return(produitMatriciel);
            //}
            //else
            //{
            //    Console.WriteLine("Format de matrice incorrect");
            //    return new Matrice(new double[0, 0]);
            //}
        }
Exemplo n.º 2
0
        //Méthode de Cramer
        public Matrice TrouverXParCramer()
        {
            if (_matriceCarree.Determinant != 0)
            {
                int n = _matriceCarree.GetLength(0);
                Matrice X = new Matrice((new double[n, 1]));
                Matrice AN = new Matrice(new double[n, n]);

                for (int i = 0; i < n; i++)
                {
                    AN = _matriceCarree.CopierMatrice();
                    for (int j = 0; j < n; j++)
                    {
                        AN[j, i] = _matrice1N[j, 0];
                    }
                    X[i, 0] = AN.Determinant / _matriceCarree.Determinant;
                }
                return X;
            }
            else
            {
                Exception e = new Exception("La matrice a un déterminant de 0");
                throw e;
            }
        }
Exemplo n.º 3
0
        //Méthode de Cramer
        public Matrice TrouverXParCramer()
        {
            if (_matriceCarree.Determinant != 0)
            {
                int     n  = _matriceCarree.GetLength(0);
                Matrice X  = new Matrice((new double[n, 1]));
                Matrice AN = new Matrice(new double[n, n]);

                for (int i = 0; i < n; i++)
                {
                    AN = _matriceCarree.CopierMatrice();
                    for (int j = 0; j < n; j++)
                    {
                        AN[j, i] = _matrice1N[j, 0];
                    }
                    X[i, 0] = AN.Determinant / _matriceCarree.Determinant;
                }
                return(X);
            }

            else
            {
                Console.WriteLine("Déterminant de la matrice = 0, donc impossible d'utiliser la méthode de Cramer");
                return(new Matrice(new double[0, 0]));
            }
        }
Exemplo n.º 4
0
        //méthode d'obtebtention de la mineure
        public Matrice Mineure(int i, int j)
        {
            Matrice mineure    = new Matrice(new double[this.GetLength(0) - 1, this.GetLength(1) - 1]);
            Matrice mineureTmp = new Matrice(new double[this.GetLength(0), this.GetLength(1)]);

            for (int k = 0; k < this.GetLength(0); k++)
            {
                for (int l = 0; l < this.GetLength(1); l++)
                {
                    mineureTmp[k, l] = this[k, l];
                    if (l > j && k > i)
                    {
                        mineureTmp[k - 1, l - 1] = this[k, l];
                    }
                    else if (l > j)
                    {
                        mineureTmp[k, l - 1] = this[k, l];
                    }
                    else if (k > i)
                    {
                        mineureTmp[k - 1, l] = this[k, l];
                    }
                }
            }
            for (int k = 0; k < mineure.GetLength(0); k++)
            {
                for (int l = 0; l < mineure.GetLength(1); l++)
                {
                    mineure[k, l] = mineureTmp[k, l];
                }
            }
            //Console.WriteLine(mineure.AfficheMatrice());
            return(mineure);
        }
Exemplo n.º 5
0
        //Méthode de Jacobi
        public Matrice TrouverXParJacobi(double epsilon)
        {
            int     n            = _matriceCarree.GetLength(0);
            Matrice d            = new Matrice((new double[n, 1]));
            Matrice xDroite      = new Matrice((new double[n, n + 1])); //
            Matrice xGauche      = new Matrice((new double[n, 1]));
            Matrice xGaucheAvant = new Matrice((new double[n, 1]));
            bool    continuer    = true;

            //création de la matrice colonne des valeurs de la diagonale et instanciation de matrice x gauche
            for (int i = 0; i < n; i++)
            {
                d[i, 0]       = _matriceCarree[i, i];
                xGauche[i, 0] = 0;
            }

            //création de la matrice des formules X
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (i != j)
                    {
                        xDroite[i, j] = -_matriceCarree[i, j] / d[i, 0];
                    }
                    else
                    {
                        xDroite[i, j] = 0;
                    }
                }
                xDroite[i, n] = _matrice1N[i, 0] / d[i, 0];
            }


            //itération pour trouver X
            while (continuer)
            {
                bool continuerTemp = false;
                for (int i = 0; i < n; i++)
                {
                    xGaucheAvant[i, 0] = xGauche[i, 0];
                    xGauche[i, 0]      = 0;


                    for (int j = 0; j < n; j++)
                    {
                        xGauche[i, 0] += xDroite[i, j] * xGaucheAvant[j, 0];
                    }
                    xGauche[i, 0] += xDroite[i, n];
                    Console.WriteLine(xGauche[i, 0]);
                    if (Math.Abs(xGauche[i, 0] - xGaucheAvant[i, 0]) >= epsilon || continuerTemp)
                    {
                        continuerTemp = true;
                    }
                    //else continuerTemp = false;
                }
                continuer = continuerTemp;
            }
            return(xGauche);
        }
Exemplo n.º 6
0
 //méthode vérification prérequis multiplication
 public bool VerifierColonneAcommeLigneB(Matrice matrice)
 {
     if (_matrice.GetLength(1) == matrice.GetLength(0))
     {
         return(true);
     }
     else
     {
         Exception e = new Exception("Format de matrice incompatible pour multiplication");
         throw e;
     }
 }
Exemplo n.º 7
0
 //constructeur
 public Systeme(Matrice matriceCarree, Matrice matrice1N)
 {
     if (matriceCarree.EstCarree && matrice1N.GetLength(1) == 1 && matriceCarree.GetLength(0) == matrice1N.GetLength(0))
     {
         _matriceCarree = matriceCarree;
         _matrice1N     = matrice1N;
     }
     else
     {
         Console.WriteLine("Format de matrice incorrect");
     }
 }
Exemplo n.º 8
0
        //méthode du produit matriciel multiple
        public Matrice FaireProduitMatriciel(out int operations, params Matrice[] matrices)
        {
            operations = 0;
            Matrice temp = this;

            for (int i = 0; i < matrices.Length; i++)
            {
                operations += temp.GetLength(0) * temp.GetLength(1) * matrices[i].GetLength(1);
                temp        = temp.FaireProduitMatriciel(matrices[i]);
            }
            return(temp);
        }
Exemplo n.º 9
0
        //méthode de vérification même format de matrice
        public bool VerifierFormatMatrice(Matrice matrice)
        {
            if (_matrice.GetLength(0) == matrice.GetLength(0) && _matrice.GetLength(1) == matrice.GetLength(1))
            {
                return(true);
            }

            else
            {
                Exception e = new Exception("Format de matrice incorrect");
                throw e;
            }
        }
Exemplo n.º 10
0
 //constructeur
 public Systeme(Matrice matriceCarree, Matrice matrice1N)
 {
     if (matriceCarree.EstCarree && matrice1N.GetLength(1) == 1 && matriceCarree.GetLength(0) == matrice1N.GetLength(0))
     {
         _matriceCarree = matriceCarree;
         _matrice1N = matrice1N;
     }
     else
     {
         Exception e = new Exception("Format de matrice incorrect pour créer un système d'équation");
         throw e;
     }
 }
Exemplo n.º 11
0
        //méthode de copie de la matrice
        public Matrice CopierMatrice()
        {
            Matrice copie = new Matrice(new double[_matrice.GetLength(0), _matrice.GetLength(1)]);

            for (int i = 0; i < _matrice.GetLength(0); i++)
            {
                for (int j = 0; j < _matrice.GetLength(1); j++)
                {
                    copie[i, j] = _matrice[i, j];
                }
            }
            return(copie);
        }
Exemplo n.º 12
0
 //Méthode d'inversion matricielle
 public Matrice TrouverXParInversionMatricielle()
 {
     if (_matriceCarree.Determinant != 0)
     {
         int     n = _matriceCarree.GetLength(0);
         Matrice X = new Matrice((new double[n, 1]));
         X = (_matriceCarree.MatriceInverse).FaireProduitMatriciel(_matrice1N);
         return(X);
     }
     else
     {
         Console.WriteLine("Déterminant de la matrice = 0, donc impossible d'utiliser la méthode de d'inversion matricielle");
         return(new Matrice(new double[0, 0]));
     }
 }
Exemplo n.º 13
0
        //méthode du produit matriciel en commencant par la fin
        public Matrice FaireProduitMatricielParFin(out int operations, params Matrice[] matrices)
        {
            operations = 0;
            Matrice matriceA = this;
            Matrice temp     = this;

            for (int i = matrices.Length - 1; i == 1; i--)
            {
                operations += matrices[i - 1].GetLength(0) * matrices[i - 1].GetLength(1) * matrices[i].GetLength(1);
                temp        = matrices[i - 1].FaireProduitMatriciel(matrices[i]);
            }
            operations += matriceA.GetLength(0) * matriceA.GetLength(1) * temp.GetLength(1);
            temp        = matriceA.FaireProduitMatriciel(temp);
            return(temp);
        }
Exemplo n.º 14
0
 //Méthode d'inversion matricielle
 public Matrice TrouverXParInversionMatricielle()
 {
     if (_matriceCarree.Determinant != 0)
     {
         int n = _matriceCarree.GetLength(0);
         Matrice X = new Matrice((new double[n, 1]));
         X = (_matriceCarree.MatriceInverse).FaireProduitMatriciel(_matrice1N);
         return X;
     }
     else
     {
         Exception e = new Exception("La matrice a un déterminant de 0");
         throw e;
     }
 }
Exemplo n.º 15
0
        //méthode du produit scalaire
        public Matrice FaireProduitScalaire(double scalaire)
        {
            int     x = _matrice.GetLength(0);
            int     y = _matrice.GetLength(1);
            Matrice produitScalaire = new Matrice(new double[x, y]);

            for (int i = 0; i < x; i++)
            {
                for (int j = 0; j < y; j++)
                {
                    produitScalaire[i, j] = _matrice[i, j] * scalaire;
                }
            }
            return(produitScalaire);
        }
Exemplo n.º 16
0
        //méthode d'addition de matrice
        public Matrice Additionner(Matrice matrice)
        {
            //if (VerifierFormatMatrice(matrice) == true)
            VerifierFormatMatrice(matrice);
            //{
            var addition = new Matrice(new double[_matrice.GetLength(0), _matrice.GetLength(1)]);

            for (var i = 0; i < matrice.GetLength(0); i++)
            {
                for (var j = 0; j < matrice.GetLength(1); j++)
                {
                    addition[i, j] = this[i, j] + matrice[i, j];
                }
            }

            return(addition);
        }
Exemplo n.º 17
0
        //méthode d'addition de matrice
        public Matrice Additionner(Matrice matrice)
        {
            //if (VerifierFormatMatrice(matrice) == true)
            VerifierFormatMatrice(matrice);
            //{
            var addition = new Matrice(new double[_matrice.GetLength(0), _matrice.GetLength(1)]);

            for (var i = 0; i < matrice.GetLength(0); i++)
            {
                for (var j = 0; j < matrice.GetLength(1); j++)
                {
                    addition[i, j] = this[i, j] + matrice[i, j];
                }
            }

            return(addition);
            //}
            //else
            //{
            //    Console.WriteLine("Format de matrice incorrect");
            //    return new Matrice(new double[0, 0]);
            //}
        }
Exemplo n.º 18
0
        //méthode de calcul du determinant
        private static double CalculDeterminant(Matrice matrice, int ordre)
        {
            {
                double  determinant = 0;
                Matrice temp        = new Matrice(new double[ordre, ordre]);

                if (ordre == 1)
                {
                    return(matrice[0, 0]);
                }
                else if (ordre == 2)
                {
                    determinant = (matrice[0, 0] * matrice[1, 1] - matrice[0, 1] * matrice[1, 0]);
                    return(determinant);
                }

                //pour matrice d'ordre > 2
                else
                {
                    for (int i = 0; i < ordre; i++)
                    {
                        Matrice mineure = new Matrice(new double[ordre - 1, ordre - 1]);
                        mineure = matrice.Mineure(0, i);
                        if (matrice.SigneComplement(1, i + 1) == true)
                        {
                            determinant += matrice[0, i] * CalculDeterminant(mineure, ordre - 1);
                        }
                        else
                        {
                            determinant -= matrice[0, i] * CalculDeterminant(mineure, ordre - 1);
                        }
                    }
                    return(determinant);
                }
            }
        }
Exemplo n.º 19
0
        //méthode du produit matriciel
        public Matrice FaireProduitMatriciel(Matrice matrice)
        {
            //si le nombre de colonnes de A est égal au nombre de lignes de B
            VerifierColonneAcommeLigneB(matrice);

            int n = _matrice.GetLength(0);
            int p = matrice.GetLength(1);
            int q = matrice.GetLength(0);

            Matrice produitMatriciel = new Matrice(new double[n, p]);

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < p; j++)
                {
                    produitMatriciel[i, j] = 0;
                    for (int z = 0; z < q; z++)
                    {
                        produitMatriciel[i, j] += _matrice[i, z] * matrice[z, j];
                    }
                }
            }
            return(produitMatriciel);
        }
Exemplo n.º 20
0
        static void Main(string[] args)
        {
            Matrice matrice1 = new Matrice(new double[3, 3] {
                { 3, 4, 1 }, { 4, 1, 2 }, { 4, 0, 2 }
            });
            Matrice matrice2 = new Matrice(new double[2, 3] {
                { 1, 2, 1 }, { 1, 2, 1 }
            });
            Matrice matrice3 = new Matrice(new double[3, 2] {
                { 3, 2 }, { 1, 2 }, { 1, 1 }
            });
            Matrice matrice4 = new Matrice(new double[2, 2] {
                { 3, 2 }, { 4, 5 }
            });
            Matrice matrice5 = new Matrice(new double[4, 4] {
                { 4, 2, 8, 3 }, { 5, 1, 7, 5 }, { 8, 0, 8, 5 }, { 3, 2, 3, 8 }
            });

            Matrice matrice6 = new Matrice(new double[3, 3] {
                { 1, 2, -1 }, { -2, 1, 1 }, { 0, 3, -3 }
            });
            Matrice matrice7 = new Matrice(new double[4, 4] {
                { 0, 2, 8, 3 }, { 0, 0, 7, 5 }, { 0, 0, 0, 5 }, { 0, 0, 0, 0 }
            });

            Matrice matriceA = new Matrice(new double[3, 3] {
                { 1, 3, 4 }, { 3, 5, -4 }, { 4, 7, -2 }
            });
            Matrice matriceB = new Matrice(new double[3, 1] {
                { 50 }, { 2 }, { 31 }
            });
            Systeme systeme1 = new Systeme(matriceA, matriceB);

            Matrice matriceC = new Matrice(new double[3, 3] {
                { 1, 1, -2 }, { 4, -1, 2 }, { 2, -6, 3 }
            });
            Matrice matriceD = new Matrice(new double[3, 1] {
                { 0 }, { 0 }, { 0 }
            });
            Systeme systeme2 = new Systeme(matriceC, matriceD);

            Matrice matriceE = new Matrice(new double[3, 3] {
                { 4, -1, 0 }, { -1, 4, -1 }, { 0, -1, 4 }
            });
            Matrice matriceF = new Matrice(new double[3, 1] {
                { 100 }, { 100 }, { 100 }
            });
            Systeme systeme3 = new Systeme(matriceE, matriceF);



            ////Additionner
            //Console.WriteLine("--------------Addition----------------");

            //(matrice1.Additionner(matrice6)).AfficheMatrice();
            //Console.WriteLine();
            ////Produit Scalaire
            //Console.WriteLine("----------Produit Scalaire----------------");
            //matrice1.FaireProduitScalaire(6).AfficheMatrice();
            //Console.WriteLine();
            ////Produit de matrices
            Console.WriteLine("--------------Produit----------------");
            int a = 0;

            Console.WriteLine((matrice2.FaireProduitMatriciel(out a, matrice3, matrice4)).AfficheMatrice());
            Console.WriteLine("Nombre de produits effectues : " + a);
            Console.WriteLine();
            ////Triangulaire ?
            //Console.WriteLine("--------------Triangulaire?----------------");
            //bool m = matrice7.EstTriangulaire(1, true);
            //matrice7.AfficheMatrice();
            //Console.WriteLine("La matrice 7 est elle superieure strict ? " +m);
            //Console.WriteLine();
            //Console.WriteLine();
            ////Trace
            //Console.WriteLine("--------------Trace----------------");
            //double n =  matrice5.Trace;
            //matrice5.AfficheMatrice();
            //Console.WriteLine("La matrice 5 a une trace equivalent à " + n);
            //Console.WriteLine();
            //Determinant
            //Console.WriteLine("--------------Determinant----------------");
            //double c = matrice5.Determinant;
            //Console.WriteLine("Le determinant de la matrice 5 est de "+c);
            //Console.WriteLine();

            ////Transposee
            //Console.WriteLine("------------Transposee----------------");
            //Console.WriteLine("matrice 6 avant");
            //matrice6.AfficheMatrice();
            //Console.WriteLine("matrice 6 apres");
            //matrice6.Transposee.AfficheMatrice();
            //Console.WriteLine();
            ////Comatrice
            //Console.WriteLine("------------Comatrice----------------");
            //Console.WriteLine("matrice 4 avant operation");
            //matrice4.AfficheMatrice();
            //Console.WriteLine("Apres");
            //matrice4.Comatrice.AfficheMatrice();
            //Console.WriteLine();

            ////Matrice inverse
            //Console.WriteLine("------------Matrice Inverse----------------");
            //Console.WriteLine("matrice 5 avant operation");
            //matrice5.AfficheMatrice();
            //Console.WriteLine("Apres operation");
            //matrice5.MatriceInverse.AfficheMatrice();

            //Console.WriteLine();

            ////Est reguliere ?
            //Console.WriteLine("--------------Matrice Reguliere?----------------");
            //bool o = matrice3.EstReguliere;
            //double p = matrice3.Determinant;
            //Console.WriteLine("Le determinant de la matrice 3 est de " + p);
            //Console.WriteLine("La matrice 3 est elle reguliere ? " + o);
            //Console.WriteLine();

            //Console.WriteLine("--------------Méthode de CRAMER----------------");
            //systeme1.TrouverXParCramer().AfficheMatrice();
            //Console.WriteLine();
            //systeme2.TrouverXParCramer().AfficheMatrice();


            //Console.WriteLine("--------------Méthode d'inversion matricielle----------------");
            //systeme1.TrouverXParInversionMatricielle().AfficheMatrice();

            //Console.WriteLine("--------------Méthode de Jacobi----------------");
            //systeme3.TrouverXParJacobi(0.02).AfficheMatrice();

            Console.WriteLine();
            Console.WriteLine("Appuyer sur Enter pour fermer");
            Console.ReadLine();

            //MODIFIER POUR QUE L'UTILISATEUR PUISSE ENTRER SES PROPRES MATRICES
        }
Exemplo n.º 21
0
        //méthode de calcul du determinant
        private static double CalculDeterminant(Matrice matrice, int ordre)
        {
            {
                //int p, h, k, i, j;
                double  determinant = 0;
                Matrice temp        = new Matrice(new double[ordre, ordre]);

                if (ordre == 1)
                {
                    return(matrice[0, 0]);
                }
                else if (ordre == 2)
                {
                    determinant = (matrice[0, 0] * matrice[1, 1] - matrice[0, 1] * matrice[1, 0]);
                    Console.WriteLine(determinant);
                    return(determinant);
                }

                //pour matrice d'ordre > 2
                else
                {
                    for (int i = 0; i < ordre; i++)
                    {
                        int     ordreTmp = ordre - 1;
                        Matrice mineure  = new Matrice(new double[ordreTmp, ordreTmp]);
                        mineure = matrice.Mineure(0, i);
                        Console.WriteLine(mineure.AfficheMatrice());
                        if (matrice.SigneComplement(1, i + 1) == true)
                        {
                            determinant += matrice[0, i] * CalculDeterminant(mineure, ordreTmp);
                        }
                        else
                        {
                            determinant -= matrice[0, i] * CalculDeterminant(mineure, ordreTmp);
                        }
                    }
                    //for (p = 0; p < ordre; p++)
                    //{
                    //    h = 0;
                    //    k = 0;
                    //    for (i = 1; i < ordre; i++)
                    //    {
                    //        for (j = 0; j < ordre; j++)
                    //        {
                    //            if (j == p)
                    //            {
                    //                continue;
                    //            }
                    //            temp[h, k] = matrice[i, j];
                    //            k++;
                    //            if (k == ordre - 1)
                    //            {
                    //                h++;
                    //                k = 0;
                    //            }
                    //        }
                    //    }
                    //    determinant = determinant + matrice[0, p] * Math.Pow(-1, p) * CalculDeterminant(temp, ordre - 1);
                    //}
                    Console.WriteLine("" + ordre);
                    return(determinant);
                }
            }
        }