FТест метода Ньютона
1using NUnit.Framework;
2
3namespace MathUtilities
4{
5
6
7
8
9
10
11
12
13
14
15
16
[TestFixture]
[TestOf(typeof(NewtonOptimizer))] public class NewtonOptimizerTest
{
[Test]
public void TestLeftParabola()
{
var o = new NewtonOptimizer(LeftParabola, start: -1.5); var got = o.GetOptimal();
Assert.AreEqual(-1, got, 1E-04);
}
17 |
private double LeftParabola(double x) => |
18 |
x * x + 2 * x + 1; |
19 |
} |
20 |
|
21
22
23
24
25
26
27
28
29
30
31
32
[TestFixture]
[TestOf(typeof(Derivative))] public class DerivativeTest
{
public Derivative derivative;
[SetUp]
public void Init()
{
derivative = new Derivative(TypicalFunction);
}
33[Test]
34[TestCase(0, 0)]
35[TestCase(1, 2)]
36public void TestDerivative(double x, double expected)
37{
38 |
var got = derivative.GetDerivative(x); |
39 |
Assert.AreEqual(expected, got); |
40 |
} |
41
42[Test]
43[TestCase(0, 0)]
44[TestCase(1, 3)]
45public void TestDerivative(double x, double expected)
{
var got = derivative.GetDerivative(x); Assert.AreEqual(expected, got);
}
51[Test]
52[TestCase(0, 0)]
53[TestCase(1, 6)]
54public void TestSecondDerivative(double x, double expected)
55{
56 |
var got = derivative.GetSecondDerivative(x); |
57 |
Assert.AreEqual(expected, got); |
58 |
} |
59
60/// <summary>
61/// x^3
62/// </summary>
63/// <remark>
64/// F'(0) = 0;
65/// F'(1) = 3;
66/// </remark>
67private double TypicalFunction(double vector) =>
69}
70}