public void ResetCalculationState()
        {
            const double Increase = 0.5;
            const int Iterations = 10;

            var criterium = new DivergenceStopCriterium(Increase, Iterations);

            // Add residuals. Blow it up instantly
            var status = criterium.DetermineStatus(
                1,
                new DenseVector(new[] {1.0f}),
                new DenseVector(new[] {1.0f}),
                new DenseVector(new[] {float.NaN}));

            Assert.AreEqual(IterationStatus.Diverged, status, "Status check fail.");

            // Reset the state
            criterium.ResetToPrecalculationState();

            Assert.AreEqual(Increase, criterium.MaximumRelativeIncrease, "Incorrect maximum");
            Assert.AreEqual(Iterations, criterium.MinimumNumberOfIterations, "Incorrect iteration count");
            Assert.AreEqual(IterationStatus.Indetermined, criterium.Status, "Status check fail.");
        }
        public void DetermineStatusWithTooFewIterations()
        {
            const float Increase = 0.5f;
            const int Iterations = 10;

            var criterium = new DivergenceStopCriterium(Increase, Iterations);

            // Add residuals. We should not diverge because we'll have to few iterations
            for (var i = 0; i < Iterations - 1; i++)
            {
                var status = criterium.DetermineStatus(
                    i,
                    new DenseVector(new[] {1.0f}),
                    new DenseVector(new[] {1.0f}),
                    new DenseVector(new[] {(i + 1)*(Increase + 0.1f)}));

                Assert.AreEqual(IterationStatus.Running, status, "Status check fail.");
            }
        }
        public void DetermineStatusWithNoDivergence()
        {
            const float Increase = 0.5f;
            const int Iterations = 10;

            var criterium = new DivergenceStopCriterium(Increase, Iterations);

            // Add residuals. We should not diverge because we won't have enough increase
            for (var i = 0; i < Iterations*2; i++)
            {
                var status = criterium.DetermineStatus(
                    i,
                    new DenseVector(new[] {1.0f}),
                    new DenseVector(new[] {1.0f}),
                    new DenseVector(new[] {(i + 1)*(Increase - 0.01f)}));

                Assert.AreEqual(IterationStatus.Running, status, "Status check fail.");
            }
        }
 public void DetermineStatusWithIllegalIterationNumberThrowsArgumentOutOfRangeException()
 {
     var criterium = new DivergenceStopCriterium(0.5, 15);
     Assert.Throws<ArgumentOutOfRangeException>(() => criterium.DetermineStatus(
         -1,
         DenseVector.Create(3, i => 4),
         DenseVector.Create(3, i => 5),
         DenseVector.Create(3, i => 6)));
 }
        public void DetermineStatusWithDivergenceThroughNaN()
        {
            const float Increase = 0.5f;
            const int Iterations = 10;

            var criterium = new DivergenceStopCriterium(Increase, Iterations);

            // Add residuals. We should not diverge because we'll have to few iterations
            for (var i = 0; i < Iterations - 5; i++)
            {
                var status = criterium.DetermineStatus(
                    i,
                    new DenseVector(new[] {1.0f}),
                    new DenseVector(new[] {1.0f}),
                    new DenseVector(new[] {(i + 1)*(Increase - 0.01f)}));

                Assert.AreEqual(IterationStatus.Running, status, "Status check fail.");
            }

            // Now make it fail by throwing in a NaN
            var status2 = criterium.DetermineStatus(
                Iterations,
                new DenseVector(new[] {1.0f}),
                new DenseVector(new[] {1.0f}),
                new DenseVector(new[] {float.NaN}));

            Assert.AreEqual(IterationStatus.Diverged, status2, "Status check fail.");
        }
        public void DetermineStatusWithDivergence()
        {
            const float Increase = 0.5f;
            const int Iterations = 10;

            var criterium = new DivergenceStopCriterium(Increase, Iterations);

            // Add residuals. We should not diverge because we'll have one to few iterations
            float previous = 1;
            for (var i = 0; i < Iterations - 1; i++)
            {
                previous *= 1 + Increase + 0.01f;
                var status = criterium.DetermineStatus(
                    i,
                    new DenseVector(new[] {1.0f}),
                    new DenseVector(new[] {1.0f}),
                    new DenseVector(new[] {previous}));

                Assert.AreEqual(IterationStatus.Running, status, "Status check fail.");
            }

            // Add the final residual. Now we should have divergence
            previous *= 1 + Increase + 0.01f;
            var status2 = criterium.DetermineStatus(
                Iterations - 1,
                new DenseVector(new[] {1.0f}),
                new DenseVector(new[] {1.0f}),
                new DenseVector(new[] {previous}));

            Assert.AreEqual(IterationStatus.Diverged, status2, "Status check fail.");
        }