LMCAS
Lamina Computer Algebra System
Toggle main menu visibility
载入中...
搜索中...
未找到
rational_square_free.cpp
浏览该文件的文档.
1
#include "
rational_polynomial.hpp
"
2
#include "
exact_factorization.hpp
"
3
4
namespace
LMCAS
{
5
6
std::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
15
Polynomial<Rational>
f = poly.
make_monic
();
16
17
if
(f.
degree
() == 1) {
18
factors.push_back({f, 1});
19
return
factors;
20
}
21
22
Polynomial<Rational>
f_prime = f.
differentiate
();
23
Polynomial<Rational>
g =
Polynomial<Rational>::gcd
(f, f_prime);
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
36
Polynomial<Rational>
y =
Polynomial<Rational>::gcd
(g, w);
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
*/
67
TfSquareFreeResult
tf_square_free
(
const
Polynomial<Rational>
& poly) {
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
82
Polynomial<Rational>
deriv = poly.
differentiate
();
83
84
Polynomial<Rational>
g =
Polynomial<Rational>::gcd
(poly, deriv);
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
}
LMCAS::Polynomial
一元多项式模板类
定义
polynomial.hpp:52
LMCAS::Polynomial::gcd
static Polynomial gcd(Polynomial a, Polynomial b)
计算两个多项式的最大公因式
定义
polynomial_division.hpp:107
LMCAS::Polynomial::differentiate
Polynomial differentiate() const
求导
定义
polynomial.hpp:307
LMCAS::Polynomial::div_mod
std::pair< Polynomial, Polynomial > div_mod(const Polynomial &other) const
多项式带余除法
定义
polynomial.hpp:207
LMCAS::Polynomial::variable_name
std::string variable_name
变量名
定义
polynomial.hpp:56
LMCAS::Polynomial::is_zero
bool is_zero() const
判断是否为零多项式
定义
polynomial.hpp:92
LMCAS::Polynomial::degree
int degree() const
获取多项式的次数
定义
polynomial.hpp:100
LMCAS::Polynomial::make_monic
Polynomial make_monic() const
首一化(使首项系数为 1)
定义
polynomial.hpp:324
exact_factorization.hpp
LMCAS
定义
assumption.hpp:11
LMCAS::tf_square_free
TfSquareFreeResult tf_square_free(const Polynomial< Rational > &poly)
计算多项式的无平方因子部分,用于 Berlekamp 模分解预处理。
定义
rational_square_free.cpp:67
LMCAS::square_free_factorization
std::vector< std::pair< Polynomial< Rational >, int > > square_free_factorization(const Polynomial< Rational > &poly)
定义
rational_square_free.cpp:6
rational_polynomial.hpp
LMCAS::TfSquareFreeResult
执行一步二次 Hensel 提升:mod m -> mod m^2.
定义
exact_factorization.hpp:111
LMCAS::TfSquareFreeResult::square_free
Polynomial< Rational > square_free
无平方因子部分。
定义
exact_factorization.hpp:112
LMCAS::TfSquareFreeResult::repeated_factor
Polynomial< Rational > repeated_factor
重复因子 gcd(f, f')。
定义
exact_factorization.hpp:113
LMCAS::TfSquareFreeResult::had_repeated_factors
bool had_repeated_factors
是否存在重复因子。
定义
exact_factorization.hpp:114
src
rational_square_free.cpp
生成于
, 为 LMCAS使用
1.18.0