def sin(x): ''' Returns the sine of a given constant, Scalar, or Element object INPUTS ======= x: Numeric constant, Scalar object or Element object RETURNS ======= sine of numeric constant, Scalar object or Element object NOTES ======= If x is a constant, this method returns the constant sin(x). If x is a Scalar object, this method returns a new Scalar with the appropriate functions performed on the Scalar's value and derivative. If x is a Element object, this method returns a new Element with the appropriate functions performed on the Element's value and Jacobian matrix. ''' try: return Element(np.sin(x._val), np.cos(x._val) * x._jacob) except AttributeError: # If contant try: # If scalar variable return Scalar(np.sin(x._val), x._der * np.cos(x._val)) except AttributeError: # If contant return np.sin(x)
def logistic(x): ''' Returns the logistic of a given constant or scalar or Element object INPUTS ======= x: Numeric constant or Scalar object or Element object RETURNS ======= logistic of numeric constant or Scalar object or Element object NOTES ======= If x is a constant, this method returns the constant logistic(x). If x is a Scalar object, this method returns a new Scalar with the appropriate functions performed on the Scalar's value and derivative. If x is a Element object, this method returns a new Element with the appropriate functions performed on the Element's value and Jacobian matrix. ''' try: return Element(1 / (1 + np.exp(-x._val)), x._jacob * (x._val**2 * np.exp(-x._val))) except AttributeError: try: return Scalar(1 / (1 + np.exp(-x._val)), x._der * (x._val**2 * np.exp(-x._val))) except AttributeError: #if constant return 1 / (1 + np.exp(-x))
def exp_(a, x): ''' Returns the exponential of a given constant or scalar or Element object with base a INPUTS ======= x: Numeric constant or Scalar object or Element object a: Numeric constant RETURNS ======= exponential of numeric constant or Scalar object or Element object NOTES ======= If x is a constant, this method returns the constant sin(x). If x is a Scalar object, this method returns a new Scalar with the appropriate functions performed on the Scalar's value and derivative. If x is a Element object, this method returns a new Element with the appropriate functions performed on the Element's value and Jacobian matrix. ''' try: return Element(a**x._val, x._jacob * np.log(a) * (a**x._val)) except AttributeError: try: # If scalar variable return Scalar(a**x._val, x._der * np.log(a) * (a**x._val)) except AttributeError: # If contant return a**x
def scalar(val): ''' Creates a Scalar object with the value given and derivative 1 INPUTS ======= val: The numeric value at which to evaluate RETURNS ======= Scalar objects ''' return Scalar(val, 1)
def log(*args): ''' Returns the log of a given constant or scalar or Element object INPUTS ======= if the lenth of input is 2: a: Numeric constant x: Numeric constant or Scalar object or Element object if the lenth of input is 1: x: Numeric constant or Scalar object or Element object RETURNS ======= log of numeric constant or Scalar object or Element object NOTES ======= If x is a constant, this method returns the constant sin(x). If x is a Scalar object, this method returns a new Scalar with the appropriate functions performed on the Scalar's value and derivative. If x is a Element object, this method returns a new Element with the appropriate functions performed on the Element's value and Jacobian matrix. ''' if len(args) == 2: return Operator.log_(args[0], args[1]) else: x = args[0] try: j = x._jacob if x._val <= 0: raise ValueError('out of domain') else: return Element(np.log(x._val), x._jacob / x._val) except AttributeError: try: # If scalar variable if x._val <= 0: raise ValueError('out of domain') else: return Scalar(np.log(x._val), x._der / x._val) except AttributeError: # If contant if x <= 0: raise ValueError('out of domain') else: return np.log(x)
def log_(a, x): ''' Returns the log of a given constant or scalar or Element object with base a INPUTS ======= x: Numeric constant or Scalar object or Element object a: Numeric constant RETURNS ======= log of numeric constant or Scalar object or Element object NOTES ======= If x is a constant, this method returns the constant sin(x). If x is a Scalar object, this method returns a new Scalar with the appropriate functions performed on the Scalar's value and derivative. If x is a Element object, this method returns a new Element with the appropriate functions performed on the Element's value and Jacobian matrix. ''' try: j = x._jacob if x._val <= 0: raise ValueError('out of domain') else: return Element(math.log(x._val, a), x._jacob / (x._val * np.log(a))) except AttributeError: try: # If scalar variable if x._val <= 0: raise ValueError('out of domain') else: return Scalar(math.log(x._val, a), x._der / (x._val * np.log(a))) except AttributeError: # If contant if x <= 0: raise ValueError('out of domain') else: return math.log(x, a)
def arccosh(x): ''' Returns the arccosh of a given constant or scalar or Element object INPUTS ======= x: Numeric constant or Scalar object or Element object RETURNS ======= arccosh of numeric constant or Scalar object or Element object NOTES ======= If x is a constant, this method returns the constant sin(x). If x is a Scalar object, this method returns a new Scalar with the appropriate functions performed on the Scalar's value and derivative. If x is a Element object, this method returns a new Element with the appropriate functions performed on the Element's value and Jacobian matrix. ''' try: j = x._jacob if x._val < 1: raise ValueError('out of domain') else: return Element( np.arccosh(x._val), x._jacob * (-np.arccosh(x._val) * np.tanh(x._val))) except AttributeError: try: # if scalar variable if x._val < 1: raise ValueError('out of domain') else: return Scalar( np.arccosh(x._val), x._der * (-np.arccosh(x._val) * np.tanh(x._val))) except AttributeError: #if constant if x < 1: raise ValueError('out of domain') else: return np.arccosh(x)
def square_root(x): ''' Returns the square root of a given constant or scalar or Element object INPUTS ======= x: Numeric constant or Scalar object or Element object RETURNS ======= square root of numeric constant or Scalar object or Element object NOTES ======= If x is a constant, this method returns the constant square root(x). If x is a Scalar object, this method returns a new Scalar with the appropriate functions performed on the Scalar's value and derivative. If x is a Element object, this method returns a new Element with the appropriate functions performed on the Element's value and Jacobian matrix. ''' try: j = x._jacob if x._val <= 0: raise ValueError('out of domain') else: return Element(x._val**0.5, x._jacob * (x._val**(-1 / 2) / 2)) except AttributeError: try: if x._val <= 0: raise ValueError('out of domain') else: return Scalar(x._val**0.5, x._der * (x._val**(-1 / 2) / 2)) except AttributeError: #if constant if x <= 0: raise ValueError('out of domain') else: return x**0.5
def exp(*args): ''' Returns the exponential of a given constant or scalar or Element object INPUTS ======= if the lenth of input is 2: The first is a and the second is x. a: Numeric constant x: Numeric constant or Scalar object or Element object if the lenth of input is 1: x: Numeric constant or Scalar object or Element object RETURNS ======= exponential of numeric constant or Scalar object or Element object NOTES ======= If x is a constant, this method returns the constant sin(x). If x is a Scalar object, this method returns a new Scalar with the appropriate functions performed on the Scalar's value and derivative. If x is a Element object, this method returns a new Element with the appropriate functions performed on the Element's value and Jacobian matrix. ''' if len(args) == 2: return Operator.exp_(args[0], args[1]) else: x = args[0] try: return Element(np.exp(x._val), x._jacob * (np.exp(x._val))) except AttributeError: try: # If scalar variable return Scalar(np.exp(x._val), x._der * np.exp(x._val)) except AttributeError: # If contant return np.exp(x)
#import sys #sys.path.append('../../') import numpy as np from VorDiff.nodes.scalar import Scalar #from VorDiff.autodiff import AutoDiff as ad # Define scalar object and initialize parameters a = 2.0 b = 4.0 c = 1.0 d = 3.0 x = Scalar(a) f = a * x + b g = c * x + d # Define functions of the scalar objects f_1 = f + g f_2 = g + f f_3 = f - g f_4 = g - f f_5 = f * g f_6 = g * f f_7 = f / g f_8 = g / f f_9 = f / 2 f_10 = 2 / f