def print_CyclicParts(self, rule): with self.new_step(): self.append("Use integration by parts, noting that the integrand" " eventually repeats itself.") u, v, du, dv = map(lambda f: sympy.Function(f)(rule.symbol), "u v du dv".split()) current_integrand = rule.context total_result = sympy.S.Zero with self.new_level(): sign = 1 for rl in rule.parts_rules: with self.new_step(): self.append("For the integrand {}:".format(self.format_math(current_integrand))) self.append( "Let {} and let {}.".format( self.format_math(sympy.Eq(u, rl.u)), self.format_math(sympy.Eq(dv, rl.dv)) ) ) v_f, du_f = _manualintegrate(rl.v_step), rl.u.diff(rule.symbol) total_result += sign * rl.u * v_f current_integrand = v_f * du_f self.append( "Then {}.".format( self.format_math( sympy.Eq( sympy.Integral(rule.context, rule.symbol), total_result - sign * sympy.Integral(current_integrand, rule.symbol), ) ) ) ) sign *= -1 with self.new_step(): self.append("Notice that the integrand has repeated itself, so " "move it to one side:") self.append( "{}".format( self.format_math_display( sympy.Eq( (1 - rule.coefficient) * sympy.Integral(rule.context, rule.symbol), total_result ) ) ) ) self.append("Therefore,") self.append( "{}".format( self.format_math_display( sympy.Eq(sympy.Integral(rule.context, rule.symbol), _manualintegrate(rule)) ) ) )
def print_CyclicParts(self, rule): with self.new_step(): self.append("Use integration by parts, noting that the integrand" " eventually repeats itself.") u, v, du, dv = [ sympy.Function(f)(rule.symbol) for f in 'u v du dv'.split() ] current_integrand = rule.context total_result = sympy.S.Zero with self.new_level(): sign = 1 for rl in rule.parts_rules: with self.new_step(): self.append("For the integrand {}:".format( self.format_math(current_integrand))) self.append("Let {} and let {}.".format( self.format_math(sympy.Eq(u, rl.u)), self.format_math(sympy.Eq(dv, rl.dv)))) v_f, du_f = _manualintegrate(rl.v_step), rl.u.diff( rule.symbol) total_result += sign * rl.u * v_f current_integrand = v_f * du_f self.append("Then {}.".format( self.format_math( sympy.Eq( sympy.Integral(rule.context, rule.symbol), total_result - sign * sympy.Integral( current_integrand, rule.symbol))))) sign *= -1 with self.new_step(): self.append( "Notice that the integrand has repeated itself, so " "move it to one side:") self.append("{}".format( self.format_math_display( sympy.Eq((1 - rule.coefficient) * sympy.Integral(rule.context, rule.symbol), total_result)))) self.append("Therefore,") self.append("{}".format( self.format_math_display( sympy.Eq(sympy.Integral(rule.context, rule.symbol), _manualintegrate(rule)))))
def print_Log(self, rule): with self.new_step(): self.append( "The integral of {} is {}.".format( self.format_math(1 / rule.func), self.format_math(_manualintegrate(rule)) ) )
def print_Add(self, rule): with self.new_step(): self.append("Integrate term-by-term:") for substep in rule.substeps: with self.new_level(): self.print_rule(substep) self.append("The result is: {}".format(self.format_math(_manualintegrate(rule))))
def print_U(self, rule): with self.new_step(), self.new_u_vars() as (u, du): # commutative always puts the symbol at the end when printed dx = sympy.Symbol('d' + rule.symbol.name, commutative=0) self.append("Let {}.".format( self.format_math(sympy.Eq(u, rule.u_func)))) self.append("Then let {} and substitute {}:".format( self.format_math( sympy.Eq(du, rule.u_func.diff(rule.symbol) * dx)), self.format_math(rule.constant * du))) integrand = rule.constant * \ rule.substep.context.subs(rule.u_var, u) self.append(self.format_math_display(sympy.Integral(integrand, u))) with self.new_level(): self.print_rule( replace_u_var(rule.substep, rule.symbol.name, u)) self.append( '<div class="collapsible"><h2>open answer</h2><ol class="content">Now replace {} to get:' .format(self.format_math(u))) self.append( self.format_math_display(_manualintegrate(rule)) + '</ol></div>')
def print_Arctan(self, rule): with self.new_step(): self.append( "The integral of {} is {}.".format( self.format_math(1 / (1 + rule.symbol ** 2)), self.format_math(_manualintegrate(rule)) ) )
def print_Constant(self, rule): with self.new_step(): self.append("The integral of a constant is the constant " "times the variable of integration:") self.append( self.format_math_display( sympy.Eq(sympy.Integral(rule.constant, rule.symbol), _manualintegrate(rule))))
def print_Constant(self, rule): with self.new_step(): self.append("The integral of a constant is the constant " "times the variable of integration:") self.append( self.format_math_display( sympy.Eq(sympy.Integral(rule.constant, rule.symbol), _manualintegrate(rule))))
def print_Add(self, rule): with self.new_step(): self.append("Integrate term-by-term:") for substep in rule.substeps: with self.new_level(): self.print_rule(substep) self.append("The result is: {}".format( self.format_math(_manualintegrate(rule))))
def print_Exp(self, rule): with self.new_step(): if rule.base == sympy.E: self.append("The integral of the exponential function is itself.") else: self.append("The integral of an exponential function is itself" " divided by the natural logarithm of the base.") self.append(self.format_math_display( sympy.Eq(sympy.Integral(rule.context, rule.symbol), _manualintegrate(rule))))
def print_Exp(self, rule): with self.new_step(): if rule.base == sympy.E: self.append("The integral of the exponential function is itself.") else: self.append("The integral of an exponential function is itself" " divided by the natural logarithm of the base.") self.append(self.format_math_display( sympy.Eq(sympy.Integral(rule.context, rule.symbol), _manualintegrate(rule))))
def print_Add(self, rule): with self.new_step(): self.append("Integrate term-by-term:") for substep in rule.substeps: with self.new_level(): self.print_rule(substep) self.append( '<div class="collapsible"><h2>open answer</h2><ol class="content">The result is: ' ) self.append( self.format_math(_manualintegrate(rule)) + '</ol></div>')
def print_Power(self, rule): with self.new_step(): self.append("The integral of {} is {} when {}:".format( self.format_math(rule.symbol ** sympy.Symbol('n')), self.format_math((rule.symbol ** (1 + sympy.Symbol('n'))) / (1 + sympy.Symbol('n'))), self.format_math(sympy.Ne(sympy.Symbol('n'), -1)), )) self.append( self.format_math_display( sympy.Eq(sympy.Integral(rule.context, rule.symbol), _manualintegrate(rule))))
def print_Power(self, rule): with self.new_step(): self.append("The integral of {} is {} when {}:".format( self.format_math(rule.symbol**sympy.Symbol('n')), self.format_math((rule.symbol**(1 + sympy.Symbol('n'))) / (1 + sympy.Symbol('n'))), self.format_math(sympy.Ne(sympy.Symbol('n'), -1)), )) self.append( self.format_math_display( sympy.Eq(sympy.Integral(rule.context, rule.symbol), _manualintegrate(rule))))
def print_ConstantTimes(self, rule): with self.new_step(): self.append("The integral of a constant times a function " "is the constant times the integral of the function:") self.append(self.format_math_display( sympy.Eq( sympy.Integral(rule.context, rule.symbol), rule.constant * sympy.Integral(rule.other, rule.symbol)))) with self.new_level(): self.print_rule(rule.substep) self.append("So, the result is: {}".format( self.format_math(_manualintegrate(rule))))
def print_ConstantTimes(self, rule): with self.new_step(): self.append("The integral of a constant times a function " "is the constant times the integral of the function:") self.append(self.format_math_display( sympy.Eq( sympy.Integral(rule.context, rule.symbol), rule.constant * sympy.Integral(rule.other, rule.symbol)))) with self.new_level(): self.print_rule(rule.substep) self.append("So, the result is: {}".format( self.format_math(_manualintegrate(rule))))
def print_Trig(self, rule): with self.new_step(): text = { 'sin': "The integral of sine is negative cosine:", 'cos': "The integral of cosine is sine:", 'sec*tan': "The integral of secant times tangent is secant:", 'csc*cot': "The integral of cosecant times cotangent is cosecant:", }.get(rule.func) if text: self.append(text) self.append(self.format_math_display( sympy.Eq(sympy.Integral(rule.context, rule.symbol), _manualintegrate(rule))))
def print_Trig(self, rule): with self.new_step(): text = { 'sin': "The integral of sine is negative cosine:", 'cos': "The integral of cosine is sine:", 'sec*tan': "The integral of secant times tangent is secant:", 'csc*cot': "The integral of cosecant times cotangent is cosecant:", }.get(rule.func) if text: self.append(text) self.append(self.format_math_display( sympy.Eq(sympy.Integral(rule.context, rule.symbol), _manualintegrate(rule))))
def finalize(self): rule = filter_unknown_alternatives(self.rule) answer = _manualintegrate(rule) if answer: simp = sympy.simplify(sympy.trigsimp(answer)) if simp != answer: answer = simp with self.new_step(): self.append("Now simplify:") self.append(self.format_math_display(simp)) with self.new_step(): self.append("Add the constant of integration:") self.append(self.format_math_constant(answer)) self.lines.append('</ol>') self.lines.append('<hr/>') self.level = 0 self.append('The answer is:') self.append(self.format_math_constant(answer)) return '\n'.join(self.lines)
def finalize(self): rule = filter_unknown_alternatives(self.rule) answer = _manualintegrate(rule) if answer: simp = sympy.simplify(sympy.trigsimp(answer)) if simp != answer: answer = simp with self.new_step(): self.append("Now simplify:") self.append(self.format_math_display(simp)) with self.new_step(): self.append("Add the constant of integration:") self.append(self.format_math_constant(answer)) self.lines.append('</ol>') self.lines.append('<hr/>') self.level = 0 self.append('The answer is:') self.append(self.format_math_constant(answer)) return '\n'.join(self.lines)
def print_ConstantTimes(self, rule): with self.new_step(): self.append( "The integral of <strong>a</strong> constant times a function " "is <strong>the</strong> constant times the integral of the function:" ) self.append( self.format_math_display( sympy.Eq( sympy.Integral(rule.context, rule.symbol), rule.constant * sympy.Integral(rule.other, rule.symbol)))) with self.new_level(): self.print_rule(rule.substep) self.append( '<div class="collapsible"><h2>open answer</h2><ol class="content">So, the result is: ' ) self.append( self.format_math(_manualintegrate(rule)) + '</ol></div>')
def print_U(self, rule): with self.new_step(), self.new_u_vars() as (u, du): # commutative always puts the symbol at the end when printed dx = sympy.Symbol("d" + rule.symbol.name, commutative=0) self.append("Let {}.".format(self.format_math(sympy.Eq(u, rule.u_func)))) self.append( "Then let {} and substitute {}:".format( self.format_math(sympy.Eq(du, rule.u_func.diff(rule.symbol) * dx)), self.format_math(rule.constant * du), ) ) integrand = rule.substep.context.subs(rule.u_var, u) self.append(self.format_math_display(sympy.Integral(integrand, u))) with self.new_level(): self.print_rule(replace_u_var(rule.substep, rule.u_var, u)) self.append("Now substitute {} back in:".format(self.format_math(u))) self.append(self.format_math_display(_manualintegrate(rule)))
def finalize(self): rule = filter_unknown_alternatives(self.rule) answer = _manualintegrate(rule) if answer: simp = sympy.simplify(sympy.trigsimp(answer)) if simp != answer: answer = simp with self.new_step(): self.append( '<div class="collapsible"><h2>open answer</h2><ol class="content">Now simplify:' ) self.append(self.format_math_display(simp) + '</ol></div>') with self.new_step(): self.append( '<div class="collapsible"><h2>open answer</h2><ol class="content">Add the constant of integration to get:' ) self.append(self.format_math_constant(answer) + '</ol></div>') self.lines.append('</ol>') self.level = 0 # self.append('The answer is:') # self.append(self.format_math_constant(answer)) return '\n'.join(self.lines)
def print_U(self, rule): with self.new_step(), self.new_u_vars() as (u, du): # commutative always puts the symbol at the end when printed dx = sympy.Symbol('d' + rule.symbol.name, commutative=0) self.append("Let {}.".format( self.format_math(sympy.Eq(u, rule.u_func)))) self.append("Then let {} and substitute {}:".format( self.format_math( sympy.Eq(du, rule.u_func.diff(rule.symbol) * dx)), self.format_math(rule.constant * du))) integrand = rule.substep.context.subs(rule.u_var, u) self.append(self.format_math_display(sympy.Integral(integrand, u))) with self.new_level(): self.print_rule(replace_u_var(rule.substep, rule.u_var, u)) self.append("Now substitute {} back in:".format( self.format_math(u))) self.append(self.format_math_display(_manualintegrate(rule)))
def print_Arctan(self, rule): with self.new_step(): self.append("The integral of {} is {}.".format( self.format_math(1 / (1 + rule.symbol**2)), self.format_math(_manualintegrate(rule))))
def print_Log(self, rule): with self.new_step(): self.append("The integral of {} is {}.".format( self.format_math(1 / rule.func), self.format_math(_manualintegrate(rule))))