1 | // emacs edit mode for this file is -*- C++ -*- |
---|
2 | /**************************************** |
---|
3 | * Computer Algebra System SINGULAR * |
---|
4 | ****************************************/ |
---|
5 | /* |
---|
6 | * ABSTRACT: flint mpoly |
---|
7 | */ |
---|
8 | |
---|
9 | #include "misc/auxiliary.h" |
---|
10 | #include "flintconv.h" |
---|
11 | #include "flint_mpoly.h" |
---|
12 | |
---|
13 | #ifdef HAVE_FLINT |
---|
14 | #if __FLINT_RELEASE >= 20503 |
---|
15 | #include "coeffs/coeffs.h" |
---|
16 | #include "coeffs/longrat.h" |
---|
17 | #include "polys/monomials/p_polys.h" |
---|
18 | |
---|
19 | BOOLEAN convSingRFlintR(fmpq_mpoly_ctx_t ctx, const ring r) |
---|
20 | { |
---|
21 | if (rRing_ord_pure_dp(r)) |
---|
22 | { |
---|
23 | fmpq_mpoly_ctx_init(ctx,r->N,ORD_DEGREVLEX); |
---|
24 | return FALSE; |
---|
25 | } |
---|
26 | else if (rRing_ord_pure_Dp(r)) |
---|
27 | { |
---|
28 | fmpq_mpoly_ctx_init(ctx,r->N,ORD_DEGLEX); |
---|
29 | return FALSE; |
---|
30 | } |
---|
31 | else if (rRing_ord_pure_lp(r)) |
---|
32 | { |
---|
33 | fmpq_mpoly_ctx_init(ctx,r->N,ORD_LEX); |
---|
34 | return FALSE; |
---|
35 | } |
---|
36 | return TRUE; |
---|
37 | } |
---|
38 | |
---|
39 | void convSingPFlintMP(fmpq_mpoly_t res, fmpq_mpoly_ctx_t ctx, poly p, int lp, const ring r) |
---|
40 | { |
---|
41 | int bits=SI_LOG2(r->bitmask); |
---|
42 | fmpq_mpoly_init3(res,lp,bits,ctx); |
---|
43 | fmpq_mpoly_resize(res,lp,ctx); |
---|
44 | ulong* exp=(ulong*)omAlloc((r->N+1)*sizeof(ulong)); |
---|
45 | int i=0; |
---|
46 | while(p!=NULL) |
---|
47 | { |
---|
48 | number n=pGetCoeff(p); |
---|
49 | fmpq_t c; |
---|
50 | convSingNFlintN_QQ(c,n); |
---|
51 | #if SIZEOF_LONG==8 |
---|
52 | p_GetExpVL(p,(int64*)exp,r); |
---|
53 | fmpq_mpoly_set_term_exp_ui(res,i,exp,ctx); |
---|
54 | #else |
---|
55 | p_GetExpV(p,(int*)exp,r); |
---|
56 | fmpq_mpoly_set_term_exp_ui(res,i,&(exp[1]),ctx); |
---|
57 | #endif |
---|
58 | fmpq_mpoly_set_term_coeff_fmpq(res,i,c,ctx); |
---|
59 | fmpq_clear(c); |
---|
60 | i++; |
---|
61 | pIter(p); |
---|
62 | } |
---|
63 | //fmpq_mpoly_print_pretty(res,r->names,ctx);PrintLn(); |
---|
64 | omFreeSize(exp,(r->N+1)*sizeof(ulong)); |
---|
65 | } |
---|
66 | |
---|
67 | poly convFlintMPSingP(fmpq_mpoly_t f, fmpq_mpoly_ctx_t ctx, const ring r) |
---|
68 | { |
---|
69 | int d=fmpq_mpoly_length(f,ctx)-1; |
---|
70 | poly p=NULL; |
---|
71 | ulong* exp=(ulong*)omAlloc0((r->N+1)*sizeof(ulong)); |
---|
72 | fmpq_t c; |
---|
73 | fmpq_init(c); |
---|
74 | for(int i=d; i>=0; i--) |
---|
75 | { |
---|
76 | fmpq_mpoly_get_term_coeff_fmpq(c,f,i,ctx); |
---|
77 | poly pp=p_Init(r); |
---|
78 | #if SIZEOF_LONG==8 |
---|
79 | fmpq_mpoly_get_term_exp_ui(exp,f,i,ctx); |
---|
80 | p_SetExpVL(pp,(int64*)exp,r); |
---|
81 | #else |
---|
82 | fmpq_mpoly_get_term_exp_ui(&(exp[1]),f,i,ctx); |
---|
83 | p_SetExpV(pp,(int*)exp,r); |
---|
84 | #endif |
---|
85 | p_Setm(pp,r); |
---|
86 | number n=convFlintNSingN_QQ(c,r->cf); |
---|
87 | //fmpq_clear(c); // LEAK? |
---|
88 | pSetCoeff0(pp,n); |
---|
89 | pNext(pp)=p; |
---|
90 | p=pp; |
---|
91 | } |
---|
92 | fmpq_clear(c); |
---|
93 | p_Test(p,r); |
---|
94 | return p; |
---|
95 | } |
---|
96 | |
---|
97 | poly Flint_Mult_MP(poly p,int lp, poly q, int lq, fmpq_mpoly_ctx_t ctx, const ring r) |
---|
98 | { |
---|
99 | fmpq_mpoly_t pp,qq,res; |
---|
100 | convSingPFlintMP(pp,ctx,p,lp,r); |
---|
101 | convSingPFlintMP(qq,ctx,q,lq,r); |
---|
102 | int bits=SI_LOG2(r->bitmask); |
---|
103 | fmpq_mpoly_init3(res,lp*lq,bits,ctx); |
---|
104 | fmpq_mpoly_mul(res,pp,qq,ctx); |
---|
105 | poly pres=convFlintMPSingP(res,ctx,r); |
---|
106 | fmpq_mpoly_clear(res,ctx); |
---|
107 | fmpq_mpoly_clear(pp,ctx); |
---|
108 | fmpq_mpoly_clear(qq,ctx); |
---|
109 | fmpq_mpoly_ctx_clear(ctx); |
---|
110 | p_Test(pres,r); |
---|
111 | return pres; |
---|
112 | } |
---|
113 | BOOLEAN convSingRFlintR(nmod_mpoly_ctx_t ctx, const ring r) |
---|
114 | { |
---|
115 | if (rRing_ord_pure_dp(r)) |
---|
116 | { |
---|
117 | nmod_mpoly_ctx_init(ctx,r->N,ORD_DEGREVLEX,r->cf->ch); |
---|
118 | return FALSE; |
---|
119 | } |
---|
120 | else if (rRing_ord_pure_Dp(r)) |
---|
121 | { |
---|
122 | nmod_mpoly_ctx_init(ctx,r->N,ORD_DEGLEX,r->cf->ch); |
---|
123 | return FALSE; |
---|
124 | } |
---|
125 | else if (rRing_ord_pure_lp(r)) |
---|
126 | { |
---|
127 | nmod_mpoly_ctx_init(ctx,r->N,ORD_LEX,r->cf->ch); |
---|
128 | return FALSE; |
---|
129 | } |
---|
130 | return TRUE; |
---|
131 | } |
---|
132 | |
---|
133 | poly convFlintMPSingP(nmod_mpoly_t f, nmod_mpoly_ctx_t ctx, const ring r) |
---|
134 | { |
---|
135 | int d=nmod_mpoly_length(f,ctx)-1; |
---|
136 | poly p=NULL; |
---|
137 | ulong* exp=(ulong*)omAlloc0((r->N+1)*sizeof(ulong)); |
---|
138 | for(int i=d; i>=0; i--) |
---|
139 | { |
---|
140 | ulong c=nmod_mpoly_get_term_coeff_ui(f,i,ctx); |
---|
141 | poly pp=p_Init(r); |
---|
142 | #if SIZEOF_LONG==8 |
---|
143 | nmod_mpoly_get_term_exp_ui(exp,f,i,ctx); |
---|
144 | p_SetExpVL(pp,(int64*)exp,r); |
---|
145 | #else |
---|
146 | nmod_mpoly_get_term_exp_ui(&(exp[1]),f,i,ctx); |
---|
147 | p_SetExpV(pp,(int*)exp,r); |
---|
148 | #endif |
---|
149 | p_Setm(pp,r); |
---|
150 | pSetCoeff0(pp,(number)c); |
---|
151 | pNext(pp)=p; |
---|
152 | p=pp; |
---|
153 | } |
---|
154 | p_Test(p,r); |
---|
155 | return p; |
---|
156 | } |
---|
157 | |
---|
158 | void convSingPFlintMP(nmod_mpoly_t res, nmod_mpoly_ctx_t ctx, poly p, int lp,const ring r) |
---|
159 | { |
---|
160 | int bits=SI_LOG2(r->bitmask); |
---|
161 | nmod_mpoly_init3(res,lp,bits,ctx); |
---|
162 | nmod_mpoly_resize(res,lp,ctx); |
---|
163 | ulong* exp=(ulong*)omAlloc((r->N+1)*sizeof(ulong)); |
---|
164 | int i=0; |
---|
165 | while(p!=NULL) |
---|
166 | { |
---|
167 | number n=pGetCoeff(p); |
---|
168 | #if SIZEOF_LONG==8 |
---|
169 | p_GetExpVL(p,(int64*)exp,r); |
---|
170 | nmod_mpoly_set_term_exp_ui(res,i,exp,ctx); |
---|
171 | #else |
---|
172 | p_GetExpV(p,(int*)exp,r); |
---|
173 | nmod_mpoly_set_term_exp_ui(res,i,&(exp[1]),ctx); |
---|
174 | #endif |
---|
175 | nmod_mpoly_set_term_coeff_ui(res,i,(ulong)n,ctx); |
---|
176 | i++; |
---|
177 | pIter(p); |
---|
178 | } |
---|
179 | //nmod_mpoly_print_pretty(res,r->names,ctx);PrintLn(); |
---|
180 | omFreeSize(exp,(r->N+1)*sizeof(ulong)); |
---|
181 | } |
---|
182 | |
---|
183 | poly Flint_Mult_MP(poly p,int lp, poly q, int lq, nmod_mpoly_ctx_t ctx, const ring r) |
---|
184 | { |
---|
185 | nmod_mpoly_t pp,qq,res; |
---|
186 | convSingPFlintMP(pp,ctx,p,lp,r); |
---|
187 | convSingPFlintMP(qq,ctx,q,lq,r); |
---|
188 | int bits=SI_LOG2(r->bitmask); |
---|
189 | nmod_mpoly_init3(res,lp*lq,bits,ctx); |
---|
190 | nmod_mpoly_mul(res,pp,qq,ctx); |
---|
191 | poly pres=convFlintMPSingP(res,ctx,r); |
---|
192 | nmod_mpoly_clear(res,ctx); |
---|
193 | nmod_mpoly_clear(pp,ctx); |
---|
194 | nmod_mpoly_clear(qq,ctx); |
---|
195 | nmod_mpoly_ctx_clear(ctx); |
---|
196 | p_Test(pres,r); |
---|
197 | return pres; |
---|
198 | } |
---|
199 | poly Flint_GCD_MP(poly p,int lp,poly q,int lq,nmod_mpoly_ctx_t ctx,const ring r) |
---|
200 | { |
---|
201 | nmod_mpoly_t pp,qq,res; |
---|
202 | convSingPFlintMP(pp,ctx,p,lp,r); |
---|
203 | convSingPFlintMP(qq,ctx,q,lq,r); |
---|
204 | int bits=SI_LOG2(r->bitmask); |
---|
205 | nmod_mpoly_init3(res,si_max(lp,lq),bits,ctx); |
---|
206 | int ok=nmod_mpoly_gcd(res,pp,qq,ctx); |
---|
207 | poly pres; |
---|
208 | if (ok) |
---|
209 | { |
---|
210 | pres=convFlintMPSingP(res,ctx,r); |
---|
211 | p_Test(pres,r); |
---|
212 | } |
---|
213 | else |
---|
214 | { |
---|
215 | pres=p_One(r); |
---|
216 | } |
---|
217 | nmod_mpoly_clear(res,ctx); |
---|
218 | nmod_mpoly_clear(pp,ctx); |
---|
219 | nmod_mpoly_clear(qq,ctx); |
---|
220 | nmod_mpoly_ctx_clear(ctx); |
---|
221 | return pres; |
---|
222 | } |
---|
223 | |
---|
224 | poly Flint_GCD_MP(poly p,int lp,poly q,int lq,fmpq_mpoly_ctx_t ctx,const ring r) |
---|
225 | { |
---|
226 | fmpq_mpoly_t pp,qq,res; |
---|
227 | convSingPFlintMP(pp,ctx,p,lp,r); |
---|
228 | convSingPFlintMP(qq,ctx,q,lq,r); |
---|
229 | int bits=SI_LOG2(r->bitmask); |
---|
230 | fmpq_mpoly_init3(res,si_max(lp,lq),bits,ctx); |
---|
231 | int ok=fmpq_mpoly_gcd(res,pp,qq,ctx); |
---|
232 | poly pres; |
---|
233 | if (ok) |
---|
234 | { |
---|
235 | pres=convFlintMPSingP(res,ctx,r); |
---|
236 | p_Test(pres,r); |
---|
237 | } |
---|
238 | else |
---|
239 | { |
---|
240 | pres=p_One(r); |
---|
241 | } |
---|
242 | fmpq_mpoly_clear(res,ctx); |
---|
243 | fmpq_mpoly_clear(pp,ctx); |
---|
244 | fmpq_mpoly_clear(qq,ctx); |
---|
245 | fmpq_mpoly_ctx_clear(ctx); |
---|
246 | return pres; |
---|
247 | } |
---|
248 | #endif |
---|
249 | #endif |
---|