Example #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);
        }
Example #2
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));
 }