示例#1
0
def test_pubo_multiplication():

    temp = PUBOMatrix({(0, 0): 1, (0, 1): 2})
    temp[(2, 0, 1)] -= 4
    temp += 2

    # __mul__
    d = temp.copy()
    g = d * 3
    assert g == {(0, ): 3, (0, 1): 6, (): 6, (0, 1, 2): -12}

    d = temp.copy()
    g = d * 0
    assert g == {}

    # __imul__
    d = temp.copy()
    d *= 3
    assert d == {(0, ): 3, (0, 1): 6, (): 6, (0, 1, 2): -12}

    d = temp.copy()
    d *= 0
    assert d == {}

    # __rmul__
    d = temp.copy()
    g = 3 * d
    assert g == {(0, ): 3, (0, 1): 6, (): 6, (0, 1, 2): -12}

    d = temp.copy()
    g = 0 * d
    assert g == {}

    # __truediv__
    d = temp.copy()
    g = d / 2
    assert g == {(0, ): .5, (0, 1): 1, (): 1, (0, 1, 2): -2}

    # __itruediv__
    d = temp.copy()
    d /= 2
    assert d == {(0, ): .5, (0, 1): 1, (): 1, (0, 1, 2): -2}

    # __floordiv__
    d = temp.copy()
    g = d // 2
    assert g == {(0, 1): 1, (): 1, (0, 1, 2): -2}

    # __ifloordiv__
    d = temp.copy()
    d //= 2
    assert d == {(0, 1): 1, (): 1, (0, 1, 2): -2}

    # todo: add __mul__ test with dicts

    # __pow__
    assert temp**2 == temp * temp
    assert temp**3 == temp * temp * temp
示例#2
0
def test_pubo_addition():

    temp = PUBOMatrix({(0, 0): 1, (0, 1): 2, (1, 0, 3): 2})
    temp1 = {(0, ): -1, (1, 0): 3}
    temp2 = {(0, 1): 5, (0, 1, 3): 2}
    temp3 = {(0, ): 2, (0, 1): -1, (0, 1, 3): 2}

    # add constant
    d = temp.copy()
    d += 5
    d[()] -= 2
    d == {(0, ): 1, (0, 1): 2, (): 3}

    # __add__
    d = temp.copy()
    g = d + temp1
    assert g == temp2

    # __iadd__
    d = temp.copy()
    d += temp1
    assert d == temp2

    # __radd__
    d = temp.copy()
    g = temp1 + d
    assert g == temp2

    # __sub__
    d = temp.copy()
    g = d - temp1
    assert g == temp3

    # __isub__
    d = temp.copy()
    d -= temp1
    assert d == temp3

    # __rsub__
    d = temp.copy()
    g = temp1 - d
    assert g == PUBOMatrix(temp3) * -1