Ejemplo n.º 1
0
        public SparseMatrix ObliczUklad(SparseMatrix y)
        {
            SparseMatrix L;

            WybierzElemGlowny(ref y);
            Decompose(out L);

            //czas obliczyć wektor Z
            SparseMatrix Z    = new SparseMatrix(rows, 1);
            int          temp = y.Get(0, 0);

            Z.Insert(0, 0, temp);            //pierwszy element zawsze równy temu z wektora y

            for (int i = 1; i < L.rows; i++) //licze elementy wektora z
            {
                temp = y.Get(i, 0);
                for (int j = 0; j < i + 1; j++)//licze wartosc i-tego elementu wektora z
                {
                    temp -= Z.Get(j, 0) * L.Get(i, j);
                }
                Z.Insert(i, 0, temp);
            }
            //obliczam ostatni wektor X
            SparseMatrix X = new SparseMatrix(rows, 1);

            temp = Z.Get(rows - 1, 0);
            X.Insert(rows - 1, 0, temp / this.Get(rows - 1, columns - 1));
            for (int i = this.rows - 2; i >= 0; i--)//licze elementy od dołu "this"
            {
                temp = Z.Get(i, 0);
                for (int j = columns - 1; j >= i; j--)
                {
                    temp -= X.Get(j, 0) * this.Get(i, j);
                }
                temp /= this.Get(i, i);
                X.Insert(i, 0, temp);
            }
            this.DoPliku("U.txt");
            L.DoPliku("L.txt");
            X.DoPliku("X.txt");
            Console.WriteLine("Macierz U:");
            PrintMatrix();
            Console.WriteLine("\nMacierz L:");
            L.PrintMatrix();
            return(X);
        }
Ejemplo n.º 2
0
        private void Decompose(out SparseMatrix L)
        {
            SparseMatrix A = this;

            if (A.rows != A.columns)
            {
                Console.WriteLine("Macierz nie jest kwadratowa, niemożliwa dekompozycja");
            }

            L = new SparseMatrix(A.rows, A.columns); //dolnotrójkątna z diagonalą niezerową
                                                     //U = new SparseMatrix(A.rows, A.columns);//górnotrójkątna z diagonalą zerową

            int temp;

            for (int i = 0; i < L.rows; i++)//uzupełniam diagonalę L jedynkami
            {
                L.Insert(i, i, 1);
            }


            //Gauss
            int k;

            for (int m = 0; m < A.rows; m++)
            {
                for (int i = m + 1; i < A.rows; i++)//redukcja i-tych wierszy za pomocą m-tego wiersza
                {
                    k = A.Get(i, m) / A.Get(m, m);
                    for (int j = 0; j < A.columns; j++)//redukcja dwóch konkretnych wierszy
                    {
                        temp = A.Get(i, j) - k * A.Get(m, j);
                        A.Insert(i, j, temp);
                    }
                    L.Insert(i, m, k);

                    //odejmij wiersze
                    //zapisz współczynnik
                }
            }
        }
Ejemplo n.º 3
0
        public SparseMatrix ObliczUklad(SparseMatrix y)
        {
            SparseMatrix L;
            WybierzElemGlowny( ref y);
            Decompose(out L);

            //czas obliczyć wektor Z
               SparseMatrix Z= new SparseMatrix(rows, 1);
            int temp = y.Get(0, 0);
               Z.Insert(0, 0, temp); //pierwszy element zawsze równy temu z wektora y

            for (int i = 1; i < L.rows; i++)//licze elementy wektora z
            {
                temp = y.Get(i, 0);
                for (int j = 0 ; j < i+1; j++)//licze wartosc i-tego elementu wektora z
                {
                    temp -= Z.Get(j ,0) * L.Get(i, j);
                }
                Z.Insert(i, 0, temp);
            }
            //obliczam ostatni wektor X
            SparseMatrix X = new SparseMatrix(rows, 1);
            temp = Z.Get(rows-1, 0);
            X.Insert(rows-1, 0 , temp/this.Get(rows-1, columns-1));
            for (int i = this.rows-2; i >= 0; i--)//licze elementy od dołu "this"
            {
                temp = Z.Get(i, 0);
                for (int j = columns-1 ; j >= i  ; j--)
                {
                    temp -= X.Get(j, 0) * this.Get(i, j);
                }
                    temp/=this.Get(i,i);
                    X.Insert(i, 0, temp);

            }
            this.DoPliku("U.txt");
            L.DoPliku("L.txt");
            X.DoPliku("X.txt");
            Console.WriteLine("Macierz U:");
            PrintMatrix();
            Console.WriteLine("\nMacierz L:");
            L.PrintMatrix();
            return X;
        }