Exemple #1
0
        static void Main(string[] args)
        {
            int m = int.Parse(Console.ReadLine());
            int n = m; Console.WriteLine();

            Matrice A = new Matrice(m, n);
            Matrice B = new Matrice(m, n);

            A.Generare(m, n); Console.WriteLine(A);
            B.Generare(m, n); Console.WriteLine(B);

            Console.WriteLine("Suma");
            Matrice sum = A.Suma(B); Console.WriteLine(sum);

            Console.WriteLine("Diferenta");
            Matrice dif = A.Scadere(B); Console.WriteLine(dif);

            Console.WriteLine("Produsul");
            Matrice produs = A.Inmultire(B); Console.WriteLine(produs);

            Console.WriteLine("Prima matrice ridicata la a 2-a");
            Matrice putere = A.Putere(2);  Console.WriteLine(putere);

            Console.WriteLine("Transpusa primei matrici");
            Matrice transpusa = A.Transpusa(); Console.WriteLine(transpusa);

            Console.ReadKey();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            Matrice a = new Matrice(3, 3);

            a.afisare();
            Console.WriteLine();
            Matrice b = new Matrice(3);

            b.afisare();
            Console.WriteLine();
            Console.WriteLine($"Adunarea a doua matrici:");
            Matrice c = a.adunare(b);

            c.afisare();

            Console.WriteLine($"Scaderea a doua matrici:");
            Matrice d = a.scadere(b);

            d.afisare();

            Console.WriteLine($"Inmultire a doua matrici:");
            Matrice e = a.inmultire(b);

            e.afisare();

            Console.WriteLine();
            Console.WriteLine($"Inversa matricii:");
            Matrice p = a.inversa();

            p.afisare();
            Console.ReadKey();
        }
Exemple #3
0
        public Matrice Transpusa()
        {
            Matrice r = new Matrice(this.a, this.c);

            for (int i = 0; i < a; i++)
            {
                for (int j = 0; j < c; j++)
                {
                    r.SetElement(i, j, matrix[j, i]);
                }
            }
            return(r);
        }
Exemple #4
0
        public Matrice Suma(Matrice B)
        {
            int     sum = 0;
            Matrice r   = new Matrice(this.a, this.c);

            for (int i = 0; i < this.a; i++)
            {
                for (int j = 0; j < this.c; j++)
                {
                    sum = matrix[i, j] + B.GetElement(i, j);
                    r.SetElement(i, j, sum);
                }
            }
            return(r);
        }
Exemple #5
0
        public Matrice Scadere(Matrice B)
        {
            int     dif;
            Matrice r = new Matrice(this.a, this.c);

            for (int i = 0; i < this.a; i++)
            {
                for (int j = 0; j < this.c; j++)
                {
                    dif = matrix[i, j] - B.GetElement(i, j);
                    r.SetElement(i, j, dif);
                }
            }
            return(r);
        }
Exemple #6
0
        public Matrice ridicare(Matrice a, int n)
        {
            Matrice ridicare = new Matrice(a.n, a.m);

            ridicare = a;
            if (n == 1)
            {
                return(a);
            }
            while (n != 1)
            {
                ridicare = ridicare.inmultire(a);
                n--;
            }
            return(ridicare);
        }
Exemple #7
0
        public Matrice Putere(int pow)
        {
            Matrice r = this;

            if (pow == 1)
            {
                return(this);
            }
            else
            {
                for (int i = 0; i < pow - 1; i++)
                {
                    r = r.Inmultire(this);
                }
                return(r);
            }
        }
Exemple #8
0
 public Matrice scadere(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.mat[i, j] = mat[i, j] - a.mat[i, j];
             }
         }
         return(rez);
     }
     Console.WriteLine("Matricile nu se pot scade, deoarece nu au aceeasi dimensiune.");
     return(null);
 }
Exemple #9
0
        public Matrice Inmultire(Matrice B)
        {
            int     aux;
            Matrice r = new Matrice(this.a, B.c);

            for (int i = 0; i < this.a; i++)
            {
                for (int j = 0; j < B.c; j++)
                {
                    aux = 0;
                    for (int k = 0; k < this.c; k++)
                    {
                        aux += matrix[i, k] * B.GetElement(k, j);
                    }
                    r.SetElement(i, j, aux);
                }
            }
            return(r);
        }
Exemple #10
0
        public Matrice inmultire(Matrice a)
        {
            if (m == a.n)
            {
                Matrice rez = new Matrice(n, a.m);


                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)
                    {
                        rez.mat[i, j] = 0.00;
                        for (int k = 0; k < a.m; k++)
                        {
                            rez.mat[i, j] += (mat[i, k] * a.mat[k, j]);
                        }
                    }
                }
                return(rez);
            }
            Console.WriteLine("Matricile nu se pot inmultii, deoarece nu au aceeasi linie-coloane.");
            return(null);
        }
Exemple #11
0
        public Matrice inversa()
        {
            if (this.n == this.m)
            {
                double d = this.det(this.mat, 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.mat[i, j] = mat[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.mat[i, k];
                                temp.mat[i, k]     = temp.mat[n - 1, k];
                                temp.mat[n - 1, k] = aux;
                            }
                            for (int k = 0; k < n; k++)
                            {
                                aux                = temp.mat[k, j];
                                temp.mat[k, j]     = temp.mat[k, n - 1];
                                temp.mat[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.mat[i, j] = semn * det(temp.mat, n - 1);

                            for (int k = 0; k < n; k++)
                            {
                                aux                = temp.mat[i, k];
                                temp.mat[i, k]     = temp.mat[n - 1, k];
                                temp.mat[n - 1, k] = aux;
                            }
                            for (int k = 0; k < n; k++)
                            {
                                aux                = temp.mat[k, j];
                                temp.mat[k, j]     = temp.mat[k, n - 1];
                                temp.mat[k, n - 1] = aux;
                            }
                        }
                    }
                    for (int i = 0; i < n; i++)
                    {
                        for (int j = 0; j < n; j++)
                        {
                            rez.mat[i, j] /= d;
                        }
                    }
                    return(rez);
                }
                Console.WriteLine("Matricea nu este inversabila. Are determinantul nul.");
                return(null);
            }
            Console.WriteLine("Matricea nu se poate inversa. Nu este patratica.");
            return(null);
        }