9 /* Construct truncated expansion of (1+t)^(degree),
10 * computing the first 1+d coefficients
12 dpoly::dpoly(int d
, const Value degree
, int offset
)
14 coeff
= Vector_Alloc(d
+1);
16 /* For small degrees, we only need to compute some coefficients */
18 if (value_posz_p(degree
) && value_cmp_si(degree
, min
) < 0)
19 min
= VALUE_TO_INT(degree
);
26 value_assign(coeff
->p
[0], c
);
27 value_assign(tmp
, degree
);
28 for (int i
= 1; i
<= min
; ++i
) {
29 value_multiply(c
, c
, tmp
);
30 value_decrement(tmp
, tmp
);
31 mpz_divexact_ui(c
, c
, i
);
32 value_assign(coeff
->p
[i
-offset
], c
);
38 void dpoly::operator += (const dpoly
& t
)
40 assert(coeff
->Size
== t
.coeff
->Size
);
41 for (int i
= 0; i
< coeff
->Size
; ++i
)
42 value_addto(coeff
->p
[i
], coeff
->p
[i
], t
.coeff
->p
[i
]);
45 void dpoly::operator *= (const Value f
)
47 for (int i
= 0; i
< coeff
->Size
; ++i
)
48 value_multiply(coeff
->p
[i
], coeff
->p
[i
], f
);
51 void dpoly::operator *= (const dpoly
& f
)
53 assert(coeff
->Size
== f
.coeff
->Size
);
54 Vector
*old
= Vector_Alloc(coeff
->Size
);
55 Vector_Copy(coeff
->p
, old
->p
, coeff
->Size
);
56 Vector_Scale(coeff
->p
, coeff
->p
, f
.coeff
->p
[0], coeff
->Size
);
57 for (int i
= 1; i
< coeff
->Size
; ++i
)
58 for (int j
= 0; i
+j
< coeff
->Size
; ++j
)
59 value_addmul(coeff
->p
[i
+j
], f
.coeff
->p
[i
], old
->p
[j
]);
63 Vector
*dpoly::div(const dpoly
& d
)
65 int len
= coeff
->Size
;
66 Vector
*denom
= Vector_Alloc(len
);
69 value_assign(denom
->p
[0], d
.coeff
->p
[0]);
70 for (int i
= 1; i
< len
; ++i
) {
71 value_multiply(denom
->p
[i
], denom
->p
[i
-1], denom
->p
[0]);
72 value_multiply(coeff
->p
[i
], coeff
->p
[i
], denom
->p
[i
-1]);
74 mpz_submul(coeff
->p
[i
], d
.coeff
->p
[1], coeff
->p
[i
-1]);
75 for (int j
= 2; j
<= i
; ++j
) {
76 value_multiply(tmp
, denom
->p
[j
-2], coeff
->p
[i
-j
]);
77 mpz_submul(coeff
->p
[i
], d
.coeff
->p
[j
], tmp
);
85 void dpoly::div(const dpoly
& d
, mpq_t count
, ZZ
& sign
)
87 int len
= coeff
->Size
;
88 Vector
*denom
= div(d
);
91 value_assign(mpq_numref(tmp
), coeff
->p
[len
-1]);
92 value_assign(mpq_denref(tmp
), denom
->p
[len
-1]);
93 mpq_canonicalize(tmp
);
96 mpq_sub(count
, count
, tmp
);
98 mpq_add(count
, count
, tmp
);
104 void dpoly::div(const dpoly
& d
, mpq_t
*count
, const mpq_t
& factor
)
106 int len
= coeff
->Size
;
107 Vector
*denom
= div(d
);
111 for (int i
= 0; i
< len
; ++i
) {
112 value_multiply(mpq_numref(tmp
), coeff
->p
[len
-1 - i
], mpq_numref(factor
));
113 value_multiply(mpq_denref(tmp
), denom
->p
[len
-1 - i
], mpq_denref(factor
));
114 mpq_add(count
[i
], count
[i
], tmp
);
121 void dpoly_r::add_term(int i
, const vector
<int>& powers
, const ZZ
& coeff
)
128 dpoly_r_term_list::iterator k
= c
[i
].find(&tmp
);
129 if (k
!= c
[i
].end()) {
130 (*k
)->coeff
+= coeff
;
133 dpoly_r_term
*t
= new dpoly_r_term
;
139 dpoly_r::dpoly_r(int len
, int dim
)
144 c
= new dpoly_r_term_list
[len
];
147 dpoly_r::dpoly_r(dpoly
& num
, int dim
)
150 len
= num
.coeff
->Size
;
151 c
= new dpoly_r_term_list
[len
];
153 vector
<int> powers(dim
, 0);
155 for (int i
= 0; i
< len
; ++i
) {
157 value2zz(num
.coeff
->p
[i
], coeff
);
158 add_term(i
, powers
, coeff
);
162 dpoly_r::dpoly_r(dpoly
& num
, dpoly
& den
, int pos
, int dim
)
165 len
= num
.coeff
->Size
;
166 c
= new dpoly_r_term_list
[len
];
171 for (int i
= 0; i
< len
; ++i
) {
172 vector
<int> powers(dim
, 0);
175 value2zz(num
.coeff
->p
[i
], coeff
);
176 add_term(i
, powers
, coeff
);
178 for (int j
= 1; j
<= i
; ++j
) {
179 dpoly_r_term_list::iterator k
;
180 for (k
= c
[i
-j
].begin(); k
!= c
[i
-j
].end(); ++k
) {
181 powers
= (*k
)->powers
;
183 value2zz(den
.coeff
->p
[j
-1], coeff
);
184 negate(coeff
, coeff
);
185 coeff
*= (*k
)->coeff
;
186 add_term(i
, powers
, coeff
);
193 dpoly_r::dpoly_r(const dpoly_r
* num
, dpoly
& den
, int pos
, int dim
)
197 c
= new dpoly_r_term_list
[len
];
201 for (int i
= 0 ; i
< len
; ++i
) {
202 dpoly_r_term_list::iterator k
;
203 for (k
= num
->c
[i
].begin(); k
!= num
->c
[i
].end(); ++k
) {
204 vector
<int> powers
= (*k
)->powers
;
206 add_term(i
, powers
, (*k
)->coeff
);
209 for (int j
= 1; j
<= i
; ++j
) {
210 dpoly_r_term_list::iterator k
;
211 for (k
= c
[i
-j
].begin(); k
!= c
[i
-j
].end(); ++k
) {
212 vector
<int> powers
= (*k
)->powers
;
214 value2zz(den
.coeff
->p
[j
-1], coeff
);
215 negate(coeff
, coeff
);
216 coeff
*= (*k
)->coeff
;
217 add_term(i
, powers
, coeff
);
225 for (int i
= 0 ; i
< len
; ++i
)
226 for (dpoly_r_term_list::iterator k
= c
[i
].begin(); k
!= c
[i
].end(); ++k
) {
232 dpoly_r
*dpoly_r::div(const dpoly
& d
) const
234 dpoly_r
*rc
= new dpoly_r(len
, dim
);
237 value2zz(d
.coeff
->p
[0], coeff0
);
238 rc
->denom
= power(coeff0
, len
);
239 ZZ inv_d
= rc
->denom
/ coeff0
;
241 for (int i
= 0; i
< len
; ++i
) {
242 for (dpoly_r_term_list::iterator k
= c
[i
].begin(); k
!= c
[i
].end(); ++k
) {
243 coeff
= (*k
)->coeff
* inv_d
;
244 rc
->add_term(i
, (*k
)->powers
, coeff
);
247 for (int j
= 1; j
<= i
; ++j
) {
248 dpoly_r_term_list::iterator k
;
249 for (k
= rc
->c
[i
-j
].begin(); k
!= rc
->c
[i
-j
].end(); ++k
) {
250 value2zz(d
.coeff
->p
[j
], coeff
);
251 coeff
= - coeff
* (*k
)->coeff
/ coeff0
;
252 rc
->add_term(i
, (*k
)->powers
, coeff
);
259 void dpoly_r::dump(void)
261 for (int i
= 0; i
< len
; ++i
) {
264 cerr
<< c
[i
].size() << endl
;
265 for (dpoly_r_term_list::iterator j
= c
[i
].begin(); j
!= c
[i
].end(); ++j
) {
266 for (int k
= 0; k
< dim
; ++k
) {
267 cerr
<< (*j
)->powers
[k
] << " ";
269 cerr
<< ": " << (*j
)->coeff
<< "/" << denom
<< endl
;