3 * Chinese remainder algorithm. */
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_POLY_CRA_H
24 #define GINAC_POLY_CRA_H
27 #include "smod_helpers.h"
29 #include <cln/integer.h>
34 * @brief Chinese reamainder algorithm for polynomials.
36 * Given two polynomials \f$e_1 \in Z_{q_1}[x_1, \ldots, x_n]\f$ and
37 * \f$e_2 \in Z_{q_2}[x_1, \ldots, x_n]\f$, compute the polynomial
38 * \f$r \in Z_{q_1 q_2}[x_1, \ldots, x_n]\f$ such that \f$ r mod q_1 = e_1\f$
39 * and \f$ r mod q_2 = e_2 \f$
41 ex chinese_remainder(const ex& e1, const cln::cl_I& q1,
42 const ex& e2, const long q2)
44 // res = v_1 + v_2 q_1
46 // v_2 = (e_2 - v_1)/q_1 mod q_2
47 const numeric q2n(q2);
48 const numeric q1n(q1);
51 ex v2 = (e2.smod(q2n) - v1.smod(q2n)).expand().smod(q2n);
52 const numeric q1_1(recip(q1, q2)); // 1/q_1 mod q_2
53 v2 = (v2*q1_1).smod(q2n);
54 ex ret = (v1 + v2*q1n).expand();
60 #endif // ndef GINAC_POLY_CRA_H