Macaulay2 Engine
Loading...
Searching...
No Matches

◆ gcd_coefficients()

TowerPolynomial DPoly::gcd_coefficients ( int level,
const TowerPolynomial f,
const TowerPolynomial g,
TowerPolynomial & result_u,
TowerPolynomial & result_v )

Definition at line 1159 of file dpoly.cpp.

1164{
1165 // Assumption:
1166 // f and g are non-zero
1167 TowerPolynomial v1, v2, v3;
1168 TowerPolynomial u1, u2, u3;
1169 TowerPolynomial q = nullptr;
1170
1171 v1 = nullptr;
1172 v2 = from_long(level, 1);
1173 v3 = copy(level, g);
1174
1175 u1 = from_long(level, 1);
1176 u2 = nullptr;
1177 u3 = copy(level, f);
1178
1179 if (v3 == nullptr || (u3 != nullptr && v3->deg > u3->deg))
1180 {
1181 swap_poly(u1, v1);
1182 swap_poly(u2, v2);
1183 swap_poly(u3, v3);
1184 }
1185
1186// At the end of the loop:
1187// u1 * f + u2 * g == u3
1188// v1 * f + v2 * g == v3
1189// (and (v1,v2,v3) is close to the gcd
1190
1191#ifdef DEBUGGCD
1192 if (level == 1)
1193 {
1194 printf("u1 = %s\n", to_string(level, u1));
1195 printf("u2 = %s\n", to_string(level, u2));
1196 printf("u3 = %s\n", to_string(level, u3));
1197
1198 printf("v1 = %s\n", to_string(level, v1));
1199 printf("v2 = %s\n", to_string(level, v2));
1200 printf("v3 = %s\n", to_string(level, v3));
1201 }
1202#endif
1203
1204 while (v3 != nullptr)
1205 {
1206 if (!make_monic3(level, v1, v2, v3))
1207 {
1208 // deallocate some polynomials, then return 0. No monic gcd
1209 dealloc_poly(q);
1210 dealloc_poly(u1);
1211 dealloc_poly(u2);
1212 dealloc_poly(u3);
1213 dealloc_poly(v1);
1214 dealloc_poly(v2);
1215 dealloc_poly(v3);
1216 result_u = nullptr;
1217 result_v = nullptr;
1218 return nullptr;
1219 }
1221 level,
1222 u3,
1223 v3); // u3 := u3 - q*v3, as v3 is monic, this is always possible
1224
1225#ifdef DEBUGGCD
1226 if (level == 1)
1227 {
1228 printf("q = %s\n", to_string(level, q));
1229 printf("u3 = %s\n", to_string(level, u3));
1230 }
1231#endif
1232
1233 negate_in_place(level, q);
1234 TowerPolynomial a = mult(level, q, v1, false);
1235 TowerPolynomial b = mult(level, q, v2, false);
1236 add_in_place(level, u1, a);
1237 add_in_place(level, u2, b);
1238
1239#ifdef DEBUGGCD
1240 if (level == 1)
1241 {
1242 printf("u1 = %s\n", to_string(level, u1));
1243 printf("u2 = %s\n", to_string(level, u2));
1244 printf("u3 = %s\n", to_string(level, u3));
1245 }
1246#endif
1247 dealloc_poly(a); // MES: totally wipeout polys a, b here!
1248 dealloc_poly(b);
1249 swap_poly(u1, v1);
1250 swap_poly(u2, v2);
1251 swap_poly(u3, v3);
1252 }
1253
1254#ifdef DEBUGGCD
1255 if (level == 1)
1256 {
1257 printf("u1 = %s\n", to_string(level, u1));
1258 printf("u2 = %s\n", to_string(level, u2));
1259 printf("u3 = %s\n", to_string(level, u3));
1260 }
1261#endif
1262
1263 // At this point u3 is monic, and is the monic gcd
1264
1265 // v3 is already 0 here.
1266 dealloc_poly(v1);
1267 dealloc_poly(v2);
1268
1269 result_u = u1;
1270 result_v = u2;
1271 return u3;
1272}
static TowerPolynomial copy(int level, const TowerPolynomial f)
Definition dpoly.cpp:483
void add_in_place(int level, TowerPolynomial &f, const TowerPolynomial g)
Definition dpoly.cpp:722
TowerPolynomial mult(int level, const TowerPolynomial f, const TowerPolynomial g, bool reduce_by_extension)
Definition dpoly.cpp:846
bool make_monic3(int level, TowerPolynomial &u1, TowerPolynomial &u2, TowerPolynomial &u3)
Definition dpoly.cpp:938
static void dealloc_poly(TowerPolynomial &f)
Definition dpoly.cpp:292
void negate_in_place(int level, TowerPolynomial &f)
Definition dpoly.cpp:604
static char * to_string(int level, const TowerPolynomial f)
Definition dpoly.cpp:417
TowerPolynomial division_in_place_monic(int level, TowerPolynomial &f, const TowerPolynomial g)
Definition dpoly.cpp:966
static TowerPolynomial from_long(int level, long c)
Definition dpoly.cpp:501
static void swap_poly(TowerPolynomial &f, TowerPolynomial &g)
Definition dpoly.cpp:1120

References add_in_place(), copy(), dealloc_poly(), division_in_place_monic(), from_long(), make_monic3(), mult(), negate_in_place(), swap_poly(), and to_string().

Referenced by invert().