def log(x, base=None): """ With one argument, return the natural logarithm of x (to base e). With two arguments, return the logarithm of x to the given base, calculated as ``log(x)/log(base)``. """ if base is None: return log(x, base=e) if isinstance(x,ADF): ad_funcs = list(map(to_auto_diff,[x])) x = ad_funcs[0].x ######################################## # Nominal value of the constructed ADF: f = log(x, base) ######################################## variables = ad_funcs[0]._get_variables(ad_funcs) if not variables or isinstance(f, bool): return f ######################################## # Calculation of the derivatives with respect to the arguments # of f (ad_funcs): lc_wrt_args = [1./(x * ln(base))] qc_wrt_args = [-1./(x**2 * ln(base))] cp_wrt_args = 0.0 ######################################## # Calculation of the derivative of f with respect to all the # variables (Variable) involved. lc_wrt_vars,qc_wrt_vars,cp_wrt_vars = _apply_chain_rule( ad_funcs,variables,lc_wrt_args,qc_wrt_args, cp_wrt_args) # The function now returns an ADF object: return ADF(f, lc_wrt_vars, qc_wrt_vars, cp_wrt_vars) else: # try: # pythonic: fails gracefully when x is not an array-like object # return [log(xi) for xi in x] # except TypeError: if x.imag: return cmath.log(x, base) else: return math.log(x.real, base)
def log(x, base=None): """ With one argument, return the natural logarithm of x (to base e). With two arguments, return the logarithm of x to the given base, calculated as ``log(x)/log(base)``. """ if base is None: return log(x, base=e) if isinstance(x, ADF): ad_funcs = list(map(to_auto_diff, [x])) x = ad_funcs[0].x ######################################## # Nominal value of the constructed ADF: f = log(x, base) ######################################## variables = ad_funcs[0]._get_variables(ad_funcs) if not variables or isinstance(f, bool): return f ######################################## # Calculation of the derivatives with respect to the arguments # of f (ad_funcs): lc_wrt_args = [1. / (x * ln(base))] qc_wrt_args = [-1. / (x**2 * ln(base))] cp_wrt_args = 0.0 ######################################## # Calculation of the derivative of f with respect to all the # variables (Variable) involved. lc_wrt_vars, qc_wrt_vars, cp_wrt_vars = _apply_chain_rule( ad_funcs, variables, lc_wrt_args, qc_wrt_args, cp_wrt_args) # The function now returns an ADF object: return ADF(f, lc_wrt_vars, qc_wrt_vars, cp_wrt_vars) else: # try: # pythonic: fails gracefully when x is not an array-like object # return [log(xi) for xi in x] # except TypeError: if x.imag: return cmath.log(x, base) else: return math.log(x.real, base)
def expm1(x): """ Return e**x - 1. For small floats x, the subtraction in exp(x) - 1 can result in a significant loss of precision; the expm1() function provides a way to compute this quantity to full precision:: >>> exp(1e-5) - 1 # gives result accurate to 11 places 1.0000050000069649e-05 >>> expm1(1e-5) # result accurate to full precision 1.0000050000166668e-05 """ if isinstance(x,ADF): ad_funcs = list(map(to_auto_diff,[x])) x = ad_funcs[0].x ######################################## # Nominal value of the constructed ADF: f = expm1(x) ######################################## variables = ad_funcs[0]._get_variables(ad_funcs) if not variables or isinstance(f, bool): return f ######################################## # Calculation of the derivatives with respect to the arguments # of f (ad_funcs): lc_wrt_args = [exp(x)] qc_wrt_args = [exp(x)] cp_wrt_args = 0.0 ######################################## # Calculation of the derivative of f with respect to all the # variables (Variable) involved. lc_wrt_vars,qc_wrt_vars,cp_wrt_vars = _apply_chain_rule( ad_funcs,variables,lc_wrt_args,qc_wrt_args, cp_wrt_args) # The function now returns an ADF object: return ADF(f, lc_wrt_vars, qc_wrt_vars, cp_wrt_vars) else: # try: # pythonic: fails gracefully when x is not an array-like object # return [expm1(xi) for xi in x] # except TypeError: return math.expm1(x)
def expm1(x): """ Return e**x - 1. For small floats x, the subtraction in exp(x) - 1 can result in a significant loss of precision; the expm1() function provides a way to compute this quantity to full precision:: >>> exp(1e-5) - 1 # gives result accurate to 11 places 1.0000050000069649e-05 >>> expm1(1e-5) # result accurate to full precision 1.0000050000166668e-05 """ if isinstance(x, ADF): ad_funcs = list(map(to_auto_diff, [x])) x = ad_funcs[0].x ######################################## # Nominal value of the constructed ADF: f = expm1(x) ######################################## variables = ad_funcs[0]._get_variables(ad_funcs) if not variables or isinstance(f, bool): return f ######################################## # Calculation of the derivatives with respect to the arguments # of f (ad_funcs): lc_wrt_args = [exp(x)] qc_wrt_args = [exp(x)] cp_wrt_args = 0.0 ######################################## # Calculation of the derivative of f with respect to all the # variables (Variable) involved. lc_wrt_vars, qc_wrt_vars, cp_wrt_vars = _apply_chain_rule( ad_funcs, variables, lc_wrt_args, qc_wrt_args, cp_wrt_args) # The function now returns an ADF object: return ADF(f, lc_wrt_vars, qc_wrt_vars, cp_wrt_vars) else: # try: # pythonic: fails gracefully when x is not an array-like object # return [expm1(xi) for xi in x] # except TypeError: return math.expm1(x)
def fabs(x): """ Return the absolute value of x. """ if isinstance(x,ADF): ad_funcs = list(map(to_auto_diff,[x])) x = ad_funcs[0].x ######################################## # Nominal value of the constructed ADF: f = fabs(x) ######################################## variables = ad_funcs[0]._get_variables(ad_funcs) if not variables or isinstance(f, bool): return f ######################################## # Calculation of the derivatives with respect to the arguments # of f (ad_funcs): # catch the x=0 exception try: lc_wrt_args = [x/fabs(x)] qc_wrt_args = [1/fabs(x)-(x**2)/fabs(x)**3] except ZeroDivisionError: lc_wrt_args = [0.0] qc_wrt_args = [0.0] cp_wrt_args = 0.0 ######################################## # Calculation of the derivative of f with respect to all the # variables (Variable) involved. lc_wrt_vars, qc_wrt_vars, cp_wrt_vars = _apply_chain_rule( ad_funcs, variables, lc_wrt_args, qc_wrt_args, cp_wrt_args) # The function now returns an ADF object: return ADF(f, lc_wrt_vars, qc_wrt_vars, cp_wrt_vars) else: # try: # pythonic: fails gracefully when x is not an array-like object # return [fabs(xi) for xi in x] # except TypeError: return math.fabs(x)
def log10(x): """ Return the base-10 logarithm of x. This is usually more accurate than ``log(x, 10)``. """ if isinstance(x,ADF): ad_funcs = list(map(to_auto_diff,[x])) x = ad_funcs[0].x ######################################## # Nominal value of the constructed ADF: f = log10(x) ######################################## variables = ad_funcs[0]._get_variables(ad_funcs) if not variables or isinstance(f, bool): return f ######################################## # Calculation of the derivatives with respect to the arguments # of f (ad_funcs): lc_wrt_args = [1/x/log(10)] qc_wrt_args = [-1./x**2/log(10)] cp_wrt_args = 0.0 ######################################## # Calculation of the derivative of f with respect to all the # variables (Variable) involved. lc_wrt_vars,qc_wrt_vars,cp_wrt_vars = _apply_chain_rule( ad_funcs,variables,lc_wrt_args,qc_wrt_args, cp_wrt_args) # The function now returns an ADF object: return ADF(f, lc_wrt_vars, qc_wrt_vars, cp_wrt_vars) else: # try: # pythonic: fails gracefully when x is not an array-like object # return [log10(xi) for xi in x] # except TypeError: if x.imag: return cmath.log10(x) else: return math.log10(x.real)
def log10(x): """ Return the base-10 logarithm of x. This is usually more accurate than ``log(x, 10)``. """ if isinstance(x, ADF): ad_funcs = list(map(to_auto_diff, [x])) x = ad_funcs[0].x ######################################## # Nominal value of the constructed ADF: f = log10(x) ######################################## variables = ad_funcs[0]._get_variables(ad_funcs) if not variables or isinstance(f, bool): return f ######################################## # Calculation of the derivatives with respect to the arguments # of f (ad_funcs): lc_wrt_args = [1 / x / log(10)] qc_wrt_args = [-1. / x**2 / log(10)] cp_wrt_args = 0.0 ######################################## # Calculation of the derivative of f with respect to all the # variables (Variable) involved. lc_wrt_vars, qc_wrt_vars, cp_wrt_vars = _apply_chain_rule( ad_funcs, variables, lc_wrt_args, qc_wrt_args, cp_wrt_args) # The function now returns an ADF object: return ADF(f, lc_wrt_vars, qc_wrt_vars, cp_wrt_vars) else: # try: # pythonic: fails gracefully when x is not an array-like object # return [log10(xi) for xi in x] # except TypeError: if x.imag: return cmath.log10(x) else: return math.log10(x.real)
def acos(x): """ Return the arc cosine of x, in radians. """ if isinstance(x,ADF): ad_funcs = list(map(to_auto_diff,[x])) x = ad_funcs[0].x ######################################## # Nominal value of the constructed ADF: f = acos(x) ######################################## variables = ad_funcs[0]._get_variables(ad_funcs) if not variables or isinstance(f, bool): return f ######################################## # Calculation of the derivatives with respect to the arguments # of f (ad_funcs): lc_wrt_args = [-1/sqrt(1-x**2)] qc_wrt_args = [x/(sqrt(1 - x**2)*(x**2 - 1))] cp_wrt_args = 0.0 ######################################## # Calculation of the derivative of f with respect to all the # variables (Variable) involved. lc_wrt_vars,qc_wrt_vars,cp_wrt_vars = _apply_chain_rule( ad_funcs,variables,lc_wrt_args,qc_wrt_args, cp_wrt_args) # The function now returns an ADF object: return ADF(f, lc_wrt_vars, qc_wrt_vars, cp_wrt_vars) else: # try: # pythonic: fails gracefully when x is not an array-like object # return [acos(xi) for xi in x] # except TypeError: if x.imag: return cmath.acos(x) else: return math.acos(x.real)
def fabs(x): """ Return the absolute value of x. """ if isinstance(x, ADF): ad_funcs = list(map(to_auto_diff, [x])) x = ad_funcs[0].x ######################################## # Nominal value of the constructed ADF: f = fabs(x) ######################################## variables = ad_funcs[0]._get_variables(ad_funcs) if not variables or isinstance(f, bool): return f ######################################## # Calculation of the derivatives with respect to the arguments # of f (ad_funcs): # catch the x=0 exception try: lc_wrt_args = [x / fabs(x)] qc_wrt_args = [1 / fabs(x) - (x**2) / fabs(x)**3] except ZeroDivisionError: lc_wrt_args = [0.0] qc_wrt_args = [0.0] cp_wrt_args = 0.0 ######################################## # Calculation of the derivative of f with respect to all the # variables (Variable) involved. lc_wrt_vars, qc_wrt_vars, cp_wrt_vars = _apply_chain_rule( ad_funcs, variables, lc_wrt_args, qc_wrt_args, cp_wrt_args) # The function now returns an ADF object: return ADF(f, lc_wrt_vars, qc_wrt_vars, cp_wrt_vars) else: # try: # pythonic: fails gracefully when x is not an array-like object # return [fabs(xi) for xi in x] # except TypeError: return math.fabs(x)
def cosh(x): """ Return the hyperbolic cosine of x. """ if isinstance(x, ADF): ad_funcs = list(map(to_auto_diff, [x])) x = ad_funcs[0].x ######################################## # Nominal value of the constructed ADF: f = cosh(x) ######################################## variables = ad_funcs[0]._get_variables(ad_funcs) if not variables or isinstance(f, bool): return f ######################################## # Calculation of the derivatives with respect to the arguments # of f (ad_funcs): lc_wrt_args = [sinh(x)] qc_wrt_args = [cosh(x)] cp_wrt_args = 0.0 ######################################## # Calculation of the derivative of f with respect to all the # variables (Variable) involved. lc_wrt_vars, qc_wrt_vars, cp_wrt_vars = _apply_chain_rule( ad_funcs, variables, lc_wrt_args, qc_wrt_args, cp_wrt_args) # The function now returns an ADF object: return ADF(f, lc_wrt_vars, qc_wrt_vars, cp_wrt_vars) else: # try: # pythonic: fails gracefully when x is not an array-like object # return [cosh(xi) for xi in x] # except TypeError: if x.imag: return cmath.cosh(x) else: return math.cosh(x.real)
def floor(x): """ Return the floor of x as a float, the largest integer value less than or equal to x. """ if isinstance(x,ADF): ad_funcs = list(map(to_auto_diff,[x])) x = ad_funcs[0].x ######################################## # Nominal value of the constructed ADF: f = floor(x) ######################################## variables = ad_funcs[0]._get_variables(ad_funcs) if not variables or isinstance(f, bool): return f ######################################## # Calculation of the derivatives with respect to the arguments # of f (ad_funcs): lc_wrt_args = [0.0] qc_wrt_args = [0.0] cp_wrt_args = 0.0 ######################################## # Calculation of the derivative of f with respect to all the # variables (Variable) involved. lc_wrt_vars,qc_wrt_vars,cp_wrt_vars = _apply_chain_rule( ad_funcs,variables,lc_wrt_args,qc_wrt_args, cp_wrt_args) # The function now returns an ADF object: return ADF(f, lc_wrt_vars, qc_wrt_vars, cp_wrt_vars) else: # try: # pythonic: fails gracefully when x is not an array-like object # return [floor(xi) for xi in x] # except TypeError: return math.floor(x)
def trunc(x): """ Return the **Real** value x truncated to an **Integral** (usually a long integer). Uses the ``__trunc__`` method. """ if isinstance(x,ADF): ad_funcs = list(map(to_auto_diff,[x])) x = ad_funcs[0].x ######################################## # Nominal value of the constructed ADF: f = trunc(x) ######################################## variables = ad_funcs[0]._get_variables(ad_funcs) if not variables or isinstance(f, bool): return f ######################################## # Calculation of the derivatives with respect to the arguments # of f (ad_funcs): lc_wrt_args = [0.0] qc_wrt_args = [0.0] cp_wrt_args = 0.0 ######################################## # Calculation of the derivative of f with respect to all the # variables (Variable) involved. lc_wrt_vars,qc_wrt_vars,cp_wrt_vars = _apply_chain_rule( ad_funcs,variables,lc_wrt_args,qc_wrt_args, cp_wrt_args) # The function now returns an ADF object: return ADF(f, lc_wrt_vars, qc_wrt_vars, cp_wrt_vars) else: try: # pythonic: fails gracefully when x is not an array-like object return [trunc(xi) for xi in x] except TypeError: return math.trunc(x)
def erf(x): """ Return the error function at x. """ if isinstance(x,ADF): ad_funcs = list(map(to_auto_diff,[x])) x = ad_funcs[0].x ######################################## # Nominal value of the constructed ADF: f = erf(x) ######################################## variables = ad_funcs[0]._get_variables(ad_funcs) if not variables or isinstance(f, bool): return f ######################################## # Calculation of the derivatives with respect to the arguments # of f (ad_funcs): lc_wrt_args = [2*exp(-x**2)/sqrt(pi)] qc_wrt_args = [-4*x*exp(-x**2)/sqrt(pi)] cp_wrt_args = 0.0 ######################################## # Calculation of the derivative of f with respect to all the # variables (Variable) involved. lc_wrt_vars,qc_wrt_vars,cp_wrt_vars = _apply_chain_rule( ad_funcs,variables,lc_wrt_args,qc_wrt_args, cp_wrt_args) # The function now returns an ADF object: return ADF(f, lc_wrt_vars, qc_wrt_vars, cp_wrt_vars) else: # try: # pythonic: fails gracefully when x is not an array-like object # return [erf(xi) for xi in x] # except TypeError: return math.erf(x)
def floor(x): """ Return the floor of x as a float, the largest integer value less than or equal to x. """ if isinstance(x, ADF): ad_funcs = list(map(to_auto_diff, [x])) x = ad_funcs[0].x ######################################## # Nominal value of the constructed ADF: f = floor(x) ######################################## variables = ad_funcs[0]._get_variables(ad_funcs) if not variables or isinstance(f, bool): return f ######################################## # Calculation of the derivatives with respect to the arguments # of f (ad_funcs): lc_wrt_args = [0.0] qc_wrt_args = [0.0] cp_wrt_args = 0.0 ######################################## # Calculation of the derivative of f with respect to all the # variables (Variable) involved. lc_wrt_vars, qc_wrt_vars, cp_wrt_vars = _apply_chain_rule( ad_funcs, variables, lc_wrt_args, qc_wrt_args, cp_wrt_args) # The function now returns an ADF object: return ADF(f, lc_wrt_vars, qc_wrt_vars, cp_wrt_vars) else: # try: # pythonic: fails gracefully when x is not an array-like object # return [floor(xi) for xi in x] # except TypeError: return math.floor(x)
def trunc(x): """ Return the **Real** value x truncated to an **Integral** (usually a long integer). Uses the ``__trunc__`` method. """ if isinstance(x, ADF): ad_funcs = list(map(to_auto_diff, [x])) x = ad_funcs[0].x ######################################## # Nominal value of the constructed ADF: f = trunc(x) ######################################## variables = ad_funcs[0]._get_variables(ad_funcs) if not variables or isinstance(f, bool): return f ######################################## # Calculation of the derivatives with respect to the arguments # of f (ad_funcs): lc_wrt_args = [0.0] qc_wrt_args = [0.0] cp_wrt_args = 0.0 ######################################## # Calculation of the derivative of f with respect to all the # variables (Variable) involved. lc_wrt_vars, qc_wrt_vars, cp_wrt_vars = _apply_chain_rule( ad_funcs, variables, lc_wrt_args, qc_wrt_args, cp_wrt_args) # The function now returns an ADF object: return ADF(f, lc_wrt_vars, qc_wrt_vars, cp_wrt_vars) else: try: # pythonic: fails gracefully when x is not an array-like object return [trunc(xi) for xi in x] except TypeError: return math.trunc(x)