예제 #1
0
def test_mon_mult_random():
    #test with random matrices
    possible_dim = np.random.randint(1, 5, (1, 10))
    dim = possible_dim[0, random.randint(1, 9)]

    shape = list()
    for i in range(dim):
        shape.append(random.randint(2, 4))
    matrix1 = np.random.randint(1, 11, (shape))
    M1 = MultiPower(matrix1)

    shape2 = list()
    for i in range(dim):
        shape2.append(random.randint(2, 4))
    matrix2 = np.ones(shape2)
    M2 = MultiPower(matrix2)

    M3 = M1 * M2

    for index, i in np.ndenumerate(M2.coeff):
        if sum(index) == 0:
            M4 = MultiPower.mon_mult(M1, index)
        else:
            M4 = M4 + MultiPower.mon_mult(M1, index)

    if M3.shape != M4.shape:
        new_M3, new_M4 = MultiPower.match_size(M3, M3, M4)
    else:
        new_M3, new_M4 = M3, M4

    assert np.allclose(new_M3.coeff, new_M4.coeff)
예제 #2
0
def test_mon_mult():
    """
    Tests monomial multiplication using normal polynomial multiplication.
    """

    #Simple 2D test cases
    cheb1 = MultiCheb(np.array([[0, 0, 0], [0, 0, 0], [0, 0, 1]]))
    mon1 = (1, 1)
    result1 = cheb1.mon_mult(mon1)
    truth1 = np.array([[0, 0, 0, 0], [0, 0.25, 0, 0.25], [0, 0, 0, 0],
                       [0, 0.25, 0, 0.25]])

    assert np.allclose(result1.coeff, truth1)

    #test with random matrices
    cheb2 = np.random.randint(-9, 9, (4, 4))
    C1 = MultiCheb(cheb2)
    C2 = cheb2poly(C1)
    C3 = MultiCheb.mon_mult(C1, (1, 1))
    C4 = MultiPower.mon_mult(C2, (1, 1))
    C5 = poly2cheb(C4)

    assert np.allclose(C3.coeff, C5.coeff)

    # test results of chebyshev mult compared to power multiplication
    cheb3 = np.random.randn(5, 4)
    c1 = MultiCheb(cheb3)
    c2 = MultiCheb(np.ones((4, 2)))
    for index, i in np.ndenumerate(c2.coeff):
        if sum(index) == 0:
            c3 = c1.mon_mult(index)
        else:
            c3 = c3 + c1.mon_mult(index)
    p1 = cheb2poly(c1)
    p2 = cheb2poly(c2)
    p3 = p1 * p2
    p4 = cheb2poly(c3)
    assert np.allclose(p3.coeff, p4.coeff)

    # test results of chebyshev mult compared to power multiplication in 3D
    cheb4 = np.random.randn(3, 3, 3)
    a1 = MultiCheb(cheb4)
    a2 = MultiCheb(np.ones((3, 3, 3)))
    for index, i in np.ndenumerate(a2.coeff):
        if sum(index) == 0:
            a3 = a1.mon_mult(index)
        else:
            a3 = a3 + a1.mon_mult(index)
    q1 = cheb2poly(a1)
    q2 = cheb2poly(a2)
    q3 = q1 * q2
    q4 = cheb2poly(a3)
    assert np.allclose(q3.coeff, q4.coeff)
예제 #3
0
def test_mon_mult():
    """
    Tests monomial multiplication using normal polynomial multiplication.
    """

    mon = (1, 2)
    Poly = MultiPower(
        np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12],
                  [13, 14, 15, 16]]))
    mon_matr = MultiPower(
        np.array([[0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]))
    P1 = mon_matr * Poly
    P2 = MultiPower.mon_mult(Poly, mon)

    mon2 = (0, 1, 1)
    Poly2 = MultiPower(np.arange(1, 9).reshape(2, 2, 2))
    mon_matr2 = MultiPower(np.array([[[0, 0], [0, 1]], [[0, 0], [0, 0]]]))
    T1 = mon_matr2 * Poly2
    T2 = MultiPower.mon_mult(Poly2, mon2)

    assert (P1 == P2)
    assert (T1 == T2)