コード例 #1
0
def test_quso_addition():

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

    # 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 == QUSOMatrix(temp3) * -1
コード例 #2
0
def test_quso_multiplication():

    temp = QUSOMatrix({(0, ): 1, (0, 1): 2})
    temp += 2

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

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

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

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

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

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

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

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

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

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

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

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

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

    # should raise a KeyError since can't fit this into QUSO.
    with assert_raises(KeyError):
        QUSOMatrix({(0, 1): 1, (2, 3): -1})**2