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