12
23
from numpy.core.numeric import asarray, any
13
24
from numpy.lib.type_check import isreal
16
#__all__.extend([key for key in dir(nx.umath)
17
# if key[0] != '_' and key not in __all__])
21
28
def _tocomplex(arr):
22
if isinstance(arr.dtype, (nt.single, nt.byte, nt.short, nt.ubyte,
29
"""Convert its input `arr` to a complex array.
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.
35
A copy of the input is always made.
44
An array with the same input data as the input but in complex form.
49
>>> import numpy as np
51
First, consider an input of type short:
53
>>> a = np.array([1,2,3],np.short)
55
>>> ac = _tocomplex(a); ac
56
array([ 1.+0.j, 2.+0.j, 3.+0.j], dtype=complex64)
61
If the input is of type double, the output is correspondingly of the
62
complex double type as well:
64
>>> b = np.array([1,2,3],np.double)
66
>>> bc = _tocomplex(b); bc
67
array([ 1.+0.j, 2.+0.j, 3.+0.j])
72
Note that even if the input was complex to begin with, a copy is still
73
made, since the astype() method always copies:
75
>>> c = np.array([1,2,3],np.csingle)
77
>>> cc = _tocomplex(c); cc
78
array([ 1.+0.j, 2.+0.j, 3.+0.j], dtype=complex64)
81
array([ 2.+0.j, 4.+0.j, 6.+0.j], dtype=complex64)
84
array([ 1.+0.j, 2.+0.j, 3.+0.j], dtype=complex64)
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)
26
90
return arr.astype(nt.cdouble)
28
92
def _fix_real_lt_zero(x):
93
"""Convert `x` to complex if it has real, negative components.
95
Otherwise, output is just the array version of the input (via asarray).
107
>>> _fix_real_lt_zero([1,2])
110
>>> _fix_real_lt_zero([-1,2])
111
array([-1.+0.j, 2.+0.j])
30
114
if any(isreal(x) & (x<0)):
34
118
def _fix_int_lt_zero(x):
119
"""Convert `x` to double if it has real, negative components.
121
Otherwise, output is just the array version of the input (via asarray).
133
>>> _fix_int_lt_zero([1,2])
136
>>> _fix_int_lt_zero([-1,2])
36
140
if any(isreal(x) & (x < 0)):
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.
147
Otherwise, output is just the array version of the input (via asarray).
159
>>> _fix_real_abs_gt_1([0,1])
162
>>> _fix_real_abs_gt_1([0,2])
163
array([ 0.+0.j, 2.+0.j])
42
166
if any(isreal(x) & (abs(x)>1)):
171
"""Return the square root of x.
184
For real, non-negative inputs this works just like numpy.sqrt():
191
But it automatically handles negative inputs:
196
array([ 0.+1.j, 2.+0.j])
47
198
x = _fix_real_lt_zero(x)
202
"""Return the natural logarithm of x.
204
If x contains negative inputs, the answer is computed and returned in the
222
Negative arguments are correctly handled (recall that for negative
223
arguments, the identity exp(log(z))==z does not hold anymore):
225
>>> log(-math.exp(1)) == (1+1j*math.pi)
51
228
x = _fix_real_lt_zero(x)
232
"""Return the base 10 logarithm of x.
234
If x contains negative inputs, the answer is computed and returned in the
248
(We set the printing precision so the example can be auto-tested)
249
>>> import numpy as np; np.set_printoptions(precision=4)
251
>>> log10([10**1,10**2])
255
>>> log10([-10**1,-10**2,10**2])
256
array([ 1.+1.3644j, 2.+1.3644j, 2.+0.j ])
55
258
x = _fix_real_lt_zero(x)
56
259
return nx.log10(x)
59
""" Take log base n of x.
262
"""Take log base n of x.
264
If x contains negative inputs, the answer is computed and returned in the
278
(We set the printing precision so the example can be auto-tested)
279
>>> import numpy as np; np.set_printoptions(precision=4)
284
>>> logn(2,[-4,-8,8])
285
array([ 2.+4.5324j, 3.+4.5324j, 3.+0.j ])
61
287
x = _fix_real_lt_zero(x)
62
288
n = _fix_real_lt_zero(n)
66
292
""" Take log base 2 of x.
294
If x contains negative inputs, the answer is computed and returned in the
308
(We set the printing precision so the example can be auto-tested)
309
>>> import numpy as np; np.set_printoptions(precision=4)
315
array([ 2.+4.5324j, 3.+4.5324j, 3.+0.j ])
68
317
x = _fix_real_lt_zero(x)
69
318
return nx.log(x)/_ln2
323
If x contains negative values, it is converted to the complex domain.
325
If p contains negative values, it is converted to floating point.
330
p : array_like of integers
338
(We set the printing precision so the example can be auto-tested)
339
>>> import numpy as np; np.set_printoptions(precision=4)
345
array([ 0.25 , 0.0625])
348
array([ 4.+0.j, 16.+0.j])
72
350
x = _fix_real_lt_zero(x)
73
351
p = _fix_int_lt_zero(p)
74
352
return nx.power(x, p)
355
"""Compute the inverse cosine of x.
357
For real x with abs(x)<=1, this returns the principal value.
359
If abs(x)>1, the complex arccos() is computed.
371
>>> import numpy as np; np.set_printoptions(precision=4)
377
array([ 0.-0.j , 0.+1.317j])
77
379
x = _fix_real_abs_gt_1(x)
78
380
return nx.arccos(x)
383
"""Compute the inverse sine of x.
385
For real x with abs(x)<=1, this returns the principal value.
387
If abs(x)>1, the complex arcsin() is computed.
399
(We set the printing precision so the example can be auto-tested)
400
>>> import numpy as np; np.set_printoptions(precision=4)
406
array([ 0. , 1.5708])
81
408
x = _fix_real_abs_gt_1(x)
82
409
return nx.arcsin(x)
412
"""Compute the inverse hyperbolic tangent of x.
414
For real x with abs(x)<=1, this returns the principal value.
416
If abs(x)>1, the complex arctanh() is computed.
428
(We set the printing precision so the example can be auto-tested)
429
>>> import numpy as np; np.set_printoptions(precision=4)
435
array([ 0.0000+0.j , 0.5493-1.5708j])
85
437
x = _fix_real_abs_gt_1(x)
86
438
return nx.arctanh(x)