LMCAS
Lamina Computer Algebra System
载入中...
搜索中...
未找到
rational_square_free.cpp
浏览该文件的文档.
3
4namespace LMCAS {
5
6std::vector<std::pair<Polynomial<Rational>, int>> square_free_factorization(
7 const Polynomial<Rational>& poly) {
8
9 std::vector<std::pair<Polynomial<Rational>, int>> factors;
10
11 if (poly.is_zero() || poly.degree() <= 0) {
12 return factors;
13 }
14
16
17 if (f.degree() == 1) {
18 factors.push_back({f, 1});
19 return factors;
20 }
21
24
25 if (g.degree() <= 0) {
26 factors.push_back({f, 1});
27 return factors;
28 }
29
30 auto [w, w_rem] = f.div_mod(g);
31
32 int multiplicity = 1;
33
34 while (w.degree() > 0) {
35
37
38 auto [z, z_rem] = w.div_mod(y);
39
40 if (z.degree() > 0) {
41
42 factors.push_back({z.make_monic(), multiplicity});
43 }
44
45 auto [g_next, g_rem] = g.div_mod(y);
46 g = g_next;
47 w = y;
48 multiplicity++;
49 }
50
51 if (g.degree() > 0) {
52 factors.push_back({g.make_monic(), multiplicity});
53 }
54
55 return factors;
56}
57
58/**
59 * @brief 计算多项式的无平方因子部分,用于 Berlekamp 模分解预处理。
60 *
61 * 令 f' = derivative(f)、g = gcd(f,f');g 为常数时 f 已无重因子,
62 * 否则取 f/g,并将结果首一化。
63 *
64 * @param[in] poly 输入的有理系数多项式
65 * @return 无平方因子预处理结果
66 */
68 TfSquareFreeResult result;
69 if (poly.degree() <= 0) {
70 result.square_free = poly;
71 result.repeated_factor = Polynomial<Rational>(Rational(1), poly.variable_name);
72 result.had_repeated_factors = false;
73 return result;
74 }
75 if (poly.degree() == 1) {
76 result.square_free = poly;
77 result.repeated_factor = Polynomial<Rational>(Rational(1), poly.variable_name);
78 result.had_repeated_factors = false;
79 return result;
80 }
81
83
85
86 if (g.degree() <= 0) {
87 result.square_free = poly.make_monic();
88 result.repeated_factor = Polynomial<Rational>(Rational(1), poly.variable_name);
89 result.had_repeated_factors = false;
90 return result;
91 }
92
93 auto [quotient, remainder] = poly.div_mod(g);
94 result.square_free = quotient.make_monic();
95 result.repeated_factor = g.make_monic();
96 result.had_repeated_factors = true;
97 return result;
98}
99
100}
一元多项式模板类
static Polynomial gcd(Polynomial a, Polynomial b)
计算两个多项式的最大公因式
Polynomial differentiate() const
求导
std::pair< Polynomial, Polynomial > div_mod(const Polynomial &other) const
多项式带余除法
std::string variable_name
变量名
bool is_zero() const
判断是否为零多项式
int degree() const
获取多项式的次数
Polynomial make_monic() const
首一化(使首项系数为 1)
TfSquareFreeResult tf_square_free(const Polynomial< Rational > &poly)
计算多项式的无平方因子部分,用于 Berlekamp 模分解预处理。
std::vector< std::pair< Polynomial< Rational >, int > > square_free_factorization(const Polynomial< Rational > &poly)
执行一步二次 Hensel 提升:mod m -> mod m^2.
Polynomial< Rational > square_free
无平方因子部分。
Polynomial< Rational > repeated_factor
重复因子 gcd(f, f')。
bool had_repeated_factors
是否存在重复因子。