1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
## Automatically adapted for numpy Sep 19, 2005 by convertcode.py
__all__ = ['iscomplexobj','isrealobj','imag','iscomplex',
'isreal','nan_to_num','real','real_if_close',
'typename','asfarray','mintypecode','asscalar',
'common_type']
import numpy.core.numeric as _nx
from numpy.core.numeric import asarray, asanyarray, array, isnan, \
obj2sctype, zeros
from ufunclike import isneginf, isposinf
_typecodes_by_elsize = 'GDFgdfQqLlIiHhBb?'
def mintypecode(typechars,typeset='GDFgdf',default='d'):
""" Return a minimum data type character from typeset that
handles all typechars given
The returned type character must be the smallest size such that
an array of the returned type can handle the data from an array of
type t for each t in typechars (or if typechars is an array,
then its dtype.char).
If the typechars does not intersect with the typeset, then default
is returned.
If t in typechars is not a string then t=asarray(t).dtype.char is
applied.
"""
typecodes = [(type(t) is type('') and t) or asarray(t).dtype.char\
for t in typechars]
intersection = [t for t in typecodes if t in typeset]
if not intersection:
return default
if 'F' in intersection and 'd' in intersection:
return 'D'
l = []
for t in intersection:
i = _typecodes_by_elsize.index(t)
l.append((i,t))
l.sort()
return l[0][1]
def asfarray(a, dtype=_nx.float_):
"""asfarray(a,dtype=None) returns a as a float array."""
dtype = _nx.obj2sctype(dtype)
if not issubclass(dtype, _nx.inexact):
dtype = _nx.float_
return asarray(a,dtype=dtype)
def real(val):
"""Return the real part of val.
Useful if val maybe a scalar or an array.
"""
return asanyarray(val).real
def imag(val):
"""Return the imaginary part of val.
Useful if val maybe a scalar or an array.
"""
return asanyarray(val).imag
def iscomplex(x):
"""Return a boolean array where elements are True if that element
is complex (has non-zero imaginary part).
For scalars, return a boolean.
"""
ax = asanyarray(x)
if issubclass(ax.dtype.type, _nx.complexfloating):
return ax.imag != 0
res = zeros(ax.shape, bool)
return +res # convet to array-scalar if needed
def isreal(x):
"""Return a boolean array where elements are True if that element
is real (has zero imaginary part)
For scalars, return a boolean.
"""
return imag(x) == 0
def iscomplexobj(x):
"""Return True if x is a complex type or an array of complex numbers.
Unlike iscomplex(x), complex(3.0) is considered a complex object.
"""
return issubclass( asarray(x).dtype.type, _nx.complexfloating)
def isrealobj(x):
"""Return True if x is not a complex type.
Unlike isreal(x), complex(3.0) is considered a complex object.
"""
return not issubclass( asarray(x).dtype.type, _nx.complexfloating)
#-----------------------------------------------------------------------------
def _getmaxmin(t):
import getlimits
f = getlimits.finfo(t)
return f.max, f.min
def nan_to_num(x):
"""
Returns a copy of replacing NaN's with 0 and Infs with large numbers
The following mappings are applied:
NaN -> 0
Inf -> limits.double_max
-Inf -> limits.double_min
"""
try:
t = x.dtype.type
except AttributeError:
t = obj2sctype(type(x))
if issubclass(t, _nx.complexfloating):
return nan_to_num(x.real) + 1j * nan_to_num(x.imag)
else:
try:
y = x.copy()
except AttributeError:
y = array(x)
if not issubclass(t, _nx.integer):
if not y.shape:
y = array([x])
scalar = True
else:
scalar = False
are_inf = isposinf(y)
are_neg_inf = isneginf(y)
are_nan = isnan(y)
maxf, minf = _getmaxmin(y.dtype.type)
y[are_nan] = 0
y[are_inf] = maxf
y[are_neg_inf] = minf
if scalar:
y = y[0]
return y
#-----------------------------------------------------------------------------
def real_if_close(a,tol=100):
"""If a is a complex array, return it as a real array if the imaginary
part is close enough to zero.
"Close enough" is defined as tol*(machine epsilon of a's element type).
"""
a = asanyarray(a)
if not issubclass(a.dtype.type, _nx.complexfloating):
return a
if tol > 1:
import getlimits
f = getlimits.finfo(a.dtype.type)
tol = f.eps * tol
if _nx.allclose(a.imag, 0, atol=tol):
a = a.real
return a
def asscalar(a):
"""Convert an array of size 1 to its scalar equivalent.
"""
return a.item()
#-----------------------------------------------------------------------------
_namefromtype = {'S1' : 'character',
'?' : 'bool',
'b' : 'signed char',
'B' : 'unsigned char',
'h' : 'short',
'H' : 'unsigned short',
'i' : 'integer',
'I' : 'unsigned integer',
'l' : 'long integer',
'L' : 'unsigned long integer',
'q' : 'long long integer',
'Q' : 'unsigned long long integer',
'f' : 'single precision',
'd' : 'double precision',
'g' : 'long precision',
'F' : 'complex single precision',
'D' : 'complex double precision',
'G' : 'complex long double precision',
'S' : 'string',
'U' : 'unicode',
'V' : 'void',
'O' : 'object'
}
def typename(char):
"""Return an english description for the given data type character.
"""
return _namefromtype[char]
#-----------------------------------------------------------------------------
#determine the "minimum common type" for a group of arrays.
array_type = [[_nx.single, _nx.double, _nx.longdouble],
[_nx.csingle, _nx.cdouble, _nx.clongdouble]]
array_precision = {_nx.single : 0,
_nx.double : 1,
_nx.longdouble : 2,
_nx.csingle : 0,
_nx.cdouble : 1,
_nx.clongdouble : 2}
def common_type(*arrays):
"""Given a sequence of arrays as arguments, return the best inexact
scalar type which is "most" common amongst them.
The return type will always be a inexact scalar type, even if all
the arrays are integer arrays.
"""
is_complex = False
precision = 0
for a in arrays:
t = a.dtype.type
if iscomplexobj(a):
is_complex = True
if issubclass(t, _nx.integer):
p = 1
else:
p = array_precision.get(t, None)
if p is None:
raise TypeError("can't get common type for non-numeric array")
precision = max(precision, p)
if is_complex:
return array_type[1][precision]
else:
return array_type[0][precision]
|