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
, int * powers
, ZZ
& coeff
)
106 for (int k
= 0; k
< c
[i
].size(); ++k
) {
107 if (memcmp(c
[i
][k
]->powers
, powers
, dim
* sizeof(int)) == 0) {
108 c
[i
][k
]->coeff
+= coeff
;
112 dpoly_r_term
*t
= new dpoly_r_term
;
113 t
->powers
= new int[dim
];
114 memcpy(t
->powers
, powers
, dim
* sizeof(int));
119 dpoly_r::dpoly_r(int len
, int dim
)
124 c
= new vector
< dpoly_r_term
* > [len
];
127 dpoly_r::dpoly_r(dpoly
& num
, int dim
)
130 len
= num
.coeff
.length();
131 c
= new vector
< dpoly_r_term
* > [len
];
134 memset(powers
, 0, dim
* sizeof(int));
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 vector
< dpoly_r_term
* > [len
];
150 for (int i
= 0; i
< len
; ++i
) {
151 ZZ coeff
= num
.coeff
[i
];
152 memset(powers
, 0, dim
* sizeof(int));
155 add_term(i
, powers
, coeff
);
157 for (int j
= 1; j
<= i
; ++j
) {
158 for (int k
= 0; k
< c
[i
-j
].size(); ++k
) {
159 memcpy(powers
, c
[i
-j
][k
]->powers
, dim
*sizeof(int));
161 coeff
= -den
.coeff
[j
-1] * c
[i
-j
][k
]->coeff
;
162 add_term(i
, powers
, coeff
);
169 dpoly_r::dpoly_r(dpoly_r
* num
, dpoly
& den
, int pos
, int dim
)
173 c
= new vector
< dpoly_r_term
* > [len
];
178 for (int i
= 0 ; i
< len
; ++i
) {
179 for (int k
= 0; k
< num
->c
[i
].size(); ++k
) {
180 memcpy(powers
, num
->c
[i
][k
]->powers
, dim
*sizeof(int));
182 add_term(i
, powers
, num
->c
[i
][k
]->coeff
);
185 for (int j
= 1; j
<= i
; ++j
) {
186 for (int k
= 0; k
< c
[i
-j
].size(); ++k
) {
187 memcpy(powers
, c
[i
-j
][k
]->powers
, dim
*sizeof(int));
189 coeff
= -den
.coeff
[j
-1] * c
[i
-j
][k
]->coeff
;
190 add_term(i
, powers
, coeff
);
198 for (int i
= 0 ; i
< len
; ++i
)
199 for (int k
= 0; k
< c
[i
].size(); ++k
) {
200 delete [] c
[i
][k
]->powers
;
206 dpoly_r
*dpoly_r::div(dpoly
& d
)
208 dpoly_r
*rc
= new dpoly_r(len
, dim
);
209 rc
->denom
= power(d
.coeff
[0], len
);
210 ZZ inv_d
= rc
->denom
/ d
.coeff
[0];
213 for (int i
= 0; i
< len
; ++i
) {
214 for (int k
= 0; k
< c
[i
].size(); ++k
) {
215 coeff
= c
[i
][k
]->coeff
* inv_d
;
216 rc
->add_term(i
, c
[i
][k
]->powers
, coeff
);
219 for (int j
= 1; j
<= i
; ++j
) {
220 for (int k
= 0; k
< rc
->c
[i
-j
].size(); ++k
) {
221 coeff
= - d
.coeff
[j
] * rc
->c
[i
-j
][k
]->coeff
/ d
.coeff
[0];
222 rc
->add_term(i
, rc
->c
[i
-j
][k
]->powers
, coeff
);
229 void dpoly_r::dump(void)
231 for (int i
= 0; i
< len
; ++i
) {
234 cerr
<< c
[i
].size() << endl
;
235 for (int j
= 0; j
< c
[i
].size(); ++j
) {
236 for (int k
= 0; k
< dim
; ++k
) {
237 cerr
<< c
[i
][j
]->powers
[k
] << " ";
239 cerr
<< ": " << c
[i
][j
]->coeff
<< "/" << denom
<< endl
;