예제 #1
0
        public void SolverCoreTestSparseMatrixNd()
        {
            const int N = 50;
            var       a = new SparseMatrix(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 SparseMatrix(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.Copy();
            // Generate random vector
            var b = Vector.Zeros(N);

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

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

            sw.Start();
            var x = new Vector(Gauss.Solve(a, b));

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

            // Verify result is the same
            Assert.IsTrue(Vector.GetLInfinityNorm(binit, b2) < 1e-6);
        }
예제 #2
0
        public void SolverCoreTest2d()
        {
            var a = new double[][] {
                new double[] { 2, 1 },
                new double[] { -1, 1 }
            };
            var b      = new Vector(1, -2);
            var x      = Gauss.SolveCore(a, b);
            var answer = new Vector(1, -1);

            Assert.IsTrue(Vector.GetLInfinityNorm(x, answer) < 1e-10);
        }
예제 #3
0
        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 Vector(8, -11, -3);
            var x      = Gauss.SolveCore(a, b);
            var answer = new Vector(2, 3, -1);

            Assert.IsTrue(Vector.GetLInfinityNorm(x, answer) < 1e-10);
        }
예제 #4
0
 /// <summary>Solve A*x = b using Gaussian elimination with partial pivoting</summary>
 /// <param name="b">    right hand side Vector</param>
 /// <returns>    The solution x = A^(-1) * b as a Vector</returns>
 public Vector SolveGE(Vector b)
 {
     return(Gauss.Solve(this, b));
 }
예제 #5
0
 /// <summary>Gaussian elimination method for A*x=b with partial pivoting</summary>
 /// <param name="b">The right hand side vector</param>
 /// <returns>The solution x</returns>
 public Vector SolveGE(Vector b)
 {
     return(Gauss.SolveCore(Copy(), b));
 }