csharpfftfsharpintegrationinterpolationlinear-algebramathdifferentiationmatrixnumericsrandomregressionstatisticsmathnet
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
284 lines
11 KiB
284 lines
11 KiB
// <copyright file="ResidualStopCriteriumTest.cs" company="Math.NET">
|
|
// Math.NET Numerics, part of the Math.NET Project
|
|
// http://numerics.mathdotnet.com
|
|
// http://github.com/mathnet/mathnet-numerics
|
|
// http://mathnetnumerics.codeplex.com
|
|
//
|
|
// Copyright (c) 2009-2013 Math.NET
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person
|
|
// obtaining a copy of this software and associated documentation
|
|
// files (the "Software"), to deal in the Software without
|
|
// restriction, including without limitation the rights to use,
|
|
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the
|
|
// Software is furnished to do so, subject to the following
|
|
// conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be
|
|
// included in all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
// OTHER DEALINGS IN THE SOFTWARE.
|
|
// </copyright>
|
|
|
|
using System;
|
|
using MathNet.Numerics.LinearAlgebra.Single;
|
|
using MathNet.Numerics.LinearAlgebra.Single.Solvers.StopCriterium;
|
|
using MathNet.Numerics.LinearAlgebra.Solvers;
|
|
using NUnit.Framework;
|
|
|
|
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.StopCriterium
|
|
{
|
|
/// <summary>
|
|
/// Residual stop criterium tests.
|
|
/// </summary>
|
|
[TestFixture]
|
|
public sealed class ResidualStopCriteriumTest
|
|
{
|
|
/// <summary>
|
|
/// Create with negative maximum throws <c>ArgumentOutOfRangeException</c>.
|
|
/// </summary>
|
|
[Test]
|
|
public void CreateWithNegativeMaximumThrowsArgumentOutOfRangeException()
|
|
{
|
|
Assert.Throws<ArgumentOutOfRangeException>(() => new ResidualStopCriterium(-0.1f));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create with illegal minimum iterations throws <c>ArgumentOutOfRangeException</c>.
|
|
/// </summary>
|
|
[Test]
|
|
public void CreateWithIllegalMinimumIterationsThrowsArgumentOutOfRangeException()
|
|
{
|
|
Assert.Throws<ArgumentOutOfRangeException>(() => new ResidualStopCriterium(-1));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Can create.
|
|
/// </summary>
|
|
[Test]
|
|
public void Create()
|
|
{
|
|
var criterium = new ResidualStopCriterium(1e-6f, 50);
|
|
Assert.IsNotNull(criterium, "There should be a criterium");
|
|
|
|
Assert.AreEqual(1e-6f, criterium.Maximum, "Incorrect maximum");
|
|
Assert.AreEqual(50, criterium.MinimumIterationsBelowMaximum, "Incorrect iteration count");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Can reset maximum.
|
|
/// </summary>
|
|
[Test]
|
|
public void ResetMaximum()
|
|
{
|
|
var criterium = new ResidualStopCriterium(1e-6f, 50);
|
|
Assert.IsNotNull(criterium, "There should be a criterium");
|
|
|
|
criterium.ResetMaximumResidualToDefault();
|
|
Assert.AreEqual(ResidualStopCriterium.DefaultMaximumResidual, criterium.Maximum, "Incorrect maximum");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Can reset minimum iterations below maximum.
|
|
/// </summary>
|
|
[Test]
|
|
public void ResetMinimumIterationsBelowMaximum()
|
|
{
|
|
var criterium = new ResidualStopCriterium(1e-6f, 50);
|
|
Assert.IsNotNull(criterium, "There should be a criterium");
|
|
|
|
criterium.ResetMinimumIterationsBelowMaximumToDefault();
|
|
Assert.AreEqual(ResidualStopCriterium.DefaultMinimumIterationsBelowMaximum, criterium.MinimumIterationsBelowMaximum, "Incorrect iteration count");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determine status with illegal iteration number throws <c>ArgumentOutOfRangeException</c>.
|
|
/// </summary>
|
|
[Test]
|
|
public void DetermineStatusWithIllegalIterationNumberThrowsArgumentOutOfRangeException()
|
|
{
|
|
var criterium = new ResidualStopCriterium(1e-6f, 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)));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determine status with non-matching solution vector throws <c>ArgumentException</c>.
|
|
/// </summary>
|
|
[Test]
|
|
public void DetermineStatusWithNonMatchingSolutionVectorThrowsArgumentException()
|
|
{
|
|
var criterium = new ResidualStopCriterium(1e-6f, 50);
|
|
Assert.IsNotNull(criterium, "There should be a criterium");
|
|
|
|
Assert.Throws<ArgumentException>(() => criterium.DetermineStatus(
|
|
1,
|
|
DenseVector.Create(4, i => 4),
|
|
DenseVector.Create(3, i => 4),
|
|
DenseVector.Create(3, i => 4)));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determine status with non-matching source vector throws <c>ArgumentException</c>.
|
|
/// </summary>
|
|
[Test]
|
|
public void DetermineStatusWithNonMatchingSourceVectorThrowsArgumentException()
|
|
{
|
|
var criterium = new ResidualStopCriterium(1e-6f, 50);
|
|
Assert.IsNotNull(criterium, "There should be a criterium");
|
|
|
|
Assert.Throws<ArgumentException>(() => criterium.DetermineStatus(
|
|
1,
|
|
DenseVector.Create(3, i => 4),
|
|
DenseVector.Create(4, i => 4),
|
|
DenseVector.Create(3, i => 4)));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determine status with non-matching residual vector throws <c>ArgumentException</c>.
|
|
/// </summary>
|
|
[Test]
|
|
public void DetermineStatusWithNonMatchingResidualVectorThrowsArgumentException()
|
|
{
|
|
var criterium = new ResidualStopCriterium(1e-6f, 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)));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Can determine status with source NaN.
|
|
/// </summary>
|
|
[Test]
|
|
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});
|
|
|
|
var status = criterium.DetermineStatus(5, solution, source, residual);
|
|
Assert.AreEqual(IterationStatus.Diverged, status, "Should be diverged");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Can determine status with residual NaN.
|
|
/// </summary>
|
|
[Test]
|
|
public void DetermineStatusWithResidualNaN()
|
|
{
|
|
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, 2.0f});
|
|
var residual = new DenseVector(new[] {1000.0f, float.NaN, 2001.0f});
|
|
|
|
var status = criterium.DetermineStatus(5, solution, source, residual);
|
|
Assert.AreEqual(IterationStatus.Diverged, status, "Should be diverged");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Can determine status with convergence at first iteration.
|
|
/// </summary>
|
|
[Test]
|
|
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");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Can determine status.
|
|
/// </summary>
|
|
[Test]
|
|
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[] {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");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Can reset calculation state.
|
|
/// </summary>
|
|
[Test]
|
|
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.Continue, status, "Should be running");
|
|
|
|
criterium.Reset();
|
|
Assert.AreEqual(IterationStatus.Continue, criterium.Status, "Should not have started");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Can clone stop criterium.
|
|
/// </summary>
|
|
[Test]
|
|
public void Clone()
|
|
{
|
|
var criterium = new ResidualStopCriterium(1e-3f, 10);
|
|
Assert.IsNotNull(criterium, "There should be a criterium");
|
|
|
|
var clone = criterium.Clone();
|
|
Assert.IsInstanceOf(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
|
|
}
|
|
}
|
|
}
|
|
|