-
Notifications
You must be signed in to change notification settings - Fork 176
/
T_Solvers.cs
91 lines (83 loc) · 2.79 KB
/
T_Solvers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
Copyright (C) 2008 Siarhei Novik (snovik@gmail.com)
This file is part of QLNet Project https://github.com/amaggiulli/qlnet
QLNet is free software: you can redistribute it and/or modify it
under the terms of the QLNet license. You should have received a
copy of the license along with this program; if not, license is
available at <https://github.com/amaggiulli/QLNet/blob/develop/LICENSE>.
QLNet is a based on QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
The QuantLib license is available online at http://quantlib.org/license.shtml.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
using System;
using Xunit;
using QLNet;
namespace TestSuite
{
[Collection("QLNet CI Tests")]
public class T_Solvers
{
class Foo : ISolver1d
{
public override double value(double x) { return x * x - 1.0; }
public override double derivative(double x) { return 2.0 * x; }
}
internal void test(Solver1D solver, string name)
{
double[] accuracy = new double[] { 1.0e-4, 1.0e-6, 1.0e-8 };
double expected = 1.0;
for (int i = 0; i < accuracy.Length; i++)
{
double root = solver.solve(new Foo(), accuracy[i], 1.5, 0.1);
if (Math.Abs(root - expected) > accuracy[i])
{
QAssert.Fail(name + " solver:\n"
+ " expected: " + expected + "\n"
+ " calculated: " + root + "\n"
+ " accuracy: " + accuracy[i]);
}
root = solver.solve(new Foo(), accuracy[i], 1.5, 0.0, 1.0);
if (Math.Abs(root - expected) > accuracy[i])
{
QAssert.Fail(name + " solver (bracketed):\n"
+ " expected: " + expected + "\n"
+ " calculated: " + root + "\n"
+ " accuracy: " + accuracy[i]);
}
}
}
[Fact]
public void testBrent()
{
test(new Brent(), "Brent");
}
[Fact]
public void testNewton()
{
test(new Newton(), "Newton");
}
[Fact]
public void testFalsePosition()
{
test(new FalsePosition(), "FalsePosition");
}
[Fact]
public void testBisection()
{
test(new Bisection(), "Bisection");
}
[Fact]
public void testRidder()
{
test(new Ridder(), "Ridder");
}
[Fact]
public void testSecant()
{
test(new Secant(), "Secant");
}
}
}