Ejemplo n.º 1
0
        protected void SwapRowDuringPivoting(int PivotRow, int relativeRow, ref MatrixColumn PivotColumn, ref MatrixRow[] localRows)
        {
            if (relativeRow > 0)
            {
                int           PivotIndex = PivotRow + relativeRow;
                MatrixElement a          = Rows[PivotRow].FirsElement;
                Rows[PivotRow].FirsElement   = Rows[PivotIndex].FirsElement;
                Rows[PivotIndex].FirsElement = a;

                /*
                 * double rhs = rightHandSide[PivotRow];
                 * rightHandSide[PivotRow] = rightHandSide[PivotIndex];
                 * rightHandSide[PivotIndex] = rhs;
                 * */
                MatrixElement b = localRows[0].FirsElement;
                localRows[0].FirsElement           = localRows[relativeRow].FirsElement;
                localRows[relativeRow].FirsElement = b;

                det = -det;

                int temp = RowOrder[PivotRow];
                RowOrder[PivotRow]   = RowOrder[PivotIndex];
                RowOrder[PivotIndex] = temp;

                SwapLocalRowDuringPivoting(PivotRow, relativeRow, ref PivotColumn);
            }
        }
Ejemplo n.º 2
0
        protected void ScalePivotColumnToPivot(ref MatrixColumn pivotColumn)
        {
            MatrixColumnElement a = pivotColumn.FirsElement;
            double pivotValue     = a.RowElement.Value;

            while (a.NotLastItem)
            {
                a = a.NextElement;
                a.RowElement.Value /= pivotValue;
            }
        }
Ejemplo n.º 3
0
 protected int GetRowOfMaxElementInColumn(ref MatrixColumn column)
 {
     if (column.ColumnNotPopulated)
     {
         return(-1);
     }
     else
     {
         return(GetRowOfMaxElementInColumn(column.FirsElement));
     }
 }
        private int LUDecomposition_CountElements(MatrixColumn Column)
        {
            MatrixColumnElement a = Column.FirsElement;
            int count             = 1;

            while (a.NotLastItem)
            {
                count++;
                a = a.NextElement;
            }
            return(count);
        }
Ejemplo n.º 5
0
        protected void SwapLocalRowDuringPivoting(int PivotRow, int relativeRow, ref MatrixColumn PivotColumn)
        {
            MatrixColumnElement FirstRow = PivotColumn.FirsElement;
            MatrixColumnElement a        = FirstRow;
            MatrixColumnElement aLast;

            while (a.NotLastItem)
            {
                aLast = a;
                a     = a.NextElement;

                if (a.Index == relativeRow)
                {
                    if (FirstRow.NextElement.Index == a.Index)
                    {
                        FirstRow.Index = a.Index;
                        if (a.NotLastItem)
                        {
                            FirstRow.NextElement = a.NextElement;
                        }
                        else
                        {
                            FirstRow.NotLastItem = false;
                            FirstRow.NextElement = null;
                        }
                        a.Index                 = 0;
                        a.NextElement           = FirstRow;
                        a.NotLastItem           = true;
                        PivotColumn.FirsElement = a;
                    }
                    else
                    {
                        MatrixColumnElement SecondElement = FirstRow.NextElement;
                        aLast.NextElement = FirstRow;
                        FirstRow.Index    = a.Index;
                        if (a.NotLastItem)
                        {
                            FirstRow.NextElement = a.NextElement;
                        }
                        else
                        {
                            FirstRow.NotLastItem = false;
                            FirstRow.NextElement = null;
                        }
                        a.Index                 = 0;
                        a.NextElement           = SecondElement;
                        a.NotLastItem           = true;
                        PivotColumn.FirsElement = a;
                    }
                    return;
                }
            }
        }
Ejemplo n.º 6
0
 protected int GetRowOfMaxElementInColumn(ref MatrixColumn column, out MatrixColumnElement MaxColumnElement)
 {
     if (column.ColumnNotPopulated)
     {
         MaxColumnElement = null;
         return(-1);
     }
     else
     {
         return(GetRowOfMaxElementInColumn(column.FirsElement, out MaxColumnElement));
     }
 }
        public void LUDecomposition()
        {
            DiagonalElements = new MatrixRow[n];
            InitializeLocalRows();

            for (int i = 0; i < n - 1; i++)
            {
                MatrixColumn        PivotColumn = GetColumn(ref LocalRows, i);
                MatrixColumnElement MaxColumnElement;
                int newRelativePivotRow = GetRowOfMaxElementInColumn(ref PivotColumn, out MaxColumnElement);
                SwapRowDuringPivoting(i, newRelativePivotRow, ref PivotColumn, ref LocalRows);
                ScalePivotColumnToPivot(ref PivotColumn);
                DiagonalElements[i] = new MatrixRow();
                DiagonalElements[i].RowNotPopulated = false;
                DiagonalElements[i].FirsElement     = PivotColumn.FirsElement.RowElement;

                MatrixColumnElement a = PivotColumn.FirsElement;
                MatrixElement       b = PivotColumn.FirsElement.RowElement;
                double        colVal;
                double        change;
                MatrixElement StartRowSearch;
                MatrixElement c;
                if (b.NotLastItem)
                {
                    if (a.NotLastItem)
                    {
                        //normal
                        while (a.NotLastItem)
                        {
                            a = a.NextElement;
                            StartRowSearch = LocalRows[a.Index].FirsElement;
                            colVal         = a.RowElement.Value;
                            c = b;


                            while (c.NotLastItem)
                            {
                                c              = c.NextItem;
                                change         = -colVal * c.Value;
                                StartRowSearch = AddToElementInRow(ref StartRowSearch, c.Index, change);
                            }
                        }
                    }
                }
                LocalRows = GetLocalRows(LocalRows, i + 1);
            }
            DiagonalElements[n - 1].FirsElement = LocalRows[0].FirsElement;
        }
        private int LUDecomposition_CountElementsColumn(MatrixColumn Column, out MatrixColumnElement[] ColumnArray)
        {
            MatrixColumnElement a = Column.FirsElement;
            int count             = 1;

            while (a.NotLastItem)
            {
                count++;
                a = a.NextElement;
            }
            ColumnArray = new MatrixColumnElement[count - 1];
            a           = Column.FirsElement;
            for (int i = 0; i < count - 1; i++)
            {
                a = a.NextElement;
                ColumnArray[i] = a;
            }
            return(count);
        }
Ejemplo n.º 9
0
        public MatrixColumn GetColumn(ref MatrixRow[] rows, int col)//, int numberOfElements)
        {
            MatrixColumn NewColumn = new MatrixColumn();

            NewColumn.ColumnNotPopulated = true;
            int lenght = rows.Length;
            //numberOfElements = 0;
            //MatrixElement FirstElement;
            MatrixElement       a;
            MatrixColumnElement b = new MatrixColumnElement();

            for (int i = 0; i < rows.Length; i++)
            {
                a = GetElementInRow(ref rows[i], col);
                if (!(a == null))
                {
                    MatrixColumnElement c = new MatrixColumnElement();
                    c.Index       = i;
                    c.NotLastItem = false;
                    c.RowElement  = a;
                    if (NewColumn.ColumnNotPopulated)
                    {
                        NewColumn.FirsElement = c;
                        b = c;
                        NewColumn.ColumnNotPopulated = false;
                    }
                    else
                    {
                        b.NextElement = c;
                        b.NotLastItem = true;
                        b             = c;
                    }
                }
            }
            return(NewColumn);
        }
        public void LUDecomposition_Parallel()
        {
            // LUDecomposition with Parallel operations conducted for multi-processors
            // M. Negahban
            // 5-8-2011
            DiagonalElements = new MatrixRow[n];
            InitializeLocalRows();

            //For parallel
            double[] A_values   = new double[n];
            int[]    A_Indecies = new int[n];
            int      NumberOfRows;

            for (int i = 0; i < n - 1; i++)
            {
                MatrixColumn        PivotColumn = GetColumn(ref LocalRows, i);
                MatrixColumnElement MaxColumnElement;
                int newRelativePivotRow = GetRowOfMaxElementInColumn(ref PivotColumn, out MaxColumnElement);
                SwapRowDuringPivoting(i, newRelativePivotRow, ref PivotColumn, ref LocalRows);
                ScalePivotColumnToPivot(ref PivotColumn);
                DiagonalElements[i] = new MatrixRow();
                DiagonalElements[i].RowNotPopulated = false;
                DiagonalElements[i].FirsElement     = PivotColumn.FirsElement.RowElement;

                MatrixColumnElement a = PivotColumn.FirsElement;
                MatrixElement       b = PivotColumn.FirsElement.RowElement;
                if (b.NotLastItem)
                {
                    if (a.NotLastItem)
                    {
                        //Parallel
                        NumberOfRows = 0;
                        while (a.NotLastItem)
                        {
                            a = a.NextElement;
                            A_values[NumberOfRows]   = a.RowElement.Value;
                            A_Indecies[NumberOfRows] = a.Index;
                            NumberOfRows++;
                        }

                        //If you want to control level of parallelism Add
                        //ParallelOptions po = new ParallelOptions();
                        //po.MaxDegreeOfParallelism = 100; // select any number grater than 0
                        //Parallel.For(0, NumberOfRows, po, j =>
                        Parallel.For(0, NumberOfRows, j =>
                        {
                            double colValJ = A_values[j];
                            MatrixElement StartRowSearchJ = LocalRows[A_Indecies[j]].FirsElement;
                            MatrixElement cJ = b;

                            while (cJ.NotLastItem)
                            {
                                cJ              = cJ.NextItem;
                                double changeJ  = -colValJ * cJ.Value;
                                StartRowSearchJ = AddToElementInRow(ref StartRowSearchJ, cJ.Index, changeJ);
                            }
                        });
                    }
                }
                LocalRows = GetLocalRows(LocalRows, i + 1);
            }
            DiagonalElements[n - 1].FirsElement = LocalRows[0].FirsElement;
        }
Ejemplo n.º 11
0
 protected void SwapRowDuringPivoting(int PivotRow, int relativeRow, MatrixColumnElement MaxColumnElement, ref MatrixColumn PivotColumn, ref MatrixRow[] localRows)
 {
     if (relativeRow > 0)
     {
         // Swap Rows
         int           PivotIndex = PivotRow + relativeRow;
         MatrixElement a          = Rows[PivotRow].FirsElement;
         Rows[PivotRow].FirsElement   = Rows[PivotIndex].FirsElement;
         Rows[PivotIndex].FirsElement = a;
         // Swap localRows
         MatrixElement b = localRows[0].FirsElement;
         localRows[0].FirsElement           = localRows[relativeRow].FirsElement;
         localRows[relativeRow].FirsElement = b;
         // Change sign of determinant
         det = -det;
         // Swap RowOrder
         int temp = RowOrder[PivotRow];
         RowOrder[PivotRow]   = RowOrder[PivotIndex];
         RowOrder[PivotIndex] = temp;
         // Swap PivotColumn
         SwapLocalRowDuringPivoting(PivotRow, relativeRow, MaxColumnElement, ref PivotColumn);
     }
 }