~ubuntu-branches/ubuntu/feisty/python-numpy/feisty

« back to all changes in this revision

Viewing changes to numpy/lib/type_check.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-07-12 10:00:24 UTC
  • Revision ID: james.westby@ubuntu.com-20060712100024-5lw9q2yczlisqcrt
Tags: upstream-0.9.8
ImportĀ upstreamĀ versionĀ 0.9.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
## Automatically adapted for numpy Sep 19, 2005 by convertcode.py
 
2
 
 
3
__all__ = ['iscomplexobj','isrealobj','imag','iscomplex',
 
4
           'isreal','nan_to_num','real','real_if_close',
 
5
           'typename','asfarray','mintypecode','asscalar',
 
6
           'common_type']
 
7
 
 
8
import numpy.core.numeric as _nx
 
9
from numpy.core.numeric import asarray, array, isnan, obj2sctype
 
10
from ufunclike import isneginf, isposinf
 
11
 
 
12
_typecodes_by_elsize = 'GDFgdfQqLlIiHhBb?'
 
13
 
 
14
def mintypecode(typechars,typeset='GDFgdf',default='d'):
 
15
    """ Return a minimum data type character from typeset that
 
16
    handles all typechars given
 
17
 
 
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,
 
21
    then its dtype.char).
 
22
 
 
23
    If the typechars does not intersect with the typeset, then default
 
24
    is returned.
 
25
 
 
26
    If t in typechars is not a string then t=asarray(t).dtype.char is
 
27
    applied.
 
28
    """
 
29
    typecodes = [(type(t) is type('') and t) or asarray(t).dtype.char\
 
30
                 for t in typechars]
 
31
    intersection = [t for t in typecodes if t in typeset]
 
32
    if not intersection:
 
33
        return default
 
34
    if 'F' in intersection and 'd' in intersection:
 
35
        return 'D'
 
36
    l = []
 
37
    for t in intersection:
 
38
        i = _typecodes_by_elsize.index(t)
 
39
        l.append((i,t))
 
40
    l.sort()
 
41
    return l[0][1]
 
42
 
 
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):
 
47
        dtype = _nx.float_
 
48
    a = asarray(a,dtype=dtype)
 
49
    return a
 
50
 
 
51
def real(val):
 
52
    return asarray(val).real
 
53
 
 
54
def imag(val):
 
55
    return asarray(val).imag
 
56
 
 
57
def iscomplex(x):
 
58
    return imag(x) != _nx.zeros_like(x)
 
59
 
 
60
def isreal(x):
 
61
    return imag(x) == _nx.zeros_like(x)
 
62
 
 
63
def iscomplexobj(x):
 
64
    return issubclass( asarray(x).dtype.type, _nx.complexfloating)
 
65
 
 
66
def isrealobj(x):
 
67
    return not issubclass( asarray(x).dtype.type, _nx.complexfloating)
 
68
 
 
69
#-----------------------------------------------------------------------------
 
70
 
 
71
def _getmaxmin(t):
 
72
    import getlimits
 
73
    f = getlimits.finfo(t)
 
74
    return f.max, f.min
 
75
 
 
76
def nan_to_num(x):
 
77
    # mapping:
 
78
    #    NaN -> 0
 
79
    #    Inf -> limits.double_max
 
80
    #   -Inf -> limits.double_min
 
81
    try:
 
82
        t = x.dtype.type
 
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):
 
88
        y = array(x)
 
89
    else:
 
90
        y = array(x)
 
91
        if not y.shape:
 
92
            y = array([x])
 
93
            scalar = True
 
94
        else:
 
95
            scalar = False
 
96
        are_inf = isposinf(y)
 
97
        are_neg_inf = isneginf(y)
 
98
        are_nan = isnan(y)
 
99
        maxf, minf = _getmaxmin(y.dtype.type)
 
100
        y[are_nan] = 0
 
101
        y[are_inf] = maxf
 
102
        y[are_neg_inf] = minf
 
103
        if scalar:
 
104
            y = y[0]
 
105
    return y
 
106
 
 
107
#-----------------------------------------------------------------------------
 
108
 
 
109
def real_if_close(a,tol=100):
 
110
    a = asarray(a)
 
111
    if a.dtype.char not in 'FDG':
 
112
        return a
 
113
    if tol > 1:
 
114
        import getlimits
 
115
        f = getlimits.finfo(a.dtype.type)
 
116
        tol = f.eps * tol
 
117
    if _nx.allclose(a.imag, 0, atol=tol):
 
118
        a = a.real
 
119
    return a
 
120
 
 
121
 
 
122
def asscalar(a):
 
123
    return a.item()
 
124
 
 
125
#-----------------------------------------------------------------------------
 
126
 
 
127
_namefromtype = {'S1' : 'character',
 
128
                 '?' : 'bool',
 
129
                 'b' : 'signed char',
 
130
                 'B' : 'unsigned char',
 
131
                 'h' : 'short',
 
132
                 'H' : 'unsigned short',
 
133
                 'i' : 'integer',
 
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',
 
145
                 'S' : 'string',
 
146
                 'U' : 'unicode',
 
147
                 'V' : 'void',
 
148
                 'O' : 'object'
 
149
                 }
 
150
 
 
151
def typename(char):
 
152
    """Return an english description for the given data type character.
 
153
    """
 
154
    return _namefromtype[char]
 
155
 
 
156
#-----------------------------------------------------------------------------
 
157
 
 
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):
 
165
    kind = 0
 
166
    precision = 0
 
167
    for a in arrays:
 
168
        t = a.dtype.char
 
169
        kind = max(kind, array_kind[t])
 
170
        precision = max(precision, array_precision[t])
 
171
    return array_type[kind][precision]