Example #1
0
        public MatrixElement GetElementInRowOrClosestBefore(ref MatrixElement StartElement, int col)
        {
            MatrixElement a = StartElement;

            if (a.Index > col)
            {
                return(null);
            }
            else
            {
                while (a.NotLastItem)
                {
                    if (a.Index == col)
                    {
                        return(a);
                    }
                    else if ((a.Index < col) && (col < a.NextItem.Index))
                    {
                        return(a);
                    }
                    a = a.NextItem;
                }
                return(a);
            }
        }
Example #2
0
        private void FindNewPivotAndSwap(int CurrentPivotRow)
        {
            int    PivotIndex = CurrentPivotRow;
            int    j          = CurrentPivotRow - 1;
            double max        = GetMatrixElement(CurrentPivotRow, CurrentPivotRow);

            max /= GetMaxElementInRowAfterColumnJ(CurrentPivotRow, j);
            max  = Math.Abs(max);

            double value;

            for (int i = CurrentPivotRow + 1; i < n; i++)
            {
                value  = GetMatrixElement(i, CurrentPivotRow);
                value /= GetMaxElementInRowAfterColumnJ(i, j);

                value = Math.Abs(value);
                if (max < value)
                {
                    max        = value;
                    PivotIndex = i;
                }
            }
            if (PivotIndex != CurrentPivotRow)
            {
                MatrixElement a = Rows[CurrentPivotRow].FirsElement;
                Rows[CurrentPivotRow].FirsElement = Rows[PivotIndex].FirsElement;
                Rows[PivotIndex].FirsElement      = a;
                double rhs = rightHandSide[CurrentPivotRow];
                rightHandSide[CurrentPivotRow] = rightHandSide[PivotIndex];
                rightHandSide[PivotIndex]      = rhs;
                det = -det;
            }
        }
Example #3
0
 public double GetMaxElementInRow(int row)
 {
     if (Rows[row].RowNotPopulated)
     {
         return(0.0D);
     }
     else
     {
         MatrixElement a      = Rows[row].FirsElement;
         double        max    = a.Value;
         double        AbsMax = Math.Abs(max);
         double        cValue;
         double        cAbsValue;
         while (a.NotLastItem)
         {
             a         = a.NextItem;
             cValue    = a.Value;
             cAbsValue = Math.Abs(cValue);
             if (AbsMax < cAbsValue)
             {
                 max    = cValue;
                 AbsMax = cAbsValue;
             }
         }
         return(max);
     }
 }
Example #4
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);
            }
        }
Example #5
0
        public double GetElementValueInRow(ref MatrixElement StartElement, int col)
        {
            MatrixElement a = StartElement;

            if (a.Index > col)
            {
                return(0.0d);
            }
            else
            {
                while (a.NotLastItem)
                {
                    if (a.Index == col)
                    {
                        return(a.Value);
                    }
                    else if ((a.Index < col) && (col < a.NextItem.Index))
                    {
                        return(0.0d);
                    }
                    a = a.NextItem;
                }
                if (a.Index == col)
                {
                    return(a.Value);
                }
                else
                {
                    return(0.0d);
                }
            }
        }
Example #6
0
        public void ScaleAllRowsAndRightHandSideAndRemoveZeros()
        {
            this.ScaleAllRowsAndRightHandSide();
            MatrixElement a = new MatrixElement();

            for (int i = 0; i < n; i++)
            {
                RemoveZeros(ref Rows[i]);
            }
        }
        private double Sum2(int row)
        {
            double        sum = 0.0d;
            MatrixElement a   = DiagonalElements[row].FirsElement;

            while (a.NotLastItem)
            {
                a    = a.NextItem;
                sum += a.Value * rightHandSide[a.Index];
            }
            return(sum);
        }
Example #8
0
        private double BackSubMul(int Row)
        {
            MatrixElement a   = Rows[Row].FirsElement;
            double        piv = a.Value;
            double        sum = rightHandSide[Row];

            while (a.NotLastItem)
            {
                a    = a.NextItem;
                sum -= a.Value * rightHandSide[a.Index];
            }
            return(sum / piv);
        }
Example #9
0
 public override void AddToMatrixElement(int row, int col, double value)
 {
     if (Math.Abs(value) < RealZero)
     {
         return;
     }
     else
     {
         if (Rows[row].RowNotPopulated)
         {
             MatrixElement newElement = new MatrixElement();
             newElement.Index          = col;
             newElement.Value          = value;
             newElement.NotLastItem    = false;
             Rows[row].FirsElement     = newElement;
             Rows[row].RowNotPopulated = false;
             LastElementUsed[row]      = newElement;
             return;
         }
         else if (col < Rows[row].FirsElement.Index)
         {
             MatrixElement newElement = new MatrixElement();
             newElement.Index       = col;
             newElement.Value       = value;
             newElement.NotLastItem = true;
             newElement.NextItem    = Rows[row].FirsElement;
             Rows[row].FirsElement  = newElement;
             LastElementUsed[row]   = newElement;
             return;
         }
         else
         {
             if (LastElementUsed[row] != null)
             {
                 if (col >= LastElementUsed[row].Index)
                 {
                     LastElementUsed[row] = this.AddToElementInRow(ref LastElementUsed[row], col, value);
                 }
                 else
                 {
                     LastElementUsed[row] = this.AddToElementInRow(ref Rows[row].FirsElement, col, value);
                 }
             }
             else
             {
                 LastElementUsed[row] = this.AddToElementInRow(ref Rows[row].FirsElement, col, value);
             }
         }
     }
 }
Example #10
0
        public static MatrixSparseLinkedList MultiplyMatrix_ByMatrix(MatrixSparseLinkedList Matrix1, MatrixSparseLinkedList Matrix2)
        {
            MatrixSparseLinkedList NewMatrix = new MatrixSparseLinkedList();

            NewMatrix.InitializeMatrix(Matrix1.n);
            MatrixSparseLinkedList A = Matrix1;
            int NumberOfRows_A       = A.n;
            MatrixSparseLinkedList B = Matrix2;
            int NumberOfRows_B       = B.n;

            for (int i = 0; i < NumberOfRows_A; i++)
            {
                if (!A.Rows[i].RowNotPopulated)
                {
                    MatrixElement E_A = A.Rows[i].FirsElement;
                    while (E_A.NotLastItem)
                    {
                        for (int j = 0; j < NumberOfRows_B; j++)
                        {
                            if (!B.Rows[j].RowNotPopulated)
                            {
                                MatrixElement E_B = B.Rows[j].FirsElement;
                                while (E_B.NotLastItem)
                                {
                                    NewMatrix.AddToMatrixElement(i, E_B.Index, E_A.Value * E_B.Value);
                                    E_B = E_B.NextItem;
                                }
                                NewMatrix.AddToMatrixElement(i, E_B.Index, E_A.Value * E_B.Value);
                            }
                        }
                        E_A = E_A.NextItem;
                    }
                    for (int j = 0; j < NumberOfRows_B; j++)
                    {
                        if (!B.Rows[j].RowNotPopulated)
                        {
                            MatrixElement E_B = B.Rows[j].FirsElement;
                            while (E_B.NotLastItem)
                            {
                                NewMatrix.AddToMatrixElement(i, E_B.Index, E_A.Value * E_B.Value);
                                E_B = E_B.NextItem;
                            }
                            NewMatrix.AddToMatrixElement(i, E_B.Index, E_A.Value * E_B.Value);
                        }
                    }
                }
            }
            return(NewMatrix);
        }
        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;
        }
Example #12
0
        protected void RemoveZeros(ref MatrixRow Row)
        {
            MatrixElement a    = Row.FirsElement;
            bool          test = true;

            while (test)
            {
                if (a.NotLastItem)
                {
                    if (Math.Abs(a.NextItem.Value) < RealZero)
                    {
                        if (a.NextItem.NotLastItem)
                        {
                            a.NextItem = a.NextItem.NextItem;
                        }
                        else
                        {
                            a.NextItem    = null;
                            a.NotLastItem = false;
                            test          = false;
                        }
                    }
                    if (test)
                    {
                        a = a.NextItem;
                    }
                }
                else
                {
                    test = false;
                }
            }
            a = Row.FirsElement;

            if (Math.Abs(a.Value) < RealZero)
            {
                if (a.NotLastItem)
                {
                    Row.FirsElement = a.NextItem;
                }
                else
                {
                    Row.RowNotPopulated = true;
                    Row.FirsElement     = null;
                }
            }
        }
Example #13
0
        private void RemoveElementsFromStartOfRow(ref MatrixRow Row, int LastColumnToRemive)
        {
            MatrixElement a    = Row.FirsElement;
            bool          test = true;

            while (test)
            {
                if (a.NotLastItem)
                {
                    if (a.NextItem.Index < LastColumnToRemive + 1)
                    {
                        if (a.NextItem.NextItem.NotLastItem)
                        {
                            a.NextItem = a.NextItem.NextItem;
                            //a = a.NextItem;
                        }
                        else
                        {
                            a.NotLastItem = false;
                            a.NextItem    = null;
                            test          = false;
                        }
                    }
                    else
                    {
                        test = false;
                    }
                }
                else
                {
                    test = false;
                }
            }
            a = Row.FirsElement;
            if (a.Index < LastColumnToRemive + 1)
            {
                if (a.NotLastItem)
                {
                    Row.FirsElement = a.NextItem;
                }
                else
                {
                    Row.FirsElement     = null;
                    Row.RowNotPopulated = true;
                }
            }
        }
Example #14
0
 public void ScaleRow(int row, double scaleFactor)
 {
     if (Rows[row].RowNotPopulated)
     {
     }
     else
     {
         MatrixElement a = Rows[row].FirsElement;
         a.Value *= scaleFactor;
         while (a.NotLastItem)
         {
             a        = a.NextItem;
             a.Value *= scaleFactor;
         }
     }
     rightHandSide[row] *= scaleFactor;
 }
Example #15
0
 public double GetMaxElementInRowAfterColumnJ(int row, int columnJ)
 {
     if (Rows[row].RowNotPopulated)
     {
         return(0.0d);// double.NaN;
     }
     else
     {
         MatrixElement a = Rows[row].FirsElement;
         double        max;
         double        AbsMax;
         if (a.Index > columnJ)
         {
             max    = a.Value;
             AbsMax = Math.Abs(max);
         }
         else
         {
             max    = 0.0d;
             AbsMax = 0.0d;
         }
         double cValue;
         double cAbsValue;
         while (a.NotLastItem)
         {
             if (a.Index > columnJ)
             {
                 a         = a.NextItem;
                 cValue    = a.Value;
                 cAbsValue = Math.Abs(cValue);
                 if (AbsMax < cAbsValue)
                 {
                     max    = cValue;
                     AbsMax = cAbsValue;
                 }
             }
             else
             {
                 a = a.NextItem;
             }
         }
         return(max);
     }
 }
        private int LUDecomposition_CountElementsRow(MatrixRow Row, out MatrixElement[] RowArray)
        {
            MatrixElement a     = Row.FirsElement;
            int           count = 1;

            while (a.NotLastItem)
            {
                count++;
                a = a.NextItem;
            }
            RowArray = new MatrixElement[count - 1];
            a        = Row.FirsElement;
            for (int i = 0; i < count - 1; i++)
            {
                a           = a.NextItem;
                RowArray[i] = a;
            }
            return(count);
        }
Example #17
0
        public static void MultiplyScalar_ByExistingMatrix(ref MatrixSparseLinkedList ExistingMatrix, double Scalar)
        {
            MatrixSparseLinkedList A = ExistingMatrix;
            int NumberOfRows         = A.n;

            for (int i = 0; i < NumberOfRows; i++)
            {
                if (!A.Rows[i].RowNotPopulated)
                {
                    MatrixElement E = A.Rows[i].FirsElement;
                    while (E.NotLastItem)
                    {
                        E.Value *= Scalar;
                        E        = E.NextItem;
                    }
                    E.Value *= Scalar;
                }
            }
        }
Example #18
0
        public static void SubtractMatrix_FromExistingMatrix(ref MatrixSparseLinkedList ExistingMatrix, MatrixSparseLinkedList MatrixToSubtract)
        {
            MatrixSparseLinkedList A = MatrixToSubtract;
            int NumberOfRows         = A.n;

            for (int i = 0; i < NumberOfRows; i++)
            {
                if (!A.Rows[i].RowNotPopulated)
                {
                    MatrixElement E = A.Rows[i].FirsElement;
                    while (E.NotLastItem)
                    {
                        ExistingMatrix.AddToMatrixElement(i, E.Index, -E.Value);
                        E = E.NextItem;
                    }
                    ExistingMatrix.AddToMatrixElement(i, E.Index, -E.Value);
                }
            }
        }
        private double Sum1(int row)
        {
            double        sum = 0.0d;
            MatrixElement a   = Rows[row].FirsElement;

            //bool test = true;
            while (a.NotLastItem)
            {
                if (a.Index < row)
                {
                    sum += a.Value * rightHandSide[a.Index];
                    a    = a.NextItem;
                }
                else
                {
                    return(sum);
                }
            }
            return(sum);
        }
Example #20
0
        protected MatrixElement AddToElementInRow_Parallel(ref MatrixRow LocalRow, ref MatrixElement FirstElement, int col, double value)
        {
            MatrixElement a = FirstElement;

            while (a.NotLastItem)
            {
                if (a.Index == col)
                {
                    a.Value += value;
                    return(a);
                }
                else if ((a.Index < col) && (col < a.NextItem.Index))
                {
                    MatrixElement newElement = new MatrixElement();
                    newElement.Index       = col;
                    newElement.Value       = value;
                    newElement.NotLastItem = true;
                    newElement.NextItem    = a.NextItem;
                    a.NextItem             = newElement;
                    return(newElement);
                }
                a = a.NextItem;
            }
            if (a.Index == col)
            {
                a.Value += value;
                return(a);
            }
            else
            {
                MatrixElement newElement = new MatrixElement();
                newElement.Index       = col;
                newElement.Value       = value;
                newElement.NotLastItem = false;
                a.NotLastItem          = true;
                a.NextItem             = newElement;
                return(newElement);
            }
        }
Example #21
0
        public static MatrixSparseLinkedList MultiplyScalar_ByMatrix(MatrixSparseLinkedList Matrix1, double Scalar)
        {
            MatrixSparseLinkedList A         = Matrix1;
            int NumberOfRows                 = A.n;
            MatrixSparseLinkedList NewMatrix = new MatrixSparseLinkedList();

            NewMatrix.InitializeMatrix(NumberOfRows);
            for (int i = 0; i < NumberOfRows; i++)
            {
                if (!A.Rows[i].RowNotPopulated)
                {
                    MatrixElement E = A.Rows[i].FirsElement;
                    while (E.NotLastItem)
                    {
                        NewMatrix.AddToMatrixElement(i, E.Index, E.Value * Scalar);
                        E = E.NextItem;
                    }
                    NewMatrix.AddToMatrixElement(i, E.Index, E.Value * Scalar);
                }
            }
            return(NewMatrix);
        }
        public Vector ATx_Symmetric(MatrixSparseLinkedList A, Vector x)
        {
            //Calculate the product y = A^T x
            int    NumberOfRows = A.n;
            Vector y            = new Vector(NumberOfRows);

            for (int i = 0; i < NumberOfRows; i++)
            {
                MatrixElement StartElement = A.Rows[i].FirsElement;
                double        xi           = x.Values[i];
                int           index;
                while (StartElement.NotLastItem)
                {
                    index            = StartElement.Index;
                    y.Values[index] += StartElement.Value * xi;
                    StartElement     = StartElement.NextItem;
                }
                index            = StartElement.Index;
                y.Values[index] += StartElement.Value * xi;
            }
            return(y);
        }
        public Vector Ax_Parallel(MatrixSparseLinkedList A, Vector x)
        {
            //Calculate the product y = A x
            int    NumberOfRows = A.n;
            Vector y            = new Vector(NumberOfRows);

            Parallel.For(0, NumberOfRows, i =>
            {
                MatrixElement StartElement = A.Rows[i].FirsElement;
                double temp = 0.0D;
                int index;
                while (StartElement.NotLastItem)
                {
                    index        = StartElement.Index;
                    temp        += StartElement.Value * x.Values[index];
                    StartElement = StartElement.NextItem;
                }
                index       = StartElement.Index;
                temp       += StartElement.Value * x.Values[index];
                y.Values[i] = temp;
            });
            return(y);
        }
Example #24
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);
     }
 }
Example #25
0
        public override Vector MultiplyByVectorU(Vector u)
        {
            Vector Y = new Vector(n); // Make right-hand-side

            for (int i = 0; i < n; i++)
            {
                Y.Values[i] = 0.0d;                    // initialize RHS
                MatrixElement E = Rows[i].FirsElement; //Get row first element
                for (int j = 0; j < n; j++)
                {
                    Y.Values[i] += E.Value * u.Values[E.Index]; //multilpy
                    if (E.NotLastItem)
                    {
                        E = E.NextItem; // go to next element in row
                    }
                    else
                    {
                        break; // break if last element in row
                    }
                }
            }
            return(Y);
        }
Example #26
0
        public static Vector MultiplyExistingMatrix_ByVector(ref MatrixSparseLinkedList ExistingMatrix, Vector V)
        {
            Vector NewVector         = new Vector(ExistingMatrix.n);
            MatrixSparseLinkedList A = ExistingMatrix;
            int NumberOfRows         = A.n;

            for (int i = 0; i < NumberOfRows; i++)
            {
                double Temp = 0.0D;
                if (!A.Rows[i].RowNotPopulated)
                {
                    MatrixElement E = A.Rows[i].FirsElement;
                    while (E.NotLastItem)
                    {
                        Temp += E.Value * V.Values[E.Index];
                        E     = E.NextItem;
                    }
                    Temp += E.Value * V.Values[E.Index];
                }
                NewVector.Values[i] = Temp;
            }
            return(NewVector);
        }
Example #27
0
 protected void AddToElementInRow(ref MatrixRow Row, int col, double value)
 {
     if (Math.Abs(value) < RealZero)
     {
         return;
     }
     else
     {
         if (Row.RowNotPopulated)
         {
             MatrixElement newElement = new MatrixElement();
             newElement.Index       = col;
             newElement.Value       = value;
             newElement.NotLastItem = false;
             Row.FirsElement        = newElement;
             Row.RowNotPopulated    = false;
             return;
         }
         else
         {
             AddToElementInRow(ref Row.FirsElement, col, value);
         }
     }
 }
        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;
        }
Example #29
0
 public override void SetMatrixElement(int row, int col, double value)
 {
     if (Math.Abs(value) < RealZero)
     {
         DeleteMatrixElement(row, col);
     }
     else
     {
         if (Rows[row].RowNotPopulated)
         {
             MatrixElement newElement = new MatrixElement();
             newElement.Index          = col;
             newElement.Value          = value;
             newElement.NotLastItem    = false;
             Rows[row].FirsElement     = newElement;
             Rows[row].RowNotPopulated = false;
             return;
         }
         else
         {
             MatrixElement a = Rows[row].FirsElement;
             if (a.Index > col)
             {
                 MatrixElement newElement = new MatrixElement();
                 newElement.Index       = col;
                 newElement.Value       = value;
                 newElement.NotLastItem = true;
                 newElement.NextItem    = a;
                 Rows[row].FirsElement  = newElement;
                 return;
             }
             else
             {
                 while (a.NotLastItem)
                 {
                     if (a.Index == col)
                     {
                         a.Value = value;
                         return;
                     }
                     else if ((a.Index < col) && (col < a.NextItem.Index))
                     {
                         MatrixElement newElement = new MatrixElement();
                         newElement.Index       = col;
                         newElement.Value       = value;
                         newElement.NotLastItem = true;
                         newElement.NextItem    = a.NextItem;
                         a.NextItem             = newElement;
                         return;
                     }
                     a = a.NextItem;
                 }
                 if (a.Index == col)
                 {
                     a.Value = value;
                     return;
                 }
                 else
                 {
                     MatrixElement newElement = new MatrixElement();
                     newElement.Index       = col;
                     newElement.Value       = value;
                     newElement.NotLastItem = false;
                     a.NotLastItem          = true;
                     a.NextItem             = newElement;
                     return;
                 }
             }
         }
     }
 }
Example #30
0
        public void DeleteMatrixElement(int row, int col)
        {
            MatrixRow TheRow = Rows[row];

            if (TheRow.RowNotPopulated)
            {
                return;
            }
            else
            {
                MatrixElement a = TheRow.FirsElement;
                if (a.Index > col)
                {
                    return;
                }
                else
                {
                    if (a.Index == col)
                    {
                        if (a.NotLastItem)
                        {
                            TheRow.FirsElement = a.NextItem;
                            return;
                        }
                        else
                        {
                            TheRow.RowNotPopulated = true;
                            TheRow.FirsElement     = null;
                            return;
                        }
                    }
                    else
                    {
                        while (a.NotLastItem)
                        {
                            if ((a.Index < col) && (col < a.NextItem.Index))
                            {
                                return;
                            }
                            else if (a.NextItem.Index == col)
                            {
                                if (a.NextItem.NotLastItem)
                                {
                                    a.NextItem = a.NextItem.NextItem;
                                    return;
                                }
                                else
                                {
                                    a.NotLastItem = false;
                                    a.NextItem    = null;
                                    return;
                                }
                            }
                            else if (a.NextItem.NotLastItem == false)
                            {
                                return;
                            }
                            a = a.NextItem;
                        }
                    }
                }
            }
        }