public void Create()
        {
            var criterium = new ResidualStopCriterium<float>(1e-6f, 50);

            Assert.AreEqual(1e-6f, criterium.Maximum, "Incorrect maximum");
            Assert.AreEqual(50, criterium.MinimumIterationsBelowMaximum, "Incorrect iteration count");
        }
        public void ResetMaximum()
        {
            var criterium = new ResidualStopCriterium(1e-8, 50);
            Assert.IsNotNull(criterium, "There should be a criterium");

            criterium.ResetMaximumResidualToDefault();
            Assert.AreEqual(ResidualStopCriterium.DefaultMaximumResidual, criterium.Maximum, "Incorrect maximum");
        }
        public void Create()
        {
            var criterium = new ResidualStopCriterium(1e-8, 50);
            Assert.IsNotNull(criterium, "There should be a criterium");

            Assert.AreEqual(1e-8, criterium.Maximum, "Incorrect maximum");
            Assert.AreEqual(50, criterium.MinimumIterationsBelowMaximum, "Incorrect iteration count");
        }
        public void DetermineStatusWithNonMatchingResidualVectorThrowsArgumentException()
        {
            var criterium = new ResidualStopCriterium <float>(1e-6f, 50);

            Assert.That(() => criterium.DetermineStatus(
                            1,
                            Vector <float> .Build.Dense(3, 4),
                            Vector <float> .Build.Dense(3, 4),
                            Vector <float> .Build.Dense(4, 4)), Throws.ArgumentException);
        }
Esempio n. 5
0
        public void DetermineStatusWithNonMatchingSolutionVectorThrowsArgumentException()
        {
            var criterium = new ResidualStopCriterium <Complex32>(1e-6f, 50);

            Assert.Throws <ArgumentException>(() => criterium.DetermineStatus(
                                                  1,
                                                  Vector <Complex32> .Build.Dense(4, 4),
                                                  Vector <Complex32> .Build.Dense(3, 4),
                                                  Vector <Complex32> .Build.Dense(3, 4)));
        }
        public void DetermineStatusWithIllegalIterationNumberThrowsArgumentOutOfRangeException()
        {
            var criterium = new ResidualStopCriterium<Complex32>(1e-6f, 50);

            Assert.Throws<ArgumentOutOfRangeException>(() => criterium.DetermineStatus(
                -1,
                DenseVector.Create(3, i => 4),
                DenseVector.Create(3, i => 5),
                DenseVector.Create(3, i => 6)));
        }
Esempio n. 7
0
        public void DetermineStatusWithIllegalIterationNumberThrowsArgumentOutOfRangeException()
        {
            var criterium = new ResidualStopCriterium <Complex>(1e-8, 50);

            Assert.That(() => criterium.DetermineStatus(
                            -1,
                            Vector <Complex> .Build.Dense(3, 4),
                            Vector <Complex> .Build.Dense(3, 5),
                            Vector <Complex> .Build.Dense(3, 6)), Throws.TypeOf <ArgumentOutOfRangeException>());
        }
Esempio n. 8
0
        public void DetermineStatusWithNonMatchingSolutionVectorThrowsArgumentException()
        {
            var criterium = new ResidualStopCriterium <float>(1e-6f, 50);

            Assert.Throws <ArgumentException>(() => criterium.DetermineStatus(
                                                  1,
                                                  DenseVector.Create(4, i => 4),
                                                  DenseVector.Create(3, i => 4),
                                                  DenseVector.Create(3, i => 4)));
        }
        public void DetermineStatusWithIllegalIterationNumberThrowsArgumentOutOfRangeException()
        {
            var criterium = new ResidualStopCriterium <Complex32>(1e-6f, 50);

            Assert.Throws <ArgumentOutOfRangeException>(() => criterium.DetermineStatus(
                                                            -1,
                                                            DenseVector.Create(3, i => 4),
                                                            DenseVector.Create(3, i => 5),
                                                            DenseVector.Create(3, i => 6)));
        }
Esempio n. 10
0
        public void DetermineStatusWithIllegalIterationNumberThrowsArgumentOutOfRangeException()
        {
            var criterium = new ResidualStopCriterium <float>(1e-6f, 50);

            Assert.Throws <ArgumentOutOfRangeException>(() => criterium.DetermineStatus(
                                                            -1,
                                                            Vector <float> .Build.Dense(3, 4),
                                                            Vector <float> .Build.Dense(3, 5),
                                                            Vector <float> .Build.Dense(3, 6)));
        }
Esempio n. 11
0
        public void DetermineStatusWithNonMatchingResidualVectorThrowsArgumentException()
        {
            var criterium = new ResidualStopCriterium <double>(1e-8, 50);

            Assert.Throws <ArgumentException>(() => criterium.DetermineStatus(
                                                  1,
                                                  Vector <double> .Build.Dense(3, 4),
                                                  Vector <double> .Build.Dense(3, 4),
                                                  Vector <double> .Build.Dense(4, 4)));
        }
        public void DetermineStatusWithNonMatchingResidualVector()
        {
            var criterium = new ResidualStopCriterium(1e-8, 50);

            Assert.IsNotNull(criterium, "There should be a criterium");

            criterium.DetermineStatus(1,
                                      new DenseVector(3, 4),
                                      new DenseVector(3, 4),
                                      new DenseVector(4, 4));
        }
        public void DetermineStatusWithResidualNaN()
        {
            var criterium = new ResidualStopCriterium <double>(1e-3, 10);
            var solution  = new DenseVector(new[] { 1.0, 1.0, 2.0 });
            var source    = new DenseVector(new[] { 1.0, 1.0, 2.0 });
            var residual  = new DenseVector(new[] { 1000.0, double.NaN, 2001.0 });

            var status = criterium.DetermineStatus(5, solution, source, residual);

            Assert.AreEqual(IterationStatus.Diverged, status, "Should be diverged");
        }
        public void DetermineStatusWithConvergenceAtFirstIteration()
        {
            var criterium = new ResidualStopCriterium <double>(1e-12);
            var solution  = new DenseVector(new[] { 1.0, 1.0, 1.0 });
            var source    = new DenseVector(new[] { 1.0, 1.0, 1.0 });
            var residual  = new DenseVector(new[] { 0.0, 0.0, 0.0 });

            var status = criterium.DetermineStatus(0, solution, source, residual);

            Assert.AreEqual(IterationStatus.Converged, status, "Should be done");
        }
        public void DetermineStatusWithSourceNaN()
        {
            var criterium = new ResidualStopCriterium <float>(1e-3f, 10);

            var solution = new DenseVector(new[] { 1.0f, 1.0f, 2.0f });
            var source   = new DenseVector(new[] { 1.0f, 1.0f, float.NaN });
            var residual = new DenseVector(new[] { 1000.0f, 1000.0f, 2001.0f });

            var status = criterium.DetermineStatus(5, solution, source, residual);

            Assert.AreEqual(IterationStatus.Diverged, status, "Should be diverged");
        }
Esempio n. 16
0
        public void DetermineStatusWithConvergenceAtFirstIteration()
        {
            var criterium = new ResidualStopCriterium <Complex32>(1e-6);

            var solution = new DenseVector(new[] { Complex32.One, Complex32.One, Complex32.One });
            var source   = new DenseVector(new[] { Complex32.One, Complex32.One, Complex32.One });
            var residual = new DenseVector(new[] { Complex32.Zero, Complex32.Zero, Complex32.Zero });

            var status = criterium.DetermineStatus(0, solution, source, residual);

            Assert.AreEqual(IterationStatus.Converged, status, "Should be done");
        }
        public void DetermineStatusWithNullResidualVectorThrowsArgumentNullException()
        {
            var criterium = new ResidualStopCriterium(1e-8, 50);

            Assert.IsNotNull(criterium, "There should be a criterium");

            Assert.Throws <ArgumentNullException>(() => criterium.DetermineStatus(
                                                      1,
                                                      new DenseVector(3, 4),
                                                      new DenseVector(3, 5),
                                                      null));
        }
        public void DetermineStatusWithIllegalIterationNumberThrowsArgumentOutOfRangeException()
        {
            var criterium = new ResidualStopCriterium(1e-8, 50);

            Assert.IsNotNull(criterium, "There should be a criterium");

            Assert.Throws <ArgumentOutOfRangeException>(() => criterium.DetermineStatus(
                                                            -1,
                                                            DenseVector.Create(3, i => 4),
                                                            DenseVector.Create(3, i => 5),
                                                            DenseVector.Create(3, i => 6)));
        }
        public void DetermineStatusWithNonMatchingResidualVectorThrowsArgumentException()
        {
            var criterium = new ResidualStopCriterium(1e-8, 50);

            Assert.IsNotNull(criterium, "There should be a criterium");

            Assert.Throws <ArgumentException>(() => criterium.DetermineStatus(
                                                  1,
                                                  DenseVector.Create(3, i => 4),
                                                  DenseVector.Create(3, i => 4),
                                                  DenseVector.Create(4, i => 4)));
        }
        public void DetermineStatusWithNonMatchingSourceVectorThrowsArgumentException()
        {
            var criterium = new ResidualStopCriterium(1e-6f, 50);

            Assert.IsNotNull(criterium, "There should be a criterium");

            Assert.Throws <ArgumentException>(() => criterium.DetermineStatus(
                                                  1,
                                                  new DenseVector(3, 4),
                                                  new DenseVector(4, 4),
                                                  new DenseVector(3, 4)));
        }
Esempio n. 21
0
        public void DetermineStatusWithResidualNaN()
        {
            var criterium = new ResidualStopCriterium <Complex32>(1e-3f, 10);

            var solution = new DenseVector(new[] { new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(2.0f, 1) });
            var source   = new DenseVector(new[] { new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(2.0f, 1) });
            var residual = new DenseVector(new[] { new Complex32(1000.0f, 1), new Complex32(float.NaN, 1), new Complex32(2001.0f, 1) });

            var status = criterium.DetermineStatus(5, solution, source, residual);

            Assert.AreEqual(IterationStatus.Diverged, status, "Should be diverged");
        }
        public void DetermineStatusWithIllegalIterationNumber()
        {
            var criterium = new ResidualStopCriterium(1e-8, 50);

            Assert.IsNotNull(criterium, "There should be a criterium");

            criterium.DetermineStatus(-1,
                                      new DenseVector(3, 4),
                                      new DenseVector(3, 5),
                                      new DenseVector(3, 6));
            Assert.Fail();
        }
        public void DetermineStatusWithNullSourceVector()
        {
            var criterium = new ResidualStopCriterium(1e-8, 50);

            Assert.IsNotNull(criterium, "There should be a criterium");

            criterium.DetermineStatus(1,
                                      new DenseVector(3, 4),
                                      null,
                                      new DenseVector(3, 6));
            Assert.Fail();
        }
Esempio n. 24
0
        public void DetermineStatusWithNullSolutionVectorThrowsArgumentNullException()
        {
            var criterium = new ResidualStopCriterium(1e-8, 50);

            Assert.IsNotNull(criterium, "There should be a criterium");

            Assert.Throws <ArgumentNullException>(() => criterium.DetermineStatus(
                                                      1,
                                                      null,
                                                      DenseVector.Create(3, i => 5),
                                                      DenseVector.Create(3, i => 6)));
        }
Esempio n. 25
0
        public void DetermineStatusWithResidualNaN()
        {
            var criterium = new ResidualStopCriterium(1e-3, 10);

            Assert.IsNotNull(criterium, "There should be a criterium");

            var solution = new DenseVector(new[] { new Complex(1.0, 1), new Complex(1.0, 1), new Complex(2.0, 1) });
            var source   = new DenseVector(new[] { new Complex(1.0, 1), new Complex(1.0, 1), new Complex(2.0, 1) });
            var residual = new DenseVector(new[] { new Complex(1000.0, 1), new Complex(double.NaN, 1), new Complex(2001.0, 1) });

            criterium.DetermineStatus(5, solution, source, residual);
            Assert.IsInstanceOf(typeof(CalculationDiverged), criterium.Status, "Should be diverged");
        }
        public void Clone()
        {
            var criterium = new ResidualStopCriterium<Complex32>(1e-3f, 10);

            var clone = criterium.Clone();
            Assert.IsInstanceOf(typeof(ResidualStopCriterium<Complex32>), clone, "Wrong criterium type");

            var clonedCriterium = clone as ResidualStopCriterium<Complex32>;
            Assert.IsNotNull(clonedCriterium);

            Assert.AreEqual(criterium.Maximum, clonedCriterium.Maximum, "Clone failed");
            Assert.AreEqual(criterium.MinimumIterationsBelowMaximum, clonedCriterium.MinimumIterationsBelowMaximum, "Clone failed");
        }
Esempio n. 27
0
        public void DetermineStatusWithSourceNaN()
        {
            var criterium = new ResidualStopCriterium(1e-3f, 10);

            Assert.IsNotNull(criterium, "There should be a criterium");

            var solution = new DenseVector(new[] { 1.0f, 1.0f, 2.0f });
            var source   = new DenseVector(new[] { 1.0f, 1.0f, float.NaN });
            var residual = new DenseVector(new[] { 1000.0f, 1000.0f, 2001.0f });

            criterium.DetermineStatus(5, solution, source, residual);
            Assert.IsInstanceOf(typeof(CalculationDiverged), criterium.Status, "Should be diverged");
        }
Esempio n. 28
0
        public void DetermineStatusWithConvergenceAtFirstIteration()
        {
            var criterium = new ResidualStopCriterium();

            Assert.IsNotNull(criterium, "There should be a criterium");

            var solution = new DenseVector(new[] { Complex.One, Complex.One, Complex.One });
            var source   = new DenseVector(new[] { Complex.One, Complex.One, Complex.One });
            var residual = new DenseVector(new[] { Complex.Zero, Complex.Zero, Complex.Zero });

            criterium.DetermineStatus(0, solution, source, residual);
            Assert.IsInstanceOf(typeof(CalculationConverged), criterium.Status, "Should be done");
        }
Esempio n. 29
0
        public void DetermineStatusWithConvergenceAtFirstIteration()
        {
            var criterium = new ResidualStopCriterium();

            Assert.IsNotNull(criterium, "There should be a criterium");

            var solution = new DenseVector(new[] { 1.0f, 1.0f, 1.0f });
            var source   = new DenseVector(new[] { 1.0f, 1.0f, 1.0f });
            var residual = new DenseVector(new[] { 0.0f, 0.0f, 0.0f });

            var status = criterium.DetermineStatus(0, solution, source, residual);

            Assert.AreEqual(IterationStatus.Converged, status, "Should be done");
        }
        public void DetermineStatusWithResidualNaN()
        {
            var criterium = new ResidualStopCriterium(1e-3, 10);

            Assert.IsNotNull(criterium, "There should be a criterium");

            var solution = new DenseVector(new[] { new Complex(1.0, 1), new Complex(1.0, 1), new Complex(2.0, 1) });
            var source   = new DenseVector(new[] { new Complex(1.0, 1), new Complex(1.0, 1), new Complex(2.0, 1) });
            var residual = new DenseVector(new[] { new Complex(1000.0, 1), new Complex(double.NaN, 1), new Complex(2001.0, 1) });

            var status = criterium.DetermineStatus(5, solution, source, residual);

            Assert.AreEqual(IterationStatus.Diverged, status, "Should be diverged");
        }
        public void Clone()
        {
            var criterium = new ResidualStopCriterium(1e-3, 10);
            Assert.IsNotNull(criterium, "There should be a criterium");

            var clone = criterium.Clone();
            Assert.IsInstanceOfType(typeof(ResidualStopCriterium), clone, "Wrong criterium type");

            var clonedCriterium = clone as ResidualStopCriterium;
            Assert.IsNotNull(clonedCriterium);
            // ReSharper disable PossibleNullReferenceException
            Assert.AreEqual(criterium.Maximum, clonedCriterium.Maximum, "Clone failed");
            Assert.AreEqual(criterium.MinimumIterationsBelowMaximum, clonedCriterium.MinimumIterationsBelowMaximum, "Clone failed");
            // ReSharper restore PossibleNullReferenceException
        }
        public void ResetCalculationState()
        {
            var criterium = new ResidualStopCriterium <double>(1e-3, 10);

            var solution = new DenseVector(new[] { 0.001, 0.001, 0.002 });
            var source   = new DenseVector(new[] { 0.001, 0.001, 0.002 });
            var residual = new DenseVector(new[] { 1.000, 1.000, 2.001 });

            var status = criterium.DetermineStatus(5, solution, source, residual);

            Assert.AreEqual(IterationStatus.Continue, status, "Should be running");

            criterium.Reset();
            Assert.AreEqual(IterationStatus.Continue, criterium.Status, "Should not have started");
        }
Esempio n. 33
0
        public void ResetCalculationState()
        {
            var criterium = new ResidualStopCriterium <Complex32>(1e-3f, 10);

            var solution = new DenseVector(new[] { new Complex32(0.001f, 1), new Complex32(0.001f, 1), new Complex32(0.002f, 1) });
            var source   = new DenseVector(new[] { new Complex32(0.001f, 1), new Complex32(0.001f, 1), new Complex32(0.002f, 1) });
            var residual = new DenseVector(new[] { new Complex32(1.000f, 0), new Complex32(1.000f, 0), new Complex32(2.001f, 0) });

            var status = criterium.DetermineStatus(5, solution, source, residual);

            Assert.AreEqual(IterationStatus.Continue, status, "Should be running");

            criterium.Reset();
            Assert.AreEqual(IterationStatus.Continue, criterium.Status, "Should not have started");
        }
Esempio n. 34
0
        public void Clone()
        {
            var criterium = new ResidualStopCriterium <Complex32>(1e-3f, 10);

            var clone = criterium.Clone();

            Assert.IsInstanceOf(typeof(ResidualStopCriterium <Complex32>), clone, "Wrong criterium type");

            var clonedCriterium = clone as ResidualStopCriterium <Complex32>;

            Assert.IsNotNull(clonedCriterium);

            Assert.AreEqual(criterium.Maximum, clonedCriterium.Maximum, "Clone failed");
            Assert.AreEqual(criterium.MinimumIterationsBelowMaximum, clonedCriterium.MinimumIterationsBelowMaximum, "Clone failed");
        }
Esempio n. 35
0
        public void ResetCalculationState()
        {
            var criterium = new ResidualStopCriterium(1e-3, 10);

            Assert.IsNotNull(criterium, "There should be a criterium");

            var solution = new DenseVector(new[] { new Complex(0.001, 1), new Complex(0.001, 1), new Complex(0.002, 1) });
            var source   = new DenseVector(new[] { new Complex(0.001, 1), new Complex(0.001, 1), new Complex(0.002, 1) });
            var residual = new DenseVector(new[] { new Complex(1.000, 0), new Complex(1.000, 0), new Complex(2.001, 0) });

            criterium.DetermineStatus(5, solution, source, residual);
            Assert.IsInstanceOf(typeof(CalculationRunning), criterium.Status, "Should be running");

            criterium.ResetToPrecalculationState();
            Assert.IsInstanceOf(typeof(CalculationIndetermined), criterium.Status, "Should not have started");
        }
Esempio n. 36
0
        public void ResetCalculationState()
        {
            var criterium = new ResidualStopCriterium(1e-3f, 10);

            Assert.IsNotNull(criterium, "There should be a criterium");

            var solution = new DenseVector(new[] { 0.001f, 0.001f, 0.002f });
            var source   = new DenseVector(new[] { 0.001f, 0.001f, 0.002f });
            var residual = new DenseVector(new[] { 1.000f, 1.000f, 2.001f });

            var status = criterium.DetermineStatus(5, solution, source, residual);

            Assert.AreEqual(IterationStatus.Running, status, "Should be running");

            criterium.ResetToPrecalculationState();
            Assert.AreEqual(IterationStatus.Indetermined, criterium.Status, "Should not have started");
        }
        public void DetermineStatus()
        {
            var criterium = new ResidualStopCriterium(1e-3, 10);
            Assert.IsNotNull(criterium, "There should be a criterium");

            // Note that the solution vector isn't actually being used so ...
            var solution = new DenseVector(new[] { double.NaN, double.NaN, double.NaN });

            // Set the source values
            var source = new DenseVector(new[] { 1.000, 1.000, 2.001 });

            // Set the residual values
            var residual = new DenseVector(new[] { 0.001, 0.001, 0.002 });

            criterium.DetermineStatus(5, solution, source, residual);
            Assert.IsInstanceOfType(typeof(CalculationRunning), criterium.Status, "Should still be running");

            criterium.DetermineStatus(16, solution, source, residual);
            Assert.IsInstanceOfType(typeof(CalculationConverged), criterium.Status, "Should be done");
        }
        public void DetermineStatusWithIllegalIterationNumberThrowsArgumentOutOfRangeException()
        {
            var criterium = new ResidualStopCriterium(1e-8, 50);
            Assert.IsNotNull(criterium, "There should be a criterium");

            Assert.Throws<ArgumentOutOfRangeException>(() => criterium.DetermineStatus(
                -1,
                new DenseVector(3, 4),
                new DenseVector(3, 5),
                new DenseVector(3, 6)));
        }
        public void DetermineStatusWithConvergenceAtFirstIteration()
        {
            var criterium = new ResidualStopCriterium();
            Assert.IsNotNull(criterium, "There should be a criterium");

            var solution = new DenseVector(new[] { 1.0, 1.0, 1.0 });
            var source = new DenseVector(new[] { 1.0, 1.0, 1.0 });
            var residual = new DenseVector(new[] { 0.0, 0.0, 0.0 });

            criterium.DetermineStatus(0, solution, source, residual);
            Assert.IsInstanceOf(typeof(CalculationConverged), criterium.Status, "Should be done");
        }
        public void DetermineStatusWithSourceNaN()
        {
            var criterium = new ResidualStopCriterium<float>(1e-3f, 10);

            var solution = new DenseVector(new[] {1.0f, 1.0f, 2.0f});
            var source = new DenseVector(new[] {1.0f, 1.0f, float.NaN});
            var residual = new DenseVector(new[] {1000.0f, 1000.0f, 2001.0f});

            var status = criterium.DetermineStatus(5, solution, source, residual);
            Assert.AreEqual(IterationStatus.Diverged, status, "Should be diverged");
        }
        public void ResetCalculationState()
        {
            var criterium = new ResidualStopCriterium<float>(1e-3f, 10);

            var solution = new DenseVector(new[] {0.001f, 0.001f, 0.002f});
            var source = new DenseVector(new[] {0.001f, 0.001f, 0.002f});
            var residual = new DenseVector(new[] {1.000f, 1.000f, 2.001f});

            var status = criterium.DetermineStatus(5, solution, source, residual);
            Assert.AreEqual(IterationStatus.Continue, status, "Should be running");

            criterium.Reset();
            Assert.AreEqual(IterationStatus.Continue, criterium.Status, "Should not have started");
        }
        /// <summary>
        /// Run example
        /// </summary>
        public void Run()
        {
            // Format matrix output to console
            var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
            formatProvider.TextInfo.ListSeparator = " ";

            // Solve next system of linear equations (Ax=b):
            // 5*x + 2*y - 4*z = -7
            // 3*x - 7*y + 6*z = 38
            // 4*x + 1*y + 5*z = 43

            // Create matrix "A" with coefficients
            var matrixA = DenseMatrix.OfArray(new[,] { { 5.00, 2.00, -4.00 }, { 3.00, -7.00, 6.00 }, { 4.00, 1.00, 5.00 } });
            Console.WriteLine(@"Matrix 'A' with coefficients");
            Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // Create vector "b" with the constant terms.
            var vectorB = new DenseVector(new[] { -7.0, 38.0, 43.0 });
            Console.WriteLine(@"Vector 'b' with the constant terms");
            Console.WriteLine(vectorB.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // Create stop criteriums to monitor an iterative calculation. There are next available stop criteriums:
            // - DivergenceStopCriterium: monitors an iterative calculation for signs of divergence;
            // - FailureStopCriterium: monitors residuals for NaN's;
            // - IterationCountStopCriterium: monitors the numbers of iteration steps;
            // - ResidualStopCriterium: monitors residuals if calculation is considered converged;

            // Stop calculation if 1000 iterations reached during calculation
            var iterationCountStopCriterium = new IterationCountStopCriterium<double>(1000);

            // Stop calculation if residuals are below 1E-10 --> the calculation is considered converged
            var residualStopCriterium = new ResidualStopCriterium<double>(1e-10);

            // Create monitor with defined stop criteriums
            var monitor = new Iterator<double>(iterationCountStopCriterium, residualStopCriterium);

            // Create Transpose Free Quasi-Minimal Residual solver
            var solver = new TFQMR();

            // 1. Solve the matrix equation
            var resultX = matrixA.SolveIterative(vectorB, solver, monitor);
            Console.WriteLine(@"1. Solve the matrix equation");
            Console.WriteLine();

            // 2. Check solver status of the iterations.
            // Solver has property IterationResult which contains the status of the iteration once the calculation is finished.
            // Possible values are:
            // - CalculationCancelled: calculation was cancelled by the user;
            // - CalculationConverged: calculation has converged to the desired convergence levels;
            // - CalculationDiverged: calculation diverged;
            // - CalculationFailure: calculation has failed for some reason;
            // - CalculationIndetermined: calculation is indetermined, not started or stopped;
            // - CalculationRunning: calculation is running and no results are yet known;
            // - CalculationStoppedWithoutConvergence: calculation has been stopped due to reaching the stopping limits, but that convergence was not achieved;
            Console.WriteLine(@"2. Solver status of the iterations");
            Console.WriteLine(monitor.Status);
            Console.WriteLine();

            // 3. Solution result vector of the matrix equation
            Console.WriteLine(@"3. Solution result vector of the matrix equation");
            Console.WriteLine(resultX.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // 4. Verify result. Multiply coefficient matrix "A" by result vector "x"
            var reconstructVecorB = matrixA * resultX;
            Console.WriteLine(@"4. Multiply coefficient matrix 'A' by result vector 'x'");
            Console.WriteLine(reconstructVecorB.ToString("#0.00\t", formatProvider));
            Console.WriteLine();
        }
        public void DetermineStatusWithNonMatchingSourceVectorThrowsArgumentException()
        {
            var criterium = new ResidualStopCriterium<float>(1e-6f, 50);

            Assert.Throws<ArgumentException>(() => criterium.DetermineStatus(
                1,
                Vector<float>.Build.Dense(3, 4),
                Vector<float>.Build.Dense(4, 4),
                Vector<float>.Build.Dense(3, 4)));
        }
        public void ResetCalculationState()
        {
            var criterium = new ResidualStopCriterium(1e-3, 10);
            Assert.IsNotNull(criterium, "There should be a criterium");

            var solution = new DenseVector(new[] { 0.001, 0.001, 0.002 });
            var source = new DenseVector(new[] { 0.001, 0.001, 0.002 });
            var residual = new DenseVector(new[] { 1.000, 1.000, 2.001 });

            criterium.DetermineStatus(5, solution, source, residual);
            Assert.IsInstanceOf(typeof(CalculationRunning), criterium.Status, "Should be running");

            criterium.ResetToPrecalculationState();
            Assert.IsInstanceOf(typeof(CalculationIndetermined), criterium.Status, "Should not have started");
        }
        public void DetermineStatusWithNullSourceVector()
        {
            var criterium = new ResidualStopCriterium(1e-8, 50);
            Assert.IsNotNull(criterium, "There should be a criterium");

            criterium.DetermineStatus(1,
                                      new DenseVector(3, 4),
                                      null,
                                      new DenseVector(3, 6));
            Assert.Fail();
        }
        public void DetermineStatusWithNonMatchingResidualVectorThrowsArgumentException()
        {
            var criterium = new ResidualStopCriterium(1e-8, 50);
            Assert.IsNotNull(criterium, "There should be a criterium");

            Assert.Throws<ArgumentException>(() => criterium.DetermineStatus(
                1,
                DenseVector.Create(3, i => 4),
                DenseVector.Create(3, i => 4),
                DenseVector.Create(4, i => 4)));
        }
        public void DetermineStatusWithNullSourceVectorThrowsArgumentNullException()
        {
            var criterium = new ResidualStopCriterium(1e-8, 50);
            Assert.IsNotNull(criterium, "There should be a criterium");

            Assert.Throws<ArgumentNullException>(() => criterium.DetermineStatus(
                1,
                new DenseVector(3, 4),
                null,
                new DenseVector(3, 6)));
        }
        public void DetermineStatusWithNonMatchingSolutionVectorThrowsArgumentException()
        {
            var criterium = new ResidualStopCriterium<Complex>(1e-8, 50);

            Assert.Throws<ArgumentException>(() => criterium.DetermineStatus(
                1,
                DenseVector.Create(4, i => 4),
                DenseVector.Create(3, i => 4),
                DenseVector.Create(3, i => 4)));
        }
        public void DetermineStatus()
        {
            var criterium = new ResidualStopCriterium(1e-3f, 10);
            Assert.IsNotNull(criterium, "There should be a criterium");

            // the solution vector isn't actually being used so ...
            var solution = new DenseVector(new[] { new Complex32(float.NaN, float.NaN), new Complex32(float.NaN, float.NaN), new Complex32(float.NaN, float.NaN) });

            // Set the source values
            var source = new DenseVector(new[] { new Complex32(1.000f, 1), new Complex32(1.000f, 1), new Complex32(2.001f, 1) });

            // Set the residual values
            var residual = new DenseVector(new[] { new Complex32(0.001f, 0), new Complex32(0.001f, 0), new Complex32(0.002f, 0) });

            criterium.DetermineStatus(5, solution, source, residual);
            Assert.IsInstanceOf(typeof(CalculationRunning), criterium.Status, "Should still be running");

            criterium.DetermineStatus(16, solution, source, residual);
            Assert.IsInstanceOf(typeof(CalculationConverged), criterium.Status, "Should be done");
        }
        public void DetermineStatusWithSourceNaN()
        {
            var criterium = new ResidualStopCriterium(1e-3f, 10);
            Assert.IsNotNull(criterium, "There should be a criterium");

            var solution = new DenseVector(new[] { new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(2.0f, 1) });
            var source = new DenseVector(new[] { new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(float.NaN, 1) });
            var residual = new DenseVector(new[] { new Complex32(1000.0f, 1), new Complex32(1000.0f, 1), new Complex32(2001.0f, 1) });

            criterium.DetermineStatus(5, solution, source, residual);
            Assert.IsInstanceOf(typeof(CalculationDiverged), criterium.Status, "Should be diverged");
        }
        public void DetermineStatusWithNonMatchingSolutionVectorThrowsArgumentException()
        {
            var criterium = new ResidualStopCriterium(1e-6f, 50);
            Assert.IsNotNull(criterium, "There should be a criterium");

            Assert.Throws<ArgumentException>(() => criterium.DetermineStatus(
                1,
                new DenseVector(4, 4),
                new DenseVector(3, 4),
                new DenseVector(3, 4)));
        }
        public void DetermineStatusWithIllegalIterationNumber()
        {
            var criterium = new ResidualStopCriterium(1e-8, 50);
            Assert.IsNotNull(criterium, "There should be a criterium");

            criterium.DetermineStatus(-1,
                                      new DenseVector(3, 4),
                                      new DenseVector(3, 5),
                                      new DenseVector(3, 6));
            Assert.Fail();
        }
        public void DetermineStatusWithSourceNaN()
        {
            var criterium = new ResidualStopCriterium(1e-3, 10);
            Assert.IsNotNull(criterium, "There should be a criterium");

            var solution = new DenseVector(new[] { 1.0, 1.0, 2.0 });
            var source = new DenseVector(new[] { 1.0, 1.0, double.NaN });
            var residual = new DenseVector(new[] { 1000.0, 1000.0, 2001.0 });

            criterium.DetermineStatus(5, solution, source, residual);
            Assert.IsInstanceOf(typeof(CalculationDiverged), criterium.Status, "Should be diverged");
        }
        public void DetermineStatus()
        {
            var criterium = new ResidualStopCriterium<float>(1e-3f, 10);

            // the solution vector isn't actually being used so ...
            var solution = new DenseVector(new[] {float.NaN, float.NaN, float.NaN});

            // Set the source values
            var source = new DenseVector(new[] {1.000f, 1.000f, 2.001f});

            // Set the residual values
            var residual = new DenseVector(new[] {0.001f, 0.001f, 0.002f});

            var status = criterium.DetermineStatus(5, solution, source, residual);
            Assert.AreEqual(IterationStatus.Continue, status, "Should still be running");

            var status2 = criterium.DetermineStatus(16, solution, source, residual);
            Assert.AreEqual(IterationStatus.Converged, status2, "Should be done");
        }
        public void ResetMinimumIterationsBelowMaximum()
        {
            var criterium = new ResidualStopCriterium(1e-8, 50);
            Assert.IsNotNull(criterium, "There should be a criterium");

            criterium.ResetMinimumIterationsBelowMaximumToDefault();
            Assert.AreEqual(ResidualStopCriterium.DefaultMinimumIterationsBelowMaximum, criterium.MinimumIterationsBelowMaximum, "Incorrect iteration count");
        }
        public void DetermineStatusWithNonMatchingSolutionVector()
        {
            var criterium = new ResidualStopCriterium(1e-8, 50);
            Assert.IsNotNull(criterium, "There should be a criterium");

            criterium.DetermineStatus(1,
                                      new DenseVector(4, 4),
                                      new DenseVector(3, 4),
                                      new DenseVector(3, 4));
        }
        public void DetermineStatusWithConvergenceAtFirstIteration()
        {
            var criterium = new ResidualStopCriterium<float>(1e-6);

            var solution = new DenseVector(new[] {1.0f, 1.0f, 1.0f});
            var source = new DenseVector(new[] {1.0f, 1.0f, 1.0f});
            var residual = new DenseVector(new[] {0.0f, 0.0f, 0.0f});

            var status = criterium.DetermineStatus(0, solution, source, residual);
            Assert.AreEqual(IterationStatus.Converged, status, "Should be done");
        }
        public void ResetCalculationState()
        {
            var criterium = new ResidualStopCriterium<Complex>(1e-3, 10);

            var solution = new DenseVector(new[] {new Complex(0.001, 1), new Complex(0.001, 1), new Complex(0.002, 1)});
            var source = new DenseVector(new[] {new Complex(0.001, 1), new Complex(0.001, 1), new Complex(0.002, 1)});
            var residual = new DenseVector(new[] {new Complex(1.000, 0), new Complex(1.000, 0), new Complex(2.001, 0)});

            var status = criterium.DetermineStatus(5, solution, source, residual);
            Assert.AreEqual(IterationStatus.Continue, status, "Should be running");

            criterium.Reset();
            Assert.AreEqual(IterationStatus.Continue, criterium.Status, "Should not have started");
        }
        public void DetermineStatusWithNullSolutionVectorThrowsArgumentNullException()
        {
            var criterium = new ResidualStopCriterium(1e-6f, 50);
            Assert.IsNotNull(criterium, "There should be a criterium");

            Assert.Throws<ArgumentNullException>(() => criterium.DetermineStatus(
                1,
                null,
                DenseVector.Create(3, i => 5),
                DenseVector.Create(3, i => 6)));
        }
        public void DetermineStatusWithIllegalIterationNumberThrowsArgumentOutOfRangeException()
        {
            var criterium = new ResidualStopCriterium<float>(1e-6f, 50);

            Assert.Throws<ArgumentOutOfRangeException>(() => criterium.DetermineStatus(
                -1,
                Vector<float>.Build.Dense(3, 4),
                Vector<float>.Build.Dense(3, 5),
                Vector<float>.Build.Dense(3, 6)));
        }