[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 | * To use: * |
---|
| 60 | * Call the method computeMinimalPolynomial(...) * |
---|
| 61 | ***********************************************************************************/ |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | #ifndef MINPOLY_H |
---|
| 67 | #define MINPOLY_H |
---|
| 68 | |
---|
[ec9db9] | 69 | //#include<iostream> |
---|
[2024f6a] | 70 | |
---|
| 71 | class NewVectorMatrix; |
---|
| 72 | |
---|
| 73 | class LinearDependencyMatrix { |
---|
| 74 | friend class NewVectorMatrix; |
---|
| 75 | private: |
---|
| 76 | unsigned p; |
---|
| 77 | unsigned long n; |
---|
| 78 | unsigned long **matrix; |
---|
| 79 | unsigned long *tmprow; |
---|
| 80 | unsigned *pivots; |
---|
| 81 | unsigned rows; |
---|
| 82 | |
---|
| 83 | public: |
---|
| 84 | LinearDependencyMatrix(unsigned n, unsigned long p); |
---|
| 85 | ~LinearDependencyMatrix(); |
---|
| 86 | |
---|
| 87 | // reset the matrix, so that we can use it to find another linear dependence |
---|
| 88 | // Note: there is no need to reinitalize the matrix and vectors! |
---|
| 89 | void resetMatrix(); |
---|
[ec9db9] | 90 | |
---|
[2024f6a] | 91 | |
---|
| 92 | // return the first nonzero entry in row (only the first n entries are checked, |
---|
| 93 | // regardless of the size, since we will also apply this for rows with |
---|
| 94 | // right hand sides). |
---|
| 95 | // If the first n entries are all zero, return -1 (so this gives a check if row is the zero vector) |
---|
| 96 | int firstNonzeroEntry(unsigned long *row); |
---|
| 97 | |
---|
| 98 | void reduceTmpRow(); |
---|
| 99 | |
---|
| 100 | void normalizeTmp(unsigned i); |
---|
[ec9db9] | 101 | |
---|
[2024f6a] | 102 | bool findLinearDependency(unsigned long* newRow, unsigned long* dep); |
---|
| 103 | |
---|
[ec9db9] | 104 | //friend std::ostream& operator<<(std::ostream& out, const LinearDependencyMatrix& mat); |
---|
[2024f6a] | 105 | }; |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | // This class is used to find a new vector for the next step in the |
---|
| 109 | // minimal polynomial algorithm. |
---|
| 110 | class NewVectorMatrix { |
---|
| 111 | private: |
---|
| 112 | unsigned p; |
---|
| 113 | unsigned long n; |
---|
| 114 | unsigned long **matrix; |
---|
| 115 | unsigned *pivots; |
---|
| 116 | unsigned rows; |
---|
| 117 | |
---|
| 118 | public: |
---|
| 119 | NewVectorMatrix(unsigned n, unsigned long p); |
---|
| 120 | ~NewVectorMatrix(); |
---|
[ec9db9] | 121 | |
---|
[2024f6a] | 122 | // return the first nonzero entry in row (only the first n entries are checked, |
---|
| 123 | // regardless of the size, since we will also apply this for rows with |
---|
| 124 | // right hand sides). |
---|
| 125 | // If the first n entries are all zero, return -1 (so this gives a check if row is the zero vector) |
---|
| 126 | int firstNonzeroEntry(unsigned long *row); |
---|
| 127 | |
---|
| 128 | void normalizeRow(unsigned long *row, unsigned i); |
---|
| 129 | |
---|
| 130 | void insertRow(unsigned long* row); |
---|
| 131 | |
---|
| 132 | // insert each row of the matrix |
---|
| 133 | void insertMatrix(LinearDependencyMatrix& mat); |
---|
| 134 | |
---|
| 135 | // Finds the smallest integer between 0 and n-1, which is not a pivot position. |
---|
| 136 | // If no such number exists, return -1. |
---|
| 137 | int findSmallestNonpivot(); |
---|
| 138 | }; |
---|
[ec9db9] | 139 | |
---|
[2024f6a] | 140 | |
---|
| 141 | // compute the minimal polynomial of matrix \in \F_p^{n \times n}. |
---|
| 142 | // The result is an array of length n + 1, where the i-th entry represents the i-th coefficient |
---|
| 143 | // of the minimal polynomial. |
---|
| 144 | // |
---|
| 145 | // result should be deleted with delete[] |
---|
| 146 | unsigned long* computeMinimalPolynomial(unsigned long** matrix, unsigned n, unsigned long p); |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | |
---|
| 150 | ///////////////////////////////// |
---|
| 151 | // auxiliary methods |
---|
| 152 | ///////////////////////////////// |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | // compute x^(-1) mod p |
---|
| 156 | // |
---|
| 157 | // NOTE: this uses long instead of unsigned long, for the XEA to work. |
---|
| 158 | // This shouldn't be a problem, since p has to be < 2^31 for the multiplication to work anyway. |
---|
| 159 | long modularInverse(long x, long p); |
---|
| 160 | |
---|
| 161 | void vectorMatrixMult(unsigned long* vec, unsigned long **mat, unsigned long* result, unsigned n, unsigned long p); |
---|
| 162 | |
---|
| 163 | // a is a vector of length at least dega + 1, and q is a vector of length at least degq + 1, |
---|
| 164 | // representing polynomials \sum_i a[i]t^i \in \F_p[t]. |
---|
| 165 | // After this method, a will be a mod q. |
---|
| 166 | // Method will change dega accordingly. |
---|
| 167 | void rem(unsigned long* a, unsigned long* q, unsigned long p, int & dega, int degq); |
---|
| 168 | |
---|
| 169 | // a is a vector of length at least dega + 1, and q is a vector of length at least degq + 1, |
---|
| 170 | // representing polynomials \sum_i a[i]t^i \in \F_p[t]. |
---|
| 171 | // After this method, a will be a / q. |
---|
| 172 | // Method will change dega accordingly. |
---|
| 173 | void quo(unsigned long* a, unsigned long* q, unsigned long p, int & dega, int degq); |
---|
| 174 | |
---|
| 175 | |
---|
[ec9db9] | 176 | // NOTE: since we don't know the size of result (the list can be longer than the degree of the polynomial), |
---|
[2024f6a] | 177 | // every entry has to be preinitialized to zero! |
---|
| 178 | void mult(unsigned long* result, unsigned long* a, unsigned long* b, unsigned long p, int dega, int degb); |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | // g = gcd(a,b). |
---|
| 182 | // returns deg(g) |
---|
| 183 | // |
---|
| 184 | // NOTE: since we don't know the size of g, every entry has to be preinitialized to zero! |
---|
| 185 | int gcd(unsigned long* g, unsigned long* a, unsigned long* b, unsigned long p, int dega, int degb); |
---|
| 186 | |
---|
| 187 | // l = lcm(a,b). |
---|
| 188 | // returns deg(l) |
---|
| 189 | // |
---|
| 190 | // has side effects for a |
---|
| 191 | // |
---|
| 192 | // NOTE: since we don't know the size of l, every entry has to be preinitialized to zero! |
---|
| 193 | int lcm(unsigned long* l, unsigned long* a, unsigned long* b, unsigned long p, int dega, int degb); |
---|
| 194 | |
---|
| 195 | #endif // MINPOLY_H |
---|