1
## Automatically adapted for numpy Sep 19, 2005 by convertcode.py
3
__all__ = ['iscomplexobj','isrealobj','imag','iscomplex',
4
'isreal','nan_to_num','real','real_if_close',
5
'typename','asfarray','mintypecode','asscalar',
8
import numpy.core.numeric as _nx
9
from numpy.core.numeric import asarray, array, isnan, obj2sctype
10
from ufunclike import isneginf, isposinf
12
_typecodes_by_elsize = 'GDFgdfQqLlIiHhBb?'
14
def mintypecode(typechars,typeset='GDFgdf',default='d'):
15
""" Return a minimum data type character from typeset that
16
handles all typechars given
18
The returned type character must be the smallest size such that
19
an array of the returned type can handle the data from an array of
20
type t for each t in typechars (or if typechars is an array,
23
If the typechars does not intersect with the typeset, then default
26
If t in typechars is not a string then t=asarray(t).dtype.char is
29
typecodes = [(type(t) is type('') and t) or asarray(t).dtype.char\
31
intersection = [t for t in typecodes if t in typeset]
34
if 'F' in intersection and 'd' in intersection:
37
for t in intersection:
38
i = _typecodes_by_elsize.index(t)
43
def asfarray(a, dtype=_nx.float_):
44
"""asfarray(a,dtype=None) returns a as a float array."""
45
dtype = _nx.obj2sctype(dtype)
46
if not issubclass(dtype, _nx.inexact):
48
a = asarray(a,dtype=dtype)
52
return asarray(val).real
55
return asarray(val).imag
58
return imag(x) != _nx.zeros_like(x)
61
return imag(x) == _nx.zeros_like(x)
64
return issubclass( asarray(x).dtype.type, _nx.complexfloating)
67
return not issubclass( asarray(x).dtype.type, _nx.complexfloating)
69
#-----------------------------------------------------------------------------
73
f = getlimits.finfo(t)
79
# Inf -> limits.double_max
80
# -Inf -> limits.double_min
83
except AttributeError:
84
t = obj2sctype(type(x))
85
if issubclass(t, _nx.complexfloating):
86
y = nan_to_num(x.real) + 1j * nan_to_num(x.imag)
87
elif issubclass(t, _nx.integer):
97
are_neg_inf = isneginf(y)
99
maxf, minf = _getmaxmin(y.dtype.type)
102
y[are_neg_inf] = minf
107
#-----------------------------------------------------------------------------
109
def real_if_close(a,tol=100):
111
if a.dtype.char not in 'FDG':
115
f = getlimits.finfo(a.dtype.type)
117
if _nx.allclose(a.imag, 0, atol=tol):
125
#-----------------------------------------------------------------------------
127
_namefromtype = {'S1' : 'character',
130
'B' : 'unsigned char',
132
'H' : 'unsigned short',
134
'I' : 'unsigned integer',
135
'l' : 'long integer',
136
'L' : 'unsigned long integer',
137
'q' : 'long long integer',
138
'Q' : 'unsigned long long integer',
139
'f' : 'single precision',
140
'd' : 'double precision',
141
'g' : 'long precision',
142
'F' : 'complex single precision',
143
'D' : 'complex double precision',
144
'G' : 'complex long double precision',
152
"""Return an english description for the given data type character.
154
return _namefromtype[char]
156
#-----------------------------------------------------------------------------
158
#determine the "minimum common type code" for a group of arrays.
159
array_kind = {'i':0, 'l': 0, 'f': 0, 'd': 0, 'g':0, 'F': 1, 'D': 1, 'G':1}
160
array_precision = {'i': 1, 'l': 1,
161
'f': 0, 'd': 1, 'g':2,
162
'F': 0, 'D': 1, 'G':2}
163
array_type = [['f', 'd', 'g'], ['F', 'D', 'G']]
164
def common_type(*arrays):
169
kind = max(kind, array_kind[t])
170
precision = max(precision, array_precision[t])
171
return array_type[kind][precision]