1 /** @file primpart_content.cpp
3 * Function to find primitive part of a multivariate polynomial. */
6 * GiNaC Copyright (C) 1999-2022 Johannes Gutenberg University Mainz, Germany
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "collect_vargs.h"
26 #include "euclid_gcd_wrap.h"
27 #include "divide_in_z_p.h"
33 * Compute the primitive part and the content of a modular multivariate
34 * polynomial e \in Z_p[x_n][x_0, \ldots, x_{n-1}], i.e. e is considered
35 * a polynomial in variables x_0, \ldots, x_{n-1} with coefficients being
36 * modular polynomials Z_p[x_n]
37 * @param e polynomial to operate on
38 * @param pp primitive part of @a e, will be computed by this function
39 * @param c content (in the sense described above) of @a e, will be
40 * computed by this function
41 * @param vars variables x_0, \ldots, x_{n-1}, x_n
44 void primpart_content(ex& pp, ex& c, ex e, const exvector& vars,
47 static const ex ex1(1);
48 static const ex ex0(0);
55 exvector rest_vars = vars;
58 collect_vargs(ec, e, rest_vars);
61 // the input polynomial factorizes into
62 // p_1(x_n) p_2(x_0, \ldots, x_{n-1})
63 c = ec.rbegin()->second;
64 ec.rbegin()->second = ex1;
65 pp = ex_collect_to_ex(ec, rest_vars).expand().smod(numeric(p));
69 // Start from the leading coefficient (which is stored as a last
70 // element of the terms array)
73 // there are at least two terms, so it's safe to...
75 while (i != ec.rend() && !g.is_equal(ex1)) {
76 g = euclid_gcd(i->second, g, vars.back(), p);
79 if (g.is_equal(ex1)) {
85 mainvar.push_back(vars.back());
86 for (i = ec.rbegin(); i != ec.rend(); ++i) {
88 if (!divide_in_z_p(i->second, g, tmp, mainvar, p))
89 throw std::logic_error(std::string(__func__) +
90 ": bogus division failure");
94 pp = ex_collect_to_ex(ec, rest_vars).expand().smod(numeric(p));