public void Create()
        {
            var criterium = new DivergenceStopCriterium(0.1, 3);
            Assert.IsNotNull(criterium, "There should be a criterium");

            Assert.AreEqual(0.1, criterium.MaximumRelativeIncrease, "Incorrect maximum");
            Assert.AreEqual(3, criterium.MinimumNumberOfIterations, "Incorrect iteration count");
        }
        public void Clone()
        {
            const double Increase = 0.5;
            const int Iterations = 10;

            var criterium = new DivergenceStopCriterium(Increase, Iterations);
            Assert.IsNotNull(criterium, "There should be a criterium");

            var clone = criterium.Clone();
            Assert.IsInstanceOf(typeof (DivergenceStopCriterium), clone, "Wrong criterium type");

            var clonedCriterium = clone as DivergenceStopCriterium;
            Assert.IsNotNull(clonedCriterium);

            Assert.AreEqual(criterium.MaximumRelativeIncrease, clonedCriterium.MaximumRelativeIncrease, "Incorrect maximum");
            Assert.AreEqual(criterium.MinimumNumberOfIterations, clonedCriterium.MinimumNumberOfIterations, "Incorrect iteration count");
        }
        public void ResetMinimumIterationsBelowMaximum()
        {
            var criterium = new DivergenceStopCriterium(0.5, 15);
            Assert.IsNotNull(criterium, "There should be a criterium");

            Assert.AreEqual(15, criterium.MinimumNumberOfIterations, "Incorrect iteration count");

            criterium.ResetNumberOfIterationsToDefault();
            Assert.AreEqual(DivergenceStopCriterium.DefaultMinimumNumberOfIterations, criterium.MinimumNumberOfIterations, "Incorrect value");
        }
        public void ResetMaximumIncrease()
        {
            var criterium = new DivergenceStopCriterium(0.5, 3);
            Assert.IsNotNull(criterium, "There should be a criterium");

            Assert.AreEqual(0.5, criterium.MaximumRelativeIncrease, "Incorrect maximum");

            criterium.ResetMaximumRelativeIncreaseToDefault();
            Assert.AreEqual(DivergenceStopCriterium.DefaultMaximumRelativeIncrease, criterium.MaximumRelativeIncrease, "Incorrect value");
        }
        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.0}),
                new DenseVector(new[] {1.0}),
                new DenseVector(new[] {double.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 double Increase = 0.5;
            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.0}),
                    new DenseVector(new[] {1.0}),
                    new DenseVector(new[] {(i + 1)*(Increase + 0.1)}));

                Assert.AreEqual(IterationStatus.Running, status, "Status check fail.");
            }
        }
        public void DetermineStatusWithNoDivergence()
        {
            const double Increase = 0.5;
            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.0}),
                    new DenseVector(new[] {1.0}),
                    new DenseVector(new[] {(i + 1)*(Increase - 0.01)}));

                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 double Increase = 0.5;
            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.0}),
                    new DenseVector(new[] {1.0}),
                    new DenseVector(new[] {(i + 1)*(Increase - 0.01)}));

                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.0}),
                new DenseVector(new[] {1.0}),
                new DenseVector(new[] {double.NaN}));

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

            var criterium = new DivergenceStopCriterium(Increase, Iterations);

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

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