Macaulay2 Engine
Loading...
Searching...
No Matches

◆ CRA() [2/5]

ring_elem ChineseRemainder::CRA ( const PolyRing * R,
const ring_elem f,
const ring_elem g,
mpz_srcptr um,
mpz_srcptr vn,
mpz_srcptr mn )
static

Definition at line 53 of file cra.cpp.

59{
60 mpz_t result_coeff;
61 mpz_t mn_half;
62 mpz_init(result_coeff);
63 mpz_init(mn_half);
64 Nterm *f = ff;
65 Nterm *g = gg;
66 Nterm head;
67 Nterm *result = &head;
68
69 mpz_tdiv_q_2exp(mn_half, mn, 1);
70
71 const Monoid *M = R->getMonoid();
72 const Ring *K = R->getCoefficientRing();
73
74 while (1)
75 {
76 if (g == nullptr)
77 {
78 // mult each term of f by n:
79 for (; f != nullptr; f = f->next)
80 {
81 result->next = R->new_term();
83 result->next = nullptr;
84 M->copy(f->monom, result->monom);
85 mpz_mul(result_coeff, f->coeff.get_mpz(), vn);
86 mpz_mod(result_coeff, result_coeff, mn);
87 if (mpz_cmp(result_coeff, mn_half) > 0)
88 {
89 mpz_sub(result_coeff, result_coeff, mn);
90 }
91 result->coeff = K->from_int(result_coeff);
92 }
93 break;
94 }
95 if (f == nullptr)
96 {
97 // mult each term of g by n:
98 for (; g != nullptr; g = g->next)
99 {
100 result->next = R->new_term();
101 result = result->next;
102 result->next = nullptr;
103 M->copy(g->monom, result->monom);
104 mpz_mul(result_coeff, g->coeff.get_mpz(), um);
105 mpz_mod(result_coeff, result_coeff, mn);
106 if (mpz_cmp(result_coeff, mn_half) > 0)
107 {
108 mpz_sub(result_coeff, result_coeff, mn);
109 }
110 result->coeff = K->from_int(result_coeff);
111 }
112 break;
113 }
114 switch (M->compare(f->monom, g->monom))
115 {
116 case -1:
117 result->next = R->new_term();
118 result = result->next;
119 result->next = nullptr;
120 M->copy(g->monom, result->monom);
121 mpz_mul(result_coeff, g->coeff.get_mpz(), um);
122 result->coeff = K->from_int(result_coeff);
123 g = g->next;
124 break;
125 case 1:
126 result->next = R->new_term();
127 result = result->next;
128 result->next = nullptr;
129 M->copy(f->monom, result->monom);
130 mpz_mul(result_coeff, f->coeff.get_mpz(), vn);
131 result->coeff = K->from_int(result_coeff);
132 f = f->next;
133 break;
134 case 0:
135 Nterm *tmf = f;
136 Nterm *tmg = g;
137 f = f->next;
138 g = g->next;
139 CRA0(tmf->coeff.get_mpz(),
140 tmg->coeff.get_mpz(),
141 um,
142 vn,
143 mn,
144 result_coeff);
145 Nterm *t = R->new_term();
146 M->copy(tmf->monom, t->monom);
147 t->coeff = K->from_int(result_coeff);
148 t->next = nullptr;
149 result->next = t;
150 result = t;
151 break;
152 }
153 }
154
155 mpz_clear(result_coeff);
156 mpz_clear(mn_half);
157 result->next = nullptr;
158 return head.next;
159}
static void CRA0(mpz_srcptr a, mpz_srcptr b, mpz_srcptr um, mpz_srcptr vn, mpz_srcptr mn, mpz_t result)
Definition cra.cpp:15
int compare(int nslots, const_monomial m, const_monomial n) const
Definition monoid.hpp:226
void copy(const_monomial m, monomial result) const
Definition monoid.cpp:475
Nterm * new_term() const
Definition poly.cpp:146
const Ring * getCoefficientRing() const
Definition polyring.hpp:200
virtual const Monoid * getMonoid() const
Definition polyring.hpp:282
virtual ring_elem from_int(mpz_srcptr n) const =0
VALGRIND_MAKE_MEM_DEFINED & result(result)
Nterm * next
Definition ringelem.hpp:157
ring_elem coeff
Definition ringelem.hpp:158
int monom[1]
Definition ringelem.hpp:160
mpz_srcptr get_mpz() const
Definition ringelem.hpp:127

References Nterm::coeff, Monoid::compare(), Monoid::copy(), CRA0(), Ring::from_int(), ring_elem::get_mpz(), PolynomialRing::getCoefficientRing(), PolynomialRing::getMonoid(), Nterm::monom, PolyRing::new_term(), Nterm::next, and result().

Referenced by CRA(), CRA(), CRA(), rawMatrixCRA(), and rawRingElementCRA().