Exemplo n.º 1
0
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
    """
Exemplo n.º 2
0
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)'