9 dpoly::dpoly(int d
, ZZ
& degree
, int offset
)
14 if (degree
>= 0 && degree
< ZZ(INIT_VAL
, min
))
17 ZZ c
= ZZ(INIT_VAL
, 1);
20 for (int i
= 1; i
<= min
; ++i
) {
27 void dpoly::operator *= (dpoly
& f
)
29 assert(coeff
.length() == f
.coeff
.length());
31 coeff
= f
.coeff
[0] * coeff
;
32 for (int i
= 1; i
< coeff
.length(); ++i
)
33 for (int j
= 0; i
+j
< coeff
.length(); ++j
)
34 coeff
[i
+j
] += f
.coeff
[i
] * old
[j
];
37 mpq_t
*dpoly::div(dpoly
& d
) const
39 int len
= coeff
.length();
42 mpq_t
* c
= new mpq_t
[coeff
.length()];
45 for (int i
= 0; i
< len
; ++i
) {
47 zz2value(coeff
[i
], tmp
);
50 for (int j
= 1; j
<= i
; ++j
) {
51 zz2value(d
.coeff
[j
], tmp
);
53 mpq_mul(qtmp
, qtmp
, c
[i
-j
]);
54 mpq_sub(c
[i
], c
[i
], qtmp
);
57 zz2value(d
.coeff
[0], tmp
);
59 mpq_div(c
[i
], c
[i
], qtmp
);
67 void dpoly::clear_div(mpq_t
*c
) const
69 int len
= coeff
.length();
71 for (int i
= 0; i
< len
; ++i
)
76 void dpoly::div(dpoly
& d
, mpq_t count
, ZZ
& sign
)
78 int len
= coeff
.length();
82 mpq_sub(count
, count
, c
[len
-1]);
84 mpq_add(count
, count
, c
[len
-1]);
89 void dpoly::div(dpoly
& d
, mpq_t
*count
, const mpq_t
& factor
)
91 int len
= coeff
.length();
94 for (int i
= 0; i
< len
; ++i
) {
95 mpq_mul(c
[len
-1 - i
], c
[len
-1 - i
], factor
);
96 mpq_add(count
[i
], count
[i
], c
[len
-1 - i
]);
102 void dpoly_r::add_term(int i
, const vector
<int>& powers
, const ZZ
& coeff
)
109 dpoly_r_term_list::iterator k
= c
[i
].find(&tmp
);
110 if (k
!= c
[i
].end()) {
111 (*k
)->coeff
+= coeff
;
114 dpoly_r_term
*t
= new dpoly_r_term
;
120 dpoly_r::dpoly_r(int len
, int dim
)
125 c
= new dpoly_r_term_list
[len
];
128 dpoly_r::dpoly_r(dpoly
& num
, int dim
)
131 len
= num
.coeff
.length();
132 c
= new dpoly_r_term_list
[len
];
134 vector
<int> powers(dim
, 0);
136 for (int i
= 0; i
< len
; ++i
) {
137 ZZ coeff
= num
.coeff
[i
];
138 add_term(i
, powers
, coeff
);
142 dpoly_r::dpoly_r(dpoly
& num
, dpoly
& den
, int pos
, int dim
)
145 len
= num
.coeff
.length();
146 c
= new dpoly_r_term_list
[len
];
150 for (int i
= 0; i
< len
; ++i
) {
151 ZZ coeff
= num
.coeff
[i
];
152 vector
<int> powers(dim
, 0);
155 add_term(i
, powers
, coeff
);
157 for (int j
= 1; j
<= i
; ++j
) {
158 dpoly_r_term_list::iterator k
;
159 for (k
= c
[i
-j
].begin(); k
!= c
[i
-j
].end(); ++k
) {
160 powers
= (*k
)->powers
;
162 coeff
= -den
.coeff
[j
-1] * (*k
)->coeff
;
163 add_term(i
, powers
, coeff
);
170 dpoly_r::dpoly_r(dpoly_r
* num
, dpoly
& den
, int pos
, int dim
)
174 c
= new dpoly_r_term_list
[len
];
178 for (int i
= 0 ; i
< len
; ++i
) {
179 dpoly_r_term_list::iterator k
;
180 for (k
= num
->c
[i
].begin(); k
!= num
->c
[i
].end(); ++k
) {
181 vector
<int> powers
= (*k
)->powers
;
183 add_term(i
, powers
, (*k
)->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 vector
<int> powers
= (*k
)->powers
;
191 coeff
= -den
.coeff
[j
-1] * (*k
)->coeff
;
192 add_term(i
, powers
, coeff
);
200 for (int i
= 0 ; i
< len
; ++i
)
201 for (dpoly_r_term_list::iterator k
= c
[i
].begin(); k
!= c
[i
].end(); ++k
) {
207 dpoly_r
*dpoly_r::div(dpoly
& d
)
209 dpoly_r
*rc
= new dpoly_r(len
, dim
);
210 rc
->denom
= power(d
.coeff
[0], len
);
211 ZZ inv_d
= rc
->denom
/ d
.coeff
[0];
214 for (int i
= 0; i
< len
; ++i
) {
215 for (dpoly_r_term_list::iterator k
= c
[i
].begin(); k
!= c
[i
].end(); ++k
) {
216 coeff
= (*k
)->coeff
* inv_d
;
217 rc
->add_term(i
, (*k
)->powers
, coeff
);
220 for (int j
= 1; j
<= i
; ++j
) {
221 dpoly_r_term_list::iterator k
;
222 for (k
= rc
->c
[i
-j
].begin(); k
!= rc
->c
[i
-j
].end(); ++k
) {
223 coeff
= - d
.coeff
[j
] * (*k
)->coeff
/ d
.coeff
[0];
224 rc
->add_term(i
, (*k
)->powers
, coeff
);
231 void dpoly_r::dump(void)
233 for (int i
= 0; i
< len
; ++i
) {
236 cerr
<< c
[i
].size() << endl
;
237 for (dpoly_r_term_list::iterator j
= c
[i
].begin(); j
!= c
[i
].end(); ++j
) {
238 for (int k
= 0; k
< dim
; ++k
) {
239 cerr
<< (*j
)->powers
[k
] << " ";
241 cerr
<< ": " << (*j
)->coeff
<< "/" << denom
<< endl
;