Ejemplo n.º 1
0
        public Matrice Putere(int putere)
        {
            Matrice m = this;

            for (int i = 0; i < putere; i++)
            {
                m = m.Inmulteste(m);
            }

            return(m);
        }
Ejemplo n.º 2
0
        public Matrice Power(int k)
        {
            if (this.n != this.m)
            {
                Console.WriteLine("You cannot raise this matrix to a power. It's not a square matrix.");
                return(null);
            }
            Matrice rez = new Matrice(this.n, this.m, this.values);

            for (int i = 0; i < k - 1; i++)
            {
                rez = rez.Multiply(this);
            }
            return(rez);
        }
Ejemplo n.º 3
0
        public Matrice Putere(int s)
        {
            if (this.m != this.n)
            {
                Console.WriteLine("Nefiind o matrice patratica,ridicarea la putere este imposibila");
                return(null);
            }
            Matrice rezultat = new Matrice(this.m, this.n, this.valori);

            for (int i = 0; i < s - 1; i++)
            {
                rezultat = rezultat.Inmultire(this);
            }
            return(rezultat);
        }
Ejemplo n.º 4
0
 public Matrice Subtract(Matrice a)
 {
     if (a.n == this.n && a.m == this.m)
     {
         Matrice rez = new Matrice(n, m);
         for (int i = 0; i < n; i++)
         {
             for (int j = 0; j < m; j++)
             {
                 rez.values[i, j] = this.values[i, j] - a.values[i, j];
             }
         }
         return(rez);
     }
     Console.WriteLine("You cannot subtract the two matrices, they do not have the same dimensions.");
     return(null);
 }
Ejemplo n.º 5
0
 public Matrice Adunare(Matrice a)
 {
     if (a.m == this.m && a.n == this.n)
     {
         Matrice rezultat = new Matrice(m, n);
         for (int i = 0; i < m; i++)
         {
             for (int j = 0; j < n; j++)
             {
                 rezultat.valori[i, j] = this.valori[i, j] + a.valori[i, j];
             }
         }
         return(rezultat);
     }
     Console.WriteLine("Cele doua matrici n-au dimensiuni egale");
     return(null);
 }
Ejemplo n.º 6
0
        public Matrice Scade(Matrice matrice2)
        {
            if (linii != matrice2.linii || coloane != matrice2.coloane)
            {
                return(null);
            }

            int[,] temp = new int[linii, coloane];

            for (int i = 0; i < linii; i++)
            {
                for (int j = 0; j < coloane; j++)
                {
                    temp[i, j] = matrice[i, j] - matrice2.matrice[i, j];
                }
            }

            return(new Matrice(temp));
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            Matrice a = new Matrice(new int[, ] {
                { 1, 2, 3 },
                { 4, 5, 6 },
                { 7, 8, 9 }
            });
            Matrice b = new Matrice(new int[, ] {
                { 4, 3, 2 },
                { 6, 5, 4 },
                { 9, 8, 7 }
            });

            Console.WriteLine("a + b = {0}", a.Aduna(b));
            Console.WriteLine("a - b = {0}", a.Scade(b));
            Console.WriteLine("a * b = {0}", a.Inmulteste(b));
            Console.WriteLine("a ^ 2 = {0}", a.Putere(2));
            Console.ReadKey();
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            double[,] aValues = new double[2, 2]
            {
                { 1, 2 },
                { 3, 4 }
            };
            double[,] bValues = new double[2, 2]
            {
                { 5, 6 },
                { 7, 8 }
            };
            Matrice a = new Matrice(2, aValues);
            Matrice b = new Matrice(2, bValues);

            Console.WriteLine(a.Add(b));
            Console.WriteLine(a.Multiply(b));
            Console.WriteLine(a.Power(4));
            Console.WriteLine(a.Inverse());
        }
Ejemplo n.º 9
0
 static void Main(string[] args)
 {
     try
     {
         Console.WriteLine("Numele meu este Szakacsi Ferenc-Adam");
         Console.WriteLine("Acest program lucreaza cu matrici");
         double[,] matrice1 = new double[2, 2]
         {
             { 1, 2 },
             { 3, 4 }
         };
         double[,] matrice2 = new double[2, 2]
         {
             { 5, 6 },
             { 7, 8 }
         };
         int     put = int.Parse(Console.ReadLine());
         Matrice a   = new Matrice(2, matrice1);
         Matrice b   = new Matrice(2, matrice2);
         Matrice c   = b.Adunare(a);
         Matrice d   = b.Scadere(a);
         Matrice f   = b.Inmultire(a);
         Matrice g   = b.Putere(put);
         Matrice h   = b.Inversare();
         Console.WriteLine("Adunarea rezulta:");
         Console.WriteLine(c);
         Console.WriteLine("Scaderea rezulta:");
         Console.WriteLine(d);
         Console.WriteLine("Inmultirea rezulta:");
         Console.WriteLine(f);
         Console.WriteLine("Ridicarea la putere rezulta:");
         Console.WriteLine(g);
         Console.WriteLine("Inversarea rezulta:");
         Console.WriteLine(h);
         Console.ReadKey();
     }
     catch (Exception e)
     {
         Console.WriteLine($" {e.Message}");
     }
 }
Ejemplo n.º 10
0
 public Matrice Multiply(Matrice a)
 {
     if (this.m != a.n)
     {
         Console.WriteLine("You cannot multiply the two matrices.");
         return(null);
     }
     else
     {
         Matrice rez = new Matrice(this.n, this.m);
         for (int i = 0; i < this.n; i++)
         {
             for (int j = 0; j < a.m; j++)
             {
                 rez.values[i, j] = 0;
                 for (int k = 0; k < this.m; k++)
                 {
                     rez.values[i, j] += this.values[i, k] * a.values[k, j];
                 }
             }
         }
         return(rez);
     }
 }
Ejemplo n.º 11
0
        public Matrice Inmulteste(Matrice matrice2)
        {
            if (linii != matrice2.linii || coloane != matrice2.coloane)
            {
                return(null);
            }

            int[,] temp = new int[linii, coloane];

            for (int i = 0; i < linii; i++)
            {
                for (int j = 0; j < coloane; j++)
                {
                    int suma = 0;
                    for (int k = 0; k < coloane; k++)
                    {
                        suma += matrice[i, k] * matrice2.matrice[k, i];
                    }
                    temp[i, j] = suma;
                }
            }

            return(new Matrice(temp));
        }
Ejemplo n.º 12
0
 public Matrice Inmultire(Matrice a)
 {
     if (this.n != a.m)
     {
         Console.WriteLine("Nu este posibila inmultirea");
         return(null);
     }
     else
     {
         Matrice rezultat = new Matrice(this.m, this.n);
         for (int i = 0; i < this.m; i++)
         {
             for (int j = 0; j < a.n; j++)
             {
                 rezultat.valori[i, j] = 0;
                 for (int k = 0; k < this.n; k++)
                 {
                     rezultat.valori[i, j] += this.valori[i, k] * a.valori[k, j];
                 }
             }
         }
         return(rezultat);
     }
 }
Ejemplo n.º 13
0
        public Matrice Inverse()
        {
            if (this.n == this.m)
            {
                double d = this.det(this.values, this.n);
                if (d != 0)
                {
                    Matrice rez  = new Matrice(this.n);
                    Matrice temp = new Matrice(this.n);

                    for (int i = 0; i < n; i++)
                    {
                        for (int j = 0; j < n; j++)
                        {
                            temp.values[i, j] = this.values[j, i];
                        }
                    }
                    double aux;
                    int    semn;

                    for (int i = 0; i < n; i++)
                    {
                        for (int j = 0; j < n; j++)
                        {
                            for (int k = 0; k < n; k++)
                            {
                                aux = temp.values[i, k];
                                temp.values[i, k]     = temp.values[n - 1, k];
                                temp.values[n - 1, k] = aux;
                            }

                            for (int k = 0; k < n; k++)
                            {
                                aux = temp.values[k, j];
                                temp.values[k, j]     = temp.values[k, n - 1];
                                temp.values[k, n - 1] = aux;
                            }

                            semn = 1;
                            if (((n - 1 - i) % 2 == 0) && (i != n - 1))
                            {
                                semn *= -1;
                            }
                            if (((n - 1 - j) % 2 == 0) && (j != n - 1))
                            {
                                semn *= -1;
                            }
                            if ((i + j) % 2 == 1)
                            {
                                semn *= -1;
                            }
                            rez.values[i, j] = semn * det(temp.values, n - 1);

                            for (int k = 0; k < n; k++)
                            {
                                aux = temp.values[i, k];
                                temp.values[i, k]     = temp.values[n - 1, k];
                                temp.values[n - 1, k] = aux;
                            }
                            for (int k = 0; k < n; k++)
                            {
                                aux = temp.values[k, j];
                                temp.values[k, j]     = temp.values[k, n - 1];
                                temp.values[k, n - 1] = aux;
                            }
                        }
                    }

                    for (int i = 0; i < n; i++)
                    {
                        for (int j = 0; j < n; j++)
                        {
                            rez.values[i, j] /= d;
                        }
                    }
                    return(rez);
                }
                Console.WriteLine("This matrix is not inversable. The determinant is zero.");
                return(null);
            }
            Console.WriteLine("You cannot inverse this matrix, it's not a square matrix.");
            return(null);
        }
Ejemplo n.º 14
0
        public Matrice Inversare()
        {
            if (this.m == this.n)
            {
                double d = this.Determinant(this.valori, this.m);
                if (d != 0)
                {
                    Matrice rezultat = new Matrice(this.m);
                    Matrice temp     = new Matrice(this.m);
                    for (int i = 0; i < m; i++)
                    {
                        for (int j = 0; j < m; j++)
                        {
                            temp.valori[i, j] = this.valori[j, i];
                        }
                    }
                    double f;
                    int    semn;
                    for (int i = 0; i < m; i++)
                    {
                        for (int j = 0; j < m; j++)
                        {
                            for (int k = 0; k < m; k++)
                            {
                                f = temp.valori[i, k];
                                temp.valori[i, k]     = temp.valori[m - 1, k];
                                temp.valori[m - 1, k] = f;
                            }

                            for (int k = 0; k < m; k++)
                            {
                                f = temp.valori[k, j];
                                temp.valori[k, j]     = temp.valori[k, m - 1];
                                temp.valori[k, m - 1] = f;
                            }
                            semn = 1;
                            if (((m - 1 - i) % 2 == 0) && (i != m - 1))
                            {
                                semn *= -1;
                            }
                            if (((m - 1 - j) % 2 == 0) && (j != m - 1))
                            {
                                semn *= -1;
                            }
                            if ((i + j) % 2 == 1)
                            {
                                semn *= -1;
                            }
                            rezultat.valori[i, j] = semn * Determinant(temp.valori, m - 1);
                            for (int k = 0; k < m; k++)
                            {
                                f = temp.valori[i, k];
                                temp.valori[i, k]     = temp.valori[m - 1, k];
                                temp.valori[m - 1, k] = f;
                            }
                            for (int k = 0; k < m; k++)
                            {
                                f = temp.valori[k, j];
                                temp.valori[k, j]     = temp.valori[k, m - 1];
                                temp.valori[k, m - 1] = f;
                            }
                        }
                    }
                    for (int i = 0; i < m; i++)
                    {
                        for (int j = 0; j < m; j++)
                        {
                            rezultat.valori[i, j] /= d;
                        }
                    }
                    return(rezultat);
                }
                Console.WriteLine("Determinantul fiind zero face inversarea imposibila");
                return(null);
            }
            Console.WriteLine("Nefiind o matrice patratica face inversarea imposibila");
            return(null);
        }