]> www.ginac.de Git - ginac.git/blob - ginac/polynomial/poly_cra.h
87ca8a4272fbf6dff58b4c0a1fa2a6325b2e7411
[ginac.git] / ginac / polynomial / poly_cra.h
1 /** @file poly_cra.h
2  *
3  *  Chinese remainder algorithm. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2018 Johannes Gutenberg University Mainz, Germany
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #ifndef GINAC_POLY_CRA_H
24 #define GINAC_POLY_CRA_H
25
26 #include "ex.h"
27 #include "smod_helpers.h"
28
29 #include <cln/integer.h>
30
31 namespace GiNaC {
32
33 /**
34  * @brief Chinese reamainder algorithm for polynomials.
35  *
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$ 
40  */
41 ex chinese_remainder(const ex& e1, const cln::cl_I& q1,
42                      const ex& e2, const long q2)
43 {
44         // res = v_1 + v_2 q_1
45         // v_1 = e_1 mod q_1
46         // v_2 = (e_2 - v_1)/q_1 mod q_2
47         const numeric q2n(q2);
48         const numeric q1n(q1);
49         ex v1 = e1.smod(q1n);
50         ex u = v1.smod(q2n);
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();
55         return ret;
56 }
57
58 } // namespace GiNaC
59
60 #endif // ndef GINAC_POLY_CRA_H