module documentation

Automatically adapted for numpy Sep 19, 2005 by convertcode.py

Function asfarray Return an array converted to a float type.
Function common_type Return a scalar type which is common to the input arrays.
Function imag Return the imaginary part of the complex argument.
Function iscomplex Returns a bool array, where True if input element is complex.
Function iscomplexobj Check for a complex type or an array of complex numbers.
Function isreal Returns a bool array, where True if input element is real.
Function isrealobj Return True if x is a not complex type or an array of complex numbers.
Function mintypecode Return the character for the minimum-size type to which given types can be safely cast.
Function nan_to_num Replace NaN with zero and infinity with large finite numbers (default behaviour) or with the numbers defined by the user using the nan, posinf and/or neginf keywords.
Function real Return the real part of the complex argument.
Function real_if_close If input is complex with all imaginary parts close to zero, return real parts.
Function typename Return a description for the given data type code.
Variable array_function_dispatch Undocumented
Variable array_precision Undocumented
Variable array_type Undocumented
Function _asfarray_dispatcher Undocumented
Function _common_type_dispatcher Undocumented
Function _getmaxmin Undocumented
Function _imag_dispatcher Undocumented
Function _is_type_dispatcher Undocumented
Function _nan_to_num_dispatcher Undocumented
Function _real_dispatcher Undocumented
Function _real_if_close_dispatcher Undocumented
Variable _namefromtype Undocumented
Variable _typecodes_by_elsize Undocumented
@array_function_dispatch(_asfarray_dispatcher)
def asfarray(a, dtype=_nx.float_): (source)

Return an array converted to a float type.

Examples

>>> np.asfarray([2, 3])
array([2.,  3.])
>>> np.asfarray([2, 3], dtype='float')
array([2.,  3.])
>>> np.asfarray([2, 3], dtype='int8')
array([2.,  3.])
Parameters
a:array_likeThe input array.
dtype:str or dtype object, optionalFloat type code to coerce input array a. If dtype is one of the 'int' dtypes, it is replaced with float64.
Returns
ndarrayout - The input a as a float ndarray.

Return a scalar type which is common to the input arrays.

The return type will always be an inexact (i.e. floating point) scalar type, even if all the arrays are integer arrays. If one of the inputs is an integer array, the minimum precision type that is returned is a 64-bit floating point dtype.

All input arrays except int64 and uint64 can be safely cast to the returned dtype without loss of information.

See Also

dtype, mintypecode

Examples

>>> np.common_type(np.arange(2, dtype=np.float32))
<class 'numpy.float32'>
>>> np.common_type(np.arange(2, dtype=np.float32), np.arange(2))
<class 'numpy.float64'>
>>> np.common_type(np.arange(4), np.array([45, 6.j]), np.array([45.0]))
<class 'numpy.complex128'>
Parameters
*arraysUndocumented
array1:ndarraysInput arrays.
array2:ndarraysInput arrays.
...:ndarraysInput arrays.
Returns
data type codeout - Data type code.

Return the imaginary part of the complex argument.

See Also

real, angle, real_if_close

Examples

>>> a = np.array([1+2j, 3+4j, 5+6j])
>>> a.imag
array([2.,  4.,  6.])
>>> a.imag = np.array([8, 10, 12])
>>> a
array([1. +8.j,  3.+10.j,  5.+12.j])
>>> np.imag(1 + 1j)
1.0
Parameters
val:array_likeInput array.
Returns
ndarray or scalarout - The imaginary component of the complex argument. If val is real, the type of val is used for the output. If val has complex elements, the returned type is float.

Returns a bool array, where True if input element is complex.

What is tested is whether the input has a non-zero imaginary part, not if the input type is complex.

See Also

isreal

iscomplexobj
Return True if x is a complex type or an array of complex numbers.

Examples

>>> np.iscomplex([1+1j, 1+0j, 4.5, 3, 2, 2j])
array([ True, False, False, False, False,  True])
Parameters
x:array_likeInput array.
Returns
ndarray of boolsout - Output array.

Check for a complex type or an array of complex numbers.

The type of the input is checked, not the value. Even if the input has an imaginary part equal to zero, iscomplexobj evaluates to True.

See Also

isrealobj, iscomplex

Examples

>>> np.iscomplexobj(1)
False
>>> np.iscomplexobj(1+0j)
True
>>> np.iscomplexobj([3, 1+0j, True])
True
Parameters
x:anyThe input can be of any type and shape.
Returns
booliscomplexobj - The return value, True if x is of a complex type or has at least one complex element.

Returns a bool array, where True if input element is real.

If element has complex type with zero complex part, the return value for that element is True.

Notes

isreal may behave unexpectedly for string or object arrays (see examples)

See Also

iscomplex

isrealobj
Return True if x is not a complex type.

Examples

>>> a = np.array([1+1j, 1+0j, 4.5, 3, 2, 2j], dtype=complex)
>>> np.isreal(a)
array([False,  True,  True,  True,  True, False])

The function does not work on string arrays.

>>> a = np.array([2j, "a"], dtype="U")
>>> np.isreal(a)  # Warns about non-elementwise comparison
False

Returns True for all elements in input array of dtype=object even if any of the elements is complex.

>>> a = np.array([1, "2", 3+4j], dtype=object)
>>> np.isreal(a)
array([ True,  True,  True])

isreal should not be used with object arrays

>>> a = np.array([1+2j, 2+1j], dtype=object)
>>> np.isreal(a)
array([ True,  True])
Parameters
x:array_likeInput array.
Returns
ndarray, boolout - Boolean array of same shape as x.

Return True if x is a not complex type or an array of complex numbers.

The type of the input is checked, not the value. So even if the input has an imaginary part equal to zero, isrealobj evaluates to False if the data type is complex.

See Also

iscomplexobj, isreal

Notes

The function is only meant for arrays with numerical values but it accepts all other objects. Since it assumes array input, the return value of other objects may be True.

>>> np.isrealobj('A string')
True
>>> np.isrealobj(False)
True
>>> np.isrealobj(None)
True

Examples

>>> np.isrealobj(1)
True
>>> np.isrealobj(1+0j)
False
>>> np.isrealobj([3, 1+0j, True])
False
Parameters
x:anyThe input can be of any type and shape.
Returns
booly - The return value, False if x is of a complex type.
@set_module('numpy')
def mintypecode(typechars, typeset='GDFgdf', default='d'): (source)

Return the character for the minimum-size type to which given types can be safely cast.

The returned type character must represent the smallest size dtype such that an array of the returned type can handle the data from an array of all types in typechars (or if typechars is an array, then its dtype.char).

See Also

dtype, sctype2char, maximum_sctype

Examples

>>> np.mintypecode(['d', 'f', 'S'])
'd'
>>> x = np.array([1.1, 2-3.j])
>>> np.mintypecode(x)
'D'
>>> np.mintypecode('abceh', default='G')
'G'
Parameters
typechars:list of str or array_likeIf a list of strings, each string should represent a dtype. If array_like, the character representation of the array dtype is used.
typeset:str or list of str, optionalThe set of characters that the returned character is chosen from. The default set is 'GDFgdf'.
default:str, optionalThe default character, this is returned if none of the characters in typechars matches a character in typeset.
Returns
strtypechar - The character representing the minimum-size type that was found.
@array_function_dispatch(_nan_to_num_dispatcher)
def nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None): (source)

Replace NaN with zero and infinity with large finite numbers (default behaviour) or with the numbers defined by the user using the nan, posinf and/or neginf keywords.

If x is inexact, NaN is replaced by zero or by the user defined value in nan keyword, infinity is replaced by the largest finite floating point values representable by x.dtype or by the user defined value in posinf keyword and -infinity is replaced by the most negative finite floating point values representable by x.dtype or by the user defined value in neginf keyword.

For complex dtypes, the above is applied to each of the real and imaginary components of x separately.

If x is not inexact, then no replacements are made.

See Also

isinf
Shows which elements are positive or negative infinity.
isneginf
Shows which elements are negative infinity.
isposinf
Shows which elements are positive infinity.
isnan
Shows which elements are Not a Number (NaN).
isfinite
Shows which elements are finite (not NaN, not infinity)

Notes

NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity.

Examples

>>> np.nan_to_num(np.inf)
1.7976931348623157e+308
>>> np.nan_to_num(-np.inf)
-1.7976931348623157e+308
>>> np.nan_to_num(np.nan)
0.0
>>> x = np.array([np.inf, -np.inf, np.nan, -128, 128])
>>> np.nan_to_num(x)
array([ 1.79769313e+308, -1.79769313e+308,  0.00000000e+000, # may vary
       -1.28000000e+002,  1.28000000e+002])
>>> np.nan_to_num(x, nan=-9999, posinf=33333333, neginf=33333333)
array([ 3.3333333e+07,  3.3333333e+07, -9.9990000e+03,
       -1.2800000e+02,  1.2800000e+02])
>>> y = np.array([complex(np.inf, np.nan), np.nan, complex(np.nan, np.inf)])
array([  1.79769313e+308,  -1.79769313e+308,   0.00000000e+000, # may vary
     -1.28000000e+002,   1.28000000e+002])
>>> np.nan_to_num(y)
array([  1.79769313e+308 +0.00000000e+000j, # may vary
         0.00000000e+000 +0.00000000e+000j,
         0.00000000e+000 +1.79769313e+308j])
>>> np.nan_to_num(y, nan=111111, posinf=222222)
array([222222.+111111.j, 111111.     +0.j, 111111.+222222.j])
Parameters
x:scalar or array_likeInput data.
copy:bool, optional

Whether to create a copy of x (True) or to replace values in-place (False). The in-place operation only occurs if casting to an array does not require a copy. Default is True.

New in version 1.13.
nan:int, float, optional

Value to be used to fill NaN values. If no value is passed then NaN values will be replaced with 0.0.

New in version 1.17.
posinf:int, float, optional

Value to be used to fill positive infinity values. If no value is passed then positive infinity values will be replaced with a very large number.

New in version 1.17.
neginf:int, float, optional

Value to be used to fill negative infinity values. If no value is passed then negative infinity values will be replaced with a very small (or negative) number.

New in version 1.17.
Returns
ndarrayout - x, with the non-finite values replaced. If copy is False, this may be x itself.

Return the real part of the complex argument.

See Also

real_if_close, imag, angle

Examples

>>> a = np.array([1+2j, 3+4j, 5+6j])
>>> a.real
array([1.,  3.,  5.])
>>> a.real = 9
>>> a
array([9.+2.j,  9.+4.j,  9.+6.j])
>>> a.real = np.array([9, 8, 7])
>>> a
array([9.+2.j,  8.+4.j,  7.+6.j])
>>> np.real(1 + 1j)
1.0
Parameters
val:array_likeInput array.
Returns
ndarray or scalarout - The real component of the complex argument. If val is real, the type of val is used for the output. If val has complex elements, the returned type is float.

If input is complex with all imaginary parts close to zero, return real parts.

"Close to zero" is defined as tol * (machine epsilon of the type for a).

See Also

real, imag, angle

Notes

Machine epsilon varies from machine to machine and between data types but Python floats on most platforms have a machine epsilon equal to 2.2204460492503131e-16. You can use 'np.finfo(float).eps' to print out the machine epsilon for floats.

Examples

>>> np.finfo(float).eps
2.2204460492503131e-16 # may vary
>>> np.real_if_close([2.1 + 4e-14j, 5.2 + 3e-15j], tol=1000)
array([2.1, 5.2])
>>> np.real_if_close([2.1 + 4e-13j, 5.2 + 3e-15j], tol=1000)
array([2.1+4.e-13j, 5.2 + 3e-15j])
Parameters
a:array_likeInput array.
tol:floatTolerance in machine epsilons for the complex part of the elements in the array.
Returns
ndarrayout - If a is real, the type of a is used for the output. If a has complex elements, the returned type is float.
@set_module('numpy')
def typename(char): (source)

Return a description for the given data type code.

See Also

dtype, typecodes

Examples

>>> typechars = ['S1', '?', 'B', 'D', 'G', 'F', 'I', 'H', 'L', 'O', 'Q',
...              'S', 'U', 'V', 'b', 'd', 'g', 'f', 'i', 'h', 'l', 'q']
>>> for typechar in typechars:
...     print(typechar, ' : ', np.typename(typechar))
...
S1  :  character
?  :  bool
B  :  unsigned char
D  :  complex double precision
G  :  complex long double precision
F  :  complex single precision
I  :  unsigned integer
H  :  unsigned short
L  :  unsigned long integer
O  :  object
Q  :  unsigned long long integer
S  :  string
U  :  unicode
V  :  void
b  :  signed char
d  :  double precision
g  :  long precision
f  :  single precision
i  :  integer
h  :  short
l  :  long integer
q  :  long long integer
Parameters
char:strData type code.
Returns
strout - Description of the input data type code.
array_function_dispatch = (source)

Undocumented

array_precision = (source)

Undocumented

array_type = (source)

Undocumented

def _asfarray_dispatcher(a, dtype=None): (source)

Undocumented

def _common_type_dispatcher(*arrays): (source)

Undocumented

def _getmaxmin(t): (source)

Undocumented

def _imag_dispatcher(val): (source)

Undocumented

def _is_type_dispatcher(x): (source)

Undocumented

def _nan_to_num_dispatcher(x, copy=None, nan=None, posinf=None, neginf=None): (source)

Undocumented

def _real_dispatcher(val): (source)

Undocumented

def _real_if_close_dispatcher(a, tol=None): (source)

Undocumented

_namefromtype: dict[str, str] = (source)

Undocumented

_typecodes_by_elsize: str = (source)

Undocumented