]> www.ginac.de Git - ginac.git/blob - ginac/polynomial/ring_traits.h
[BUGFIX] Fix crash in parser.
[ginac.git] / ginac / polynomial / ring_traits.h
1 /** @file ring_traits.h
2  *
3  *  Functions for polynomial ring arithmetic. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2020 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_RING_TRAITS_H
24 #define GINAC_RING_TRAITS_H
25
26 #include <cln/integer.h>
27 #include <cln/modinteger.h>
28
29 namespace cln {
30
31 static inline cln::cl_I div(const cln::cl_I& x, const cln::cl_I& y)
32 {
33         return cln::exquo(x, y);
34 }
35
36 /// Exact integer division.
37 /// Check if y divides x, if yes put the quotient into q, otherwise don't
38 /// touch q. Returns true if y divides x and false if not.
39 static inline bool div(cln::cl_I& q, const cln::cl_I& x, const cln::cl_I& y)
40 {
41         const cln::cl_I_div_t qr = cln::truncate2(x, y);
42         if (zerop(qr.remainder)) {
43                 q = qr.quotient;
44                 return true;
45         }
46         return false;
47 }
48
49 static inline cln::cl_I get_ring_elt(const cln::cl_I& sample, const int val)
50 {
51         return cln::cl_I(val);
52 }
53
54 static inline cln::cl_MI get_ring_elt(const cln::cl_MI& sample, const int val)
55 {
56         return sample.ring()->canonhom(val);
57 }
58
59 template<typename T>
60 static inline T the_one(const T& sample)
61 {
62         return get_ring_elt(sample, 1);
63 }
64
65 } // namespace cln
66
67 #endif // GINAC_RING_TRAITS_H