예제 #1
0
    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
예제 #2
0
    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)
예제 #3
0
    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))
예제 #4
0
    def element(val, jacob):
        '''
        Creates an Element object with the value given and jacobian matrix 

        INPUTS
        =======
        val: The numeric value of the function
        jacob: The jacobian matrix value of the function at which to evaluate

        RETURNS
        =======
        Element objects
        '''
        return Element(val, jacob)
예제 #5
0
    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)
예제 #6
0
    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)
예제 #7
0
    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)
예제 #8
0
    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
예제 #9
0
    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)