An extension to numpy using discrete fourier transforms to compute the curl of 2D and 3D functions. This produces results far more accurate than using 10th-order finite difference derivatives (which is as far as I tested).
curl2D
computes the curl for a 2D input function
curl3D
computes the curl for a 3D input function
rcurl2D
computes the curl for a 2D purely real input function
rcurl3D
computes the curl for a 3D purely real input function
You can download it simply with the download button, or using
wget https://raw.githubusercontent.com/morgannamaths/Python-Curl-Function/main/python_curl_function.py
in the terminal.
Download the python_curl_function.py
file and store it in the same file as your project (or wherever, really), and import it into your code using from python_curl_function import CurlFunction
.
Mathematically, the curl
which can be further expanded to
Clearly, there are 6 derivatives (2 in the 2D case) required to compute. This code utilises the discrete fourier transform to compute these, which (based on testing) are more accurate than at least 10th-order finite difference derivatives, at the cost of being more computationally expensive.
where
For real functions, you can instead use rcurl2D
and rcurl3D
to perform a real discrete fourier transform. This is because the discrete fourier transform of a real function is Hermitian-symmetric and thus we can discard half of the transformed values to save on memory and computation.