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 | //return fmpz_get_si(f); |
---|
29 | return (int)*f; |
---|
30 | } |
---|
31 | |
---|
32 | void convSingIFlintI(fmpz_t f, int p) |
---|
33 | { |
---|
34 | fmpz_init(f); |
---|
35 | *f=p; |
---|
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 | number n; |
---|
49 | if(COEFF_IS_MPZ(*f)) |
---|
50 | nlMPZ(COEFF_TO_PTR(*f),n,NULL); |
---|
51 | else |
---|
52 | { |
---|
53 | mpz_t z; |
---|
54 | mpz_init(z); |
---|
55 | fmpz_get_mpz(z,f); |
---|
56 | nlMPZ(z,n,NULL); |
---|
57 | mpz_clear(z); |
---|
58 | } |
---|
59 | return n; |
---|
60 | } |
---|
61 | |
---|
62 | number convFlintNSingN (fmpq_t f, const coeffs cf) |
---|
63 | { |
---|
64 | #if __FLINT_RELEASE > 20502 |
---|
65 | number z; |
---|
66 | if (nCoeff_is_Q(cf)) |
---|
67 | { |
---|
68 | z=ALLOC_RNUMBER(); |
---|
69 | #if defined(LDEBUG) |
---|
70 | z->debug=123456; |
---|
71 | #endif |
---|
72 | z->s=0; |
---|
73 | mpz_init(z->z); |
---|
74 | mpz_init(z->n); |
---|
75 | fmpq_get_mpz_frac(z->z,z->n,f); |
---|
76 | } |
---|
77 | else |
---|
78 | { |
---|
79 | mpz_t a,b; |
---|
80 | mpz_init(a); |
---|
81 | mpz_init(b); |
---|
82 | fmpq_get_mpz_frac(a,b,f); |
---|
83 | number na=n_InitMPZ(a,cf); |
---|
84 | number nb=n_InitMPZ(b,cf); |
---|
85 | z=n_Div(na,nb,cf); |
---|
86 | n_Delete(&na,cf); |
---|
87 | n_Delete(&nb,cf); |
---|
88 | mpz_clear(a); |
---|
89 | mpz_clear(b); |
---|
90 | } |
---|
91 | n_Normalize(z,cf); |
---|
92 | n_Test(z,cf); |
---|
93 | return z; |
---|
94 | #else |
---|
95 | WerrorS("not implemented"); |
---|
96 | return NULL; |
---|
97 | #endif |
---|
98 | } |
---|
99 | |
---|
100 | number convFlintNSingN (fmpz_t f, const coeffs cf) |
---|
101 | { |
---|
102 | #if __FLINT_RELEASE > 20502 |
---|
103 | number z; |
---|
104 | mpz_t a; |
---|
105 | mpz_init(a); |
---|
106 | fmpz_get_mpz(a,f); |
---|
107 | z=n_InitMPZ(a,cf); |
---|
108 | mpz_clear(a); |
---|
109 | n_Normalize(z,cf); |
---|
110 | n_Test(z,cf); |
---|
111 | return z; |
---|
112 | #else |
---|
113 | WerrorS("not implemented"); |
---|
114 | return NULL; |
---|
115 | #endif |
---|
116 | } |
---|
117 | |
---|
118 | number convFlintNSingN_QQ (fmpq_t f, const coeffs cf) |
---|
119 | { |
---|
120 | #if __FLINT_RELEASE > 20502 |
---|
121 | if (fmpz_is_one(fmpq_denref(f))) |
---|
122 | { |
---|
123 | if (fmpz_fits_si(fmpq_numref(f))) |
---|
124 | { |
---|
125 | long i=fmpz_get_si(fmpq_numref(f)); |
---|
126 | return n_Init(i,cf); |
---|
127 | } |
---|
128 | } |
---|
129 | number z=ALLOC_RNUMBER(); |
---|
130 | #if defined(LDEBUG) |
---|
131 | z->debug=123456; |
---|
132 | #endif |
---|
133 | mpz_init(z->z); |
---|
134 | if (fmpz_is_one(fmpq_denref(f))) |
---|
135 | { |
---|
136 | z->s=3; |
---|
137 | fmpz_get_mpz(z->z,fmpq_numref(f)); |
---|
138 | } |
---|
139 | else |
---|
140 | { |
---|
141 | z->s=0; |
---|
142 | mpz_init(z->n); |
---|
143 | fmpq_get_mpz_frac(z->z,z->n,f); |
---|
144 | } |
---|
145 | n_Test(z,cf); |
---|
146 | return z; |
---|
147 | #else |
---|
148 | WerrorS("not implemented"); |
---|
149 | return NULL; |
---|
150 | #endif |
---|
151 | } |
---|
152 | |
---|
153 | void convSingNFlintN(fmpz_t f, mpz_t n) |
---|
154 | { |
---|
155 | fmpz_init(f); |
---|
156 | fmpz_set_mpz(f,n); |
---|
157 | } |
---|
158 | |
---|
159 | void convSingNFlintN(fmpz_t f, number n) |
---|
160 | { |
---|
161 | fmpz_init(f); |
---|
162 | fmpz_set_mpz(f,(mpz_ptr)n); |
---|
163 | } |
---|
164 | |
---|
165 | void convSingNFlintN(fmpq_t f, number n, const coeffs cf) |
---|
166 | { |
---|
167 | if (nCoeff_is_Q(cf)) |
---|
168 | { |
---|
169 | fmpq_init(f); |
---|
170 | if (SR_HDL(n)&SR_INT) |
---|
171 | fmpq_set_si(f,SR_TO_INT(n),1); |
---|
172 | else if (n->s<3) |
---|
173 | { |
---|
174 | fmpz_set_mpz(fmpq_numref(f), n->z); |
---|
175 | fmpz_set_mpz(fmpq_denref(f), n->n); |
---|
176 | } |
---|
177 | else |
---|
178 | { |
---|
179 | mpz_t one; |
---|
180 | mpz_init_set_si(one,1); |
---|
181 | fmpz_set_mpz(fmpq_numref(f), n->z); |
---|
182 | fmpz_set_mpz(fmpq_denref(f), one); |
---|
183 | mpz_clear(one); |
---|
184 | } |
---|
185 | } |
---|
186 | else |
---|
187 | { |
---|
188 | coeffs QQ=nInitChar(n_Q,NULL); |
---|
189 | nMapFunc nMap=n_SetMap(cf,QQ); |
---|
190 | if (nMap!=NULL) |
---|
191 | { |
---|
192 | number nn=nMap(n,cf,QQ); |
---|
193 | convSingNFlintN(f,nn,QQ); |
---|
194 | } |
---|
195 | nKillChar(QQ); |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | void convSingNFlintN_QQ(fmpq_t f, number n) |
---|
200 | { |
---|
201 | fmpq_init(f); |
---|
202 | if (SR_HDL(n)&SR_INT) |
---|
203 | fmpq_set_si(f,SR_TO_INT(n),1); |
---|
204 | else if (n->s<3) |
---|
205 | { |
---|
206 | fmpz_set_mpz(fmpq_numref(f), n->z); |
---|
207 | fmpz_set_mpz(fmpq_denref(f), n->n); |
---|
208 | } |
---|
209 | else |
---|
210 | { |
---|
211 | mpz_t one; |
---|
212 | mpz_init_set_si(one,1); |
---|
213 | fmpz_set_mpz(fmpq_numref(f), n->z); |
---|
214 | fmpz_set_mpz(fmpq_denref(f), one); |
---|
215 | mpz_clear(one); |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | void convSingNFlintNN(fmpq_t re, fmpq_t im, number n, const coeffs cf) |
---|
220 | { |
---|
221 | number n_2=n_RePart(n,cf); |
---|
222 | convSingNFlintN(re,n_2,cf); |
---|
223 | n_Delete(&n_2,cf); |
---|
224 | n_2=n_ImPart(n,cf); |
---|
225 | convSingNFlintN(im,n_2,cf); |
---|
226 | n_Delete(&n_2,cf); |
---|
227 | } |
---|
228 | |
---|
229 | void convSingPFlintP(fmpq_poly_t res, poly p, const ring r) |
---|
230 | { |
---|
231 | int d=p_GetExp(p,1,r); |
---|
232 | fmpq_poly_init2(res,d+1); |
---|
233 | _fmpq_poly_set_length (res, d + 1); |
---|
234 | while(p!=NULL) |
---|
235 | { |
---|
236 | number n=pGetCoeff(p); |
---|
237 | fmpq_t c; |
---|
238 | convSingNFlintN(c,n,r->cf); |
---|
239 | fmpq_poly_set_coeff_fmpq(res,p_GetExp(p,1,r),c); |
---|
240 | fmpq_clear(c); |
---|
241 | pIter(p); |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | void convSingImPFlintP(fmpq_poly_t res, poly p, const ring r) |
---|
246 | { |
---|
247 | int d=p_GetExp(p,1,r); |
---|
248 | fmpq_poly_init2(res,d+1); |
---|
249 | _fmpq_poly_set_length (res, d + 1); |
---|
250 | while(p!=NULL) |
---|
251 | { |
---|
252 | number n=n_ImPart(pGetCoeff(p),r->cf); |
---|
253 | fmpq_t c; |
---|
254 | convSingNFlintN(c,n,r->cf); |
---|
255 | fmpq_poly_set_coeff_fmpq(res,p_GetExp(p,1,r),c); |
---|
256 | fmpq_clear(c); |
---|
257 | n_Delete(&n,r->cf); |
---|
258 | pIter(p); |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | poly convFlintPSingP(fmpq_poly_t f, const ring r) |
---|
263 | { |
---|
264 | int d=fmpq_poly_length(f); |
---|
265 | poly p=NULL; |
---|
266 | fmpq_t c; |
---|
267 | fmpq_init(c); |
---|
268 | for(int i=0; i<=d; i++) |
---|
269 | { |
---|
270 | fmpq_poly_get_coeff_fmpq(c,f,i); |
---|
271 | number n=convFlintNSingN(c,r->cf); |
---|
272 | poly pp=p_Init(r); |
---|
273 | pSetCoeff0(pp,n); |
---|
274 | p_SetExp(pp,1,i,r); |
---|
275 | p_Setm(pp,r); |
---|
276 | p=p_Add_q(p,pp,r); |
---|
277 | } |
---|
278 | fmpq_clear(c); |
---|
279 | p_Test(p,r); |
---|
280 | return p; |
---|
281 | } |
---|
282 | |
---|
283 | bigintmat* singflint_LLL(bigintmat* m, bigintmat* T) |
---|
284 | { |
---|
285 | int r=m->rows(); |
---|
286 | int c=m->cols(); |
---|
287 | bigintmat* res=new bigintmat(r,c,m->basecoeffs()); |
---|
288 | fmpz_mat_t M, Transf; |
---|
289 | fmpz_mat_init(M, r, c); |
---|
290 | if(T != NULL) |
---|
291 | { |
---|
292 | fmpz_mat_init(Transf, T->rows(), T->rows()); |
---|
293 | } |
---|
294 | fmpz_t dummy; |
---|
295 | mpz_t n; |
---|
296 | int i,j; |
---|
297 | for(i=r;i>0;i--) |
---|
298 | { |
---|
299 | for(j=c;j>0;j--) |
---|
300 | { |
---|
301 | n_MPZ(n, BIMATELEM(*m, i, j),m->basecoeffs()); |
---|
302 | convSingNFlintN(dummy,n); |
---|
303 | mpz_clear(n); |
---|
304 | fmpz_set(fmpz_mat_entry(M, i-1, j-1), dummy); |
---|
305 | fmpz_clear(dummy); |
---|
306 | } |
---|
307 | } |
---|
308 | if(T != NULL) |
---|
309 | { |
---|
310 | for(i=T->rows();i>0;i--) |
---|
311 | { |
---|
312 | for(j=T->rows();j>0;j--) |
---|
313 | { |
---|
314 | n_MPZ(n, BIMATELEM(*T, i, j),T->basecoeffs()); |
---|
315 | convSingNFlintN(dummy,n); |
---|
316 | mpz_clear(n); |
---|
317 | fmpz_set(fmpz_mat_entry(Transf, i-1, j-1), dummy); |
---|
318 | fmpz_clear(dummy); |
---|
319 | } |
---|
320 | } |
---|
321 | } |
---|
322 | fmpz_lll_t fl; |
---|
323 | fmpz_lll_context_init_default(fl); |
---|
324 | if(T != NULL) |
---|
325 | fmpz_lll(M, Transf, fl); |
---|
326 | else |
---|
327 | fmpz_lll(M, NULL, fl); |
---|
328 | for(i=r;i>0;i--) |
---|
329 | { |
---|
330 | for(j=c;j>0;j--) |
---|
331 | { |
---|
332 | convFlintNSingN(n, fmpz_mat_entry(M, i-1, j-1)); |
---|
333 | n_Delete(&(BIMATELEM(*res,i,j)),res->basecoeffs()); |
---|
334 | BIMATELEM(*res,i,j)=n_InitMPZ(n,res->basecoeffs()); |
---|
335 | mpz_clear(n); |
---|
336 | } |
---|
337 | } |
---|
338 | if(T != NULL) |
---|
339 | { |
---|
340 | for(i=T->rows();i>0;i--) |
---|
341 | { |
---|
342 | for(j=T->cols();j>0;j--) |
---|
343 | { |
---|
344 | convFlintNSingN(n, fmpz_mat_entry(Transf, i-1, j-1)); |
---|
345 | n_Delete(&(BIMATELEM(*T,i,j)),T->basecoeffs()); |
---|
346 | BIMATELEM(*T,i,j)=n_InitMPZ(n,T->basecoeffs()); |
---|
347 | mpz_clear(n); |
---|
348 | } |
---|
349 | } |
---|
350 | } |
---|
351 | return res; |
---|
352 | } |
---|
353 | |
---|
354 | intvec* singflint_LLL(intvec* m, intvec* T) |
---|
355 | { |
---|
356 | int r=m->rows(); |
---|
357 | int c=m->cols(); |
---|
358 | intvec* res = new intvec(r,c,(int)0); |
---|
359 | fmpz_mat_t M,Transf; |
---|
360 | fmpz_mat_init(M, r, c); |
---|
361 | if(T != NULL) |
---|
362 | fmpz_mat_init(Transf, r, r); |
---|
363 | fmpz_t dummy; |
---|
364 | int i,j; |
---|
365 | for(i=r;i>0;i--) |
---|
366 | { |
---|
367 | for(j=c;j>0;j--) |
---|
368 | { |
---|
369 | convSingIFlintI(dummy,IMATELEM(*m,i,j)); |
---|
370 | fmpz_set(fmpz_mat_entry(M, i-1, j-1), dummy); |
---|
371 | fmpz_clear(dummy); |
---|
372 | } |
---|
373 | } |
---|
374 | if(T != NULL) |
---|
375 | { |
---|
376 | for(i=T->rows();i>0;i--) |
---|
377 | { |
---|
378 | for(j=T->rows();j>0;j--) |
---|
379 | { |
---|
380 | convSingIFlintI(dummy,IMATELEM(*T,i,j)); |
---|
381 | fmpz_set(fmpz_mat_entry(Transf, i-1, j-1), dummy); |
---|
382 | fmpz_clear(dummy); |
---|
383 | } |
---|
384 | } |
---|
385 | } |
---|
386 | fmpz_lll_t fl; |
---|
387 | fmpz_lll_context_init_default(fl); |
---|
388 | if(T != NULL) |
---|
389 | fmpz_lll(M, Transf, fl); |
---|
390 | else |
---|
391 | fmpz_lll(M, NULL, fl); |
---|
392 | for(i=r;i>0;i--) |
---|
393 | { |
---|
394 | for(j=c;j>0;j--) |
---|
395 | { |
---|
396 | IMATELEM(*res,i,j)=convFlintISingI(fmpz_mat_entry(M, i-1, j-1)); |
---|
397 | } |
---|
398 | } |
---|
399 | if(T != NULL) |
---|
400 | { |
---|
401 | for(i=Transf->r;i>0;i--) |
---|
402 | { |
---|
403 | for(j=Transf->r;j>0;j--) |
---|
404 | { |
---|
405 | IMATELEM(*T,i,j)=convFlintISingI(fmpz_mat_entry(Transf, i-1, j-1)); |
---|
406 | } |
---|
407 | } |
---|
408 | } |
---|
409 | return res; |
---|
410 | } |
---|
411 | #endif |
---|
412 | #endif |
---|