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