~jtaylor/ubuntu/precise/python-numpy/multiarch-fix-818867

« back to all changes in this revision

Viewing changes to numpy/lib/scimath.py

  • Committer: Bazaar Package Importer
  • Author(s): Ondrej Certik, Riku Voipio, Tiziano Zito, Carlos Galisteo, Ondrej Certik
  • Date: 2008-07-08 15:08:16 UTC
  • mfrom: (0.1.21 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080708150816-ekf992jcp2k1eua3
Tags: 1:1.1.0-3
[ Riku Voipio ]
* debian/control: atlas is not available on armel, and after a quick look
  neither on alpha. I'd also suggest dropping
  libatlas-sse-dev|libatlas-sse2-dev|libatlas-3dnow-dev alternative combo
  away, these are potentially dangerous on buildd's. Ondrej: dropped.
  (Closes: #489568)

[ Tiziano Zito ]
* patch: build _dotblas.c when ATLAS is not installed, build-conflict with
  atlas, build-depend on blas+lapack only, as it used to be (Closes: #489726)

[ Carlos Galisteo ]
* debian/control
  - Added Homepage field.

[ Ondrej Certik ]
* Checked the package on i386 and amd64, both with and without atlas, all
  tests run and the numpy package is faster if atlas is around. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
Wrapper functions to more user-friendly calling of certain math functions
3
3
whose output data-type is different than the input data-type in certain
4
4
domains of the input.
 
5
 
 
6
For example, for functions like log() with branch cuts, the versions in this
 
7
module provide the mathematically valid answers in the complex plane:
 
8
 
 
9
>>> import math
 
10
>>> from numpy.lib import scimath
 
11
>>> scimath.log(-math.exp(1)) == (1+1j*math.pi)
 
12
True
 
13
 
 
14
Similarly, sqrt(), other base logarithms, power() and trig functions are
 
15
correctly handled.  See their respective docstrings for specific examples.
5
16
"""
6
17
 
7
18
__all__ = ['sqrt', 'log', 'log2', 'logn','log10', 'power', 'arccos',
12
23
from numpy.core.numeric import asarray, any
13
24
from numpy.lib.type_check import isreal
14
25
 
15
 
 
16
 
#__all__.extend([key for key in dir(nx.umath)
17
 
#                if key[0] != '_' and key not in __all__])
18
 
 
19
26
_ln2 = nx.log(2.0)
20
27
 
21
28
def _tocomplex(arr):
22
 
    if isinstance(arr.dtype, (nt.single, nt.byte, nt.short, nt.ubyte,
23
 
                              nt.ushort)):
 
29
    """Convert its input `arr` to a complex array.
 
30
 
 
31
    The input is returned as a complex array of the smallest type that will fit
 
32
    the original data: types like single, byte, short, etc. become csingle,
 
33
    while others become cdouble.
 
34
 
 
35
    A copy of the input is always made.
 
36
 
 
37
    Parameters
 
38
    ----------
 
39
    arr : array
 
40
 
 
41
    Returns
 
42
    -------
 
43
    array
 
44
        An array with the same input data as the input but in complex form.
 
45
 
 
46
    Examples
 
47
    --------
 
48
 
 
49
    >>> import numpy as np
 
50
 
 
51
    First, consider an input of type short:
 
52
 
 
53
    >>> a = np.array([1,2,3],np.short)
 
54
 
 
55
    >>> ac = _tocomplex(a); ac
 
56
    array([ 1.+0.j,  2.+0.j,  3.+0.j], dtype=complex64)
 
57
 
 
58
    >>> ac.dtype
 
59
    dtype('complex64')
 
60
 
 
61
    If the input is of type double, the output is correspondingly of the
 
62
    complex double type as well:
 
63
 
 
64
    >>> b = np.array([1,2,3],np.double)
 
65
 
 
66
    >>> bc = _tocomplex(b); bc
 
67
    array([ 1.+0.j,  2.+0.j,  3.+0.j])
 
68
 
 
69
    >>> bc.dtype
 
70
    dtype('complex128')
 
71
 
 
72
    Note that even if the input was complex to begin with, a copy is still
 
73
    made, since the astype() method always copies:
 
74
 
 
75
    >>> c = np.array([1,2,3],np.csingle)
 
76
 
 
77
    >>> cc = _tocomplex(c); cc
 
78
    array([ 1.+0.j,  2.+0.j,  3.+0.j], dtype=complex64)
 
79
 
 
80
    >>> c *= 2; c
 
81
    array([ 2.+0.j,  4.+0.j,  6.+0.j], dtype=complex64)
 
82
 
 
83
    >>> cc
 
84
    array([ 1.+0.j,  2.+0.j,  3.+0.j], dtype=complex64)
 
85
    """
 
86
    if issubclass(arr.dtype.type, (nt.single, nt.byte, nt.short, nt.ubyte,
 
87
                                   nt.ushort,nt.csingle)):
24
88
        return arr.astype(nt.csingle)
25
89
    else:
26
90
        return arr.astype(nt.cdouble)
27
91
 
28
92
def _fix_real_lt_zero(x):
 
93
    """Convert `x` to complex if it has real, negative components.
 
94
 
 
95
    Otherwise, output is just the array version of the input (via asarray).
 
96
 
 
97
    Parameters
 
98
    ----------
 
99
    x : array_like
 
100
 
 
101
    Returns
 
102
    -------
 
103
    array
 
104
 
 
105
    Examples
 
106
    --------
 
107
    >>> _fix_real_lt_zero([1,2])
 
108
    array([1, 2])
 
109
 
 
110
    >>> _fix_real_lt_zero([-1,2])
 
111
    array([-1.+0.j,  2.+0.j])
 
112
    """
29
113
    x = asarray(x)
30
114
    if any(isreal(x) & (x<0)):
31
115
        x = _tocomplex(x)
32
116
    return x
33
117
 
34
118
def _fix_int_lt_zero(x):
 
119
    """Convert `x` to double if it has real, negative components.
 
120
 
 
121
    Otherwise, output is just the array version of the input (via asarray).
 
122
 
 
123
    Parameters
 
124
    ----------
 
125
    x : array_like
 
126
 
 
127
    Returns
 
128
    -------
 
129
    array
 
130
 
 
131
    Examples
 
132
    --------
 
133
    >>> _fix_int_lt_zero([1,2])
 
134
    array([1, 2])
 
135
 
 
136
    >>> _fix_int_lt_zero([-1,2])
 
137
    array([-1.,  2.])
 
138
    """
35
139
    x = asarray(x)
36
140
    if any(isreal(x) & (x < 0)):
37
141
        x = x * 1.0
38
142
    return x
39
143
 
40
144
def _fix_real_abs_gt_1(x):
 
145
    """Convert `x` to complex if it has real components x_i with abs(x_i)>1.
 
146
 
 
147
    Otherwise, output is just the array version of the input (via asarray).
 
148
 
 
149
    Parameters
 
150
    ----------
 
151
    x : array_like
 
152
 
 
153
    Returns
 
154
    -------
 
155
    array
 
156
 
 
157
    Examples
 
158
    --------
 
159
    >>> _fix_real_abs_gt_1([0,1])
 
160
    array([0, 1])
 
161
 
 
162
    >>> _fix_real_abs_gt_1([0,2])
 
163
    array([ 0.+0.j,  2.+0.j])
 
164
    """
41
165
    x = asarray(x)
42
166
    if any(isreal(x) & (abs(x)>1)):
43
167
        x = _tocomplex(x)
44
168
    return x
45
169
 
46
170
def sqrt(x):
 
171
    """Return the square root of x.
 
172
 
 
173
    Parameters
 
174
    ----------
 
175
    x : array_like
 
176
 
 
177
    Returns
 
178
    -------
 
179
    array_like output.
 
180
 
 
181
    Examples
 
182
    --------
 
183
 
 
184
    For real, non-negative inputs this works just like numpy.sqrt():
 
185
    >>> sqrt(1)
 
186
    1.0
 
187
 
 
188
    >>> sqrt([1,4])
 
189
    array([ 1.,  2.])
 
190
 
 
191
    But it automatically handles negative inputs:
 
192
    >>> sqrt(-1)
 
193
    (0.0+1.0j)
 
194
 
 
195
    >>> sqrt([-1,4])
 
196
    array([ 0.+1.j,  2.+0.j])
 
197
    """
47
198
    x = _fix_real_lt_zero(x)
48
199
    return nx.sqrt(x)
49
200
 
50
201
def log(x):
 
202
    """Return the natural logarithm of x.
 
203
 
 
204
    If x contains negative inputs, the answer is computed and returned in the
 
205
    complex domain.
 
206
 
 
207
    Parameters
 
208
    ----------
 
209
    x : array_like
 
210
 
 
211
    Returns
 
212
    -------
 
213
    array_like
 
214
 
 
215
    Examples
 
216
    --------
 
217
    >>> import math
 
218
 
 
219
    >>> log(math.exp(1))
 
220
    1.0
 
221
 
 
222
    Negative arguments are correctly handled (recall that for negative
 
223
    arguments, the identity exp(log(z))==z does not hold anymore):
 
224
 
 
225
    >>> log(-math.exp(1)) == (1+1j*math.pi)
 
226
    True
 
227
    """
51
228
    x = _fix_real_lt_zero(x)
52
229
    return nx.log(x)
53
230
 
54
231
def log10(x):
 
232
    """Return the base 10 logarithm of x.
 
233
 
 
234
    If x contains negative inputs, the answer is computed and returned in the
 
235
    complex domain.
 
236
 
 
237
    Parameters
 
238
    ----------
 
239
    x : array_like
 
240
 
 
241
    Returns
 
242
    -------
 
243
    array_like
 
244
 
 
245
    Examples
 
246
    --------
 
247
 
 
248
    (We set the printing precision so the example can be auto-tested)
 
249
    >>> import numpy as np; np.set_printoptions(precision=4)
 
250
 
 
251
    >>> log10([10**1,10**2])
 
252
    array([ 1.,  2.])
 
253
 
 
254
 
 
255
    >>> log10([-10**1,-10**2,10**2])
 
256
    array([ 1.+1.3644j,  2.+1.3644j,  2.+0.j    ])
 
257
    """
55
258
    x = _fix_real_lt_zero(x)
56
259
    return nx.log10(x)
57
260
 
58
261
def logn(n, x):
59
 
    """ Take log base n of x.
 
262
    """Take log base n of x.
 
263
 
 
264
    If x contains negative inputs, the answer is computed and returned in the
 
265
    complex domain.
 
266
 
 
267
    Parameters
 
268
    ----------
 
269
    x : array_like
 
270
 
 
271
    Returns
 
272
    -------
 
273
    array_like
 
274
 
 
275
    Examples
 
276
    --------
 
277
 
 
278
    (We set the printing precision so the example can be auto-tested)
 
279
    >>> import numpy as np; np.set_printoptions(precision=4)
 
280
 
 
281
    >>> logn(2,[4,8])
 
282
    array([ 2.,  3.])
 
283
 
 
284
    >>> logn(2,[-4,-8,8])
 
285
    array([ 2.+4.5324j,  3.+4.5324j,  3.+0.j    ])
60
286
    """
61
287
    x = _fix_real_lt_zero(x)
62
288
    n = _fix_real_lt_zero(n)
64
290
 
65
291
def log2(x):
66
292
    """ Take log base 2 of x.
 
293
 
 
294
    If x contains negative inputs, the answer is computed and returned in the
 
295
    complex domain.
 
296
 
 
297
    Parameters
 
298
    ----------
 
299
    x : array_like
 
300
 
 
301
    Returns
 
302
    -------
 
303
    array_like
 
304
 
 
305
    Examples
 
306
    --------
 
307
 
 
308
    (We set the printing precision so the example can be auto-tested)
 
309
    >>> import numpy as np; np.set_printoptions(precision=4)
 
310
 
 
311
    >>> log2([4,8])
 
312
    array([ 2.,  3.])
 
313
 
 
314
    >>> log2([-4,-8,8])
 
315
    array([ 2.+4.5324j,  3.+4.5324j,  3.+0.j    ])
67
316
    """
68
317
    x = _fix_real_lt_zero(x)
69
318
    return nx.log(x)/_ln2
70
319
 
71
320
def power(x, p):
 
321
    """Return x**p.
 
322
 
 
323
    If x contains negative values, it is converted to the complex domain.
 
324
 
 
325
    If p contains negative values, it is converted to floating point.
 
326
 
 
327
    Parameters
 
328
    ----------
 
329
    x : array_like
 
330
    p : array_like of integers
 
331
 
 
332
    Returns
 
333
    -------
 
334
    array_like
 
335
 
 
336
    Examples
 
337
    --------
 
338
    (We set the printing precision so the example can be auto-tested)
 
339
    >>> import numpy as np; np.set_printoptions(precision=4)
 
340
 
 
341
    >>> power([2,4],2)
 
342
    array([ 4, 16])
 
343
 
 
344
    >>> power([2,4],-2)
 
345
    array([ 0.25  ,  0.0625])
 
346
 
 
347
    >>> power([-2,4],2)
 
348
    array([  4.+0.j,  16.+0.j])
 
349
    """
72
350
    x = _fix_real_lt_zero(x)
73
351
    p = _fix_int_lt_zero(p)
74
352
    return nx.power(x, p)
75
353
 
76
354
def arccos(x):
 
355
    """Compute the inverse cosine of x.
 
356
 
 
357
    For real x with abs(x)<=1, this returns the principal value.
 
358
 
 
359
    If abs(x)>1, the complex arccos() is computed.
 
360
 
 
361
    Parameters
 
362
    ----------
 
363
    x : array_like
 
364
 
 
365
    Returns
 
366
    -------
 
367
    array_like
 
368
 
 
369
    Examples
 
370
    --------
 
371
    >>> import numpy as np; np.set_printoptions(precision=4)
 
372
 
 
373
    >>> arccos(1)
 
374
    0.0
 
375
 
 
376
    >>> arccos([1,2])
 
377
    array([ 0.-0.j   ,  0.+1.317j])
 
378
    """
77
379
    x = _fix_real_abs_gt_1(x)
78
380
    return nx.arccos(x)
79
381
 
80
382
def arcsin(x):
 
383
    """Compute the inverse sine of x.
 
384
 
 
385
    For real x with abs(x)<=1, this returns the principal value.
 
386
 
 
387
    If abs(x)>1, the complex arcsin() is computed.
 
388
 
 
389
    Parameters
 
390
    ----------
 
391
    x : array_like
 
392
 
 
393
    Returns
 
394
    -------
 
395
    array_like
 
396
 
 
397
    Examples
 
398
    --------
 
399
    (We set the printing precision so the example can be auto-tested)
 
400
    >>> import numpy as np; np.set_printoptions(precision=4)
 
401
 
 
402
    >>> arcsin(0)
 
403
    0.0
 
404
 
 
405
    >>> arcsin([0,1])
 
406
    array([ 0.    ,  1.5708])
 
407
    """
81
408
    x = _fix_real_abs_gt_1(x)
82
409
    return nx.arcsin(x)
83
410
 
84
411
def arctanh(x):
 
412
    """Compute the inverse hyperbolic tangent of x.
 
413
 
 
414
    For real x with abs(x)<=1, this returns the principal value.
 
415
 
 
416
    If abs(x)>1, the complex arctanh() is computed.
 
417
 
 
418
    Parameters
 
419
    ----------
 
420
    x : array_like
 
421
 
 
422
    Returns
 
423
    -------
 
424
    array_like
 
425
 
 
426
    Examples
 
427
    --------
 
428
    (We set the printing precision so the example can be auto-tested)
 
429
    >>> import numpy as np; np.set_printoptions(precision=4)
 
430
 
 
431
    >>> arctanh(0)
 
432
    0.0
 
433
 
 
434
    >>> arctanh([0,2])
 
435
    array([ 0.0000+0.j    ,  0.5493-1.5708j])
 
436
    """
85
437
    x = _fix_real_abs_gt_1(x)
86
438
    return nx.arctanh(x)