Пример #1
0
        public bool InitMethod(ILinearOperator A, IVector x0, IVector b, bool malloc = false, IFactorization Factorizer = null)
        {
            if (malloc)
            {
                x = new Vector(x0.Size);
            }
            else
            {
                x = x0;
            }

            this.x0     = x0;
            this.b      = b;
            this.A      = A;
            currentIter = 0;
            norm_b      = b.Norm;

            lastResidual = A.Multiply(x0).Add(b, -1).Norm / norm_b;
            if (Double.IsNaN(lastResidual) || Double.IsInfinity(lastResidual))
            {
                return(false);
            }
            init            = true;
            x_temp          = new Vector(x.Size);
            inverseDioganal = A.Diagonal.Clone();
            for (int i = 0; i < inverseDioganal.Size; i++)
            {
                inverseDioganal[i] = 1.0 / inverseDioganal[i];
            }
            L_Ux = A.LMult(x0, false, 0).Add(A.UMult(x0, false, 0));
            return(true);
        }
Пример #2
0
        public IVector InitMethod(ILinearOperator A, IVector x0, IVector b, bool malloc = false)
        {
            if (malloc)
            {
                x = new Vector(x0.Size);
            }
            else
            {
                x = x0;
            }

            this.x0     = x0;
            this.b      = b;
            this.A      = A;
            currentIter = 0;
            norm_b      = b.Norm;

            try
            {
                lastResidual = A.Multiply(x0).Add(b, -1).Norm / norm_b;
            }
            catch (DivideByZeroException e)
            {
                return(null);
            }
            init            = true;
            x_temp          = new Vector(x.Size);
            inverseDioganal = A.Diagonal.Clone();
            for (int i = 0; i < inverseDioganal.Size; i++)
            {
                inverseDioganal[i] = 1.0 / inverseDioganal[i];
            }
            L_Ux = A.LMult(x0, false, 0).Add(A.UMult(x0, false, 0));
            return(x);
        }
Пример #3
0
        public void MakeStep(out int iter, out double residual)
        {
            if (!init)
            {
                throw new InvalidOperationException("Решатель не инициализирован, выполнение операции невозможно");
            }

            currentIter++;

            //x_k=(L+D)^(-1)*(b-Ux)
            var x_k = A.LSolve(b.Add(Ux, -1), true);

            double w = 1.0;

            double tempResidual = Double.MaxValue;

            while (w >= 0.1)
            {
                ////????????????????
                for (int i = 0; i < x.Size; i++)
                {
                    x_temp[i] = w * x_k[i] + (1 - w) * x[i];
                }
                ////????????????????

                Ux = Ux = A.UMult(x_temp, false, 0);

                //tempResidual = ||b - (Ux+Lx+Dx)|| / ||b||
                tempResidual = Ux.Add(A.LMult(x_temp, false, 0)).Add(A.Diagonal.HadamardProduct(x_temp)).Add(b, -1).Norm / norm_b;

                w -= 0.1;
                if (tempResidual < lastResidual)
                {
                    break;
                }
            }
            lastResidual = tempResidual;
            ////????????????????
            for (int i = 0; i < x.Size; i++)
            {
                x[i] = x_temp[i];
            }
            ////???????????????

            residual = lastResidual;
            iter     = currentIter;
        }
Пример #4
0
        public void MakeStep(out int iter, out double residual)
        {
            if (!init)
            {
                throw new InvalidOperationException("Решатель не инициализирован, выполнение операции невозможно");
            }

            currentIter++;

            //x_k = D^(-1)*(b-(L+U)x)
            var x_k = inverseDioganal.HadamardProduct(b.Add(L_Ux, -1));

            double w = 1.0;

            while (w >= 0.1)
            {
                for (int i = 0; i < x.Size; i++)
                {
                    x_temp[i] = w * x_k[i] + (1 - w) * x[i];
                }

                L_Ux = A.LMult(x_temp, false, 0).Add(A.UMult(x_temp, false, 0));
                //tempResidual = ||b - (Ux+Lx+Dx)|| / ||b||
                double tempResidual = A.Diagonal.HadamardProduct(x_temp).Add(L_Ux).Add(b, -1).Norm / norm_b;

                if (tempResidual < lastResidual)
                {
                    lastResidual = tempResidual;
                    break;
                }
                w -= 0.1;
            }

            for (int i = 0; i < x.Size; i++)
            {
                x[i] = x_temp[i];
            }

            residual = lastResidual;
            iter     = currentIter;
        }
Пример #5
0
 public IVector LMult(IVector x, bool UseDiagonal, DiagonalElement diagonalElement = DiagonalElement.One)
 {
     Counters.LMult.Inc();
     return(linear.LMult(x, UseDiagonal, diagonalElement));
 }