Example #1
0
def test_mon_mult_random():

    np.random.seed(988)

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

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

    shape2 = list()
    for i in range(dim):
        shape2.append(np.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_coeff, new_M4_coeff = utils.match_size(M3.coeff,M4.coeff)
    else:
        new_M3_coeff, new_M4_coeff = M3.coeff, M4.coeff

    assert np.allclose(new_M3_coeff, new_M4_coeff)
Example #2
0
def Mxi_Matrix(i, basisDict, VB, dim, poly_type, verbose=False):
    '''
    Uses the reduced Macaulay matrix to construct the Moller-Stetter matrix M_xi, which
    represents the linear map of multiplying by xi in the space C[x1, ..., xn]/I.

    Parameters
    ----------
    i : int
        The index of the variable xi to make the Moller-Stetter matrix of, where variables
        are indexed as x1, x2, ..., xn.
    basisDict: dictionary
        A dictionary which maps monomials not in the basis to linear combinations
        of monomials in the basis. Generated using the TVB method.
    VB: numpy array
        Represents a vector basis for the space C[x1, ..., xn]/I created with the TVB method.
        Each row represents a monomial in the basis as the degrees of each variable.
        For example, x^2y^5 would be represented as [2,5].
    dim: int
        The dimension of the system (n)
    verbose : bool
        Prints information about how the roots are computed.

    Returns
    -------
    Mxi : 2D numpy array
        The Moller-Stetter matrix which represents multiplying by xi
    '''
    VB = VB.tolist() #convert to list bc numpy's __contains__() function is broken

    #Construct the polynomial to create the MS Matrix of (xi)
    xi_ind = np.zeros(dim, dtype=int)
    xi_ind[i-1] = 1
    coef = np.zeros((2,)*dim)
    coef[tuple(xi_ind)] = 1
    if poly_type == "MultiPower":
        xi = MultiPower(np.array(coef))
    elif poly_type == "MultiCheb":
        xi = MultiCheb(np.array(coef))

    if verbose:
        print("\nCoefficients of polynomial whose Moller-Stetter matrix we construt\n", xi.coeff)

    # Build multiplication matrix M_xi
    Mxi = np.zeros((len(VB), len(VB)))
    for j in range(len(VB)): #multiply each monomial in the basis by xi
        product_coef = xi.mon_mult(VB[j], returnType = 'Matrix')
        for monomial in zip(*np.where(product_coef != 0)):
            if list(monomial) in VB: #convert to list to test if list of lists
                Mxi[VB.index(list(monomial))][j] += product_coef[monomial]
            else:
                Mxi[:,j] -= product_coef[monomial]*basisDict[monomial]

    # Construct var_dict
    var_dict = {}
    for i in range(len(VB)):
        mon = VB[i]
        if np.sum(mon) == 1 or np.sum(mon) == 0:
            var_dict[tuple(mon)] = i

    return Mxi
Example #3
0
def test_mon_mult():
    """
    Tests monomial multiplication using normal polynomial multiplication.
    """

    np.random.seed(4)

    #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)
Example #4
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 np.allclose(P1.coeff, P2.coeff, atol = 1.0e-10)
    assert np.allclose(T1.coeff, T2.coeff, atol = 1.0e-10)
Example #5
0
def MSMultMatrix(polys, poly_type, number_of_roots, verbose=False, MSmatrix=0):
    '''
    Finds the multiplication matrix using the reduced Macaulay matrix.

    Parameters
    ----------
    polys : array-like
        The polynomials to find the common zeros of
    poly_type : string
        The type of the polynomials in polys
    MSmatrix : int
        Controls which Moller-Stetter matrix is constructed. The options are:
            0 (default) -- The Moller-Stetter matrix of a random polynomial
            Some positive integer i < dimension -- The Moller-Stetter matrix of x_i
    verbose : bool
        Prints information about how the roots are computed.

    Returns
    -------
    multiplicationMatrix : 2D numpy array
        The multiplication matrix for a random polynomial f
    var_dict : dictionary
        Maps each variable to its position in the vector space basis
    '''
    basisDict, VB = MacaulayReduction(polys, number_of_roots, verbose=verbose)

    dim = max(f.dim for f in polys)

    # Get the polynomial to make the MS matrix of
    if MSmatrix == 0:  #random poly
        f = _random_poly(poly_type, dim)[0]
    else:  #multiply by x_i where i is determined by MSmatrix
        xi_ind = np.zeros(dim, dtype=int)
        xi_ind[MSmatrix - 1] = 1
        coef = np.zeros((2, ) * dim)
        coef[tuple(xi_ind)] = 1
        if poly_type == "MultiPower":
            f = MultiPower(np.array(coef))
        elif poly_type == "MultiCheb":
            f = MultiCheb(np.array(coef))
        else:
            raise ValueError()
    if verbose:
        print(
            "\nCoefficients of polynomial whose Moller-Stetter matrix we construt\n",
            f.coeff)

    #Dictionary of terms in the vector basis their spots in the matrix.
    VBdict = {}
    spot = 0
    for row in VB:
        VBdict[tuple(row)] = spot
        spot += 1

    # Build multiplication matrix m_f
    mMatrix = np.zeros((len(VB), len(VB)))
    for i in range(VB.shape[0]):
        f_coeff = f.mon_mult(VB[i], returnType='Matrix')
        for term in zip(*np.where(f_coeff != 0)):
            if term in VBdict:
                mMatrix[VBdict[term]][i] += f_coeff[term]
            else:
                mMatrix[:, i] -= f_coeff[term] * basisDict[term]

    # Construct var_dict
    var_dict = {}
    for i in range(len(VB)):
        mon = VB[i]
        if np.sum(mon) == 1 or np.sum(mon) == 0:
            var_dict[tuple(mon)] = i

    return mMatrix, var_dict