1 | #include "digitech.h" |
---|
2 | #include "kbuckets.h" |
---|
3 | #include "ideals.h" |
---|
4 | static ideal zero_ideal; |
---|
5 | #if 1 |
---|
6 | void bit_reduce(poly & f,ring r){ |
---|
7 | poly p=f; |
---|
8 | kBucket_pt erg_bucket= kBucketCreate(r); |
---|
9 | kBucketInit(erg_bucket,NULL,0 /*pLength(P.p)*/); |
---|
10 | while(p){ |
---|
11 | poly next=pNext(p); |
---|
12 | pNext(p)=NULL; |
---|
13 | |
---|
14 | int i; |
---|
15 | int max=rVar(r); |
---|
16 | for(i=1;i<=max;i++){ |
---|
17 | unsigned long exp=p_GetExp(p,i,r); |
---|
18 | if(exp!=0) |
---|
19 | p_SetExp(p,i,1,r); |
---|
20 | |
---|
21 | } |
---|
22 | p_Setm(p,r); |
---|
23 | int pseudo_len=0; |
---|
24 | kBucket_Add_q(erg_bucket,p,&pseudo_len); |
---|
25 | p=next; |
---|
26 | } |
---|
27 | |
---|
28 | int len=0; |
---|
29 | poly erg; |
---|
30 | kBucketClear(erg_bucket,&erg, &len); |
---|
31 | kBucketDestroy(&erg_bucket); |
---|
32 | f=erg; |
---|
33 | } |
---|
34 | #else |
---|
35 | |
---|
36 | |
---|
37 | |
---|
38 | void do_bit_reduce(poly f, kBucket_pt bucket){ |
---|
39 | ring r=bucket->bucket_ring; |
---|
40 | int p=rChar(r); |
---|
41 | int max=rVar(r); |
---|
42 | while (f!=NULL) |
---|
43 | { |
---|
44 | // int len=0; |
---|
45 | // poly erg; |
---|
46 | // kBucketClear(bucket,&erg, &len); |
---|
47 | // kBucketDestroy(&bucket); |
---|
48 | // return erg; |
---|
49 | //return; |
---|
50 | //} |
---|
51 | |
---|
52 | BOOLEAN changed=FALSE; |
---|
53 | poly next=pNext(f); |
---|
54 | pNext(f)=NULL; |
---|
55 | int i; |
---|
56 | for(i=1;i<=max;i++){ |
---|
57 | unsigned long exp; |
---|
58 | while((exp=p_GetExp(f,i,r))>=p){ |
---|
59 | p_SetExp(f,i,exp-p+1,r); |
---|
60 | changed=TRUE; |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | if (changed) { |
---|
65 | p_Setm(f,r); |
---|
66 | |
---|
67 | int pseudo_len=0; |
---|
68 | kBucket_Add_q(bucket,f,&pseudo_len); |
---|
69 | //do_bit_reduce(next,bucket); |
---|
70 | } else { |
---|
71 | //do_bit_reduce(next,bucket); |
---|
72 | int pseudo_len=0; |
---|
73 | kBucket_Add_q(bucket,f,&pseudo_len); |
---|
74 | |
---|
75 | } |
---|
76 | f=next; |
---|
77 | } |
---|
78 | } |
---|
79 | poly do_bitreduce(poly f, ring r){ |
---|
80 | poly erg=NULL; |
---|
81 | poly *append_to=&erg; |
---|
82 | int p=rChar(r); |
---|
83 | int max=rVar(r); |
---|
84 | kBucket_pt bucket= kBucketCreate(r); |
---|
85 | kBucketInit(bucket,NULL,0 /*pLength(P.p)*/); |
---|
86 | while(f!=NULL){ |
---|
87 | BOOLEAN changed=FALSE; |
---|
88 | poly next=pNext(f); |
---|
89 | pNext(f)=NULL; |
---|
90 | assume(pNext(f)==NULL); |
---|
91 | int i; |
---|
92 | for(i=1;i<=max;i++){ |
---|
93 | unsigned long exp; |
---|
94 | while((exp=p_GetExp(f,i,r))>=p){ |
---|
95 | p_SetExp(f,i,exp-p+1,r); |
---|
96 | changed=TRUE; |
---|
97 | } |
---|
98 | } |
---|
99 | if (changed) { |
---|
100 | p_Setm(f,r); |
---|
101 | int pseudo_len=0; |
---|
102 | kBucket_Add_q(bucket,f,&pseudo_len); |
---|
103 | } else { |
---|
104 | (*append_to)=f; |
---|
105 | append_to=&(pNext(f)); |
---|
106 | } |
---|
107 | f=next; |
---|
108 | } |
---|
109 | { |
---|
110 | int pseudo_len=0; |
---|
111 | kBucket_Add_q(bucket,erg,&pseudo_len); |
---|
112 | pseudo_len=0; |
---|
113 | kBucketClear(bucket,&erg,&pseudo_len); |
---|
114 | } |
---|
115 | return erg; |
---|
116 | } |
---|
117 | void bit_reduce2(poly & f,ring r){ |
---|
118 | if (f==NULL) return; |
---|
119 | if (pNext(f)==NULL){ |
---|
120 | int p=rChar(r); |
---|
121 | int max=rVar(r); |
---|
122 | BOOLEAN changed=FALSE; |
---|
123 | poly next=pNext(f); |
---|
124 | assume(pNext(f)==NULL); |
---|
125 | int i; |
---|
126 | for(i=1;i<=max;i++){ |
---|
127 | unsigned long exp; |
---|
128 | while((exp=p_GetExp(f,i,r))>=p){ |
---|
129 | p_SetExp(f,i,exp-p+1,r); |
---|
130 | changed=TRUE; |
---|
131 | } |
---|
132 | } |
---|
133 | if (changed) |
---|
134 | p_Setm(f,r); |
---|
135 | return; |
---|
136 | } |
---|
137 | // kBucket_pt bucket= kBucketCreate(r); |
---|
138 | // kBucketInit(bucket,NULL,0 /*pLength(P.p)*/); |
---|
139 | // do_bit_reduce(f,bucket); |
---|
140 | // int len=0; |
---|
141 | // kBucketClear(bucket,&f, &len); |
---|
142 | // kBucketDestroy(&bucket); |
---|
143 | f=do_bitreduce(f,r); |
---|
144 | |
---|
145 | } |
---|
146 | void bit_reduce1(poly & f,ring r){ |
---|
147 | if (f==NULL) return; |
---|
148 | |
---|
149 | |
---|
150 | if (pNext(f)==NULL){ |
---|
151 | int p=rChar(r); |
---|
152 | int max=rVar(r); |
---|
153 | BOOLEAN changed=FALSE; |
---|
154 | poly next=pNext(f); |
---|
155 | assume(pNext(f)==NULL); |
---|
156 | int i; |
---|
157 | for(i=1;i<=max;i++){ |
---|
158 | unsigned long exp; |
---|
159 | while((exp=p_GetExp(f,i,r))>=p){ |
---|
160 | p_SetExp(f,i,exp-p+1,r); |
---|
161 | changed=TRUE; |
---|
162 | } |
---|
163 | } |
---|
164 | if (changed) |
---|
165 | p_Setm(f,r); |
---|
166 | return; |
---|
167 | } |
---|
168 | kBucket_pt bucket= kBucketCreate(r); |
---|
169 | kBucketInit(bucket,NULL,0 /*pLength(P.p)*/); |
---|
170 | do_bit_reduce(f,bucket); |
---|
171 | int len=0; |
---|
172 | kBucketClear(bucket,&f, &len); |
---|
173 | kBucketDestroy(&bucket); |
---|
174 | |
---|
175 | } |
---|
176 | // void bit_reduce_arg(poly & f,ring r){ |
---|
177 | // kBucket_pt bucket= kBucketCreate(r); |
---|
178 | // kBucketInit(bucket,NULL,0 /*pLength(P.p)*/); |
---|
179 | // f=do_bit_reduce(f,bucket); |
---|
180 | // } |
---|
181 | #endif |
---|
182 | |
---|
183 | poly uni_subst_bits(poly outer_uni, poly inner_multi, ring r){ |
---|
184 | zero_ideal=idInit(0,1); |
---|
185 | //assumes outer_uni is univariate and ordering global |
---|
186 | int d_max=p_GetExp(outer_uni,1,r); |
---|
187 | poly* potences=(poly*) omalloc((d_max+1)*sizeof(poly)); |
---|
188 | potences[0]=p_ISet(1,r); |
---|
189 | int i; |
---|
190 | for(i=1;i<=d_max;i++){ |
---|
191 | potences[i]=pp_Mult_qq(potences[i-1],inner_multi,r); |
---|
192 | bit_reduce(potences[i],r); |
---|
193 | } |
---|
194 | |
---|
195 | poly p=outer_uni; |
---|
196 | kBucket_pt erg_bucket= kBucketCreate(r); |
---|
197 | kBucketInit(erg_bucket,NULL,0 /*pLength(P.p)*/); |
---|
198 | |
---|
199 | |
---|
200 | while(p){ |
---|
201 | int d=p_GetExp(p,1,r); |
---|
202 | assume(potences[d]!=NULL); //mustn't always hold, but for most input |
---|
203 | int pseudo_len=0; |
---|
204 | kBucket_Add_q(erg_bucket,p_Mult_nn(potences[d],p_GetCoeff(p,r),r),&pseudo_len); |
---|
205 | potences[d]=NULL; |
---|
206 | p=pNext(p); |
---|
207 | } |
---|
208 | |
---|
209 | |
---|
210 | |
---|
211 | |
---|
212 | //free potences |
---|
213 | for(i=0;i<=d_max;i++){ |
---|
214 | p_Delete(&potences[i],r); |
---|
215 | } |
---|
216 | omfree(potences); |
---|
217 | int len=0; |
---|
218 | poly erg; |
---|
219 | kBucketClear(erg_bucket,&erg, &len); |
---|
220 | kBucketDestroy(&erg_bucket); |
---|
221 | return(erg); |
---|
222 | } |
---|