1 | // emacs edit mode for this file is -*- C++ -*- |
---|
2 | /**************************************** |
---|
3 | * Computer Algebra System SINGULAR * |
---|
4 | ****************************************/ |
---|
5 | /* |
---|
6 | * ABSTRACT: convert data between Singular and Flint |
---|
7 | */ |
---|
8 | |
---|
9 | |
---|
10 | |
---|
11 | #include "misc/auxiliary.h" |
---|
12 | #include "flintconv.h" |
---|
13 | |
---|
14 | #ifdef HAVE_FLINT |
---|
15 | #if __FLINT_RELEASE >= 20500 |
---|
16 | #include "coeffs/coeffs.h" |
---|
17 | #include "coeffs/longrat.h" |
---|
18 | #include "polys/monomials/p_polys.h" |
---|
19 | |
---|
20 | #include "polys/sbuckets.h" |
---|
21 | #include "polys/clapconv.h" |
---|
22 | |
---|
23 | #include "simpleideals.h" |
---|
24 | |
---|
25 | |
---|
26 | int convFlintISingI (fmpz_t f) |
---|
27 | { |
---|
28 | int res; |
---|
29 | res = fmpz_get_si(f); |
---|
30 | return res; |
---|
31 | } |
---|
32 | |
---|
33 | void convSingIFlintI(fmpz_t f, int p) |
---|
34 | { |
---|
35 | fmpz_init(f); |
---|
36 | fmpz_set_si(f,p); |
---|
37 | return; |
---|
38 | } |
---|
39 | |
---|
40 | void convFlintNSingN (mpz_t z, fmpz_t f) |
---|
41 | { |
---|
42 | mpz_init(z); |
---|
43 | fmpz_get_mpz(z,f); |
---|
44 | } |
---|
45 | |
---|
46 | number convFlintNSingN (fmpz_t f) |
---|
47 | { |
---|
48 | mpz_t z; |
---|
49 | mpz_init(z); |
---|
50 | fmpz_get_mpz(z,f); |
---|
51 | number n; |
---|
52 | nlMPZ(z,n,NULL); |
---|
53 | return n; |
---|
54 | } |
---|
55 | number convFlintNSingN (fmpq_t f, const coeffs cf) |
---|
56 | { |
---|
57 | #if __FLINT_RELEASE > 20502 |
---|
58 | number z; |
---|
59 | if (nCoeff_is_Q(cf)) |
---|
60 | { |
---|
61 | z=ALLOC_RNUMBER(); |
---|
62 | #if defined(LDEBUG) |
---|
63 | z->debug=123456; |
---|
64 | #endif |
---|
65 | mpz_init(z->z); |
---|
66 | mpz_init(z->n); |
---|
67 | fmpq_get_mpz_frac(z->z,z->n,f); |
---|
68 | } |
---|
69 | else |
---|
70 | { |
---|
71 | mpz_t a,b; |
---|
72 | mpz_init(a); |
---|
73 | mpz_init(b); |
---|
74 | fmpq_get_mpz_frac(a,b,f); |
---|
75 | number na=n_InitMPZ(a,cf); |
---|
76 | number nb=n_InitMPZ(b,cf); |
---|
77 | z=n_Div(na,nb,cf); |
---|
78 | n_Delete(&na,cf); |
---|
79 | n_Delete(&nb,cf); |
---|
80 | mpz_clear(a); |
---|
81 | mpz_clear(b); |
---|
82 | } |
---|
83 | n_Normalize(z,cf); |
---|
84 | return z; |
---|
85 | #else |
---|
86 | WerrorS("not implemented"); |
---|
87 | return NULL; |
---|
88 | #endif |
---|
89 | } |
---|
90 | |
---|
91 | void convSingNFlintN(fmpz_t f, mpz_t n) |
---|
92 | { |
---|
93 | fmpz_init(f); |
---|
94 | fmpz_set_mpz(f,n); |
---|
95 | } |
---|
96 | |
---|
97 | void convSingNFlintN(fmpz_t f, number n) |
---|
98 | { |
---|
99 | fmpz_init(f); |
---|
100 | fmpz_set_mpz(f,(mpz_ptr)n); |
---|
101 | } |
---|
102 | |
---|
103 | void convSingNFlintN(fmpq_t f, number n, const coeffs cf) |
---|
104 | { |
---|
105 | if (nCoeff_is_Q(cf)) |
---|
106 | { |
---|
107 | fmpq_init(f); |
---|
108 | if (SR_HDL(n)&SR_INT) |
---|
109 | fmpq_set_si(f,SR_TO_INT(n),1); |
---|
110 | else if (n->s<3) |
---|
111 | { |
---|
112 | fmpz_set_mpz(fmpq_numref(f), n->z); |
---|
113 | fmpz_set_mpz(fmpq_denref(f), n->n); |
---|
114 | } |
---|
115 | else |
---|
116 | { |
---|
117 | mpz_t one; |
---|
118 | mpz_init_set_si(one,1); |
---|
119 | fmpz_set_mpz(fmpq_numref(f), n->z); |
---|
120 | fmpz_set_mpz(fmpq_denref(f), one); |
---|
121 | mpz_clear(one); |
---|
122 | } |
---|
123 | } |
---|
124 | else |
---|
125 | { |
---|
126 | coeffs QQ=nInitChar(n_Q,NULL); |
---|
127 | nMapFunc nMap=n_SetMap(cf,QQ); |
---|
128 | if (nMap!=NULL) |
---|
129 | { |
---|
130 | number nn=nMap(n,cf,QQ); |
---|
131 | convSingNFlintN(f,nn,QQ); |
---|
132 | } |
---|
133 | nKillChar(QQ); |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | void convSingNFlintNN(fmpq_t re, fmpq_t im, number n, const coeffs cf) |
---|
138 | { |
---|
139 | number n_2=n_RePart(n,cf); |
---|
140 | convSingNFlintN(re,n_2,cf); |
---|
141 | n_Delete(&n_2,cf); |
---|
142 | n_2=n_ImPart(n,cf); |
---|
143 | convSingNFlintN(im,n_2,cf); |
---|
144 | n_Delete(&n_2,cf); |
---|
145 | } |
---|
146 | |
---|
147 | void convSingPFlintP(fmpq_poly_t res, poly p, const ring r) |
---|
148 | { |
---|
149 | int d=p_GetExp(p,1,r); |
---|
150 | fmpq_poly_init2(res,d+1); |
---|
151 | _fmpq_poly_set_length (res, d + 1); |
---|
152 | while(p!=NULL) |
---|
153 | { |
---|
154 | number n=pGetCoeff(p); |
---|
155 | fmpq_t c; |
---|
156 | convSingNFlintN(c,n,r->cf); |
---|
157 | fmpq_poly_set_coeff_fmpq(res,p_GetExp(p,1,r),c); |
---|
158 | fmpq_clear(c); |
---|
159 | pIter(p); |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | void convSingImPFlintP(fmpq_poly_t res, poly p, const ring r) |
---|
164 | { |
---|
165 | int d=p_GetExp(p,1,r); |
---|
166 | fmpq_poly_init2(res,d+1); |
---|
167 | _fmpq_poly_set_length (res, d + 1); |
---|
168 | while(p!=NULL) |
---|
169 | { |
---|
170 | number n=n_ImPart(pGetCoeff(p),r->cf); |
---|
171 | fmpq_t c; |
---|
172 | convSingNFlintN(c,n,r->cf); |
---|
173 | fmpq_poly_set_coeff_fmpq(res,p_GetExp(p,1,r),c); |
---|
174 | fmpq_clear(c); |
---|
175 | n_Delete(&n,r->cf); |
---|
176 | pIter(p); |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | poly convFlintPSingP(fmpq_poly_t f, const ring r) |
---|
181 | { |
---|
182 | int d=fmpq_poly_length(f); |
---|
183 | poly p=NULL; |
---|
184 | for(int i=0; i<=d; i++) |
---|
185 | { |
---|
186 | fmpq_t c; |
---|
187 | fmpq_poly_get_coeff_fmpq(c,f,i); |
---|
188 | number n=convFlintNSingN(c,r->cf); |
---|
189 | poly pp=p_Init(r); |
---|
190 | pSetCoeff0(pp,n); |
---|
191 | p_SetExp(pp,1,i,r); |
---|
192 | p_Setm(pp,r); |
---|
193 | p=p_Add_q(p,pp,r); |
---|
194 | } |
---|
195 | return p; |
---|
196 | } |
---|
197 | |
---|
198 | bigintmat* singflint_LLL(bigintmat* m, bigintmat* T) |
---|
199 | { |
---|
200 | int r=m->rows(); |
---|
201 | int c=m->cols(); |
---|
202 | bigintmat* res=new bigintmat(r,c,m->basecoeffs()); |
---|
203 | fmpz_mat_t M, Transf; |
---|
204 | fmpz_mat_init(M, r, c); |
---|
205 | if(T != NULL) |
---|
206 | { |
---|
207 | fmpz_mat_init(Transf, T->rows(), T->rows()); |
---|
208 | } |
---|
209 | fmpz_t dummy; |
---|
210 | mpz_t n; |
---|
211 | int i,j; |
---|
212 | for(i=r;i>0;i--) |
---|
213 | { |
---|
214 | for(j=c;j>0;j--) |
---|
215 | { |
---|
216 | n_MPZ(n, BIMATELEM(*m, i, j),m->basecoeffs()); |
---|
217 | convSingNFlintN(dummy,n); |
---|
218 | mpz_clear(n); |
---|
219 | fmpz_set(fmpz_mat_entry(M, i-1, j-1), dummy); |
---|
220 | fmpz_clear(dummy); |
---|
221 | } |
---|
222 | } |
---|
223 | if(T != NULL) |
---|
224 | { |
---|
225 | for(i=T->rows();i>0;i--) |
---|
226 | { |
---|
227 | for(j=T->rows();j>0;j--) |
---|
228 | { |
---|
229 | n_MPZ(n, BIMATELEM(*T, i, j),T->basecoeffs()); |
---|
230 | convSingNFlintN(dummy,n); |
---|
231 | mpz_clear(n); |
---|
232 | fmpz_set(fmpz_mat_entry(Transf, i-1, j-1), dummy); |
---|
233 | fmpz_clear(dummy); |
---|
234 | } |
---|
235 | } |
---|
236 | } |
---|
237 | fmpz_lll_t fl; |
---|
238 | fmpz_lll_context_init_default(fl); |
---|
239 | if(T != NULL) |
---|
240 | fmpz_lll(M, Transf, fl); |
---|
241 | else |
---|
242 | fmpz_lll(M, NULL, fl); |
---|
243 | for(i=r;i>0;i--) |
---|
244 | { |
---|
245 | for(j=c;j>0;j--) |
---|
246 | { |
---|
247 | convFlintNSingN(n, fmpz_mat_entry(M, i-1, j-1)); |
---|
248 | n_Delete(&(BIMATELEM(*res,i,j)),res->basecoeffs()); |
---|
249 | BIMATELEM(*res,i,j)=n_InitMPZ(n,res->basecoeffs()); |
---|
250 | mpz_clear(n); |
---|
251 | } |
---|
252 | } |
---|
253 | if(T != NULL) |
---|
254 | { |
---|
255 | for(i=T->rows();i>0;i--) |
---|
256 | { |
---|
257 | for(j=T->cols();j>0;j--) |
---|
258 | { |
---|
259 | convFlintNSingN(n, fmpz_mat_entry(Transf, i-1, j-1)); |
---|
260 | n_Delete(&(BIMATELEM(*T,i,j)),T->basecoeffs()); |
---|
261 | BIMATELEM(*T,i,j)=n_InitMPZ(n,T->basecoeffs()); |
---|
262 | mpz_clear(n); |
---|
263 | } |
---|
264 | } |
---|
265 | } |
---|
266 | return res; |
---|
267 | } |
---|
268 | |
---|
269 | intvec* singflint_LLL(intvec* m, intvec* T) |
---|
270 | { |
---|
271 | int r=m->rows(); |
---|
272 | int c=m->cols(); |
---|
273 | intvec* res = new intvec(r,c,(int)0); |
---|
274 | fmpz_mat_t M,Transf; |
---|
275 | fmpz_mat_init(M, r, c); |
---|
276 | if(T != NULL) |
---|
277 | fmpz_mat_init(Transf, r, r); |
---|
278 | fmpz_t dummy; |
---|
279 | int i,j; |
---|
280 | for(i=r;i>0;i--) |
---|
281 | { |
---|
282 | for(j=c;j>0;j--) |
---|
283 | { |
---|
284 | convSingIFlintI(dummy,IMATELEM(*m,i,j)); |
---|
285 | fmpz_set(fmpz_mat_entry(M, i-1, j-1), dummy); |
---|
286 | fmpz_clear(dummy); |
---|
287 | } |
---|
288 | } |
---|
289 | if(T != NULL) |
---|
290 | { |
---|
291 | for(i=T->rows();i>0;i--) |
---|
292 | { |
---|
293 | for(j=T->rows();j>0;j--) |
---|
294 | { |
---|
295 | convSingIFlintI(dummy,IMATELEM(*T,i,j)); |
---|
296 | fmpz_set(fmpz_mat_entry(Transf, i-1, j-1), dummy); |
---|
297 | fmpz_clear(dummy); |
---|
298 | } |
---|
299 | } |
---|
300 | } |
---|
301 | fmpz_lll_t fl; |
---|
302 | fmpz_lll_context_init_default(fl); |
---|
303 | if(T != NULL) |
---|
304 | fmpz_lll(M, Transf, fl); |
---|
305 | else |
---|
306 | fmpz_lll(M, NULL, fl); |
---|
307 | for(i=r;i>0;i--) |
---|
308 | { |
---|
309 | for(j=c;j>0;j--) |
---|
310 | { |
---|
311 | IMATELEM(*res,i,j)=convFlintISingI(fmpz_mat_entry(M, i-1, j-1)); |
---|
312 | } |
---|
313 | } |
---|
314 | if(T != NULL) |
---|
315 | { |
---|
316 | for(i=Transf->r;i>0;i--) |
---|
317 | { |
---|
318 | for(j=Transf->r;j>0;j--) |
---|
319 | { |
---|
320 | IMATELEM(*T,i,j)=convFlintISingI(fmpz_mat_entry(Transf, i-1, j-1)); |
---|
321 | } |
---|
322 | } |
---|
323 | } |
---|
324 | return res; |
---|
325 | } |
---|
326 | #endif |
---|
327 | #endif |
---|