10 /* Construct truncated expansion of (1+t)^(degree),
11 * computing the first 1+d coefficients
13 dpoly::dpoly(int d
, const Value degree
, int offset
)
15 coeff
= Vector_Alloc(d
+1);
17 /* For small degrees, we only need to compute some coefficients */
19 if (value_posz_p(degree
) && value_cmp_si(degree
, min
) < 0)
20 min
= VALUE_TO_INT(degree
);
27 value_assign(coeff
->p
[0], c
);
28 value_assign(tmp
, degree
);
29 for (int i
= 1; i
<= min
; ++i
) {
30 value_multiply(c
, c
, tmp
);
31 value_decrement(tmp
, tmp
);
32 mpz_divexact_ui(c
, c
, i
);
33 value_assign(coeff
->p
[i
-offset
], c
);
39 void dpoly::operator += (const dpoly
& t
)
41 assert(coeff
->Size
== t
.coeff
->Size
);
42 for (int i
= 0; i
< coeff
->Size
; ++i
)
43 value_addto(coeff
->p
[i
], coeff
->p
[i
], t
.coeff
->p
[i
]);
46 void dpoly::operator *= (const Value f
)
48 for (int i
= 0; i
< coeff
->Size
; ++i
)
49 value_multiply(coeff
->p
[i
], coeff
->p
[i
], f
);
52 void dpoly::operator *= (const dpoly
& f
)
54 assert(coeff
->Size
== f
.coeff
->Size
);
55 Vector
*old
= Vector_Alloc(coeff
->Size
);
56 Vector_Copy(coeff
->p
, old
->p
, coeff
->Size
);
57 Vector_Scale(coeff
->p
, coeff
->p
, f
.coeff
->p
[0], coeff
->Size
);
58 for (int i
= 1; i
< coeff
->Size
; ++i
)
59 for (int j
= 0; i
+j
< coeff
->Size
; ++j
)
60 value_addmul(coeff
->p
[i
+j
], f
.coeff
->p
[i
], old
->p
[j
]);
64 Vector
*dpoly::div(const dpoly
& d
)
66 int len
= coeff
->Size
;
67 Vector
*denom
= Vector_Alloc(len
);
71 /* Make sure denominators are positive */
72 if (value_neg_p(d
.coeff
->p
[0])) {
73 Vector_Oppose(d
.coeff
->p
, d
.coeff
->p
, d
.coeff
->Size
);
74 Vector_Oppose(coeff
->p
, coeff
->p
, coeff
->Size
);
76 value_assign(denom
->p
[0], d
.coeff
->p
[0]);
77 for (int i
= 1; i
< len
; ++i
) {
78 value_multiply(denom
->p
[i
], denom
->p
[i
-1], denom
->p
[0]);
79 value_multiply(coeff
->p
[i
], coeff
->p
[i
], denom
->p
[i
-1]);
81 mpz_submul(coeff
->p
[i
], d
.coeff
->p
[1], coeff
->p
[i
-1]);
82 for (int j
= 2; j
<= i
; ++j
) {
83 value_multiply(tmp
, denom
->p
[j
-2], coeff
->p
[i
-j
]);
84 mpz_submul(coeff
->p
[i
], d
.coeff
->p
[j
], tmp
);
92 void dpoly::div(const dpoly
& d
, mpq_t count
, int sign
)
94 int len
= coeff
->Size
;
95 Vector
*denom
= div(d
);
98 value_assign(mpq_numref(tmp
), coeff
->p
[len
-1]);
99 value_assign(mpq_denref(tmp
), denom
->p
[len
-1]);
100 mpq_canonicalize(tmp
);
103 mpq_sub(count
, count
, tmp
);
105 mpq_add(count
, count
, tmp
);
111 void dpoly::div(const dpoly
& d
, mpq_t
*count
, const mpq_t
& factor
)
113 int len
= coeff
->Size
;
114 Vector
*denom
= div(d
);
118 for (int i
= 0; i
< len
; ++i
) {
119 value_multiply(mpq_numref(tmp
), coeff
->p
[len
-1 - i
], mpq_numref(factor
));
120 value_multiply(mpq_denref(tmp
), denom
->p
[len
-1 - i
], mpq_denref(factor
));
121 mpq_add(count
[i
], count
[i
], tmp
);
122 mpq_canonicalize(count
[i
]);
129 void dpoly_r::add_term(int i
, const vector
<int>& powers
, const ZZ
& coeff
)
136 dpoly_r_term_list::iterator k
= c
[i
].find(&tmp
);
137 if (k
!= c
[i
].end()) {
138 (*k
)->coeff
+= coeff
;
141 dpoly_r_term
*t
= new dpoly_r_term
;
147 dpoly_r::dpoly_r(int len
, int dim
)
152 c
= new dpoly_r_term_list
[len
];
155 dpoly_r::dpoly_r(dpoly
& num
, int dim
)
158 len
= num
.coeff
->Size
;
159 c
= new dpoly_r_term_list
[len
];
161 vector
<int> powers(dim
, 0);
163 for (int i
= 0; i
< len
; ++i
) {
165 value2zz(num
.coeff
->p
[i
], coeff
);
166 add_term(i
, powers
, coeff
);
170 dpoly_r::dpoly_r(dpoly
& num
, dpoly
& den
, int pos
, int dim
)
173 len
= num
.coeff
->Size
;
174 c
= new dpoly_r_term_list
[len
];
179 for (int i
= 0; i
< len
; ++i
) {
180 vector
<int> powers(dim
, 0);
183 value2zz(num
.coeff
->p
[i
], coeff
);
184 add_term(i
, powers
, coeff
);
186 for (int j
= 1; j
<= i
; ++j
) {
187 dpoly_r_term_list::iterator k
;
188 for (k
= c
[i
-j
].begin(); k
!= c
[i
-j
].end(); ++k
) {
189 powers
= (*k
)->powers
;
191 value2zz(den
.coeff
->p
[j
-1], coeff
);
192 negate(coeff
, coeff
);
193 coeff
*= (*k
)->coeff
;
194 add_term(i
, powers
, coeff
);
201 dpoly_r::dpoly_r(const dpoly_r
* num
, dpoly
& den
, int pos
, int dim
)
205 c
= new dpoly_r_term_list
[len
];
209 for (int i
= 0 ; i
< len
; ++i
) {
210 dpoly_r_term_list::iterator k
;
211 for (k
= num
->c
[i
].begin(); k
!= num
->c
[i
].end(); ++k
) {
212 vector
<int> powers
= (*k
)->powers
;
214 add_term(i
, powers
, (*k
)->coeff
);
217 for (int j
= 1; j
<= i
; ++j
) {
218 dpoly_r_term_list::iterator k
;
219 for (k
= c
[i
-j
].begin(); k
!= c
[i
-j
].end(); ++k
) {
220 vector
<int> powers
= (*k
)->powers
;
222 value2zz(den
.coeff
->p
[j
-1], coeff
);
223 negate(coeff
, coeff
);
224 coeff
*= (*k
)->coeff
;
225 add_term(i
, powers
, coeff
);
233 for (int i
= 0 ; i
< len
; ++i
)
234 for (dpoly_r_term_list::iterator k
= c
[i
].begin(); k
!= c
[i
].end(); ++k
) {
240 dpoly_r
*dpoly_r::div(const dpoly
& d
) const
242 dpoly_r
*rc
= new dpoly_r(len
, dim
);
245 value2zz(d
.coeff
->p
[0], coeff0
);
246 rc
->denom
= power(coeff0
, len
);
247 ZZ inv_d
= rc
->denom
/ coeff0
;
249 for (int i
= 0; i
< len
; ++i
) {
250 for (dpoly_r_term_list::iterator k
= c
[i
].begin(); k
!= c
[i
].end(); ++k
) {
251 coeff
= (*k
)->coeff
* inv_d
;
252 rc
->add_term(i
, (*k
)->powers
, coeff
);
255 for (int j
= 1; j
<= i
; ++j
) {
256 dpoly_r_term_list::iterator k
;
257 for (k
= rc
->c
[i
-j
].begin(); k
!= rc
->c
[i
-j
].end(); ++k
) {
258 value2zz(d
.coeff
->p
[j
], coeff
);
259 coeff
= - coeff
* (*k
)->coeff
/ coeff0
;
260 rc
->add_term(i
, (*k
)->powers
, coeff
);
267 void dpoly_r::dump(void)
269 for (int i
= 0; i
< len
; ++i
) {
272 cerr
<< c
[i
].size() << endl
;
273 for (dpoly_r_term_list::iterator j
= c
[i
].begin(); j
!= c
[i
].end(); ++j
) {
274 for (int k
= 0; k
< dim
; ++k
) {
275 cerr
<< (*j
)->powers
[k
] << " ";
277 cerr
<< ": " << (*j
)->coeff
<< "/" << denom
<< endl
;