def _set_var(self, rep, var): if len(var) == 0: self.rep = rep return (rep.inner_vars, tuple()) else: self.rep = rep.sort_vars(tuple_union(var, rep.inner_vars)) return var, tuple_minus(rep.inner_vars, var)
def _inner_vars(self): vars_ = (self.var, ) inner_ = tuple() for c in self: if isinstance(c, DP): inner_ = tuple_union(inner_, c.inner_vars) else: continue return vars_ + inner_
def _add(f, g): if g.inner_vars == f.inner_vars: m = min(g.degree(non_negative=True), f.degree(non_negative=True)) + 1 coeffs_ = [f[i] + g[i] for i in range(m)] if f.deg > g.deg: coeffs_ += f[m:] else: coeffs_ += g[m:] return dp(f.var, coeffs_) else: vars_ = tuple_union(f.inner_vars, g.inner_vars) return f.sort_vars(vars_)._add(g.sort_vars(vars_))
def diff(f, *var): validate_type(f, Poly) if len(var) > 1: raise ValueError( "the number of variable argument must be 0 or 1, not %s" % str(len(var))) elif len(var) == 1: var_ = var[0] sorted_vars = tuple_union(var, tuple_minus(f.inner_vars, var)) rep_ = f.rep.sort_vars(sorted_vars) else: var_ = f.indet_vars[0] rep_ = f.rep return poly(rep_.diff(var_), *f.indet_vars, dom=f.dom)
def _mul(f, g): if g.inner_vars == f.inner_vars: f_d, g_d = f.degree(non_negative=True), g.degree(non_negative=True) maxdeg = f_d + g_d coeffs_ = [] for i in range(maxdeg + 1): co_ = 0 for j in range(i + 1): if j < f_d + 1 and i - j < g_d + 1: co_ += f[j] * g[i - j] else: pass coeffs_.append(co_) return dp(f.var, coeffs_) else: vars_ = tuple_union(f.inner_vars, g.inner_vars) return f.sort_vars(vars_)._mul(g.sort_vars(vars_))
def __init__(self, *var, **options): self.options = dict() if "mod" in options: self.mod = self.options["mod"] = options["mod"] else: self.mod = 0 if "rel" in options and not isinstance(options["rel"], int): self.rel = self.options["rel"] = relation(options["rel"]) var_tuple_ = tuple(r["var"] for r in self.rel) self.const_vars = self.options["const_vars"] = tuple_union( var, var_tuple_) else: self.rel = 0 if len(var) == 0: self.const_vars = tuple() else: self.const_vars = self.options["const_vars"] = var
def __new__(cls, rep, *var, **options): if "dom" in options: """ reduce rep with options["dom"] """ if isinstance(rep, (DP, Poly, Symbol)): if isinstance(rep, DP): rep = options["dom"].reduce(rep) elif isinstance(rep, Poly): rep = rep.reduce() else: rep = options["dom"].reduce(rep.as_dp()) if rep.is_constant(): rep = rep.get_constant() var_ = tuple_union(options["dom"].indet_vars, var) else: var_ = var if isinstance(rep, int): self = super().__new__(Integer) elif isinstance(rep, Symbol): if rep in var: self = super().__new__(Poly) else: self = super().__new__(Constant) elif isinstance(rep, DP): if len(var_) > 0: if np.any( np.array(rep.degree(*var_)) > np.array((0, ) * len(var_))): self = super().__new__(Poly) else: self = super().__new__(Constant) else: self = super().__new__(Poly) else: raise TypeError( "rep must be int, Symbol, DP, Poly, Contant or Integer object, not %s" % rep.__class__.__name__) return self
def __init__(self, rep, *var, **options): if isinstance(rep, Poly): self.rep = rep.rep self.indet_vars = rep.indet_vars self.const_vars = rep.const_vars self.inner_vars = rep.inner_vars self.coeff_dom = rep.coeff_dom self.dom = rep.dom else: if isinstance(rep, DP): pass elif isinstance(rep, int): rep = dp_from_int(rep, var) elif isinstance(rep, Symbol): rep = rep.as_dp() else: raise TypeError("rep must be Poly or DP, not %s" % rep.__class__.__name__) """ * Set domain options Given "dom" option, initialize with it. Not given, construct PolynomialRing object and set to "dom" option """ if "dom" in options: validate_type(options["dom"], PolynomialRing) dom = options["dom"] self.indet_vars, self.const_vars = self._set_var( rep, tuple_union(dom.indet_vars, var)) self.inner_vars = tuple_union(self.indet_vars, self.const_vars) self.coeff_dom = ring(*self.const_vars, mod=dom.coeff_dom.mod, rel=dom.coeff_dom.rel) self.dom = polynomialring(*self.indet_vars, coeff_dom=self.coeff_dom, quo=dom.quo) else: self.indet_vars, self.const_vars = self._set_var(rep, var) self.inner_vars = tuple_union(self.indet_vars, self.const_vars) if "coeff_dom" in options: validate_type(options["coeff_dom"], Ring) self.coeff_dom = options["coeff_dom"] else: if "mod" in options: validate_type(options["mod"], int) mod = options["mod"] else: mod = 0 if "rel" in options: validate_type(options["rel"], int, tuple, DP, Relation) rel = options["rel"] else: rel = 0 self.coeff_dom = ring(*self.const_vars, mod=mod, rel=rel) if len(self.coeff_dom.const_vars) == 0: pass else: self.rep = self.rep.sort_vars( tuple_union(self.inner_vars, self.coeff_dom.const_vars)) if "quo" in options: validate_type(options["rel"], int, tuple, DP, Relation) quo = options["rel"] else: quo = 0 self.dom = polynomialring(*self.indet_vars, coeff_dom=self.coeff_dom, quo=quo) self.rep = self.reduce()