當前位置: 首頁>>代碼示例>>Python>>正文


Python Polynomial.constant方法代碼示例

本文整理匯總了Python中polynomial.Polynomial.constant方法的典型用法代碼示例。如果您正苦於以下問題:Python Polynomial.constant方法的具體用法?Python Polynomial.constant怎麽用?Python Polynomial.constant使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在polynomial.Polynomial的用法示例。


在下文中一共展示了Polynomial.constant方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: solve_via_basis_selection

# 需要導入模塊: from polynomial import Polynomial [as 別名]
# 或者: from polynomial.Polynomial import constant [as 別名]

#.........這裏部分代碼省略.........

    # Check that lambda * b = action_b * b + action_r * r at solution
    for bi, b_row, r_row in zip(basis, c_action_b, c_action_r):
        assert len(r_row) == len(dependent)
        assert len(b_row) == len(basis)
        lhs = bi*lambda_poly
        rhs = (sum(cj*rj for cj, rj in zip(r_row, x_dependent)) +
               sum(cj*bj for cj, bj in zip(b_row, x_basis)))
        if diagnostic_solutions is not None:
            for solution in diagnostic_solutions:
                lvalue = lhs(*solution)
                rvalue = rhs(*solution)
                if verbosity >= 2:
                    print '    at %s, lhs=%s, rhs=%s' % (solution, lvalue, rvalue)
                if abs(lvalue - rvalue) > 1e-8:
                    raise DiagnosticError('expected action equation lhs (=%f) to match rhs (=%f) at %s:\n'
                                          '  lhs=%s\n  rhs=%s' %
                                          (lvalue, rvalue, solution, lhs, rhs))

    # Compute action matrix form for lambda_poly * basis
    soln = np.linalg.solve(c1, c2)
    c_action = c_action_b - np.dot(c_action_r, soln)

    if verbosity >= 2:
        print 'Solution for required monomials:'
        print soln
        for row, monomial in zip(soln, dependent):
            print '  %s = - %s * basis {at points on variety}' % (as_monomial(monomial), row)

    # Check that basis + soln * required = 0 at diagnostic solutions
    if diagnostic_solutions is not None:
        c_solved = np.hstack((np.eye(len(required) + len(eliminated)), soln))
        for solution in diagnostic_solutions:
            values = np.dot(c_solved, evaluate_poly_vector(x_post, solution))
            if verbosity >= 2:
                print '  Evaluated at %s: %s' % (solution, values)
            if np.abs(values).max() > 1e-8:
                idx = np.abs(values).argmax()
                raise DiagnosticError('expected equation %d to evaluate to zero but received %f' % (idx, values[idx]))

    # Check that lambda * b = action * b at solution (note that lambda is a polynomial, not a matrix here)
    if verbosity >= 2 or diagnostic_solutions is not None:
        if verbosity >= 2:
            print 'Action matrix:'
            print c_action
        for bi, row in zip(basis, c_action):
            assert len(basis) == len(row)
            lhs = bi*lambda_poly
            rhs = sum(cj * bj for cj, bj in zip(row, x_basis))
            if verbosity >= 2:
                print '  %s * (%s) = %s = %s' % (as_term(bi, nvars), lambda_poly, lhs, rhs)
            if diagnostic_solutions is not None:
                for solution in diagnostic_solutions:
                    lvalue = lhs(*solution)
                    rvalue = rhs(*solution)
                    if verbosity >= 2:
                        print '    at %s, lhs=%s, rhs=%s' % (solution, lvalue, rvalue)
                    if abs(lvalue - rvalue) > 1e-8:
                        raise DiagnosticError('expected action equation lhs (=%f) to match rhs (=%f) at %s:\n'
                                              '  lhs=%s\n  rhs=%s' %
                                              (lvalue, rvalue, solution, lhs, rhs))

    # Find indices within basis
    unit_index = basis.index(Polynomial.constant(1, nvars))

    # Compute eigenvalues and eigenvectors
    eigvals, eigvecs = np.linalg.eig(c_action)

    if verbosity >= 2:
        print 'Eigenvectors:'
        print eigvecs

    # Divide out the unit monomial row
    nrm = eigvecs[unit_index]
    mask = np.abs(nrm) > 1e-8
    monomial_values = (eigvecs[:, mask] / eigvecs[unit_index][mask]).T

    if verbosity >= 2:
        print 'Normalized eigenvectors:'
        print monomial_values

    # Test each solution
    solutions = []
    for values in monomial_values:
        #candidate = [eigvec[i]/eigvec[unit_index] for i in var_indices]
        for solution in solve_monomial_equations(basis, values):
            values = [f(*solution) for f in equations]
            if verbosity >= 1:
                print 'Candidate solution: %s -> Values = %s' % (solution, values)
            if np.linalg.norm(values) < 1e-8:
                solutions.append(solution)

    # Report final solutions
    base_vars = Polynomial.coordinates(lambda_poly.num_vars)
    if verbosity >= 1:
        print 'Solutions:'
        for solution in solutions:
            print '  ' + ' '.join('%s=%s' % (var, val) for var, val in zip(base_vars, solution))

    return SolutionSet(solutions)
開發者ID:alexflint,項目名稱:polygamy,代碼行數:104,代碼來源:solvers.py


注:本文中的polynomial.Polynomial.constant方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。