My Project
Toggle main menu visibility
Loading...
Searching...
No Matches
kernel
linear_algebra
minpoly.h
Go to the documentation of this file.
1
/***********************************************************************************
2
* Author: Sebastian Jambor, 2011 *
3
* (C) GPL (e-mail from June 6, 2012, 17:00:31 MESZ) *
4
* sebastian@momo.math.rwth-aachen.de *
5
* *
6
* Implementation of an algorithm to compute the minimal polynomial of a *
7
* square matrix A \in \F_p^{n \times n}. *
8
* *
9
* Let V_1, \dotsc, V_k \in \F_p^{1 \times n} be vectors such that *
10
* V_1, V_1*A, V_1*A^2, \dotsc, V_2, V_2*A, V_2*A^2, \dotsc *
11
* generate \F_p^{1 \times n}. *
12
* Let mpV_i be the monic polynomial of smallest degree such that *
13
* V_i*mpV_i(A) = 0. *
14
* Then the minimal polynomial of A is the least common multiple of the mpV_i. *
15
* *
16
* *
17
* The algorithm uses two classes: *
18
* *
19
* 1. LinearDependencyMatrix *
20
* This is used to find a linear dependency between the vectors V, V*A, \ldotsc. *
21
* To to this, it has an internal n \times (2n + 1) matrix. *
22
* Every time a new row VA^i is inserted, it is reduced via Gauss' Algorithm, *
23
* using right hand sides. If VA^i is reduced to zero, then the vectors are *
24
* linearly dependent, and the dependency can be read of at the right hand sides. *
25
* *
26
* Example: Compute the minimal polynomial of A = [[0,1],[1,1]] with V = [1,0] *
27
* over F_5. *
28
* Then LinearDependencyMatrix will be: *
29
* After the first step (i.e., after inserting V = [1,0]): *
30
* ( 1 0 | 1 0 0 ) *
31
* After the second step (i.e., after inserting VA = [0,1]): *
32
* ( 1 0 | 1 0 0 ) *
33
* ( 0 1 | 0 1 0 ) *
34
* In the third step, where VA^2 = [1,1] is inserted, the row *
35
* ( 1 1 | 0 0 1 ) *
36
* is reduced to *
37
* ( 0 0 | 4 4 1) *
38
* Thus VA^2 + 4*VA + 4*V = 0, so mpV = t^2 + 4*t + 4. *
39
* *
40
* *
41
* *
42
* 2. NewVectorMatrix *
43
* If one vector V_1 is not enough to compute the minimal polynomial, i.e. the *
44
* vectors V_1, V_1*A, V_1*A^2, \dotsc don't generate \F_p^{1 \times n}, then *
45
* we have to find a vector V_2 which is not in the span of the V_1*A^i. *
46
* This is done with NewVectorMatrix, which simply holds a reduced n \times n *
47
* matrix, where the rows generate the span of the V_jA^i. *
48
* To find a vector which is not in the span, simply take the k-th standard *
49
* vector, where k is not a pivot element of A. *
50
* *
51
* *
52
* For efficiency reasons, the matrix entries in LinearDependencyMatrix *
53
* and NewVectorMatrix are not initialized to zero. Instead, a variable rows *
54
* is used to indicate the number of rows which are nonzero (all further *
55
* rows are regarded as zero rows). Furthermore, the array pivots stores the *
56
* pivot entries of the rows, i.e., pivots[i] indicates the position of the *
57
* first non-zero entry in the i-th row, which is normalized to 1. *
58
***********************************************************************************/
59
60
61
62
63
#ifndef MINPOLY_H
64
#define MINPOLY_H
65
66
class
NewVectorMatrix
;
67
68
class
LinearDependencyMatrix
{
69
friend
class
NewVectorMatrix
;
70
private
:
71
unsigned
p
;
72
unsigned
long
n
;
73
unsigned
long
**
matrix
;
74
unsigned
long
*
tmprow
;
75
unsigned
*
pivots
;
76
unsigned
rows
;
77
78
public
:
79
LinearDependencyMatrix
(
unsigned
n
,
unsigned
long
p
);
80
~LinearDependencyMatrix
();
81
82
// reset the matrix, so that we can use it to find another linear dependence
83
// Note: there is no need to reinitialize the matrix and vectors!
84
void
resetMatrix
();
85
86
87
// return the first nonzero entry in row (only the first n entries are checked,
88
// regardless of the size, since we will also apply this for rows with
89
// right hand sides).
90
// If the first n entries are all zero, return -1 (so this gives a check if row is the zero vector)
91
int
firstNonzeroEntry
(
unsigned
long
*row);
92
93
void
reduceTmpRow
();
94
95
void
normalizeTmp
(
unsigned
i
);
96
97
bool
findLinearDependency
(
unsigned
long
* newRow,
unsigned
long
* dep);
98
99
//friend std::ostream& operator<<(std::ostream& out, const LinearDependencyMatrix& mat);
100
};
101
102
103
// This class is used to find a new vector for the next step in the
104
// minimal polynomial algorithm.
105
class
NewVectorMatrix
{
106
private
:
107
unsigned
p
;
108
unsigned
long
n
;
109
unsigned
long
**
matrix
;
110
unsigned
*
pivots
;
111
unsigned
*
nonPivots
;
112
unsigned
rows
;
113
114
public
:
115
NewVectorMatrix
(
unsigned
n
,
unsigned
long
p
);
116
~NewVectorMatrix
();
117
118
// return the first nonzero entry in row (only the first n entries are checked,
119
// regardless of the size, since we will also apply this for rows with
120
// right hand sides).
121
// If the first n entries are all zero, return -1 (so this gives a check if row is the zero vector)
122
int
firstNonzeroEntry
(
unsigned
long
*row);
123
124
// // let piv be the pivot position of row i. then this method eliminates entry piv of row
125
// void subtractIthRow(unsigned long *row, unsigned i);
126
127
void
normalizeRow
(
unsigned
long
*row,
unsigned
i
);
128
129
void
insertRow
(
unsigned
long
* row);
130
131
// insert each row of the matrix
132
void
insertMatrix
(
LinearDependencyMatrix
& mat);
133
134
// Finds the smallest integer between 0 and n-1, which is not a pivot position.
135
// If no such number exists, return -1.
136
int
findSmallestNonpivot
();
137
138
int
findLargestNonpivot
();
139
};
140
141
142
// compute the minimal polynomial of matrix \in \F_p^{n \times n}.
143
// The result is an array of length n + 1, where the i-th entry represents the i-th coefficient
144
// of the minimal polynomial.
145
//
146
// result should be deleted with delete[]
147
unsigned
long
*
computeMinimalPolynomial
(
unsigned
long
**
matrix
,
unsigned
n,
unsigned
long
p
);
148
149
150
151
/////////////////////////////////
152
// auxiliary methods
153
/////////////////////////////////
154
155
156
// compute x^(-1) mod p
157
//
158
// NOTE: this uses long long instead of unsigned long, for the XEA to work.
159
// This shouldn't be a problem, since p has to be < 2^31 for the multiplication to work anyway.
160
//
161
// There is no need to distinguish between 32bit and 64bit architectures: On 64bit, long long
162
// is the same as long, and on 32bit, we need long long so that the variables can hold negative values.
163
unsigned
long
modularInverse
(
long
long
x
,
long
long
p
);
164
165
void
vectorMatrixMult
(
unsigned
long
*
vec
,
unsigned
long
**mat,
unsigned
**nonzeroIndices,
unsigned
*nonzeroCounts,
unsigned
long
*
result
,
unsigned
n,
unsigned
long
p
);
166
167
// a is a vector of length at least dega + 1, and q is a vector of length at least degq + 1,
168
// representing polynomials \sum_i a[i]t^i \in \F_p[t].
169
// After this method, a will be a mod q.
170
// Method will change dega accordingly.
171
void
rem
(
unsigned
long
* a,
unsigned
long
* q,
unsigned
long
p
,
int
& dega,
int
degq);
172
173
// a is a vector of length at least dega + 1, and q is a vector of length at least degq + 1,
174
// representing polynomials \sum_i a[i]t^i \in \F_p[t].
175
// After this method, a will be a / q.
176
// Method will change dega accordingly.
177
void
quo
(
unsigned
long
* a,
unsigned
long
* q,
unsigned
long
p
,
int
& dega,
int
degq);
178
179
180
// NOTE: since we don't know the size of result (the list can be longer than the degree of the polynomial),
181
// every entry has to be preinitialized to zero!
182
void
mult
(
unsigned
long
*
result
,
unsigned
long
* a,
unsigned
long
*
b
,
unsigned
long
p
,
int
dega,
int
degb);
183
184
185
// g = gcd(a,b).
186
// returns deg(g)
187
//
188
// NOTE: since we don't know the size of g, every entry has to be preinitialized to zero!
189
int
gcd
(
unsigned
long
*
g
,
unsigned
long
* a,
unsigned
long
*
b
,
unsigned
long
p
,
int
dega,
int
degb);
190
191
// l = lcm(a,b).
192
// returns deg(l)
193
//
194
// has side effects for a
195
//
196
// NOTE: since we don't know the size of l, every entry has to be preinitialized to zero!
197
int
lcm
(
unsigned
long
*
l
,
unsigned
long
* a,
unsigned
long
*
b
,
unsigned
long
p
,
int
dega,
int
degb);
198
199
200
// method suggested by Hans Schoenemann to multiply elements in finite fields
201
// on 32bit and 64bit machines
202
static
inline
unsigned
long
multMod
(
unsigned
long
a,
unsigned
long
b
,
unsigned
long
p
)
203
{
204
#if SIZEOF_LONG == 4
205
#define ULONG64 (unsigned long long)
206
#else
207
#define ULONG64 (unsigned long)
208
#endif
209
return
(
unsigned
long
)((
ULONG64
a)*(
ULONG64
b
) % (
ULONG64
p
));
210
}
211
212
#endif
// MINPOLY_H
l
int l
Definition
cfEzgcd.cc:100
i
int i
Definition
cfEzgcd.cc:132
x
Variable x
Definition
cfModGcd.cc:4090
p
int p
Definition
cfModGcd.cc:4086
g
g
Definition
cfModGcd.cc:4098
b
CanonicalForm b
Definition
cfModGcd.cc:4111
LinearDependencyMatrix
Definition
minpoly.h:68
LinearDependencyMatrix::tmprow
unsigned long * tmprow
Definition
minpoly.h:74
LinearDependencyMatrix::findLinearDependency
bool findLinearDependency(unsigned long *newRow, unsigned long *dep)
Definition
minpoly.cc:96
LinearDependencyMatrix::normalizeTmp
void normalizeTmp(unsigned i)
Definition
minpoly.cc:88
LinearDependencyMatrix::firstNonzeroEntry
int firstNonzeroEntry(unsigned long *row)
Definition
minpoly.cc:51
LinearDependencyMatrix::p
unsigned p
Definition
minpoly.h:71
LinearDependencyMatrix::LinearDependencyMatrix
LinearDependencyMatrix(unsigned n, unsigned long p)
Definition
minpoly.cc:19
LinearDependencyMatrix::reduceTmpRow
void reduceTmpRow()
Definition
minpoly.cc:60
LinearDependencyMatrix::resetMatrix
void resetMatrix()
Definition
minpoly.cc:46
LinearDependencyMatrix::pivots
unsigned * pivots
Definition
minpoly.h:75
LinearDependencyMatrix::n
unsigned long n
Definition
minpoly.h:72
LinearDependencyMatrix::matrix
unsigned long ** matrix
Definition
minpoly.h:73
LinearDependencyMatrix::rows
unsigned rows
Definition
minpoly.h:76
LinearDependencyMatrix::NewVectorMatrix
friend class NewVectorMatrix
Definition
minpoly.h:69
NewVectorMatrix
Definition
minpoly.h:105
NewVectorMatrix::normalizeRow
void normalizeRow(unsigned long *row, unsigned i)
Definition
minpoly.cc:225
NewVectorMatrix::nonPivots
unsigned * nonPivots
Definition
minpoly.h:111
NewVectorMatrix::p
unsigned p
Definition
minpoly.h:107
NewVectorMatrix::NewVectorMatrix
NewVectorMatrix(unsigned n, unsigned long p)
Definition
minpoly.cc:181
NewVectorMatrix::rows
unsigned rows
Definition
minpoly.h:112
NewVectorMatrix::matrix
unsigned long ** matrix
Definition
minpoly.h:109
NewVectorMatrix::insertRow
void insertRow(unsigned long *row)
Definition
minpoly.cc:236
NewVectorMatrix::pivots
unsigned * pivots
Definition
minpoly.h:110
NewVectorMatrix::findLargestNonpivot
int findLargestNonpivot()
Definition
minpoly.cc:366
NewVectorMatrix::insertMatrix
void insertMatrix(LinearDependencyMatrix &mat)
Definition
minpoly.cc:331
NewVectorMatrix::firstNonzeroEntry
int firstNonzeroEntry(unsigned long *row)
Definition
minpoly.cc:216
NewVectorMatrix::n
unsigned long n
Definition
minpoly.h:108
NewVectorMatrix::findSmallestNonpivot
int findSmallestNonpivot()
Definition
minpoly.cc:339
result
return result
Definition
facAbsBiFact.cc:76
vec
fq_nmod_poly_t * vec
Definition
facHensel.cc:108
matrix
ip_smatrix * matrix
Definition
matpol.h:43
mult
void mult(unsigned long *result, unsigned long *a, unsigned long *b, unsigned long p, int dega, int degb)
Definition
minpoly.cc:647
quo
void quo(unsigned long *a, unsigned long *q, unsigned long p, int °a, int degq)
Definition
minpoly.cc:597
modularInverse
unsigned long modularInverse(long long x, long long p)
Definition
minpoly.cc:744
computeMinimalPolynomial
unsigned long * computeMinimalPolynomial(unsigned long **matrix, unsigned n, unsigned long p)
Definition
minpoly.cc:428
vectorMatrixMult
void vectorMatrixMult(unsigned long *vec, unsigned long **mat, unsigned **nonzeroIndices, unsigned *nonzeroCounts, unsigned long *result, unsigned n, unsigned long p)
Definition
minpoly.cc:393
lcm
int lcm(unsigned long *l, unsigned long *a, unsigned long *b, unsigned long p, int dega, int degb)
Definition
minpoly.cc:709
rem
void rem(unsigned long *a, unsigned long *q, unsigned long p, int °a, int degq)
Definition
minpoly.cc:572
multMod
static unsigned long multMod(unsigned long a, unsigned long b, unsigned long p)
Definition
minpoly.h:202
ULONG64
#define ULONG64
gcd
int gcd(int a, int b)
Definition
walkSupport.cc:836
Generated on
for My Project by
doxygen 1.18.0
for
Singular