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)
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)
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)
def multMatrix(poly, GB, basis): ''' Finds the matrix of the linear operator m_f on A = C[x_1,...,x_n]/I where f is the polynomial argument. The linear operator m_f is defined as m_f([g]) = [f]*[g] where [f] represents the coset of f in A. Since m_f is a linear operator on A, it can be represented by its matrix with respect to the vector space basis. parameters ---------- poly : polynomial object The polynomial f for which to find the matrix m_f. GB: list of polynomial objects Polynomials that make up a Groebner basis for the ideal basis : list of tuples The monomials that make up a basis for the vector space A return ------ multOperatorMatrix : square numpy array The matrix m_f ''' # Reshape poly's coefficienet matrix if it is not in the same number # of variables as the polynomials in the Groebner basis. # (i.e. len(shape) is the number of variables the polynomial is in) numVars = len(GB[0].shape) polyVars = len(poly.coeff.shape) if polyVars != numVars: new_shape = [i for i in poly.coeff.shape] for j in range(numVars-polyVars): new_shape.append(1) if type(poly) is MultiPower: poly = MultiPower(poly.coeff.reshape(tuple(new_shape))) if type(poly) is MultiCheb: poly = MultiCheb(poly.coeff.reshape(tuple(new_shape))) dim = len(basis) operatorMatrix = np.zeros((dim, dim)) for i in range(dim): monomial = basis[i] poly_ = poly.mon_mult(monomial) operatorMatrix[:,i] = coordinateVector(poly_, GB, basis) return operatorMatrix