def simplify(self):
        left_arg = self.left_arg.simplify()
        right_arg = self.right_arg.simplify()

        left_val = None
        right_val = None

        if isinstance(left_arg,
                      constexpr.ConstantExpression) and not left_arg.is_e():
            left_val = left_arg.get_value()

        if isinstance(right_arg,
                      constexpr.ConstantExpression) and not right_arg.is_e():
            right_val = right_arg.get_value()

        if left_val == 0:
            return constexpr.ConstantExpression("0")

        if (left_val is not None and right_val is not None and right_val != 0
                and left_val % right_val == 0):
            return constexpr.ConstantExpression(str(left_val // right_val))

        if right_val == 1: return left_arg

        if left_arg == right_arg: return constexpr.ConstantExpression("1")
        return DivisionExpression(left_arg, right_arg)
Ejemplo n.º 2
0
    def simplify(self):
        arg = self.arg.simplify()

        if isinstance(arg, constexpr.ConstantExpression) and not arg.is_e():
            val = arg.get_value()
            if val == 0: return constexpr.ConstantExpression("0")
            return constexpr.ConstantExpression(str(-1 * val))

        if isinstance(arg, NegativeExpression): return arg.arg
        return NegativeExpression(arg)
 def const_divider_derivative(self):
     rule = "d/dx cf(x) = c d/dx f(x)"
     child_derivative = self.left_arg.get_derivative()
     derivative = multiexpr.MultiplicationExpression(
         DivisionExpression(constexpr.ConstantExpression("1"),
                            self.right_arg), child_derivative.result)
     rule_application = multiexpr.MultiplicationExpression(
         DivisionExpression(constexpr.ConstantExpression("1"),
                            self.right_arg),
         derivexpr.DerivativeExpression(self.left_arg))
     return Derivative(self, derivative, rule_application, [rule],
                       [child_derivative])
Ejemplo n.º 4
0
 def simplify(self):
   left_arg = self.left_arg.simplify()
   right_arg = self.right_arg.simplify()
   
   if isinstance(right_arg, constexpr.ConstantExpression) and not right_arg.is_e():
     right_val = right_arg.get_value()
     if (right_val == 0): return constexpr.ConstantExpression("1")
     if (right_val == 1): return left_arg
   return PowerExpression(left_arg, right_arg)
Ejemplo n.º 5
0
  def get_derivative(self):
    if self.is_constant(): return utils.constant_derivative(self)

    sqrt_rule = "d/dx sqrt(x) = 1/(2*sqrt(x))"

    derivative = divexpr.DivisionExpression(
      constexpr.ConstantExpression("1"),
      multiexpr.MultiplicationExpression(constexpr.ConstantExpression("2"), self)
    )

    if self.arg == varexpr.VariableExpression():
      return Derivative(
        self,
        derivative,
        None,
        [sqrt_rule],
        None
      )
    
    return utils.apply_chain_rule(self, sqrt_rule, derivative)
Ejemplo n.º 6
0
    def get_derivative(self):
        if self.is_constant(): return utils.constant_derivative(self)

        ln_rule = "d/dx ln(x) = 1/x"

        derivative = divexpr.DivisionExpression(
            constexpr.ConstantExpression("1"), self.arg)

        if self.arg == varexpr.VariableExpression():
            return Derivative(self, derivative, None, [ln_rule], None)

        return utils.apply_chain_rule(self, ln_rule, derivative)
Ejemplo n.º 7
0
    def get_derivative(self):
        if self.is_constant(): return utils.constant_derivative(self)

        tan_rule = "d/dx tan(x) = sec^2(x)"

        derivative = powexpr.PowerExpression(secexpr.SecExpression(self.arg),
                                             constexpr.ConstantExpression("2"))

        if self.arg == varexpr.VariableExpression():
            return Derivative(self, derivative, None, [tan_rule], None)

        return utils.apply_chain_rule(self, tan_rule, derivative)
  def simplify(self):
    left_arg = self.left_arg.simplify()
    right_arg = self.right_arg.simplify()

    left_val = None
    right_val = None

    if isinstance(left_arg, constexpr.ConstantExpression) and not left_arg.is_e():
      left_val = left_arg.get_value()
    
    if isinstance(right_arg, constexpr.ConstantExpression) and not right_arg.is_e():
      right_val = right_arg.get_value()
  
    if left_val == 0 or right_val == 0:
      return constexpr.ConstantExpression("0")
    
    if left_val is not None and right_val is not None:
      return constexpr.ConstantExpression(str(left_val * right_val))

    if left_val == 1: return right_arg
    if right_val == 1: return left_arg

    return MultiplicationExpression(left_arg, right_arg)
Ejemplo n.º 9
0
    def simplify(self):
        left_arg = self.left_arg.simplify()
        right_arg = self.right_arg.simplify()

        left_val = None
        right_val = None

        if isinstance(left_arg,
                      constexpr.ConstantExpression) and not left_arg.is_e():
            left_val = left_arg.get_value()

        if isinstance(right_arg,
                      constexpr.ConstantExpression) and not right_arg.is_e():
            right_val = right_arg.get_value()

        if left_val is not None and right_val is not None:
            return constexpr.ConstantExpression(str(left_val - right_val))

        if left_val == 0: return negexpr.NegativeExpression(right_arg)
        if right_val == 0: return left_arg

        if left_arg == right_arg: return constexpr.ConstantExpression("0")
        return SubtractionExpression(left_arg, right_arg)
Ejemplo n.º 10
0
    def get_derivative(self):
        if self.is_constant(): return utils.constant_derivative(self)

        if self.right_arg.is_constant(): return self.const_divider_derivative()

        rule = "d/dx f(x)/g(x) = (g(x) d/dx f(x) - f(x) d/dx g(x))/(g(x))^2"

        left_derivative = self.left_arg.get_derivative()
        right_derivative = self.right_arg.get_derivative()

        result = DivisionExpression(
            subexpr.SubtractionExpression(
                multiexpr.MultiplicationExpression(self.right_arg,
                                                   left_derivative.result),
                multiexpr.MultiplicationExpression(self.left_arg,
                                                   right_derivative.result)),
            powexpr.PowerExpression(self.right_arg,
                                    constexpr.ConstantExpression("2")))

        rule_application = DivisionExpression(
            subexpr.SubtractionExpression(
                multiexpr.MultiplicationExpression(
                    self.right_arg,
                    derivexpr.DerivativeExpression(self.left_arg)),
                multiexpr.MultiplicationExpression(
                    self.left_arg,
                    derivexpr.DerivativeExpression(self.right_arg))),
            powexpr.PowerExpression(self.right_arg,
                                    constexpr.ConstantExpression("2")))

        return Derivative(
            self,
            result,
            rule_application,
            [rule],
            [left_derivative, right_derivative],
        )
Ejemplo n.º 11
0
  def get_derivative(self):
    if self.is_constant(): return utils.constant_derivative(self)

    cot_rule = "d/dx cot(x) = -csc^2(x)"

    derivative = negexpr.NegativeExpression(
      powexpr.PowerExpression(
        cscexpr.CscExpression(self.arg), constexpr.ConstantExpression(2)
      )
    )

    if self.arg == varexpr.VariableExpression():
      return Derivative(
        self,
        derivative,
        None,
        [cot_rule],
        None
      )
    
    return utils.apply_chain_rule(self, cot_rule, derivative)
Ejemplo n.º 12
0
  def const_power_derivative(self):
    rule = "d/dx x^n = n*x^(n-1)"

    derivative = multiexpr.MultiplicationExpression(
      self.right_arg,
      PowerExpression(
        self.left_arg,
        subexpr.SubtractionExpression(self.right_arg, constexpr.ConstantExpression("1"))
      )
    )

    if self.left_arg == varexpr.VariableExpression():
      return Derivative(
        self,
        derivative,
        derivative,
        [rule],
        None
      )
    
    return utils.apply_chain_rule(self, rule, derivative, self.left_arg)
Ejemplo n.º 13
0
def constant_derivative(expression):
    return Derivative(expression, ce.ConstantExpression("0"),
                      ce.ConstantExpression("0"), ['d/dx c = 0'], None)
Ejemplo n.º 14
0
 def get_derivative(self):
     return Derivative(self, constexpr.ConstantExpression("1"), None,
                       ["d/dx x = 1"], None)
Ejemplo n.º 15
0
 def is_constant(self):
   if self.left_arg.is_constant() and self.right_arg.is_constant():
     return True
   return self.right_arg == constexpr.ConstantExpression("0")