def LM(self,poly): from sympy import S,Poly if poly==0:return poly poly = Poly(poly,self.variables) polyDict = poly.as_dict() exponents = polyDict.keys() largest = exponents[0] for i in xrange(len(polyDict)): if self.compare(exponents[i],largest): largest = exponents[i] return Poly({largest:S(1)},self.variables)
def test_poly_internals(): p = Poly(x**2*y*z + x*y*z**3 + x*y + y*z, x, y, z) assert p.as_dict() == \ {(1, 1, 3): 1, (1, 1, 0): 1, (2, 1, 1): 1, (0, 1, 1): 1} assert Poly._permute(p, x) == \ {(2,): y*z, (0,): y*z, (1,): y + y*z**3} assert Poly._permute(p, y) == \ {(1,): x + z + x*z**3 + z*x**2} assert Poly._permute(p, z) == \ {(0,): x*y, (3,): x*y, (1,): y + y*x**2} assert Poly._permute(p, x, y) == \ {(0, 1): z, (1, 1): 1 + z**3, (2, 1): z} assert Poly._permute(p, y, x) == \ {(1, 2): z, (1, 0): z, (1, 1): 1 + z**3} assert Poly._permute(p, x, z) == \ {(0, 1): y, (1, 0): y, (1, 3): y, (2, 1): y} assert Poly._permute(p, z, x) == \ {(1, 2): y, (0, 1): y, (1, 0): y, (3, 1): y} assert Poly._permute(p, y, z) == \ {(1, 0): x, (1, 3): x, (1, 1): 1 + x**2} assert Poly._permute(p, z, y) == \ {(0, 1): x, (3, 1): x, (1, 1): 1 + x**2} q = Poly(x**2*y*z + 2*x*y*z**3 + 3*x*y + 4*y*z, x, y, z) assert q.as_dict() == \ {(1, 1, 3): 2, (1, 1, 0): 3, (2, 1, 1): 1, (0, 1, 1): 4} assert Poly._permute(q, z, y, x) == \ {(0, 1, 1): 3, (1, 1, 0): 4, (3, 1, 1): 2, (1, 1, 2): 1}
def new_poly(V_s, fields, n_coeffs): """Make a new polynomial function that has the same powers as V_s function but with coefficients C1, C2...""" from sympy import Poly from sympy import diff, Symbol, var, simplify, sympify, S from sympy.core.sympify import SympifyError P = Poly(V_s, *fields) d = P.as_dict() e = {} for key in d.iterkeys(): #print d[key], str(d[key]) for i in xrange(1, n_coeffs + 1): if 'C' + str(i) in str(d[key]): e[key] = sympify('C' + str(i)) P2 = Poly(e, *fields) return str(P2.as_basic())
def new_poly(V_s, fields, n_coeffs): """Make a new polynomial function that has the same powers as V_s function but with coefficients C1, C2...""" from sympy import Poly from sympy import diff, Symbol, var, simplify, sympify, S from sympy.core.sympify import SympifyError P = Poly(V_s,*fields) d = P.as_dict() e = {} for key in d.iterkeys(): #print d[key], str(d[key]) for i in xrange(1, n_coeffs+1): if 'C' + str(i) in str(d[key]): e[key] = sympify('C'+str(i)) P2 = Poly(e,*fields) return str(P2.as_basic())
def _expansion_search(expr, rep_list, lib=None): """ Search for and substitute terms that match a series expansion of fundamental math functions. e: expression rep_list: list containing dummy variables """ if debug: print("_expansion_search: ", expr) try: if isinstance(expr, Mul): exprs = [expr] elif isinstance(expr, Add): exprs = expr.args else: return expr if lib is not None: fncs, series = lib else: fncs, series = _fncs, _series newargs = [] for expr in exprs: # Try to simplify the expression if isinstance(expr, Mul): # Split the expression into a LIST of commutative and a LIST of non-commutative factor (i.e. operators). c, nc = expr.args_cnc() if nc and c: # Construct an expression of commutative elements ONLY. expr = Mul(*c).expand() #out = find_expansion(expr, *rep_list) eq = Poly(expr, *rep_list) bdict = eq.as_dict() if any([ n > 1 for n in [sum([i != 0 for i in k]) for k in bdict.keys()] ]): raise ValueError( "Expressions cross-products of symbols are not supported (yet?).." ) # Construct the list of 'vectors' vecl = [] fncl = [] for x in rep_list: for n in range(3): #range(eq.degree()-1): X = pow(x, n) vecl.extend([((X * p.subs(_s, x)).expand() + O(pow(x, eq.degree() + 1))).removeO() for p in series]) fncl.extend([X * f(x) for f in fncs]) vecl.extend( [pow(x, n) for n in range(max(eq.degree(), 3))]) fncl.extend( [pow(x, n) for n in range(max(eq.degree(), 3))]) # Create vector coefficients q = symbols(f'q:{len(vecl)}') # Take inner product of 'vector' list and vector coefficients A = Add(*[x * y for x, y in zip(q, vecl)]).as_poly(*rep_list) terms = [] for k, eq in A.as_dict().items(): terms.append(eq - bdict.get(k, 0)) H = linsolve(terms, *q) h = H.subs({k: 0 for k in q}) out = Add(*[c * fnc for fnc, c in zip(fncl, list(h)[0])]) newargs.append(out * Mul(*nc)) else: newargs.append(expr) else: newargs.append(expr) return Add(*newargs) except Exception as e: print("Failed to identify series expansions: " + str(e)) return e