Anyone who has worked with scientists knows the tension between MATLAB and Python, the oft-repeated closed vs open source debate. For those who straddle the gap, it’s important to know where to go to get help when you’re moving between the two worlds. From a Python perspective, the most upto date reference is Numpy for MATLAB users on the scipy wiki.
During the process of porting the conformal mapping toolkit, one of the issues that came up related to MATLAB’s eps function. This function determines what the interval to the next floating point number is - an important prerequisite in doing approximately equal tests on floating point numbers.
The guide (correctly) describes that eps
is equivalent to the numpy
ufunc np.spacing(1)
.
More information about exactly what spacing does is provided by the numpy
info
function:
import numpy as np
np.info(np.spacing)
The documentation states that the ufunc returns “the distance between x and the nearest adjacent number”. However, MATLAB`s eps seems to differ from spacing in two important ways. Firstly, the values it returns are always positive (it’s the absolute) spacing information. Second, and more importantly, it also deals with complex values, which is critical for conformal mapping.
So a slightly revised version of eps
for python might read:
def eps(z):
"""Equivalent to MATLAB eps
"""
zre = np.real(z)
zim = np.imag(z)
return np.spacing(np.max([zre, zim]))