Exemplo n.º 1
0
def test_puso_addition():

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

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

    # __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 == PUSOMatrix(temp3) * -1
Exemplo n.º 2
0
def test_puso_multiplication():

    temp = PUSOMatrix({(0, ): 1, (0, 1): 2, (0, 2, 3): 4})
    temp += 2

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

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

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

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

    # __rmul__
    d = temp.copy()
    g = 3 * d
    assert g == {(0, ): 3, (0, 1): 6, (): 6, (0, 2, 3): 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, 2, 3): 2}

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

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

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

    # __mul__ but with dict
    d = temp.copy()
    d *= {(1, ): 2, (0, ): -1}
    assert d == {
        (0, 1): 2,
        (): -1,
        (0, ): 2,
        (1, ): 2,
        (0, 1, 2, 3): 8,
        (2, 3): -4
    }

    # __pow__
    d = temp.copy()
    d **= 2
    assert d == {
        (): 25,
        (1, ): 4,
        (2, 3): 8,
        (0, ): 4,
        (1, 2, 3): 16,
        (0, 1): 8,
        (0, 2, 3): 16
    }

    temp = d.copy()
    assert d**3 == d * d * d

    temp = d.copy()
    assert d**4 == d * d * d * d