summaryrefslogtreecommitdiff
path: root/scipy/base/scimath.py
blob: 18d75b2df10f317a16e4299947c50fe9b1da382a (plain)
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
## Automatically adapted for scipy Sep 19, 2005 by convertcode.py

"""
Wrapper functions to more user-friendly calling of certain math functions
whose output is different than the input in certain domains of the input.
"""

__all__ = ['sqrt', 'log', 'log2','logn','log10', 'power', 'arccos',
           'arcsin', 'arctanh']

import numeric as _nx
from numeric import *

from type_check import isreal

__all__.extend([key for key in dir(_nx.umath) \
                if key[0]!='_' and key not in __all__])

_ln2 = log(2.0)

def _tocomplex(arr):
    if arr.dtypechar in ['f', 'h', 'B', 'b','H']:
        return arr.astype('F')
    else:
        return arr.astype('D')

def _fix_real_lt_zero(x):
    x = asarray(x)
    if any(isreal(x) & (x<0)):
        x = _tocomplex(x)
    return asscalar(x)

def _fix_real_abs_gt_1(x):
    x = asarray(x)
    if any(isreal(x) & (abs(x)>1)):
        x = _tocomplex(x)
    return x
    
def sqrt(x):
    x = _fix_real_lt_zero(x)
    return sqrt(x)

def log(x):
    x = _fix_real_lt_zero(x)
    return log(x)

def log10(x):
    x = _fix_real_lt_zero(x)
    return log10(x)    

def logn(n,x):
    """ Take log base n of x.
    """
    x = _fix_real_lt_zero(x)
    n = _fix_real_lt_zero(n)
    return log(x)/log(n)

def log2(x):
    """ Take log base 2 of x.
    """
    x = _fix_real_lt_zero(x)
    return log(x)/_ln2

def power(x, p):
    x = _fix_real_lt_zero(x)
    return power(x, p)


def arccos(x):
    x = _fix_real_abs_gt_1(x)
    return arccos(x)

def arcsin(x):
    x = _fix_real_abs_gt_1(x)
    return arcsin(x)

def arctanh(x):
    x = _fix_real_abs_gt_1(x)
    return arctanh(x)