calcfunctions.py 1.49 KB
Newer Older
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
"""
Provide the mathematical functions that numpy doesn't.

Specifically, the secant/cosecant/cotangents and their inverses and
hyperbolic counterparts
"""
import numpy


# Normal Trig
def sec(arg):
    """
    Secant
    """
    return 1 / numpy.cos(arg)


def csc(arg):
    """
    Cosecant
    """
    return 1 / numpy.sin(arg)


def cot(arg):
    """
    Cotangent
    """
    return 1 / numpy.tan(arg)


# Inverse Trig
# http://en.wikipedia.org/wiki/Inverse_trigonometric_functions#Relationships_among_the_inverse_trigonometric_functions
def arcsec(val):
    """
    Inverse secant
    """
    return numpy.arccos(1. / val)


def arccsc(val):
    """
    Inverse cosecant
    """
    return numpy.arcsin(1. / val)


def arccot(val):
    """
    Inverse cotangent
    """
    if numpy.real(val) < 0:
        return -numpy.pi / 2 - numpy.arctan(val)
    else:
        return numpy.pi / 2 - numpy.arctan(val)


# Hyperbolic Trig
def sech(arg):
    """
    Hyperbolic secant
    """
    return 1 / numpy.cosh(arg)


def csch(arg):
    """
    Hyperbolic cosecant
    """
    return 1 / numpy.sinh(arg)


def coth(arg):
    """
    Hyperbolic cotangent
    """
    return 1 / numpy.tanh(arg)


# And their inverses
def arcsech(val):
    """
    Inverse hyperbolic secant
    """
    return numpy.arccosh(1. / val)


def arccsch(val):
    """
    Inverse hyperbolic cosecant
    """
    return numpy.arcsinh(1. / val)


def arccoth(val):
    """
    Inverse hyperbolic cotangent
    """
    return numpy.arctanh(1. / val)