/// <summary>
 /// Returns the upper triagonal matrix that was created during the LU decomposition.
 /// </summary>
 /// <remarks>
 /// This method is used for debugging purposes only and should normally not be used.
 /// </remarks>
 /// <returns>A new matrix containing the upper triagonal elements.</returns>
 internal Matrix <Complex> UpperTriangle()
 {
     return(_upper.Clone());
 }
            public static Status Solve(int NumberOfEquations, SparseMatrix aMatrix, SparseArray bVector, SparseArray xVector)
            {
                var aMatrixClone = aMatrix.Clone();

                var RowMaximumVector = new Dictionary<int, double>();

                int i;
                for (i = 0; i < NumberOfEquations; i++)
                {
                    double temp = 0;

                    for (var j = 0; j < NumberOfEquations; j++)
                    {
                        var Test = Math.Abs(aMatrix[i, j]);

                        if (Test > temp)
                            temp = Test;
                    }

                    RowMaximumVector[i] = temp;

                    if (temp == 0)
                        return Status.Singular;
                }

                var PivotRowArray = new Dictionary<int, int>();

                for (var r = 0; r < NumberOfEquations; r++)
                {
                    double MaximumValue = 0;
                    var RowMaximumValueIndex = r;

                    double Temp;

                    for (i = r; i < NumberOfEquations; i++)
                    {
                        Temp = aMatrixClone[i, r];

                        for (var j = 0; j < r; j++) Temp = Temp - aMatrixClone[i, j] * aMatrixClone[j, r];

                        aMatrixClone[i, r] = Temp;

                        var test = Math.Abs(Temp / RowMaximumVector[i]);

                        if (!(test > MaximumValue))
                            continue;

                        MaximumValue = test;
                        RowMaximumValueIndex = i;
                    }

                    if (MaximumValue == 0)
                        return Status.IllConditioned;

                    RowMaximumVector[RowMaximumValueIndex] = RowMaximumVector[r];
                    PivotRowArray[r] = RowMaximumValueIndex;

                    for (i = 0; i < NumberOfEquations; i++)
                    {
                        Temp = aMatrixClone[r, i];
                        aMatrixClone[r, i] = aMatrixClone[RowMaximumValueIndex, i];
                        aMatrixClone[RowMaximumValueIndex, i] = Temp;
                    }

                    for (i = r + 1; i < NumberOfEquations; i++)
                    {
                        Temp = aMatrixClone[r, i];

                        for (var j = 0; j < r; j++) Temp = Temp - aMatrixClone[r, j] * aMatrixClone[j, i];
                        aMatrixClone[r, i] = Temp / aMatrixClone[r, r];
                    }
                }

                var ResidualVector = new Dictionary<int, double>();

                for (i = 0; i < NumberOfEquations; i++)
                {
                    xVector[i] = 0;
                    ResidualVector[i] = bVector[i];
                }

                var Iteration = 0;
                var NotConverged = true;

                do
                {

                    for (i = 0; i < NumberOfEquations; i++)
                    {
                        var PivotRowIndex = PivotRowArray[i];
                        var Temp = ResidualVector[PivotRowIndex];
                        ResidualVector[PivotRowIndex] = ResidualVector[i];

                        for (var j = 0; j < i; j++) Temp -= aMatrixClone[i, j] * ResidualVector[j];

                        ResidualVector[i] = Temp / aMatrixClone[i, i];
                    }

                    for (i = NumberOfEquations - 1; i >= 0; i--)
                    {
                        var Temp = ResidualVector[i];

                        for (var j = i + 1; j < NumberOfEquations; j++) Temp -= aMatrixClone[i, j] * ResidualVector[j];

                        ResidualVector[i] = Temp;
                    }

                    double NormOfX = 0;
                    double NormOfError = 0;

                    for (i = 0; i < NumberOfEquations; i++)
                    {
                        var Test = Math.Abs(xVector[i]);

                        if (Test > NormOfX) NormOfX = Test;

                        Test = Math.Abs(ResidualVector[i]);

                        if (Test > NormOfError) NormOfError = Test;
                    }

                    if (Iteration != 0)
                    {
                        if ((Iteration == 1) && (NormOfError / NormOfX > 0.5))
                            return Status.IllConditioned;

                        NotConverged = NormOfError / NormOfX >= Epsilon;
                    }

                    for (i = 0; i < NumberOfEquations; i++) xVector[i] = xVector[i] + ResidualVector[i];

                    for (i = 0; i < NumberOfEquations; i++)
                    {
                        var Temp = bVector[i];

                        for (var j = 0; j < NumberOfEquations; j++) Temp = Temp - aMatrix[i, j] * xVector[j];

                        ResidualVector[i] = Temp;
                    }

                    Iteration++;
                }
                while (NotConverged);

                return Status.Success;
            }
 /// <summary>
 /// Returns the upper triagonal matrix that was created during the LU decomposition.
 /// </summary>
 /// <remarks>
 /// This method is used for debugging purposes only and should normally not be used.
 /// </remarks>
 /// <returns>A new matrix containing the upper triagonal elements.</returns>
 internal Matrix <float> UpperTriangle()
 {
     return(_upper.Clone());
 }
 /// <summary>
 /// Returns the lower triagonal matrix that was created during the LU decomposition.
 /// </summary>
 /// <remarks>
 /// This method is used for debugging purposes only and should normally not be used.
 /// </remarks>
 /// <returns>A new matrix containing the lower triagonal elements.</returns>
 internal Matrix <float> LowerTriangle()
 {
     return(_lower.Clone());
 }
 /// <summary>
 /// Returns the upper triagonal matrix that was created during the LU decomposition.
 /// </summary>
 /// <remarks>
 /// This method is used for debugging purposes only and should normally not be used.
 /// </remarks>
 /// <returns>A new matrix containing the upper triagonal elements.</returns>
 internal Matrix UpperTriangle()
 {
     return((Matrix)_upper.Clone());
 }
 /// <summary>
 /// Returns the lower triagonal matrix that was created during the LU decomposition.
 /// </summary>
 /// <remarks>
 /// This method is used for debugging purposes only and should normally not be used.
 /// </remarks>
 /// <returns>A new matrix containing the lower triagonal elements.</returns>
 internal Matrix LowerTriangle()
 {
     return((Matrix)_lower.Clone());
 }
示例#7
0
 /// <summary>
 /// Returns the lower triagonal matrix that was created during the LU decomposition.
 /// </summary>
 /// <remarks>
 /// This method is used for debugging purposes only and should normally not be used.
 /// </remarks>
 /// <returns>A new matrix containing the lower triagonal elements.</returns>
 internal Matrix <double> LowerTriangle()
 {
     return(_lower.Clone());
 }
示例#8
0
 /// <summary>
 /// Returns the lower triagonal matrix that was created during the LU decomposition.
 /// </summary>
 /// <remarks>
 /// This method is used for debugging purposes only and should normally not be used.
 /// </remarks>
 /// <returns>A new matrix containing the lower triagonal elements.</returns>
 internal Matrix <Complex> LowerTriangle()
 {
     return(_lower.Clone());
 }
示例#9
0
 /// <summary>
 /// Returns the upper triagonal matrix that was created during the LU decomposition.
 /// </summary>
 /// <remarks>
 /// This method is used for debugging purposes only and should normally not be used.
 /// </remarks>
 /// <returns>A new matrix containing the upper triagonal elements.</returns>
 internal Matrix <double> UpperTriangle()
 {
     return(_upper.Clone());
 }
示例#10
0
        private static void TestRandomSymmetric(IDisposableSolver <Complex> solver, SparseMatrix matrix, string name)
        {
            Console.Write("Testing {0} (symmetric) ... ", name);

            RunTest(solver, (SparseMatrix)matrix.Clone(), true);
        }
示例#11
0
        protected virtual void TestRandomMulti(SparseMatrix matrix)
        {
            Console.Write("Testing {0} (multi) ... ", name);

            var A = (SparseMatrix)matrix.Clone();

            int count = 3;

            int n = A.RowCount;

            var b = Vector.Create(n, 0.0);
            var x = Vector.Create(n, 0.0);
            var s = Vector.Create(n, 0.0);

            var X = new DenseMatrix(n, count);
            var S = new DenseMatrix(n, count);
            var B = new DenseMatrix(n, count);

            for (int i = 0; i < count; i++)
            {
                x = Vector.Create(n, i + 1);

                X.SetColumn(i, x);
                S.SetColumn(i, x);

                A.Multiply(x, b);

                B.SetColumn(i, b);
            }

            X.Clear();

            try
            {
                timer.Restart();

                using (var solver = CreateSolver(A, false))
                {
                    //solver.Solve(B, X);
                }

                timer.Stop();

                Display.Time(timer.ElapsedTicks);

                double error = 0.0;

                for (int i = 0; i < count; i++)
                {
                    X.Column(i, x);
                    S.Column(i, s);

                    error += Helper.ComputeError(x, s);
                }

                if (error / count > ERROR_THRESHOLD)
                {
                    Display.Warning("relative error too large");
                }
                else
                {
                    Display.Ok("OK");
                }
            }
            catch (DllNotFoundException)
            {
                throw;
            }
            catch (Exception e)
            {
                Display.Error(e.Message);
            }
        }
示例#12
0
        protected virtual void TestRandomSymmetric(SparseMatrix matrix)
        {
            Console.Write("Testing {0} (symmetric) ... ", name);

            RunTest((SparseMatrix)matrix.Clone(), true);
        }
示例#13
0
        protected virtual void TestRandom(SparseMatrix matrix)
        {
            Console.Write("Testing {0} ... ", name);

            RunTest((SparseMatrix)matrix.Clone(), false);
        }