ex ex_collect_to_ex(const ex_collect_t& ec, const exvector& vars) { exvector ev; ev.reserve(ec.size()); for (std::size_t i = 0; i < ec.size(); ++i) { exvector tv; tv.reserve(vars.size() + 1); for (std::size_t j = 0; j < vars.size(); ++j) { const exp_vector_t& exp_vector(ec[i].first); bug_on(exp_vector.size() != vars.size(), "expected " << vars.size() << " variables, " "expression has " << exp_vector.size() << " instead"); if (exp_vector[j] != 0) tv.push_back(pow(vars[j], exp_vector[j])); } tv.push_back(ec[i].second); ex tmp = dynallocate<mul>(tv); ev.push_back(tmp); } ex ret = dynallocate<add>(ev); return ret; }
static ex make_divide_expr(const exvector& args) { exvector rest_args; rest_args.reserve(args.size() - 1); std::copy(args.begin() + 1, args.end(), std::back_inserter(rest_args)); ex rest_base = (new mul(rest_args))->setflag(status_flags::dynallocated); ex rest = pow(rest_base, *_num_1_p); return (new mul(args[0], rest))->setflag(status_flags::dynallocated); }
exp_vector_t degree_vector(ex e, const exvector& vars) { e = e.expand(); exp_vector_t dvec(vars.size()); for (std::size_t i = vars.size(); i-- != 0; ) { const int deg_i = e.degree(vars[i]); e = e.coeff(vars[i], deg_i); dvec[i] = deg_i; } return dvec; }
/** * Compute the primitive part and the content of a modular multivariate * polynomial e \in Z_p[x_n][x_0, \ldots, x_{n-1}], i.e. e is considered * a polynomial in variables x_0, \ldots, x_{n-1} with coefficients being * modular polynomials Z_p[x_n] * @param e polynomial to operate on * @param pp primitive part of @a e, will be computed by this function * @param c content (in the sense described above) of @a e, will be * computed by this function * @param vars variables x_0, \ldots, x_{n-1}, x_n * @param p modulus */ void primpart_content(ex& pp, ex& c, ex e, const exvector& vars, const long p) { static const ex ex1(1); static const ex ex0(0); e = e.expand(); if (e.is_zero()) { pp = ex0; c = ex1; return; } exvector rest_vars = vars; rest_vars.pop_back(); ex_collect_t ec; collect_vargs(ec, e, rest_vars); if (ec.size() == 1) { // the input polynomial factorizes into // p_1(x_n) p_2(x_0, \ldots, x_{n-1}) c = ec.rbegin()->second; ec.rbegin()->second = ex1; pp = ex_collect_to_ex(ec, rest_vars).expand().smod(numeric(p)); return; } // Start from the leading coefficient (which is stored as a last // element of the terms array) auto i = ec.rbegin(); ex g = i->second; // there are at least two terms, so it's safe to... ++i; while (i != ec.rend() && !g.is_equal(ex1)) { g = euclid_gcd(i->second, g, vars.back(), p); ++i; } if (g.is_equal(ex1)) { pp = e; c = ex1; return; } exvector mainvar; mainvar.push_back(vars.back()); for (i = ec.rbegin(); i != ec.rend(); ++i) { ex tmp(0); if (!divide_in_z_p(i->second, g, tmp, mainvar, p)) throw std::logic_error(std::string(__func__) + ": bogus division failure"); i->second = tmp; } pp = ex_collect_to_ex(ec, rest_vars).expand().smod(numeric(p)); c = g; }
static unsigned check_factorization(const exvector& factors) { ex e = (new mul(factors))->setflag(status_flags::dynallocated); ex ef = factor(e.expand()); if (ef.nops() != factors.size()) { clog << "wrong number of factors, expected " << factors.size() << ", got " << ef.nops(); return 1; } for (size_t i = 0; i < ef.nops(); ++i) { if (find(factors.begin(), factors.end(), ef.op(i)) == factors.end()) { clog << "wrong factorization: term not found: " << ef.op(i); return 1; } } return 0; }
static void collect_term(ex_collect_priv_t& ec, const ex& e, const exvector& vars) { if (e.is_zero()) return; static const ex ex1(1); exp_vector_t key(vars.size()); ex pre_coeff = e; for (std::size_t i = 0; i < vars.size(); ++i) { const int var_i_pow = pre_coeff.degree(vars[i]); key[i] = var_i_pow; pre_coeff = pre_coeff.coeff(vars[i], var_i_pow); } auto i = ec.find(key); if (i != ec.end()) i->second += pre_coeff; else ec.insert(ex_collect_priv_t::value_type(key, pre_coeff)); }
GinacConstFunction::GinacConstFunction( const exvector& exs, const lst& params) : Function( exs.size(), 0, params.nops()), exs_(exs), params_(params) { if(print__all){ //std::cerr << "GinacConstFunction = "; /**/ //for(unsigned int i = 0; i < exs_.size(); i++ ){ /**/ // std::cerr << "[f("<<i<<") ="<<exs_[i] << "] " ; /**/ //} /**/ //std::cerr << std::endl; /**/ //} } void GinacConstFunction::eval(bool do_f, bool do_g, bool do_H){ lst argsAndParams; lst::const_iterator params_it = params_.begin(); for( unsigned int i = 0; params_it != params_.end(); params_it++, i++ ){ argsAndParams.append( ex_to<symbol>(*params_it) == p(i)); } //std::cerr << "NEXT FUNC"<< std::endl; /**/ if (do_f){ for(unsigned int i = 0; i < exs_.size(); i++ ){ f(i) = ex_to<numeric>(exs_[i].subs( argsAndParams )).to_double(); //std::cerr << "f("<<i<<") ="<<f(i)<< std::endl; /**/ } } if (do_g){ clear_g(); } if (do_H){ clear_h(); } }
static ex make_binop_expr(const int binop, const exvector& args) { switch (binop) { case '+': return (new add(args))->setflag(status_flags::dynallocated); case '-': return make_minus_expr(args); case '*': return (new mul(args))->setflag(status_flags::dynallocated); case '/': return make_divide_expr(args); case '^': if (args.size() != 2) throw std::invalid_argument( std::string(__func__) + ": power should have exactly 2 operands"); return pow(args[0], args[1]); default: throw std::invalid_argument( std::string(__func__) + ": invalid binary operation: " + char(binop)); } }
/** * Exact polynomial division of a, b \in Z_p[x_0, \ldots, x_n] * It doesn't check whether the inputs are proper polynomials, so be careful * of what you pass in. * * @param a first multivariate polynomial (dividend) * @param b second multivariate polynomial (divisor) * @param q quotient (returned) * @param var variables X iterator to first element of vector of symbols * * @return "true" when exact division succeeds (the quotient is returned in * q), "false" otherwise. * @note @a p = 0 means the base ring is Z */ bool divide_in_z_p(const ex &a, const ex &b, ex &q, const exvector& vars, const long p) { static const ex _ex1(1); if (b.is_zero()) throw(std::overflow_error("divide_in_z: division by zero")); if (b.is_equal(_ex1)) { q = a; return true; } if (is_exactly_a<numeric>(a)) { if (is_exactly_a<numeric>(b)) { // p == 0 means division in Z if (p == 0) { const numeric tmp = ex_to<numeric>(a/b); if (tmp.is_integer()) { q = tmp; return true; } else return false; } else { q = (a*recip(ex_to<numeric>(b), p)).smod(numeric(p)); return true; } } else return false; } if (a.is_equal(b)) { q = _ex1; return true; } // Main symbol const ex &x = vars.back(); // Compare degrees int adeg = a.degree(x), bdeg = b.degree(x); if (bdeg > adeg) return false; // Polynomial long division (recursive) ex r = a.expand(); if (r.is_zero()) return true; int rdeg = adeg; ex eb = b.expand(); ex blcoeff = eb.coeff(x, bdeg); exvector v; v.reserve(std::max(rdeg - bdeg + 1, 0)); exvector rest_vars(vars); rest_vars.pop_back(); while (rdeg >= bdeg) { ex term, rcoeff = r.coeff(x, rdeg); if (!divide_in_z_p(rcoeff, blcoeff, term, rest_vars, p)) break; term = (term*power(x, rdeg - bdeg)).expand(); v.push_back(term); r = (r - term*eb).expand(); if (p != 0) r = r.smod(numeric(p)); if (r.is_zero()) { q = (new add(v))->setflag(status_flags::dynallocated); return true; } rdeg = r.degree(x); } return false; }
static ex lst_reader(const exvector& ev) { return GiNaC::lst(ev.begin(), ev.end()); }