Skip to content

Commit 53e1d7d

Browse files
committed
improve complex differentiation trick example
1 parent d15adf4 commit 53e1d7d

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

unpythonic/tests/test_fpnumerics.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
from operator import add, mul
1212
from itertools import repeat
13-
from math import sin, pi, log2
13+
from math import sin, cos, pi, log2
14+
from cmath import sin as complex_sin
1415

1516
from ..fun import curry
1617
from ..funutil import Values
@@ -219,18 +220,19 @@ def best_differentiate_with_tol(h0, f, x, eps):
219220
# J. N. Lyness. 1967. Numerical algorithms based on the theory of complex variables,
220221
# Proc. ACM 22nd Nat. Conf., Thompson Book Co., Washington, DC, pp. 124–134.
221222
#
223+
# See also
224+
# Joaquim J Martins, Peter Sturdza, Juan J Alonso. The complex-step derivative approximation.
225+
# ACM Transactions on Mathematical Software, Association for Computing Machinery, 2003, 29,
226+
# pp.245-262. 10.1145/838250.838251. hal-01483287.
227+
# https://hal.archives-ouvertes.fr/hal-01483287/document
228+
#
222229
# So this technique has been known since the late 1960s, but even as of this writing,
223230
# 55 years later (2022), it has not seen much use.
224-
try:
225-
# We need a `sin` that can handle complex numbers, so stdlib's won't cut the mustard.
226-
import numpy as np
227-
eps = 1e-150
228-
def complex_diff(f, x):
229-
return np.imag((f(x + eps * 1j) / eps))
230-
# This is so accurate in this simple case that we can test for floating point equality.
231-
test[complex_diff(np.sin, 0.1) == np.cos(0.1)]
232-
except ImportError:
233-
warn["Could not import NumPy; alternative numerical differentiation test skipped."]
231+
eps = 1e-150
232+
def complex_diff(f, x):
233+
return (f(x + eps * 1j) / eps).imag
234+
# This is so accurate in this simple case that we can test for floating point equality.
235+
test[complex_diff(complex_sin, 0.1) == cos(0.1)]
234236

235237
# pi approximation with Euler series acceleration
236238
#

0 commit comments

Comments
 (0)