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])
Exemplo n.º 2
0
def apply_chain_rule(expression, rule, u_derivative, child=None):
    chain_rule = "dy/dx = dy/(du)(du)/dx"

    if child is None:
        child = expression.arg
    child_derivative = child.get_derivative()

    return Derivative(
        expression,
        me.MultiplicationExpression(u_derivative, child_derivative.result),
        me.MultiplicationExpression(u_derivative,
                                    de.DerivativeExpression(child)),
        [rule, chain_rule], [child_derivative])
Exemplo n.º 3
0
    def get_derivative(self):
        if self.is_constant(): return utils.constant_derivative(self)

        sec_rule = "d/dx sec(x) = sec(x)*tan(x)"

        derivative = multiexpr.MultiplicationExpression(
            SecExpression(self.arg), tanexpr.TanExpression(self.arg))

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

        return utils.apply_chain_rule(self, sec_rule, derivative)
    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],
        )
Exemplo 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)
Exemplo n.º 6
0
  def get_derivative(self):
    if self.is_constant(): return utils.constant_derivative(self)

    csc_rule = "d/dx csc(x) = -csc(x)*cot(x)"

    derivative = multiexpr.MultiplicationExpression(
      negexpr.NegativeExpression(
        CscExpression(self.arg)
      ), cotexpr.CotExpression(self.arg)
    )

    if self.arg == varexpr.VariableExpression():
      return Derivative(
        self,
        derivative,
        None,
        [csc_rule],
        None
      )
    
    return utils.apply_chain_rule(self, csc_rule, derivative)
Exemplo n.º 7
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)
Exemplo n.º 8
0
  def const_base_derivative(self):
    derivative = None
    rule = None

    rule_application = None
    if isinstance(self.left_arg, constexpr.ConstantExpression) and self.left_arg.is_e():
      derivative = self
      rule = "d/dx e^x = e^x"
    else:
      derivative = multiexpr.MultiplicationExpression(self, lnexpr.LnExpression(self.left_arg))
      rule = "d/dx a^x = a^x * ln(a)"
      rule_application = derivative

    if self.right_arg == varexpr.VariableExpression():
      return Derivative(
        self,
        derivative,
        rule_application,
        [rule],
        None
      )
    
    return utils.apply_chain_rule(self, rule, derivative, self.right_arg)
Exemplo n.º 9
0
  def get_derivative(self):
    if self.is_constant(): return utils.constant_derivative(self)

    if self.right_arg.is_constant(): return self.const_power_derivative()
    if self.left_arg.is_constant(): return self.const_base_derivative()

    rule = "d/dx u ^ v = u ^ v * (ln (u) * d/dx v + (v * d/dx u)/ u)"
    
    left_derivative = self.left_arg.get_derivative()
    right_derivative = self.right_arg.get_derivative()

    result = multiexpr.MultiplicationExpression( 
      self,
      addexpr.AdditionExpression(
        multiexpr.MultiplicationExpression(lnexpr.LnExpression(self.left_arg), right_derivative.result),
        divexpr.DivisionExpression(
          multiexpr.MultiplicationExpression(self.right_arg, left_derivative.result), self.left_arg
        )
      )
    )

    rule_application = multiexpr.MultiplicationExpression( 
      self,
      addexpr.AdditionExpression(
        multiexpr.MultiplicationExpression(
          lnexpr.LnExpression(self.left_arg), derivexpr.DerivativeExpression(self.right_arg)
        ),
        divexpr.DivisionExpression(
          multiexpr.MultiplicationExpression(
            self.right_arg, derivexpr.DerivativeExpression(self.left_arg)
          ), self.left_arg
        )
      )
    )

    return Derivative(
      self,
      result,
      rule_application,
      [rule],
      [left_derivative, right_derivative],
    )