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)
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)
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 MSMultMatrix(polys, poly_type, max_cond_num, macaulay_zero_tol, 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 verbose : bool Prints information about how the roots are computed. 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 max_cond_num : float The maximum condition number of the Macaulay Matrix Reduction macaulay_zero_tol : float What is considered 0 in the macaulay matrix reduction. 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 : dict A dictionary of terms not in the vector basis a matrixes of things in the vector basis that the term can be reduced to. VB : numpy array The terms in the vector basis, each row being a term. Raises ------ ConditioningError if MacaulayReduction(...) raises a ConditioningError. ''' try: basisDict, VB, varsRemoved = MacaulayReduction( polys, max_cond_num=max_cond_num, macaulay_zero_tol=macaulay_zero_tol, verbose=verbose) except ConditioningError as e: raise e 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, basisDict, VB