Ejemplo n.º 1
0
        public static double Dot(Vector v1, Vector v2)
        {
            int length = v1.Length;
            if (v2.Length != length)
                throw new ArgumentException("Vector size must match.");

            double output = 0;
            for (int i = 0; i < length; i++)
                output += v1[i] * v2[i];

            return output;
        }
Ejemplo n.º 2
0
        public static CGResult Solve(Matrix A, Vector F, double epsilon)
        {
            int M = F.Length;

            Vector r, u, p, s;
            double alpha, rho0, oldRho, newRho;

            u = new Vector(new double[M]);
            r = A * u - F;
            p = 1 * r;
            newRho = rho0 = Vector.Dot(r, r);

            int iterations = 0;
            for (int i = 0; i < M; i++)
            {
                s = A * p;
                alpha = newRho / Vector.Dot(s, p);
                u = u - alpha * p;
                r = r - alpha * s;
                oldRho = newRho;
                newRho = Vector.Dot(r, r);

                if (newRho < epsilon * rho0)
                {
                    iterations = i + 1;
                    break;
                }

                p = r + (newRho / oldRho) * p;
            }
            double error = (A * u - F).Norm;

            return new CGResult(u, iterations, error);
        }
Ejemplo n.º 3
0
 public CGResult(Vector vector, int iterations, double error)
 {
     this.vector = vector;
     this.iterations = iterations;
     this.error = error;
 }