本文整理汇总了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)