def test_Variable(): variable1 = Variable(2, 'x', 3) assert variable1.__str__() == "2{x}^{3}" variable1.integrate('x') assert variable1.__str__() == "0.5{x}^{4}" # FIXME: Optimize integrate """
def integrateTokens(funclist, wrtVar): """Integrates given tokens wrt given variable Arguments: funclist {list} -- list of function tokens wrtVar {string} -- with respect to variable Returns: intFunc {list} -- list of integrated tokens animNew {list} -- equation tokens for step-by-step commentsNew {list} -- comments for step-by-step """ intFunc = [] animNew = [] commentsNew = [ "Integrating with respect to " + r"$" + wrtVar + r"$" + "\n" ] for func in funclist: if isinstance(func, Operator): # add isfunctionOf intFunc.append(func) else: newfunc = [] commentsNew[0] += r"$" + r"\int \ " + r"( " + func.__str__( ) + ")" + r" d" + wrtVar + r"$" funcCopy = copy.deepcopy(func) if wrtVar in funcCopy.functionOf(): if isinstance(funcCopy, Variable): log = False funcCopy, log = funcCopy.integrate(wrtVar) if log: commentsNew[0] += r"$" + r"= " + funcCopy[0].__str__( ) + r"*" + funcCopy[2].__str__() + r"\ ;\ " + r"$" newfunc.extend(funcCopy) else: commentsNew[0] += r"$" + r"= " + funcCopy.__str__( ) + r"\ ;\ " + r"$" newfunc.append(funcCopy) elif isinstance(funcCopy, Trigonometric): funcCopy = funcCopy.integrate(wrtVar) newfunc.append(funcCopy) commentsNew[0] += r"$" + r"= " + funcCopy.__str__( ) + r"\ ;\ " + r"$" else: if isinstance(funcCopy, Variable): funcCopy.value.append(wrtVar) funcCopy.power.append(1) if isinstance(funcCopy, Constant): coeff = funcCopy.value funcCopy = Variable() funcCopy.coefficient = coeff funcCopy.value.append(wrtVar) funcCopy.power.append(1) newfunc.append(funcCopy) commentsNew[0] += r"$" + r"= " + funcCopy.__str__( ) + r"\ ;\ " + r"$" intFunc.extend(newfunc) animNew.extend(intFunc) return intFunc, animNew, commentsNew
def test_Variable(): variable1 = Variable(2, 'x', 3) assert variable1.__str__() == "2{x}^{3}" variable1, _ = variable1.integrate('x') assert variable1.__str__() == "0.5{x}^{4}" constant = Constant(3) variable = Variable(2, 'x', 3) add = variable + constant assert add.__str__() == "{(2{x}^{3}+{3})}" variable1 = Variable(2, 'x', 3) variable2 = Variable(4, 'x', 3) add1 = variable1 + variable2 assert add1.__str__() == "6{x}^{3}" variable1 = Variable(2, 'x', 3) constant = Constant(3) exp1 = Expression([variable1, Plus(), constant]) variable2 = Variable(4, 'x', 3) add2 = variable2 + exp1 assert add2.__str__() == "{(6{x}^{3}+{3})}" variable1 = Variable(2, 'x', 3) constant = Constant(3) exp1 = variable1 + constant variable2 = Variable(4, 'x', 3) add2 = variable2 - exp1 assert add2.__str__() == "{(2{x}^{3}-{3})}" variable1 = Variable(2, 'x', 3) constant = Constant(3) exp1 = Expression([variable1, Plus(), constant]) variable2 = Variable(4, 'x', 3) add2 = exp1 - variable2 assert add2.__str__() == "{(-2{x}^{3}+{3})}" constant = Constant(3) variable = Variable(2, 'x', 3) add = variable - constant assert add.__str__() == "{(2{x}^{3}-{3})}" variable1 = Variable(2, 'x', 3) variable2 = Variable(4, 'x', 3) variable3 = Variable(2, 'x', 4) variable4 = Variable(2, 'x', 3) add1 = variable1 - variable2 add2 = variable3 - variable4 assert add1.__str__() == "-2{x}^{3}" assert add2.__str__() == "{(2{x}^{4}-2{x}^{3})}" constant = Constant(3) variable = Variable(2, 'x', 3) add = variable * constant assert add.__str__() == "6{x}^{3}" variable1 = Variable(2, 'x', 3) variable2 = Variable(4, 'x', 3) add2 = variable1 * variable2 assert add2.__str__() == "8{x}^{6}" variable1 = Variable(2, 'x', 3) variable2 = Variable(4, 'y', 3) add2 = variable1 * variable2 assert add2.__str__() == "8{x}^{3}{y}^{3}" variable1 = Variable(2, 'x', 3) variable2 = Variable(4, 'y', 4) add1 = variable1 / variable2 assert add1.__str__() == "0.5{x}^{3}{y}^{-4}" variable1 = Variable(2, 'x', 3) constant = Constant(3) exp1 = variable1 - constant variable2 = Variable(4, 'x', 3) add2 = variable2 / exp1 assert add2.__str__() == "{(4.0{x}^{3}*{(2{x}^{3}-{3})}^{-1})}" variable1 = Variable(2, 'x', 3) constant = Constant(3) exp1 = variable1 - constant variable2 = Variable(4, 'x', 3) add2 = variable2 * exp1 assert add2.__str__() == "{(8{x}^{6}-12{x}^{3})}" variable1 = Variable(2, 'x', 3) constant1 = Constant(3) exp1 = variable1 - constant1 variable2 = Variable(4, 'x', 3) constant2 = Constant(4) exp2 = variable2 - constant2 add2 = exp1 * exp2 assert add2.__str__() == "{({(8{x}^{6}-8{x}^{3})}-{(12{x}^{3}-{12})})}" variable2 = Variable(3, 'x', -1) variable2, _ = variable2.integrate('x') assert tokensToString(variable2) == '3 * log(x)'
def integrateTokens(funclist, wrtVar): """Integrates given tokens wrt given variable Arguments: funclist {list} -- list of funtion tokens wrtVar {string} -- with respect to variable Returns: intFunc {list} -- list of integrated tokens animNew {list} -- equation tokens for step-by-step commentsNew {list} -- comments for step-by-step """ intFunc = [] animNew = [] commentsNew = [ "Integrating with respect to " + r"$" + wrtVar + r"$" + "\n" ] for func in funclist: if isinstance(func, Operator): # add isFuntionOf intFunc.append(func) else: newfunc = [] while (isinstance(func, Function)): commentsNew[0] += r"$" + "\int \ " + "( " + func.__str__( ) + ")" + " d" + wrtVar + r"$" funcCopy = copy.deepcopy(func) if wrtVar in funcCopy.functionOf(): if not isinstance(funcCopy, Constant): for i, var in enumerate(funcCopy.value): log = False if var == wrtVar: if (funcCopy.power[i] == -1): log = True funcLog = Logarithm() funcLog.operand = Variable() funcLog.operand.coefficient = 1 funcLog.operand.value.append( funcCopy.value[i]) funcLog.operand.power.append(1) del funcCopy.power[i] del funcCopy.value[i] if funcCopy.value == []: funcCopy.__class__ = Constant funcCopy.value = funcCopy.coefficient funcCopy.coefficient = 1 funcCopy.power = 1 funcCopy = [funcCopy] funcJoin = Binary() funcJoin.value = '*' funcCopy.append(funcJoin) funcCopy.append(funcLog) else: funcCopy.power[i] += 1 funcCopy.coefficient /= funcCopy.power[i] if log: commentsNew[0] += r"$" + "= " + funcCopy[0].__str__( ) + "*" + funcCopy[2].__str__() + "\ ;\ " + r"$" newfunc.extend(funcCopy) else: commentsNew[0] += r"$" + "= " + funcCopy.__str__( ) + "\ ;\ " + r"$" newfunc.append(funcCopy) else: if isinstance(funcCopy, Variable): funcCopy.value.append(wrtVar) funcCopy.power.append(1) if isinstance(funcCopy, Constant): coeff = funcCopy.value funcCopy = Variable() funcCopy.coefficient = coeff funcCopy.value.append(wrtVar) funcCopy.power.append(1) newfunc.append(funcCopy) commentsNew[0] += r"$" + "= " + funcCopy.__str__( ) + "\ ;\ " + r"$" if func.operand is None: break else: func = func.operand if isinstance(func, Constant): intFunc = Zero() break intFunc.extend(newfunc) animNew.extend(intFunc) return intFunc, animNew, commentsNew