NumPy real_if_close converts a complex array only when every imaginary part is close to zero

A calculation can produce complex values whose imaginary components are very small. Deciding whether to discard those components needs an explicit rule. Academia Master Trader presents this English lesson for readers in Nicaragua, using NumPy real_if_close to explain an array-wide conversion based on a stated tolerance rather than an automatic declaration that imaginary values are meaningless.

The decision concerns the whole array

After import numpy as np, define a = np.array([2+0.001j,5+0.002j]).

The call np.real_if_close(a,tol=0.01) returns a floating-point array containing 2.0 and 5.0. Both imaginary components are within the selected closeness rule.

Now define b = np.array([2+0.001j,5+0.2j]). With the same tolerance, np.real_if_close(b,tol=0.01) keeps a complex array.

The second component prevents conversion of the array as a whole. The first entry does not receive a separate real dtype while the second remains complex.

This is a conditional conversion, not an element-by-element Boolean mask.

All imaginary components must satisfy the rule before the complex array becomes real

Tolerance has two interpretations

The default tolerance is 100. For tolerance values greater than one, the threshold is the tolerance multiplied by machine epsilon for the relevant floating-point type.

Machine epsilon describes the spacing associated with that type near one. A tolerance of 100 therefore does not mean an imaginary component of one hundred is acceptable.

When the supplied tolerance is at most one, NumPy uses it as an absolute tolerance instead. The earlier value 0.01 uses this branch.

These interpretations make the parameter's scale important. State which interpretation applies before choosing a number for a workflow.

The documented examples show the boundary's effect

NumPy documents the input [2.1+4e-14j,5.2+3e-15j] with tol=1000. On the documented floating-point setting, the result is a real array containing 2.1 and 5.2.

Increasing the first imaginary component to 4e-13j keeps the result complex under the same setting.

The threshold comes from the imaginary components and the floating-point type. It is not a percentage of each entry's real component.

Avoid relying on a value exactly at a threshold in an explanation. Values clearly on either side make the intended distinction easier to inspect.

Conversion requires a reason beyond smallness

A small imaginary component may be numerical residue, or it may carry information that matters to the application. The function does not decide which interpretation is scientifically appropriate.

Choose the rule from the calculation's meaning and required accuracy. Keep the complex result when the reason for discarding its imaginary part has not been established.

If conversion occurs, later code receives a real-valued array. Check that downstream expectations match that possible change in dtype.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the tolerance and its interpretation whenever a complex-to-real conversion affects the result.