| 1186 | assert_array_equal(x2.mask, [False, False, True, False, False]) |
| 1187 | |
| 1188 | def test_second_order_accurate(self): |
| 1189 | # Testing that the relative numerical error is less that 3% for |
| 1190 | # this example problem. This corresponds to second order |
| 1191 | # accurate finite differences for all interior and boundary |
| 1192 | # points. |
| 1193 | x = np.linspace(0, 1, 10) |
| 1194 | dx = x[1] - x[0] |
| 1195 | y = 2 * x ** 3 + 4 * x ** 2 + 2 * x |
| 1196 | analytical = 6 * x ** 2 + 8 * x + 2 |
| 1197 | num_error = np.abs((np.gradient(y, dx, edge_order=2) / analytical) - 1) |
| 1198 | assert_(np.all(num_error < 0.03) == True) |
| 1199 | |
| 1200 | # test with unevenly spaced |
| 1201 | rng = np.random.default_rng(0) |
| 1202 | x = np.sort(rng.random(10)) |
| 1203 | y = 2 * x ** 3 + 4 * x ** 2 + 2 * x |
| 1204 | analytical = 6 * x ** 2 + 8 * x + 2 |
| 1205 | num_error = np.abs((np.gradient(y, x, edge_order=2) / analytical) - 1) |
| 1206 | assert_(np.all(num_error < 0.03) == True) |
| 1207 | |
| 1208 | def test_spacing(self): |
| 1209 | f = np.array([0, 2., 3., 4., 5., 5.]) |