Debian Codegraph @

src/ImathTest/testRoots.cpp

raw
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5
6 #ifdef NDEBUG
7 # undef NDEBUG
8 #endif
9
10 #include <ImathFun.h>
11 #include <ImathRoots.h>
12 #include <algorithm>
13 #include <assert.h>
14 #include <iomanip>
15 #include <iostream>
16 #include "testRoots.h"
17
18 using namespace std;
19
20 void
21 sort (int nx, double& x0, double& x1, double& x2)
22 {
23 if (nx == 2)
24 {
25 if (x0 > x1)
26 swap (x0, x1);
27 }
28
29 if (nx == 3)
30 {
31 if (x0 > x1)
32 swap (x0, x1);
33 if (x1 > x2)
34 swap (x1, x2);
35 if (x0 > x1)
36 swap (x0, x1);
37 }
38 }
39
40 void
41 sort (int nx, double x[])
42 {
43 if (nx == 2)
44 {
45 if (x[0] > x[1])
46 swap (x[0], x[1]);
47 }
48
49 if (nx == 3)
50 {
51 if (x[0] > x[1])
52 swap (x[0], x[1]);
53 if (x[1] > x[2])
54 swap (x[1], x[2]);
55 if (x[0] > x[1])
56 swap (x[0], x[1]);
57 }
58 }
59
60 void
61 solve (double a,
62 double b,
63 double c,
64 double d, // coefficients
65 int nx, // number of expected solutions
66 double x0,
67 double x1,
68 double x2) // expected solutions
69 {
70 cout << "coefficients: " << setw (3) << a << ' ' << setw (3) << b << ' ' << setw (3) << c << ' '
71 << setw (3) << d << ' ';
72
73 //
74 // Solve the equation a*x^3 + b*x^2 + c*x +d
75 //
76
77 double x[3] = { 0, 0, 0 };
78 int n = IMATH_INTERNAL_NAMESPACE::solveCubic (a, b, c, d, x);
79
80 //
81 // Sort the numerical solutions.
82 // Sort the expected solutions.
83 //
84 sort (nx, x0, x1, x2);
85 sort (n, x);
86
87 //
88 // Compare the numerical and the expected solutions.
89 //
90
91 assert (n == nx);
92
93 cout << " solutions: ";
94
95 if (n == -1)
96 cout << "[-inf, inf]";
97
98 if (n == 0)
99 cout << "none";
100
101 const double e = 0.0000001; // maximum expected error for
102 // the test cases listed below
103 if (n >= 1)
104 {
105 cout << x[0];
106 assert (IMATH_INTERNAL_NAMESPACE::equal (x[0], x0, e));
107 }
108 if (n >= 2)
109 {
110 cout << ' ' << x[1];
111 assert (IMATH_INTERNAL_NAMESPACE::equal (x[1], x1, e));
112 }
113 if (n >= 3)
114 {
115 cout << ' ' << x[2];
116 assert (IMATH_INTERNAL_NAMESPACE::equal (x[2], x2, e));
117 }
118
119 cout << endl;
120 }
121
122 void
123 solve (double a,
124 double b,
125 double c, // coefficients
126 int nx, // number of expected solutions
127 double x0,
128 double x1) // expected solutions
129 {
130 cout << "coefficients: " << setw (3) << a << ' ' << setw (3) << b << ' ' << setw (3) << c
131 << ' ';
132 //
133 // Solve the equation a*x^2 + b*x^1 + c*x
134 //
135
136 double x[2] = { 0.0, 0.0 };
137 int n = IMATH_INTERNAL_NAMESPACE::solveQuadratic (a, b, c, x);
138
139 //
140 // Sort the numerical solutions.
141 // Sort the expected solutions.
142 //
143 // Dummy variable for sort
144 double x2 = 0;
145 sort (nx, x0, x1, x2);
146 sort (n, x);
147
148 //
149 // Compare the numerical and the expected solutions.
150 //
151
152 assert (n == nx);
153 cout << " solutions: ";
154
155 if (n == -1)
156 cout << "[-inf, inf]";
157
158 if (n == 0)
159 cout << "none";
160
161 const double e = 0.0000001; // maximum expected error for
162 // the test cases listed below
163
164 if (n >= 1)
165 {
166 cout << x[0];
167 assert (IMATH_INTERNAL_NAMESPACE::equal (x[0], x0, e));
168 }
169
170 if (n >= 2)
171 {
172 cout << ' ' << x[1];
173 assert (IMATH_INTERNAL_NAMESPACE::equal (x[1], x1, e));
174 }
175
176 cout << endl;
177 }
178
179 void
180 testRoots()
181 {
182 cout << "Testing functions in ImathRoots.h" << endl;
183
184 cout << endl << "solveCubic" << endl;
185 // Solve cubiec equations
186 //
187 // coefficients number of expected solutions
188 // | |
189 // | | expected solutions
190 // | | |
191 // +-------+--------+ | +------+-----+
192 // | | | | |
193 solve (1, 6, 11, 6, 3, -1, -2, -3); // real solutions: -1, -2, -3
194 solve (2, 2, -20, 16, 3, 1, -4, 2); // real solutions: 1, -4, 2
195 solve (3, -3, 1, -1, 1, 1, 0, 0); // real solutions: 1
196 solve (2, 0, -24, -32, 2, 4, -2, 0); // real solutions: 4, -2
197 solve (1, 0, 0, 0, 1, 0, 0, 0); // real solutions: 0
198 solve (8, -24, 24, -8, 1, 1, 0, 0); // real solutions: 1
199 solve (0, 2, -10, 12, 2, 2, 3, 0); // real solutions: 2, 3
200 solve (0, 1, -1, -20, 2, 5, -4, 0); // real solutions: 5, -4
201 solve (0, 3, -12, 12, 1, 2, 0, 0); // real solutions: 2
202 solve (0, 1, 0, 0, 1, 0, 0, 0); // real solutions: 0
203 solve (0, 1, 0, 1, 0, 0, 0, 0); // real solutions: none
204 solve (0, 0, 3, -6, 1, 2, 0, 0); // real solutions: 2
205 solve (0, 0, 5, 15, 1, -3, 0, 0); // real solutions: -3
206 solve (0, 0, 1, 0, 1, 0, 0, 0); // real solutions: 0
207 solve (0, 0, 0, 1, 0, 0, 0, 0); // real solutions: none
208 solve (0, 0, 0, 0, -1, 0, 0, 0); // real solutions: [-inf, inf]
209 solve (36, -37, 0, 12, 1, -0.4715155, 0, 0);
210
211 cout << endl << "solveQuadratic" << endl;
212 // Solve quadratic equations
213 //
214 // coefficients number of expected solutions
215 // | |
216 // | | expected solutions
217 // | | |
218 // +-----+-----+ | +---+---+
219 // | | | | |
220 solve (1, 3, 2, 2, -1, -2); // real solutions: -1, -2
221 solve (1, 0, -9, 2, -3, 3); // real solutions: -3, 3
222 solve (1, -4, 0, 2, 4, 0); // real solutions: 0, 4
223 solve (2, -4, 2, 1, 1, 0); // real solutions: 1
224 solve (0, -4, 8, 1, 2, 0); // real solutions: 2
225 solve (0, 7, 0, 1, 0, 0); // real solutions: 0
226 solve (10, 0, 0, 1, 0, 0); // real solutions: 0
227 solve (0, 0, 0, -1, 0, 0); // real solutions: [-inf, inf]
228 solve (0, 0, 1, 0, 0, 0); // real solutions: none
229 solve (3, -6, 30, 0, 0, 0); // real solutions: none
230 cout << "ok\n" << endl;
231 }