public static void Inverse(MatrixR A, double s, double tolerance, out VectorR x, out double lambda)
        {
            int n = A.GetCols();

            x      = new VectorR(n);
            lambda = 0.0;
            double  delta    = 0.0;
            MatrixR identity = new MatrixR(n, n);

            A = A - s * (identity.Identity());
            LinearSystem ls = new LinearSystem();

            A = ls.LUInverse(A);

            Random random = new Random();

            for (int i = 0; i < n; i++)
            {
                x[i] = random.NextDouble();
            }
            do
            {
                VectorR temp = x;
                x = MatrixR.Transform(A, x);
                x.Normalize();
                if (VectorR.DotProduct(temp, x) < 0)
                {
                    x = -x;
                }
                VectorR dx = temp - x;
                delta = dx.GetNorm();
            }while (delta > tolerance);
            lambda = s + 1.0 / (VectorR.DotProduct(x, MatrixR.Transform(A, x)));
        }
        public static void RayleighQuotient(MatrixR A, double tolerance, int flag, out VectorR x, out double lambda)
        {
            int    n      = A.GetCols();
            double delta  = 0.0;
            Random random = new Random();

            x = new VectorR(n);
            if (flag != 2)
            {
                for (int i = 0; i < n; i++)
                {
                    x[i] = random.NextDouble();
                }
                x.Normalize();
                lambda = VectorR.DotProduct(x, MatrixR.Transform(A, x));
            }
            else
            {
                lambda = 0.0;
                Rayleigh(A, 1e-2, out x, out lambda);
            }

            double       temp     = lambda;
            MatrixR      identity = new MatrixR(n, n);
            LinearSystem ls       = new LinearSystem();

            do
            {
                temp = lambda;
                double d = ls.LUCrout(A - lambda * identity.Identity(), x);
                x.Normalize();
                lambda = VectorR.DotProduct(x, MatrixR.Transform(A, x));
                delta  = Math.Abs((temp - lambda) / lambda);
            }while (delta > tolerance);
        }
Exemple #3
0
        public static VectorR NewtonMultiEquations(MFunction f, VectorR x0, double tolerance)
        {
            LinearSystem ls = new LinearSystem();
            VectorR      dx = new VectorR(x0.GetSize());

            do
            {
                MatrixR A = Jacobian(f, x0);
                if (Math.Sqrt(VectorR.DotProduct(f(x0), f(x0)) / x0.GetSize()) < tolerance)
                {
                    return(x0);
                }
                dx = ls.GaussJordan(A, -f(x0));
                x0 = x0 + dx;
            }while (Math.Sqrt(VectorR.DotProduct(dx, dx)) > tolerance);
            return(x0);
        }