示例#1
0
        public void SolveGETest()
        {
            const int N = 50;
            var       a = new SparseDoubleMatrix(N, N);

            for (int i = 0; i < N; i++)
            {
                a[i, i] = 1;
            }
            // Apply random rotations around each pair of axes. This will keep det(A) ~ 1
            var rand = new Random();

            for (int i = 0; i < N; i++)
            {
                for (int j = i + 1; j < N; j++)
                {
                    double angle = rand.NextDouble() * 2 * Math.PI;
                    var    r     = new SparseDoubleMatrix(N, N);
                    for (int k = 0; k < N; k++)
                    {
                        r[k, k] = 1;
                    }
                    r[i, i] = r[j, j] = Math.Cos(angle);
                    r[i, j] = Math.Sin(angle);
                    r[j, i] = -Math.Sin(angle);
                    a       = a * r;
                }
            }

            var ainit = a.Clone();
            // Generate random vector
            var b = new DoubleVector(N);

            for (int i = 0; i < N; i++)
            {
                b[i] = rand.NextDouble();
            }

            var binit = b.Clone();
            // Solve system
            var solver = new GaussianEliminationSolver();
            var sw     = new Stopwatch();

            sw.Start();
            var x = solver.SolveDestructive(a, b.GetInternalData());

            sw.Stop();
            Trace.WriteLine("Gaussian elimination took: " + sw.ElapsedTicks);
            // Put solution into system
            var b2 = ainit * x;

            // Verify result is the same
            Assert.IsTrue(VectorMath.LInfinityNorm(binit, b2) < 1e-6);
        }
        public void SolverCoreTest2d()
        {
            var a = new double[][] {
                new double[] { 2, 1 },
                new double[] { -1, 1 }
            };
            var b = new double[] { 1, -2 };
            var x = new double[2];

            solver.SolveDestructive(new MatrixWrapperStructForLeftSpineJaggedArray <double>(a, 2, 2), b, x);
            var answer = new double[] { 1, -1 };

            Assert.IsTrue(VectorMath.LInfinityNorm(x, answer) < 1e-10);
        }
        public void SolverCoreTest3d()
        {
            var a = new double[][] {
                new double[] { 2, 1, -1 },
                new double[] { -3, -1, 2 },
                new double[] { -2, 1, 2 }
            };

            var b = new double[] { 8, -11, -3 };
            var x = new double[3];

            solver.SolveDestructive(new MatrixWrapperStructForLeftSpineJaggedArray <double>(a, 3, 3), b, x);
            var answer = new DoubleVector(3)
            {
                [0] = 2, [1] = 3, [2] = -1
            };

            Assert.IsTrue(VectorMath.LInfinityNorm(x, answer) < 1e-10);
        }
        public void SolverCoreTestMatrixNd()
        {
            const int N = 50;
            var       a = new DoubleMatrix(N, N);

            // Make matrix diagonal
            for (int i = 0; i < N; i++)
            {
                a[i, i] = 1;
            }
            // Apply random rotations around each pair of axes. This will keep det(A) ~ 1
            var rand = new Random();

            for (int i = 0; i < N; i++)
            {
                for (int j = i + 1; j < N; j++)
                {
                    double angle = rand.NextDouble() * 2 * Math.PI;
                    var    r     = new DoubleMatrix(N, N);
                    for (int k = 0; k < N; k++)
                    {
                        r[k, k] = 1;
                    }
                    r[i, i] = r[j, j] = Math.Cos(angle);
                    r[i, j] = Math.Sin(angle);
                    r[j, i] = -Math.Sin(angle);
                    a       = a * r;
                }
            }

            // Generate random vector
            var b = DoubleVector.Zeros(N);

            for (int i = 0; i < N; i++)
            {
                b[i] = rand.NextDouble();
            }

            // Solve system
            var sw = new Stopwatch();

            sw.Start();
            var x = solver.Solve(a, b, (len) => new DoubleVector(len));

            sw.Stop();
            Trace.WriteLine("Gaussian elimination took: " + sw.ElapsedTicks);

            // Solve system
            sw.Start();
            var x2 = solver.Solve(a, b, (l) => new DoubleVector(l));

            sw.Stop();
            Trace.WriteLine("Jama solve elimination took: " + sw.ElapsedTicks);

            Trace.WriteLine("Difference is " + VectorMath.LInfinityNorm(x, x2));

            // Put solution into system
            var b2 = a * x;

            // Verify result is the same
            Assert.IsTrue(VectorMath.LInfinityNorm(b, b2) < 1e-6);
        }