1 /** @file euclid_gcd_wrap.h
3 * Euclidean GCD and supporting functions. */
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
23 #ifndef GINAC_PGCD_EUCLID_GCD_H
24 #define GINAC_PGCD_EUCLID_GCD_H
27 #include "gcd_euclid.h"
28 #include "smod_helpers.h"
31 #include "operators.h"
33 #include "relational.h"
38 static void ex2upoly(umodpoly& u, ex e, const ex& var, const long p)
41 cln::cl_modint_ring R = cln::find_modint_ring(cln::cl_I(p));
42 u.resize(e.degree(var) + 1);
43 for (int i = 0; i <= e.degree(var); ++i) {
44 ex ce = e.coeff(var, i);
45 bug_on(!is_a<numeric>(ce), "i = " << i << ", " <<
46 "coefficient is not a number: " << ce);
47 const cln::cl_I c = to_cl_I(ce);
48 u[i] = R->canonhom(c);
52 static ex umodpoly2ex(const umodpoly& a, const ex& var, const long p)
54 cln::cl_modint_ring R = cln::find_modint_ring(cln::cl_I(p));
55 const numeric pnum(p);
56 exvector ev(a.size());
57 for (std::size_t i = a.size(); i-- != 0; ) {
58 const cln::cl_I c = smod(R->retract(a[i]), p);
59 const ex term = numeric(c)*power(var, i);
62 ex ret = dynallocate<add>(ev);
66 static ex euclid_gcd(ex A, ex B, const ex& var, const long p)
72 ex2upoly(a, A, var, p);
73 ex2upoly(b, B, var, p);
76 ex ge = umodpoly2ex(g, var, p);
82 #endif // ndef GINAC_PGCD_EUCLID_GCD_H