Chris@87
|
1 """
|
Chris@87
|
2 Wrapper functions to more user-friendly calling of certain math functions
|
Chris@87
|
3 whose output data-type is different than the input data-type in certain
|
Chris@87
|
4 domains of the input.
|
Chris@87
|
5
|
Chris@87
|
6 For example, for functions like `log` with branch cuts, the versions in this
|
Chris@87
|
7 module provide the mathematically valid answers in the complex plane::
|
Chris@87
|
8
|
Chris@87
|
9 >>> import math
|
Chris@87
|
10 >>> from numpy.lib import scimath
|
Chris@87
|
11 >>> scimath.log(-math.exp(1)) == (1+1j*math.pi)
|
Chris@87
|
12 True
|
Chris@87
|
13
|
Chris@87
|
14 Similarly, `sqrt`, other base logarithms, `power` and trig functions are
|
Chris@87
|
15 correctly handled. See their respective docstrings for specific examples.
|
Chris@87
|
16
|
Chris@87
|
17 """
|
Chris@87
|
18 from __future__ import division, absolute_import, print_function
|
Chris@87
|
19
|
Chris@87
|
20 import numpy.core.numeric as nx
|
Chris@87
|
21 import numpy.core.numerictypes as nt
|
Chris@87
|
22 from numpy.core.numeric import asarray, any
|
Chris@87
|
23 from numpy.lib.type_check import isreal
|
Chris@87
|
24
|
Chris@87
|
25
|
Chris@87
|
26 __all__ = [
|
Chris@87
|
27 'sqrt', 'log', 'log2', 'logn', 'log10', 'power', 'arccos', 'arcsin',
|
Chris@87
|
28 'arctanh'
|
Chris@87
|
29 ]
|
Chris@87
|
30
|
Chris@87
|
31
|
Chris@87
|
32 _ln2 = nx.log(2.0)
|
Chris@87
|
33
|
Chris@87
|
34
|
Chris@87
|
35 def _tocomplex(arr):
|
Chris@87
|
36 """Convert its input `arr` to a complex array.
|
Chris@87
|
37
|
Chris@87
|
38 The input is returned as a complex array of the smallest type that will fit
|
Chris@87
|
39 the original data: types like single, byte, short, etc. become csingle,
|
Chris@87
|
40 while others become cdouble.
|
Chris@87
|
41
|
Chris@87
|
42 A copy of the input is always made.
|
Chris@87
|
43
|
Chris@87
|
44 Parameters
|
Chris@87
|
45 ----------
|
Chris@87
|
46 arr : array
|
Chris@87
|
47
|
Chris@87
|
48 Returns
|
Chris@87
|
49 -------
|
Chris@87
|
50 array
|
Chris@87
|
51 An array with the same input data as the input but in complex form.
|
Chris@87
|
52
|
Chris@87
|
53 Examples
|
Chris@87
|
54 --------
|
Chris@87
|
55
|
Chris@87
|
56 First, consider an input of type short:
|
Chris@87
|
57
|
Chris@87
|
58 >>> a = np.array([1,2,3],np.short)
|
Chris@87
|
59
|
Chris@87
|
60 >>> ac = np.lib.scimath._tocomplex(a); ac
|
Chris@87
|
61 array([ 1.+0.j, 2.+0.j, 3.+0.j], dtype=complex64)
|
Chris@87
|
62
|
Chris@87
|
63 >>> ac.dtype
|
Chris@87
|
64 dtype('complex64')
|
Chris@87
|
65
|
Chris@87
|
66 If the input is of type double, the output is correspondingly of the
|
Chris@87
|
67 complex double type as well:
|
Chris@87
|
68
|
Chris@87
|
69 >>> b = np.array([1,2,3],np.double)
|
Chris@87
|
70
|
Chris@87
|
71 >>> bc = np.lib.scimath._tocomplex(b); bc
|
Chris@87
|
72 array([ 1.+0.j, 2.+0.j, 3.+0.j])
|
Chris@87
|
73
|
Chris@87
|
74 >>> bc.dtype
|
Chris@87
|
75 dtype('complex128')
|
Chris@87
|
76
|
Chris@87
|
77 Note that even if the input was complex to begin with, a copy is still
|
Chris@87
|
78 made, since the astype() method always copies:
|
Chris@87
|
79
|
Chris@87
|
80 >>> c = np.array([1,2,3],np.csingle)
|
Chris@87
|
81
|
Chris@87
|
82 >>> cc = np.lib.scimath._tocomplex(c); cc
|
Chris@87
|
83 array([ 1.+0.j, 2.+0.j, 3.+0.j], dtype=complex64)
|
Chris@87
|
84
|
Chris@87
|
85 >>> c *= 2; c
|
Chris@87
|
86 array([ 2.+0.j, 4.+0.j, 6.+0.j], dtype=complex64)
|
Chris@87
|
87
|
Chris@87
|
88 >>> cc
|
Chris@87
|
89 array([ 1.+0.j, 2.+0.j, 3.+0.j], dtype=complex64)
|
Chris@87
|
90 """
|
Chris@87
|
91 if issubclass(arr.dtype.type, (nt.single, nt.byte, nt.short, nt.ubyte,
|
Chris@87
|
92 nt.ushort, nt.csingle)):
|
Chris@87
|
93 return arr.astype(nt.csingle)
|
Chris@87
|
94 else:
|
Chris@87
|
95 return arr.astype(nt.cdouble)
|
Chris@87
|
96
|
Chris@87
|
97 def _fix_real_lt_zero(x):
|
Chris@87
|
98 """Convert `x` to complex if it has real, negative components.
|
Chris@87
|
99
|
Chris@87
|
100 Otherwise, output is just the array version of the input (via asarray).
|
Chris@87
|
101
|
Chris@87
|
102 Parameters
|
Chris@87
|
103 ----------
|
Chris@87
|
104 x : array_like
|
Chris@87
|
105
|
Chris@87
|
106 Returns
|
Chris@87
|
107 -------
|
Chris@87
|
108 array
|
Chris@87
|
109
|
Chris@87
|
110 Examples
|
Chris@87
|
111 --------
|
Chris@87
|
112 >>> np.lib.scimath._fix_real_lt_zero([1,2])
|
Chris@87
|
113 array([1, 2])
|
Chris@87
|
114
|
Chris@87
|
115 >>> np.lib.scimath._fix_real_lt_zero([-1,2])
|
Chris@87
|
116 array([-1.+0.j, 2.+0.j])
|
Chris@87
|
117
|
Chris@87
|
118 """
|
Chris@87
|
119 x = asarray(x)
|
Chris@87
|
120 if any(isreal(x) & (x < 0)):
|
Chris@87
|
121 x = _tocomplex(x)
|
Chris@87
|
122 return x
|
Chris@87
|
123
|
Chris@87
|
124 def _fix_int_lt_zero(x):
|
Chris@87
|
125 """Convert `x` to double if it has real, negative components.
|
Chris@87
|
126
|
Chris@87
|
127 Otherwise, output is just the array version of the input (via asarray).
|
Chris@87
|
128
|
Chris@87
|
129 Parameters
|
Chris@87
|
130 ----------
|
Chris@87
|
131 x : array_like
|
Chris@87
|
132
|
Chris@87
|
133 Returns
|
Chris@87
|
134 -------
|
Chris@87
|
135 array
|
Chris@87
|
136
|
Chris@87
|
137 Examples
|
Chris@87
|
138 --------
|
Chris@87
|
139 >>> np.lib.scimath._fix_int_lt_zero([1,2])
|
Chris@87
|
140 array([1, 2])
|
Chris@87
|
141
|
Chris@87
|
142 >>> np.lib.scimath._fix_int_lt_zero([-1,2])
|
Chris@87
|
143 array([-1., 2.])
|
Chris@87
|
144 """
|
Chris@87
|
145 x = asarray(x)
|
Chris@87
|
146 if any(isreal(x) & (x < 0)):
|
Chris@87
|
147 x = x * 1.0
|
Chris@87
|
148 return x
|
Chris@87
|
149
|
Chris@87
|
150 def _fix_real_abs_gt_1(x):
|
Chris@87
|
151 """Convert `x` to complex if it has real components x_i with abs(x_i)>1.
|
Chris@87
|
152
|
Chris@87
|
153 Otherwise, output is just the array version of the input (via asarray).
|
Chris@87
|
154
|
Chris@87
|
155 Parameters
|
Chris@87
|
156 ----------
|
Chris@87
|
157 x : array_like
|
Chris@87
|
158
|
Chris@87
|
159 Returns
|
Chris@87
|
160 -------
|
Chris@87
|
161 array
|
Chris@87
|
162
|
Chris@87
|
163 Examples
|
Chris@87
|
164 --------
|
Chris@87
|
165 >>> np.lib.scimath._fix_real_abs_gt_1([0,1])
|
Chris@87
|
166 array([0, 1])
|
Chris@87
|
167
|
Chris@87
|
168 >>> np.lib.scimath._fix_real_abs_gt_1([0,2])
|
Chris@87
|
169 array([ 0.+0.j, 2.+0.j])
|
Chris@87
|
170 """
|
Chris@87
|
171 x = asarray(x)
|
Chris@87
|
172 if any(isreal(x) & (abs(x) > 1)):
|
Chris@87
|
173 x = _tocomplex(x)
|
Chris@87
|
174 return x
|
Chris@87
|
175
|
Chris@87
|
176 def sqrt(x):
|
Chris@87
|
177 """
|
Chris@87
|
178 Compute the square root of x.
|
Chris@87
|
179
|
Chris@87
|
180 For negative input elements, a complex value is returned
|
Chris@87
|
181 (unlike `numpy.sqrt` which returns NaN).
|
Chris@87
|
182
|
Chris@87
|
183 Parameters
|
Chris@87
|
184 ----------
|
Chris@87
|
185 x : array_like
|
Chris@87
|
186 The input value(s).
|
Chris@87
|
187
|
Chris@87
|
188 Returns
|
Chris@87
|
189 -------
|
Chris@87
|
190 out : ndarray or scalar
|
Chris@87
|
191 The square root of `x`. If `x` was a scalar, so is `out`,
|
Chris@87
|
192 otherwise an array is returned.
|
Chris@87
|
193
|
Chris@87
|
194 See Also
|
Chris@87
|
195 --------
|
Chris@87
|
196 numpy.sqrt
|
Chris@87
|
197
|
Chris@87
|
198 Examples
|
Chris@87
|
199 --------
|
Chris@87
|
200 For real, non-negative inputs this works just like `numpy.sqrt`:
|
Chris@87
|
201
|
Chris@87
|
202 >>> np.lib.scimath.sqrt(1)
|
Chris@87
|
203 1.0
|
Chris@87
|
204 >>> np.lib.scimath.sqrt([1, 4])
|
Chris@87
|
205 array([ 1., 2.])
|
Chris@87
|
206
|
Chris@87
|
207 But it automatically handles negative inputs:
|
Chris@87
|
208
|
Chris@87
|
209 >>> np.lib.scimath.sqrt(-1)
|
Chris@87
|
210 (0.0+1.0j)
|
Chris@87
|
211 >>> np.lib.scimath.sqrt([-1,4])
|
Chris@87
|
212 array([ 0.+1.j, 2.+0.j])
|
Chris@87
|
213
|
Chris@87
|
214 """
|
Chris@87
|
215 x = _fix_real_lt_zero(x)
|
Chris@87
|
216 return nx.sqrt(x)
|
Chris@87
|
217
|
Chris@87
|
218 def log(x):
|
Chris@87
|
219 """
|
Chris@87
|
220 Compute the natural logarithm of `x`.
|
Chris@87
|
221
|
Chris@87
|
222 Return the "principal value" (for a description of this, see `numpy.log`)
|
Chris@87
|
223 of :math:`log_e(x)`. For real `x > 0`, this is a real number (``log(0)``
|
Chris@87
|
224 returns ``-inf`` and ``log(np.inf)`` returns ``inf``). Otherwise, the
|
Chris@87
|
225 complex principle value is returned.
|
Chris@87
|
226
|
Chris@87
|
227 Parameters
|
Chris@87
|
228 ----------
|
Chris@87
|
229 x : array_like
|
Chris@87
|
230 The value(s) whose log is (are) required.
|
Chris@87
|
231
|
Chris@87
|
232 Returns
|
Chris@87
|
233 -------
|
Chris@87
|
234 out : ndarray or scalar
|
Chris@87
|
235 The log of the `x` value(s). If `x` was a scalar, so is `out`,
|
Chris@87
|
236 otherwise an array is returned.
|
Chris@87
|
237
|
Chris@87
|
238 See Also
|
Chris@87
|
239 --------
|
Chris@87
|
240 numpy.log
|
Chris@87
|
241
|
Chris@87
|
242 Notes
|
Chris@87
|
243 -----
|
Chris@87
|
244 For a log() that returns ``NAN`` when real `x < 0`, use `numpy.log`
|
Chris@87
|
245 (note, however, that otherwise `numpy.log` and this `log` are identical,
|
Chris@87
|
246 i.e., both return ``-inf`` for `x = 0`, ``inf`` for `x = inf`, and,
|
Chris@87
|
247 notably, the complex principle value if ``x.imag != 0``).
|
Chris@87
|
248
|
Chris@87
|
249 Examples
|
Chris@87
|
250 --------
|
Chris@87
|
251 >>> np.emath.log(np.exp(1))
|
Chris@87
|
252 1.0
|
Chris@87
|
253
|
Chris@87
|
254 Negative arguments are handled "correctly" (recall that
|
Chris@87
|
255 ``exp(log(x)) == x`` does *not* hold for real ``x < 0``):
|
Chris@87
|
256
|
Chris@87
|
257 >>> np.emath.log(-np.exp(1)) == (1 + np.pi * 1j)
|
Chris@87
|
258 True
|
Chris@87
|
259
|
Chris@87
|
260 """
|
Chris@87
|
261 x = _fix_real_lt_zero(x)
|
Chris@87
|
262 return nx.log(x)
|
Chris@87
|
263
|
Chris@87
|
264 def log10(x):
|
Chris@87
|
265 """
|
Chris@87
|
266 Compute the logarithm base 10 of `x`.
|
Chris@87
|
267
|
Chris@87
|
268 Return the "principal value" (for a description of this, see
|
Chris@87
|
269 `numpy.log10`) of :math:`log_{10}(x)`. For real `x > 0`, this
|
Chris@87
|
270 is a real number (``log10(0)`` returns ``-inf`` and ``log10(np.inf)``
|
Chris@87
|
271 returns ``inf``). Otherwise, the complex principle value is returned.
|
Chris@87
|
272
|
Chris@87
|
273 Parameters
|
Chris@87
|
274 ----------
|
Chris@87
|
275 x : array_like or scalar
|
Chris@87
|
276 The value(s) whose log base 10 is (are) required.
|
Chris@87
|
277
|
Chris@87
|
278 Returns
|
Chris@87
|
279 -------
|
Chris@87
|
280 out : ndarray or scalar
|
Chris@87
|
281 The log base 10 of the `x` value(s). If `x` was a scalar, so is `out`,
|
Chris@87
|
282 otherwise an array object is returned.
|
Chris@87
|
283
|
Chris@87
|
284 See Also
|
Chris@87
|
285 --------
|
Chris@87
|
286 numpy.log10
|
Chris@87
|
287
|
Chris@87
|
288 Notes
|
Chris@87
|
289 -----
|
Chris@87
|
290 For a log10() that returns ``NAN`` when real `x < 0`, use `numpy.log10`
|
Chris@87
|
291 (note, however, that otherwise `numpy.log10` and this `log10` are
|
Chris@87
|
292 identical, i.e., both return ``-inf`` for `x = 0`, ``inf`` for `x = inf`,
|
Chris@87
|
293 and, notably, the complex principle value if ``x.imag != 0``).
|
Chris@87
|
294
|
Chris@87
|
295 Examples
|
Chris@87
|
296 --------
|
Chris@87
|
297
|
Chris@87
|
298 (We set the printing precision so the example can be auto-tested)
|
Chris@87
|
299
|
Chris@87
|
300 >>> np.set_printoptions(precision=4)
|
Chris@87
|
301
|
Chris@87
|
302 >>> np.emath.log10(10**1)
|
Chris@87
|
303 1.0
|
Chris@87
|
304
|
Chris@87
|
305 >>> np.emath.log10([-10**1, -10**2, 10**2])
|
Chris@87
|
306 array([ 1.+1.3644j, 2.+1.3644j, 2.+0.j ])
|
Chris@87
|
307
|
Chris@87
|
308 """
|
Chris@87
|
309 x = _fix_real_lt_zero(x)
|
Chris@87
|
310 return nx.log10(x)
|
Chris@87
|
311
|
Chris@87
|
312 def logn(n, x):
|
Chris@87
|
313 """
|
Chris@87
|
314 Take log base n of x.
|
Chris@87
|
315
|
Chris@87
|
316 If `x` contains negative inputs, the answer is computed and returned in the
|
Chris@87
|
317 complex domain.
|
Chris@87
|
318
|
Chris@87
|
319 Parameters
|
Chris@87
|
320 ----------
|
Chris@87
|
321 n : int
|
Chris@87
|
322 The base in which the log is taken.
|
Chris@87
|
323 x : array_like
|
Chris@87
|
324 The value(s) whose log base `n` is (are) required.
|
Chris@87
|
325
|
Chris@87
|
326 Returns
|
Chris@87
|
327 -------
|
Chris@87
|
328 out : ndarray or scalar
|
Chris@87
|
329 The log base `n` of the `x` value(s). If `x` was a scalar, so is
|
Chris@87
|
330 `out`, otherwise an array is returned.
|
Chris@87
|
331
|
Chris@87
|
332 Examples
|
Chris@87
|
333 --------
|
Chris@87
|
334 >>> np.set_printoptions(precision=4)
|
Chris@87
|
335
|
Chris@87
|
336 >>> np.lib.scimath.logn(2, [4, 8])
|
Chris@87
|
337 array([ 2., 3.])
|
Chris@87
|
338 >>> np.lib.scimath.logn(2, [-4, -8, 8])
|
Chris@87
|
339 array([ 2.+4.5324j, 3.+4.5324j, 3.+0.j ])
|
Chris@87
|
340
|
Chris@87
|
341 """
|
Chris@87
|
342 x = _fix_real_lt_zero(x)
|
Chris@87
|
343 n = _fix_real_lt_zero(n)
|
Chris@87
|
344 return nx.log(x)/nx.log(n)
|
Chris@87
|
345
|
Chris@87
|
346 def log2(x):
|
Chris@87
|
347 """
|
Chris@87
|
348 Compute the logarithm base 2 of `x`.
|
Chris@87
|
349
|
Chris@87
|
350 Return the "principal value" (for a description of this, see
|
Chris@87
|
351 `numpy.log2`) of :math:`log_2(x)`. For real `x > 0`, this is
|
Chris@87
|
352 a real number (``log2(0)`` returns ``-inf`` and ``log2(np.inf)`` returns
|
Chris@87
|
353 ``inf``). Otherwise, the complex principle value is returned.
|
Chris@87
|
354
|
Chris@87
|
355 Parameters
|
Chris@87
|
356 ----------
|
Chris@87
|
357 x : array_like
|
Chris@87
|
358 The value(s) whose log base 2 is (are) required.
|
Chris@87
|
359
|
Chris@87
|
360 Returns
|
Chris@87
|
361 -------
|
Chris@87
|
362 out : ndarray or scalar
|
Chris@87
|
363 The log base 2 of the `x` value(s). If `x` was a scalar, so is `out`,
|
Chris@87
|
364 otherwise an array is returned.
|
Chris@87
|
365
|
Chris@87
|
366 See Also
|
Chris@87
|
367 --------
|
Chris@87
|
368 numpy.log2
|
Chris@87
|
369
|
Chris@87
|
370 Notes
|
Chris@87
|
371 -----
|
Chris@87
|
372 For a log2() that returns ``NAN`` when real `x < 0`, use `numpy.log2`
|
Chris@87
|
373 (note, however, that otherwise `numpy.log2` and this `log2` are
|
Chris@87
|
374 identical, i.e., both return ``-inf`` for `x = 0`, ``inf`` for `x = inf`,
|
Chris@87
|
375 and, notably, the complex principle value if ``x.imag != 0``).
|
Chris@87
|
376
|
Chris@87
|
377 Examples
|
Chris@87
|
378 --------
|
Chris@87
|
379 We set the printing precision so the example can be auto-tested:
|
Chris@87
|
380
|
Chris@87
|
381 >>> np.set_printoptions(precision=4)
|
Chris@87
|
382
|
Chris@87
|
383 >>> np.emath.log2(8)
|
Chris@87
|
384 3.0
|
Chris@87
|
385 >>> np.emath.log2([-4, -8, 8])
|
Chris@87
|
386 array([ 2.+4.5324j, 3.+4.5324j, 3.+0.j ])
|
Chris@87
|
387
|
Chris@87
|
388 """
|
Chris@87
|
389 x = _fix_real_lt_zero(x)
|
Chris@87
|
390 return nx.log2(x)
|
Chris@87
|
391
|
Chris@87
|
392 def power(x, p):
|
Chris@87
|
393 """
|
Chris@87
|
394 Return x to the power p, (x**p).
|
Chris@87
|
395
|
Chris@87
|
396 If `x` contains negative values, the output is converted to the
|
Chris@87
|
397 complex domain.
|
Chris@87
|
398
|
Chris@87
|
399 Parameters
|
Chris@87
|
400 ----------
|
Chris@87
|
401 x : array_like
|
Chris@87
|
402 The input value(s).
|
Chris@87
|
403 p : array_like of ints
|
Chris@87
|
404 The power(s) to which `x` is raised. If `x` contains multiple values,
|
Chris@87
|
405 `p` has to either be a scalar, or contain the same number of values
|
Chris@87
|
406 as `x`. In the latter case, the result is
|
Chris@87
|
407 ``x[0]**p[0], x[1]**p[1], ...``.
|
Chris@87
|
408
|
Chris@87
|
409 Returns
|
Chris@87
|
410 -------
|
Chris@87
|
411 out : ndarray or scalar
|
Chris@87
|
412 The result of ``x**p``. If `x` and `p` are scalars, so is `out`,
|
Chris@87
|
413 otherwise an array is returned.
|
Chris@87
|
414
|
Chris@87
|
415 See Also
|
Chris@87
|
416 --------
|
Chris@87
|
417 numpy.power
|
Chris@87
|
418
|
Chris@87
|
419 Examples
|
Chris@87
|
420 --------
|
Chris@87
|
421 >>> np.set_printoptions(precision=4)
|
Chris@87
|
422
|
Chris@87
|
423 >>> np.lib.scimath.power([2, 4], 2)
|
Chris@87
|
424 array([ 4, 16])
|
Chris@87
|
425 >>> np.lib.scimath.power([2, 4], -2)
|
Chris@87
|
426 array([ 0.25 , 0.0625])
|
Chris@87
|
427 >>> np.lib.scimath.power([-2, 4], 2)
|
Chris@87
|
428 array([ 4.+0.j, 16.+0.j])
|
Chris@87
|
429
|
Chris@87
|
430 """
|
Chris@87
|
431 x = _fix_real_lt_zero(x)
|
Chris@87
|
432 p = _fix_int_lt_zero(p)
|
Chris@87
|
433 return nx.power(x, p)
|
Chris@87
|
434
|
Chris@87
|
435 def arccos(x):
|
Chris@87
|
436 """
|
Chris@87
|
437 Compute the inverse cosine of x.
|
Chris@87
|
438
|
Chris@87
|
439 Return the "principal value" (for a description of this, see
|
Chris@87
|
440 `numpy.arccos`) of the inverse cosine of `x`. For real `x` such that
|
Chris@87
|
441 `abs(x) <= 1`, this is a real number in the closed interval
|
Chris@87
|
442 :math:`[0, \\pi]`. Otherwise, the complex principle value is returned.
|
Chris@87
|
443
|
Chris@87
|
444 Parameters
|
Chris@87
|
445 ----------
|
Chris@87
|
446 x : array_like or scalar
|
Chris@87
|
447 The value(s) whose arccos is (are) required.
|
Chris@87
|
448
|
Chris@87
|
449 Returns
|
Chris@87
|
450 -------
|
Chris@87
|
451 out : ndarray or scalar
|
Chris@87
|
452 The inverse cosine(s) of the `x` value(s). If `x` was a scalar, so
|
Chris@87
|
453 is `out`, otherwise an array object is returned.
|
Chris@87
|
454
|
Chris@87
|
455 See Also
|
Chris@87
|
456 --------
|
Chris@87
|
457 numpy.arccos
|
Chris@87
|
458
|
Chris@87
|
459 Notes
|
Chris@87
|
460 -----
|
Chris@87
|
461 For an arccos() that returns ``NAN`` when real `x` is not in the
|
Chris@87
|
462 interval ``[-1,1]``, use `numpy.arccos`.
|
Chris@87
|
463
|
Chris@87
|
464 Examples
|
Chris@87
|
465 --------
|
Chris@87
|
466 >>> np.set_printoptions(precision=4)
|
Chris@87
|
467
|
Chris@87
|
468 >>> np.emath.arccos(1) # a scalar is returned
|
Chris@87
|
469 0.0
|
Chris@87
|
470
|
Chris@87
|
471 >>> np.emath.arccos([1,2])
|
Chris@87
|
472 array([ 0.-0.j , 0.+1.317j])
|
Chris@87
|
473
|
Chris@87
|
474 """
|
Chris@87
|
475 x = _fix_real_abs_gt_1(x)
|
Chris@87
|
476 return nx.arccos(x)
|
Chris@87
|
477
|
Chris@87
|
478 def arcsin(x):
|
Chris@87
|
479 """
|
Chris@87
|
480 Compute the inverse sine of x.
|
Chris@87
|
481
|
Chris@87
|
482 Return the "principal value" (for a description of this, see
|
Chris@87
|
483 `numpy.arcsin`) of the inverse sine of `x`. For real `x` such that
|
Chris@87
|
484 `abs(x) <= 1`, this is a real number in the closed interval
|
Chris@87
|
485 :math:`[-\\pi/2, \\pi/2]`. Otherwise, the complex principle value is
|
Chris@87
|
486 returned.
|
Chris@87
|
487
|
Chris@87
|
488 Parameters
|
Chris@87
|
489 ----------
|
Chris@87
|
490 x : array_like or scalar
|
Chris@87
|
491 The value(s) whose arcsin is (are) required.
|
Chris@87
|
492
|
Chris@87
|
493 Returns
|
Chris@87
|
494 -------
|
Chris@87
|
495 out : ndarray or scalar
|
Chris@87
|
496 The inverse sine(s) of the `x` value(s). If `x` was a scalar, so
|
Chris@87
|
497 is `out`, otherwise an array object is returned.
|
Chris@87
|
498
|
Chris@87
|
499 See Also
|
Chris@87
|
500 --------
|
Chris@87
|
501 numpy.arcsin
|
Chris@87
|
502
|
Chris@87
|
503 Notes
|
Chris@87
|
504 -----
|
Chris@87
|
505 For an arcsin() that returns ``NAN`` when real `x` is not in the
|
Chris@87
|
506 interval ``[-1,1]``, use `numpy.arcsin`.
|
Chris@87
|
507
|
Chris@87
|
508 Examples
|
Chris@87
|
509 --------
|
Chris@87
|
510 >>> np.set_printoptions(precision=4)
|
Chris@87
|
511
|
Chris@87
|
512 >>> np.emath.arcsin(0)
|
Chris@87
|
513 0.0
|
Chris@87
|
514
|
Chris@87
|
515 >>> np.emath.arcsin([0,1])
|
Chris@87
|
516 array([ 0. , 1.5708])
|
Chris@87
|
517
|
Chris@87
|
518 """
|
Chris@87
|
519 x = _fix_real_abs_gt_1(x)
|
Chris@87
|
520 return nx.arcsin(x)
|
Chris@87
|
521
|
Chris@87
|
522 def arctanh(x):
|
Chris@87
|
523 """
|
Chris@87
|
524 Compute the inverse hyperbolic tangent of `x`.
|
Chris@87
|
525
|
Chris@87
|
526 Return the "principal value" (for a description of this, see
|
Chris@87
|
527 `numpy.arctanh`) of `arctanh(x)`. For real `x` such that
|
Chris@87
|
528 `abs(x) < 1`, this is a real number. If `abs(x) > 1`, or if `x` is
|
Chris@87
|
529 complex, the result is complex. Finally, `x = 1` returns``inf`` and
|
Chris@87
|
530 `x=-1` returns ``-inf``.
|
Chris@87
|
531
|
Chris@87
|
532 Parameters
|
Chris@87
|
533 ----------
|
Chris@87
|
534 x : array_like
|
Chris@87
|
535 The value(s) whose arctanh is (are) required.
|
Chris@87
|
536
|
Chris@87
|
537 Returns
|
Chris@87
|
538 -------
|
Chris@87
|
539 out : ndarray or scalar
|
Chris@87
|
540 The inverse hyperbolic tangent(s) of the `x` value(s). If `x` was
|
Chris@87
|
541 a scalar so is `out`, otherwise an array is returned.
|
Chris@87
|
542
|
Chris@87
|
543
|
Chris@87
|
544 See Also
|
Chris@87
|
545 --------
|
Chris@87
|
546 numpy.arctanh
|
Chris@87
|
547
|
Chris@87
|
548 Notes
|
Chris@87
|
549 -----
|
Chris@87
|
550 For an arctanh() that returns ``NAN`` when real `x` is not in the
|
Chris@87
|
551 interval ``(-1,1)``, use `numpy.arctanh` (this latter, however, does
|
Chris@87
|
552 return +/-inf for `x = +/-1`).
|
Chris@87
|
553
|
Chris@87
|
554 Examples
|
Chris@87
|
555 --------
|
Chris@87
|
556 >>> np.set_printoptions(precision=4)
|
Chris@87
|
557
|
Chris@87
|
558 >>> np.emath.arctanh(np.matrix(np.eye(2)))
|
Chris@87
|
559 array([[ Inf, 0.],
|
Chris@87
|
560 [ 0., Inf]])
|
Chris@87
|
561 >>> np.emath.arctanh([1j])
|
Chris@87
|
562 array([ 0.+0.7854j])
|
Chris@87
|
563
|
Chris@87
|
564 """
|
Chris@87
|
565 x = _fix_real_abs_gt_1(x)
|
Chris@87
|
566 return nx.arctanh(x)
|