Vol. 45 No. 6 IPSJ Journal June 2004 Regular Paper A Practical Implementation of Modular Algorithms for Frobenius Normal Forms of Rational Matrices

Shuichi Moritsugu†

Modular algorithms for computing the Frobenius normal forms of and rational ma- trices are presented and their implementation is reported. These methods compute Frobenius normal forms over Zpi , where pi’s are distinct primes, and then construct the normal forms over Z or Q by the Chinese remainder theorem. Our implementation includes: (1) detection of unlucky primes, (2) a new formula for the efficient computation of a transformation ma- trix, and (3) extension of our preceding algorithm over Z to one over Q. Through experiments using a number of test matrices, we confirm that our modular algorithm is more efficient in practical terms than the straightforward implementation of conventional methods.

• the structure of eigenvectors or generalized 1. Introduction eigenvectors. In a previous paper19), we studied a mod- In addition, algorithms for symbolic computa- ular algorithm for the exact computation of tion of Jordan normal forms can be realized by Frobenius normal forms of integer matrices, and transforming the Frobenius normal forms9),20). substantiated its efficiency by computational Therefore, Frobenius normal forms are the most experiments. In this paper, we extend the suitable for constructing algo- above algorithm to the case of rational matri- rithms by means of computer algebra systems. ces. We propose several algorithms and com- They also have important applications such as pare their efficiency and effectiveness through the eigenvalue method for solving systems of experiments. algebraic equations22),23),30). The motivation for these studies is the fact Algorithms for Frobenius normal forms have that computing various types of normal forms been discussed in a number of papers, but these of matrices is a very important subject in sym- have been concerned mainly with the theoreti- bolic linear algebra. In particular, the Jordan cal computational complexity. As an improve- normal form is theoretically fundamental, but it ment over the classical O(n4) algorithm16),26), requires an algebraic extension field to which all the O(n3) algorithm was proposed in Storjo- the eigenvalues belong. Hence, we have to han- hann 29), and a series of algorithms by Gies- dle algebraic numbers efficiently in computer brecht6)∼8) are known to give the best complex- algebra systems, but this is often so difficult ity. Nevertheless, implementations of such algo- that computing Jordan normal forms becomes rithms have rarely been reported, and Maple2) generally inefficient. is the only existing commercial computer alge- In contrast, the Frobenius normal form is bra system to provide a function for Frobenius computed only by rational operations of the normal forms. elements without algebraic extensions, The purpose of this paper is to describe the and it is absolutely unique, while the Jordan implementation of computing normal forms and normal form is unique up to the permutation of transformation matrices, comparing algorithms Jordan blocks. Moreover, the Frobenius normal from a practical point of view. The algorithm form provides the same detailed information on adopted here belongs to the O(n3 log n) class1) the matrix as the ; for ex- and is not necessarily optimal. However, in- ample24): stead of precise analysis of the complexity, we • the characteristic polynomial, focus on the practical application of modular • the minimal polynomial, methods for the computation of normal forms. • the algebraic and geometric multiplicities The basic idea of the modular algorithm of the eigenvalues, and was first explained by Howell12), where only Frobenius normal forms are computed, with- † Institute of Library and Information Science, Uni- out transformation matrices. In Mathieu and versity of Tsukuba Ford18) and Mukhopadhyay25), p-adic methods

1630 Vol. 45 No. 6 Implementation of Modular Algorithms for Frobenius Normal Forms 1631   for the computation of characteristic polyno- 01 mials were proposed, in which only block di-    ..  agonalized forms or upper triangular forms are  00 . O    computed. The latest paper by Giesbrecht and C =  . . . .  (1) ...... Storjohann8) also describes a modular method    00··· 01 and an algorithm for a transformation ma- c c ··· c c trix. The detection of unlucky primes is treated 0 1 n−2 n−1 in both Howell12) and Giesbrecht and Storjo- is called the associated with 8) n n−1 hann , but since the notions in those stud- the polynomial f(x)=x − cn−1x −···− ies are slightly different, the procedure is eluci- c1x − c0. In particular, the companion matrix dated in this paper from the viewpoint of actual associated with the polynomial of degree one implementation. f(x)=x − c0 is the 1 × 1 [c0]. As another approach for Frobenius normal Lemma 2. The characteristic polynomial forms, we previously proposed a fraction-free ϕC (x) and the minimal polynomial φC (x) of the algorithm21) for polynomial matrices. This al- companion matrix C are equal to the associated n n−1 gorithm is also applicable to integer matrices, polynomial f(x)=x −cn−1x −· · ·−c1x−c0. but it does not necessarily yield a dramatic im- provement in efficiency. In this paper, there- Theorem 3 (Frobenius Normal Form). fore, we apply modular methods to integer and (i) Using a suitable regular matrix S, every rational matrices, and focus particularly on the n × n square matrix A can be transformed following: into a block as follows: −1 • detection of unlucky primes and its imple- F = S AS = C1 ⊕ C2 ⊕···⊕Ct. (2) mentation, This is called the Frobenius normal form (or • efficient computation of transformation rational normal form) of A. Each block ma- matrices with simple elements, and trix Ci (i =1, ···,t)isadi × di compan- • recovery of rational numbers and algo- ion matrix in the form of Eq.(1), and the rithms for rational matrices. associated polynomial ϕi+1(x)ofCi+1 di- The implementation for integer matrices was vides the associated polynomial ϕi(x)ofCi reported in our previous paper19). Theories (i =1,...,t− 1). necessary for both integer matrices and ratio- (ii) For every given matrix A, its Frobenius nal matrices are summarized here in sections 2 normal form F of Eq.(2) is uniquely deter- and 3. We show an extension of the method mined, while a S is to rational matrices in section 4, and discuss not unique. Furthermore, the minimal poly- its efficiency with reference to the experimental nomial of A is given by φA(x)=ϕ1(x), and results in section 5. Most other papers discuss the characteristic polynomial of A is given by only integer matrices, because it is theoretically ϕA(x)=ϕ1(x) · ϕ2(x) ···ϕt(x). sufficient to extract the common denominator For the computation of characteristic poly- of rational matrices. However, our results show nomials, there is a similar algorithm using ma- that such an approach is quite inefficient in ac- trix block diagonalization, as in Eq.(2), known tual implementation. as Danilevskii’s method3),4), where the condi- tion ϕt(x) | ϕt− (x) |···|ϕ (x) is not required. 2. Frobenius Normal Forms of Matri- 1 1 Hence, the result given by Danilevskii’s method ces is not unique and ϕ1(x) is not necessarily iden- In the following sections, we assume that the tical to the minimal polynomial of A. elements of a given matrix are rational num- The normal form in the strict sense of bers; that is, A =[aij] ,aij ∈ Q. Refer to text- Theorem 3 is deduced by the constructive books by Iri and Kan13) and Kan and Iri14) for proof of the “cyclic decomposition theorem details of the normal forms theory. of a finite-dimensional ”5),11),13). Definition 1 (Companion Matrix). The computational complexity is analyzed as The following n × n square matrix O(n3 log n)1), where O(n3) corresponds to basic matrix operations such as multiplication, and O(log n) is interpreted as the number of com- panion blocks t in the normal form of Eq.(2). However, instead of the “constructive proof” 1632 IPSJ Journal June 2004 above, this paper adopts the algorithm ex- adopt the form in Definition 1 for the symbolic pressed by the composition of elementary trans- formulation of eigenvectors. Let S−1AS = C formations14), which is the conversion by the in Eq.(1), and let λ be a root of the associated authors of 13) themselves. To our knowledge, polynomial f(x)ofC. If we put there is nothing in the English literature that n− T u := 1,λ,λ2,...,λ 1 , gives such an algorithm explicitly, even though the formulation in Iri and Kan13) is essentially then we have Cu = λu, A(Su)=λ(Su). identical to that in Hoffman and Kunze11). These relations give the symbolic expression of According to the formulation in Kan and an eigenvector of C and A with the eigenvalue Iri14), we eliminate the elements step by λ. (Hence, we do not compute S−1 in the actual step analogously to the Gaussian elimination implementation.) method, and finally obtain the normal form F Remark 2. If all the elements of A are in- as well as a transformation matrix S. The num- tegers, then its Frobenius normal form F con- ber of loop counts is not stable, but depends on sists only of . On the other hand, we can the arrangement of the elements in the original take the transformation matrices S and S−1 so matrix A. However, this algorithm, whose com- that only one of them is integral, even though plexity is also considered to be O(n3 log n), is A is rational. There exists no known algorithm certainly deterministic. In order to compute a that yields both S and S−1 as integer matrices. similar transformation A → S−1AS, we apply the following elementary transformations suc- 3. Modular Algorithm for Integer Ma- cessively: trices Definition 4 (Elementary Transforma- tions). The following three operations are In this section, we summarize the modular called the elementary transformations. algorithm for integer matrices given in our pre- op1(k, l): Exchange the k-th row and the - vious paper19). We assume that the straightfor- th row of A, and then exchange the k-th ward program based on procedure (3) over Q is column and the -th column of A. already implemented. We propose an algorithm op2(k, c): Multiply the k-th row of A by c−1, that consists of the following two steps: and then multiply the k-th column of A by (i) compute the normal form F s.t. AS = c. SF, using modular arithmetic, and op3(k, l, c): Add the -th row multiplied by c (ii) compute a transformation matrix S with to the k-th row of A, and then subtract the simpler integer elements. k-th column multiplied by c from the -th 3.1 Chinese Remainder Theorem column of A. The elementary transformation op2(k, c) in- In these transformations, the operations on cludes division by c, but this step is per- −1 rows correspond to the transformation matrix formed in Zp as the multiplication by s ≡ c S−1, and the operations on columns correspond (mod p), where cs + pt = 1 from the Euclidean to S. When we apply suitable transformations  algorithm. Therefore, every rational operation ···S−1 S−1 S−1AS S S ··· successively 3 2 1 1 2 3 , in the elementary transformations in Definition we finally obtain F, S, S−1 in Eq.(2). This pro- 4 can be executed modulo a prime p, and the cedure is carried out by performing only ratio- same procedure as over Q yields the Frobenius nal operations in Q, and can be realized by normal form over Zp such that ApSp = SpFp. preparing two unit matrices with the same size In order to recover F over Z from several im- F ,F ,... for A, as follows: ages p1 p2 , we apply the Chinese remain- der theorem (CRT). Since the elements of S in E A E −→ S−1 F S (3) procedure (3) generally produce much longer Remark 1. In some of the literature, in- integers than those of F , recovery of S by the cluding Kan and Iri14), a companion matrix is CRT is very inefficient. Hence, we abandon this S defined as the transposed form of Eq.(1), and procedure and do not reserve each pi . the Frobenius normal form as the transposed Let us denote by F (k) the normal form F form of Eq.(2). Both definitions are related by (mod p1 ···pk). Applying the Newtonian solu- F T = S−1AT S ⇐⇒ F = ST AS−T ; hence, tion to the CRT (Theorem 9 in the appendix), they are essentially identical. However, we we compute F (1) → F (2) → ··· for the moduli Vol. 45 No. 6 Implementation of Modular Algorithms for Frobenius Normal Forms 1633 p1,p1p2,... successively. call the prime number p “strictly unlucky.” In the lifting procedure, when we obtain In this definition, we do not care about trans- (k−1) (k) F = F for (mod p1 ···pk−1) and (mod formation matrices S and Sp. It is obvious that p1 ···pk−1pk), we check the condition ϕ(A)=0 F ≡ Fp (mod p) ⇒ S ≡ Sp (mod p); how- over Z, where ϕ(x) is the minimal polynomial ever, if we have F ≡ Fp (mod p) and S ≡ Sp of F (k). Since the minimal polynomial of A is (mod p), then we do not regard p as strictly unique, if we have the above condition, F (k) unlucky. In order to detect such strictly un- coincides with the normal form over Z. lucky primes, instead of the pivoting pattern, We can also estimate the bound for the ele- the block structure and their sizes (d1,...,dt) ments of F and compute Fp’s using a sufficient are noted in the normal form F = C1 ⊕···⊕Ct. number of primes to recover the integer8).How- Lemma 8 (Giesbrecht6),8)). If and only ever, we selected the above incremental formula if we have lexicographically so that the minimum necessary number of Fp’s (p) (p) (d ,...,dt) (d ,...,d  ), would be computed in the framework of sequen- 1 1 t tial computation. We should also note that it then p is strictly unlucky. is difficult to extend the bound to the case of According to this lemma, we can apply the rational matrices. CRT using every Fp that has the same block 3.2 Detecting and Avoiding Unlucky structure as F ,evenifS ≡ Sp (mod p). The Primes existence of such p’s is discussed later with ref- In this section, we discuss the criterion for de- erence to an example. tecting unlucky primes, arranging various defi- Nevertheless, we implemented the history of nitions used in several previous studies. First, pivoting on the of the criterion in Lemma we introduce the following broad definition: 6, because we also implemented the recovery Definition 5 (Unlucky Prime). of S by means of the CRT, using Sp’s for the Let AS = SF be the normal form and a comparison of algorithms19). If we recover only transformation matrix computed over Z, and F by means of the CRT, then we can adopt the ApSp = SpFp be those computed over Zp.Ifwe criterion of Lemma 8, but the program has not have F ≡ Fp (mod p)orS ≡ Sp (mod p), yet been optimized. then we call the prime number p “unlucky.” The difficulty in detecting unlucky primes lies The following lemma shows that unlucky in the fact that neither the correct pivoting primes (in the sense of Definition 5) are de- pattern over Z nor the block structure in F tected by recording and comparing the history is known beforehand. In our implementation, of pivoting in the elimination process. we presume the correct pattern by a majority Lemma 6 (Howell12)). The pivoting decision, comparing the results for first three pattern for a lucky prime p is identical to that primes p1, p2, and p3. As explained in Morit- over Z. sugu19), this presumption is correct with very If an unlucky prime p divides the pivot ele- high probability if we use prime numbers of ap- ment at some step and the pivot is reduced to propriate magnitude; for example, word size. 0, we have to apply op1(k, ) to choose a non- Cases where the presumption is incorrect will zero pivot, but such an exchange of rows and rarely occur unless we purposely use very small columns does not occur in the computation over primes; for example, those with values less than Z. Hence, we discard the result over such mod- 500. Therefore, the detection of unlucky primes ulus p, and apply the CRT to pi’s that hold the is practically resolved. same pivoting pattern as that over Z. Since the 3.3 Construction of a Transformation number of prime factors of the pivot element Matrix for each step is finite, only a finite number of Applying similar transformations (3) succes- unlucky primes exist for a certain matrix. sively, we can compute a transformation matrix In contrast, other papers6),8) that also pro- S together with the normal form F , but the re- pose a modular algorithm using the CRT adopt sult S = S1S2 ··· has very long integers as its the following narrow definition: elements. If we also recover S by means of the Definition 7 (Strictly Unlucky Prime). CRT, we have to use a larger number of moduli Let A, F , Ap, and Fp be the same as in Defini- pi’s than for the recovery of F . Experimen- 19) tion 5. If we have F ≡ Fp (mod p), then we tal results have shown that this approach is 1634 IPSJ Journal June 2004 fairly inefficient. formation Matrix). However, since transformation matrices S are % input: an integer matrix A and its Frobe- not unique, we have only to obtain one regu- nius normal form F = C1 ⊕ C2 ⊕···⊕Ct lar matrix S such that AS = SF. Hence, we % Let the associated polynomial of each Ck F ϕ x xdk − c xdk−1− first recover by means of the CRT, and then be k( )= k,dk−1 we compute S with elements that are as simple ···−ck,1x − ck,0. AS SF possible, so that = is satisfied. This ap- % output: an integer matrix proach is also proposed in Giesbrecht and Stor- S = s , ··· s ,d ··· st, ··· st,d johann8), using another formulation. 1 1 1 1 1 t For the sake of simplicity, we first consider the s.t. AS = SF case where F has one companion block. The (Step) for k =1tot do s relation Compute k,dk by   ϕ A s 1 solving k( ) k,dk = 0; j d −  ..  for = k 1downto1do AS S  .  s As − c s =   k,j = k,j+1 k,j k,dk ; 1 If the initial vector sk,d is inappropriately c c ··· c k 0 1 n−1 chosen, this algorithm might return a singular n n−1 ϕ(x)=x − cn−1x −···−c1x − c0 matrix S even when the condition AS = SF is S

S s s ··· s sian elimination, then we try this algorithm = 1 2 n : s  again, choosing another initial vector k,dk .A  As1 = c0sn necessary condition for such appropriate initial  19)  As2 = s1 + c1sn vectors is discussed in our previous paper , ··· which guarantees the existence of an appropri-  As s c s s  n−1 = n−2 + n−2 n ate k,dk . In our actual implementation, we set s Asn = sn−1 + cn−1sn. the vector k,dk with randomly chosen simple but generic elements. Hence, Algorithm 1 is By eliminating sn− ,...,s in turn upward 1 1 probabilistic at this step, but experiments indi- from the lowest equation, we obtain   cate that it is practical enough. n n−1 A − cn−1A −···−c1A − c0E sn 3.4 Modular Algorithm for Integer = 0;i.e., ϕ(A)sn = 0. (4) Matrices The whole algorithm for integer matrices is When the normal form F has more than one given below. For the sake of simplicity, the pro- block, we have the following conditions for sd, gram will simply stop when 10 unlucky primes where we let d be the size of the target block: are detected, where the first presumption might • For the first companion block, ϕ(x) is the be incorrect. Otherwise, the CRT process is minimal polynomial of A. Then we have terminated when F (k−1) = F (k), and we check ϕ(A)=O according to Cayley-Hamilton’s ϕ(A)=O, where ϕ(x) is the minimal polyno- theorem, and sd can be arbitrary. mial of F (k). Since the minimal polynomial is • For the second block downward, we need unique, F (k) is exactly equal to the Frobenius to solve the system of linear equations (4) normal form of A when ϕ(A)=O. actually, where 0 ≤ rankϕ(A)

% To simplify the following description, we Remark 3. Let us compute the bound for assume a majority decision; for example, F using Lemma 2.1 in 8): p p the patterns for 1 and 3 are identical but γ =2nen/2Annn/2, p2 gives a different pattern. (Refer to the (5) where A = max|Aij|. discussion in §3.2 for details.) ij F (3) F F n A (S3) Construct from p1 and p3 Substituting = 10 and = 10, we obtain 20 by the CRT; γ  1.5 × 10 , while the final modulus pi ≤ (S4) k := 3; count := 1; 1014 was used in the actual computation of the (S5) Do until (F (k−1) = F (k)) CRT. k := k +1; F A S ≡ S F 4. Algorithms for Rational Matrices Compute pk s.t. pk pk pk pk (mod pk); Two ways can be considered for treating ra- If pk does not yield tional matrices, as shown in the following sub- the presumed pivoting pattern sections. then { count := count +1; 4.1 An Algorithm Executeed over the If count = 10 then STOP; } Integers (k) else construct F from We let A˜ = kA (aij ∈ Q,k,a˜ij ∈ Z), F (k−1) F A k and pk by the CRT; where is a rational matrix and is the com- (S6) If ϕ(A) = O then goto (S5); mon denominator of aij’s. We can perform the % ϕ(x) = min.pol. of F (k) computation over Z as far as possible by ap- (S7) Construct S from A, F (k) plying Algorithm 2 to A˜. The relation between by Algorithm 1 ( over Z ); the Frobenius normal forms of A and A˜ is illus- (S8) While rank(S)

[ 8 -7 9 -2 -10 -10 -10 6 -5 -10 ] [3-7-51-67-94-69] [-8-5-2-71 1 5 9-9-7] [ -5 1 -6 7 -9 4 -6 9 -8 -5 ] [ -2 -7 1 1 4 -6 9 -1 -7 4 ] [ -3 1 -3 -3 1 2 2 -3 -8 -8 ] [ -1 4 -3 3 0 -4 -2 6 -1 -3 ] [ -3 0 2 -1 3 9 -7 -10 -7 1 ] [ -5 -10 -9 9 -10 1 -8 -2 3 6 ] [7-7-714-7-297 9] frobenius_nf(A,’F,’after); p = 13 pivot = (nil nil nil nil nil nil nil nil nil) #block = 1 size = [10] p = 17 pivot = (nil (2 . 3) nil nil nil nil nil nil nil) #block = 1 size = [10] p = 19 pivot = (nil nil nil nil nil nil nil nil nil) #block = 1 size = [10] lucky primes = (13 19) lucky pivots = (nil nil nil nil nil nil nil nil nil) initial mod = 247 p = 23 is unlucky : pivot = (nil nil nil nil nil (6 . 7) nil nil nil) #block = 1 size = [10] p = 37 is unlucky : pivot = (nil nil nil nil nil (6 . 7) nil nil nil) #block = 1 size = [10]

#block = 1 size = [10] regular_p = t (mod 19) S is regular (time for test : 0) max length = 11 digits (time : 0) time for construction of S : 0 product of 9 lucky primes (except 2 unlucky primes) : 14 digits

Time for Frobenius : 15

Fig. 1 Example of execution of Algorithm 2.

=: F. (8) Combining the modular arithmetic, CRT, and Consequently, we obtain F , which is the Frobe- Algorithm 6, we compute the Frobenius normal nius normal form of A/k˜ = A. Since form F (over Q): S−1AS (SV˜ )−1A(SV˜ )=F,   1 S AS SF   a transformation matrix such that =  1  is given by S := SV˜ . In this formulation, we  1  =   note that S is integral (sij ∈ Z), even though  p q r s    A and F are rational (aij,fij ∈ Q). 1 Remark 4. Most other papers discuss the x y Frobenius normal forms only for integer matri- (p,q,r,s; x,y ∈ Q) ces, ignoring rational matrices. It seems that =: F. (9) applying the above-mentioned procedure is con- We note that a transformation matrix S is un- sidered to be sufficient. However, we discuss the known at this point. If we apply Algorithm 1 direct recovery of rationals in the next subsec- to this A and F , we obtain S such that sij ∈ Q. tion, and implement both algorithms to com- However, we consider executing Algorithm 1 pare their efficiency. over the integers for efficiency in our implemen- 4.2 An Algorithm Using Integer-to- tation, because integer computation is much Rational Conversion faster than rational computation in our envi- We can also recover the Frobenius nor- ronment (§5.1). mal form F (over Q) directly from F (k) Therefore, we convert F into F˜, which is the (mod p1p2 ···pk) by using integer-to-rational normal form of the integer matrix A˜ = kA, and conversion (Algorithm 6 in the appendix). is identical to (6). Using the same matrix V as When we first convert A (over Q)intoAp ≡ A (7), we apply the inverse of the similar trans- (mod p), we discard the unlucky prime p that formation (8) to kF: divides the denominator of an element aij in A. Vol. 45 No. 6 Implementation of Modular Algorithms for Frobenius Normal Forms 1637 V kF V −1 S˜ A˜ F˜ ( )  (Q3) Construct (over Z) from and 1 by Algorithm 1.   S SV˜  1  (Q4) Let := (over Z).   % This result S is essentially identical  1  =  k4p k3q k2r ks  to S in the Zmod algorithm.   1 5. Experimental Results k2x ky 5.1 Environment for Implementation =: F.˜ (10) We have been developing the program code Thus we obtain the normal form over Z for A˜. for modular computation of Frobenius normal Applying Algorithm 1 to A˜ and F˜, we construct forms in the following environment: S˜ such that A˜S˜ = S˜F˜. Finally, we obtain the • Computer algebra system: transformation matrix S by letting S := SV˜ as Reduce 3.710) (Windows version) in the previous section. We should note again + RLISP ’8817) that this result S is also integral. – Computation of polynomials and ratio- 4.3 Practical Algorithms nal numbers: We implemented the following three algo- algebraic mode of Reduce rithms and experimented to compare their ef- – Computation over Z and Zp: ficiency. For all three algorithms, the input is RLISP (symbolic mode of Reduce) an n × n matrix A (over Q), and the output is • CPU: Pentium 4 (2 GHz) the Frobenius normal form F (over Q) and a Main memory: 1.5 GB transformation matrix S (over Q or Z). (limited to 1.0 GB for the Reduce system) In the first algorithm, given by Kan and • List of prime numbers less than 231: 14) Iri , rational arithmetic is used straightfor- {p1,p2,...,p1000} = wardly. {2147483647, 2147483629, ..., 2147462143} Algorithm 3 (Rat). • Generation of test matrices: (Step) According to procedure (3), compute (i) Prepare a characteristic polynomial F and S (AS = SF) directly over Q. with random rational coefficients whose In the second algorithm, shown in §4.1, the sizes are normalized. computation is performed over the integers as (ii) Construct the Frobenius normal far as possible. form associated with the characteristic Algorithm 4 (Zmod). polynomial. (iii) Randomize the normal form by a (Z1) Extract the common denominator: similar transformation. A˜ := kA. (Maximal size of elements in the result: (Z2) Compute F˜ (over Z) by using about 200 digits/200 digits for n = 100) modular arithmetic and the CRT. • Verification and comparison: (Z3) Construct S˜ (over Z) from A˜ and F˜ Maple 72) (Windows version) by Algorithm 1. 5.2 Experimental Results (Z4) Convert F˜ into F (over Q) by Eq.(8), S SV˜ We prepared two groups of test matrices and and let := (over Z). § % Steps (Z2) and (Z3) correspond to applied the three algorithms shown in 4.3. Algorithm 2. First, we note that no unlucky prime was de- tected in any of these examples. In the third algorithm, discussed in §4.2, Table 1 shows the result for nonderogatory which we propose as the most practical one, matrices, whose Frobenius normal forms con- integer-to-rational conversion is used to recover sist of one companion block. The number of rationals from modular numbers. primes used as moduli is given in the #pi col- Algorithm 5 (Qmod). umn. We set two types of matrices in each size (Q1) Compute F (over Q) by using and divided the results into two subgroups: modular arithmetic, the CRT, and Upper: Non-diagonalizable (i.e., those whose integer-to-rational conversion. minimal polynomials are not square-free), (Q2) Convert F into F˜ (over Z) by Eq.(10), Lower: Diagonalizable (i.e., those whose min- which is the normal form of A˜ := kA. imal polynomials are square-free). The difference between these properties sig- 1638 IPSJ Journal June 2004

Table 1 CPU time (sec) for rational matrices I. Rat Zmod Qmod Maple n TR #pi TZ TZ /TR #pi TQ TQ/TR TM TQ/TM 30 19.20 171 13.34 0.695 13 2.48 0.129 108.36 0.023 35 37.20 234 29.53 0.794 15 5.36 0.144 238.91 0.022 40 66.45 285 54.08 0.814 16 9.56 0.144 481.57 0.020 45 110.61 376 106.97 0.967 18 18.33 0.166 871.97 0.021 50 168.42 465 189.12 1.123 20 31.88 0.189 1427.22 0.022 100 3845.44 >1000 (failure) 39 1170.11 0.304 51088.66 0.023 30 23.22 162 12.50 0.538 12 2.28 0.098 135.86 0.017 35 55.34 216 26.83 0.485 14 4.83 0.087 371.56 0.013 40 152.30 320 63.08 0.414 18 11.09 0.073 1152.73 0.010 45 330.14 417 123.64 0.375 20 21.22 0.064 2635.40 0.008 50 696.86 530 228.25 0.328 23 37.70 0.054 5897.08 0.006 100 42331.16 >1000 (failure) 34 1067.83 0.025 (not tried)

cf. max length of sij (digits) for n =50 Rat Zmod Qmod Upper 2113 / 2102 4318 4318 Lower 5023 / 5024 4899 4899

Table 2 CPU time (sec) for rational matrices II. Rat Zmod Qmod Maple n (Structure) TR #pi TZ TZ /TR #pi TQ TQ/TR TM TQ/TM 30 (15+15) 18.23 38 4.31 0.237 7 2.34 0.129 146.25 0.016 35 (20+15) 45.67 75 13.28 0.291 9 6.06 0.133 216.72 0.028 40 (25+15) 115.50 117 31.50 0.273 11 13.38 0.116 632.09 0.021 45 (30+15) 263.70 164 65.86 0.250 13 28.22 0.107 1579.33 0.018 ...... 50 (20+15+15) 128.24 75 38.92 0.304 9 19.47 0.152 866.04 0.022 55 (20+20+15) 351.95 75 63.78 0.181 9 36.27 0.103 2633.78 0.014 60 (25+20+15) 522.86 117 128.56 0.246 11 69.61 0.133 4350.91 0.016 65 (25+25+15) 1386.89 117 191.43 0.138 11 114.97 0.083 13083.23 0.009 70 (30+25+15) 1573.34 164 336.09 0.214 13 198.11 0.126 14743.82 0.013 ...... 70 (25+20+15+10) 511.84 117 292.49 0.571 11 145.27 0.284 6839.38 0.021 80 (30+25+15+10) 2635.15 164 650.16 0.247 13 375.71 0.143 34695.14 0.011 90 (30+25+20+15) 4168.56 164 900.19 0.216 13 519.23 0.125 46700.11 0.011 100 (35+30+20+15) 3640.00 210 1974.35 0.542 14 1163.09 0.320 47808.79 0.024

cf. max length of sij (digits) for n = 100 Rat Zmod Qmod 4104 / 4096 2056 2052 nificantly affected the computation time when Table 1, whereas the efficiency of the Qmod al- the Rat algorithm was used. The cause of this gorithm is confirmed. The values of output S lies mainly in the length of sij output; that is, produced by Zmod and Qmod are essentially the upper subgroup yielded much shorter sij’s identical, although they vary slightly in each Rat s than the lower subgroup. Therefore, the computation because the initial vectors k,dk in algorithm is affected not only by the size n of Algorithm 1 are randomly chosen. matrices, but also to their properties. All the test matrices in Table 2 are diagonal- In contrast, the Zmod and Qmod algorithms izable; that is, their minimal polynomials are showed a common tendency for both the upper square-free. We also experimented with using and lower subgroups, and seem to be entirely non-diagonalizable matrices, but the difference dominated by the matrix size. Moreover, the was not so clear as in Table 1. Qmod algorithm is always faster than Zmod, To confirm the results and compare the com- because Qmod needs much fewer primes than putation time, we used the “frobenius” function Zmod. Consequently, the Zmod algorithm is in Maple 7. The results for the timing data not useful, particularly for the upper subgroup, showed a similar tendency to those of the Rat whereas the Qmod algorithm is superior to the algorithm, and seem to be influenced by the Rat and Zmod algorithms for both subgroups. length of elements in a transformation matrix Table 2 shows the result for derogatory ma- P , which corresponds to S−T in our formula- trices, whose Frobenius normal forms consist of tion. more than one companion block. On the whole, the Zmod algorithm is not so inefficient as in Vol. 45 No. 6 Implementation of Modular Algorithms for Frobenius Normal Forms 1639

study. 6. Concluding Remarks ( 6 ) Divide-and-conquer algorithms for in- In this paper, we implemented modular algo- teger-to-rational conversion28) are now being rithms for the Frobenius normal forms of inte- studied. Since it is not easy to implement such ger and rational matrices, and confirmed their fast algorithms in our environment, we imple- efficiency by experiments. Modular methods mented a classical algorithm by Wang31),32). for integer matrices have been proposed by sev- Although the step of integer-to-rational conver- eral authors, but it seems that no implemen- sion is not necessarily dominant in the present tation including rational matrices has yet been framework of computation, implementation of reported elsewhere. Finally, we itemize the re- such divide-and-conquer algorithms should also sults of our studies. be considered in the future development of ( 1 ) We implemented the criterion of the practical computer algebra systems. history of pivoting for detecting unlucky References primes, and confirmed that it works correctly. The criterion of the block structure in the nor- 1) Augot, D. and Camion, P.: On the Compu- mal form F is also discussed. If a transforma- tation of Minimal Polynomials, Cyclic Vectors, tion matrix S is not recovered by the CRT, the and Frobenius Forms, Linear Algebra and Its detection of “strictly unlucky primes” is suffi- Applications, Vol.260, pp.61–94 (1997). cient for the recovery of F . 2) Char, B.W., et al.: Maple V Library Reference ( 2 ) The number of primes used as moduli Manual, Springer, N.Y. (1991). is significantly reduced by the Newtonian so- 3) Danilevskii, A.M.: On the Numerical Solution of the Secular Equation, Mat. Sb., Vol.2, No.1, lution to the CRT in the form of Eq.(12), in pp.169–172 (1937). (In Russian) comparison with the Lagrangian solution using 4) Faddeev, D. and Faddeeva, V.: Computational a theoretical bound given by Eq.(5). Moreover, Methods of Linear Algebra, W.H. Freeman, San since it seems difficult to extend the theoreti- Francisco (1963). cal bound to the rational case, we consider our 5) Gantmacher, F.R.: The Theory of Matrices, approach using the incremental formula to be (2nd ed.) Vol.1, Chelsea, N.Y. (1959). more practical than using theoretical bounds. 6) Giesbrecht, M.: Fast Algorithms for Rational ( 3 ) A transformation matrix S should be Forms of Integer Matrices, ISSAC 94, pp.305– constructed after the normal form F is com- 311, ACM (1994). puted in order to avoid swelling of its elements. 7) Giesbrecht, M.: Nearly Optimal Algorithms This procedure is probablistic, but in practical for Canonical Matrix Forms, SIAM J.Comput., terms it returns a regular matrix almost with- Vol.24, No.5, pp.948–969 (1995). out fail. 8) Giesbrecht, M. and Storjohann, A.: Comput- ( 4 ) Among commercial computer algebra ing Rational Forms of Integer Matrices, J.Sym- systems, only Maple provides a function for bolic Computation, Vol.34, No.3, pp.157–172 (2002). Frobenius normal forms. The details of its algo- 9) Gil, I.: Computation of the Jordan Canoni- rithm are not open, but it tends to be affected cal Form of a Square Matrix (Using the Axiom by swelling of the elements in the transfor- Programming Language), ISSAC 92, pp.138– mation matrix computed simultaneously. This 145, ACM (1992). Maple package has been converted into the Re- 10) Hearn, A.C.: Reduce User’s Manual (Ver.3.7), duce library27), but it seems to be working less RAND Corp., Santa Monica (1999). efficiently in Reduce than in Maple. 11) Hoffman, K. and Kunze, R.: Linear Algebra ( 5 ) For rational matrices, the Qmod algo- (2nd ed.), Prentice-Hall, New Jersey (1971). rithm should be used rather than the Zmod al- 12) Howell, J.A.: An Algorithm for the Exact Re- gorithm. When the number of primes pi needed duction of a Matrix to Frobenius Form Us- for each algorithm is compared, it is obvious ing Modular Arithmetic, I & II, Math. Comp., that Qmod is superior to Zmod for matrices Vol.27, No.124, pp.887–920 (1973). with longer elements. Hence, algorithms appli- 13) Iri, M. and Kan, T.: Linear Algebra: Ma- cable only to integer matrices, which have been trix and Its Normal Forms, Kyoiku-Shuppan, studied by other authors, are not sufficient for Tokyo (1977). (In Japanese) 14) Kan, T. and Iri, M.: Jordan Normal Forms, rational matrices. Our success in developing Qmod Univ. of Tokyo Press, Tokyo (1982). (In the algorithm, which is directly targeted Japanese) at rational matrices, shows the benefits of our 1640 IPSJ Journal June 2004

15) Lauer, M.: Computing by Homomorphic Kyoto Univ., Vol.1295, pp.80–86 (2002). (In Images, Computer Algebra : Symbolic and Japanese) Algebraic Computation, Buchberger, B., et 29) Storjohann, A.: An O(n3) Algorithm for the al. (eds.), pp.139–168, 2nd edition, Springer- Frobenius Normal Form, ISSAC 98, pp.101– Verlag, Wien (1983). 104, ACM (1998). 16) L¨uneburg, H.: On the Rational Normal Form 30) Takeshima, T. and Yokoyama, K.: A Solution of Endomorphisms: A Primer to Construc- of Systems of Algebraic Equations: Applica- tive Algebra, Wissenschaftsverlag, Mannheim tion of Eigenvectors of Linear Maps on Residue (1987). Class Rings, Comm.Symb.Algeb.Manip., Vol.6, 17) Marti, J.: RLISP ’88: An Evolutionary Ap- No.4, pp.27–36 (1990). (In Japanese) proach to Program Design and Reuse, World 31) Wang, P.S.: A p-adic Algorithm for Univari- Scientific, Singapore (1993). ate Partial Fractions, SYMSAC 81, pp.212– 18) Mathieu, M.-H. and Ford, D.: On p-adic Com- 217, ACM (1981). putation of the Rational Form of a Matrix, J. 32) Wang, P.S., Guy, M.J.T. and Davenport, J.H.: Symbolic Computation, Vol.10, No.5, pp.453– P -adic Reconstruction of Rational Numbers, 464 (1990). ACM SIGSAM Bull., Vol.16, No.2, pp.2–3 19) Moritsugu, S.: Modular Computation of (1982). Frobenius Normal Forms of Integer Matrices, J. Japan Soc. Symbolic and Algebraic Com- Appendix putation, Vol.9, No.4, pp.52–67 (2003). (In A.1 Solutions to the CRT Japanese) Theorem 9 (CRT: the Case of Two 20) Moritsugu, S. and Kuriyama, K.: Exact Com- Moduli). When m1 and m2 are relatively putation of Jordan Normal Forms of Matrices prime integers, the solution of the following sys- by Computer Algebra, Trans.Japan Soc.Indust. tem of modular equations Appl. Math., Vol.2, No.1, pp.91–103 (1992). (In x ≡ a1 (mod m1) Japanese) x ≡ a m 21) Moritsugu, S. and Kuriyama, K.: Fraction- 2 (mod 2) Free Method for Computing Rational Normal is given by computing x ≡ a −a m s a m m , Forms of Polynomial Matrices, RISC-Linz Re- ( 2 1) 1 + 1 (mod 1 2) (11) −1 port Series 97-18, RISC-Linz (1997). where m1s + m2t = 1; that is, s ≡ m1 22) Moritsugu, S. and Kuriyama, K.: On Multi- (mod m2). ple Zeros of Systems of Algebraic Equations, The number of moduli required to recover the ISSAC 99, pp.23–30, ACM (1999). Frobenius normal form F over Q is not known 23) Moritsugu, S. and Kuriyama, K.: A Linear Al- in advance. For integer matrices, several the- gebra Method for Solving Systems of Algebraic oretical bounds are known, but they are often Equations, J.Japan Soc.Symbolic and Algebraic overestimated. Hence, it is recommended that Computation, Vol.7, No.4, pp.2–22 (2000). the Newtonian solution to the general CRT be 24) Moritsugu, S. and Kuriyama, K.: Symbolic adopted rather than the Lagrangian solution15). Computation of Eigenvalues, Eigenvectors and Therefore, using prime numbers p1,p2,p3,..., Generalized Eigenvectors of Matrices by Com- in order to lift the modulus to p p ,pp p ,..., puter Algebra, Trans. Japan Soc. Indust. Appl. 1 2 1 2 3 we repeatedly apply Theorem 9 to the following Math., Vol.11, No.2, pp.103–120 (2001). (In system: Japanese) x ≡ a p ···p 25) Mukhopadhyay, A.: Exact Computation of the k−1 (mod 1 k−1) x ≡ a p Characteristic Polynomial of an Integer Ma- k (mod k) trix, AAECC 3, Lect. Notes in Comp. Sci., (k =2, 3,...), (12) Vol.229, pp.316–324 (1985). which is the Newtonian solution to the CRT 26) Ozello, P.: Calcul Exact des Formes de Jor- with more than three moduli. dan et de Frobenius d’une Matrice, PhD The- A.2 Algorithm for Integer-to-Rational sis, Universit´e Scientifique Technologique et Conversion M´edicale de Grenoble (1987). The following algorithm is well known as an 27) Rebbeck, M.: A Linear Algebra Package extension of the classical Euclidean algorithm: for REDUCE, Technical Report, ZIB, Berlin Algorithm 6 (Integer-to-Rational Con- (1994). version31),32)). 28) Sasaki, T., Takahashi, Y. and Sugimoto, T.: On Integer-to-Rational Conversion Algorithm for Long Integers, Papers Res. Inst. Math. Sci., Vol. 45 No. 6 Implementation of Modular Algorithms for Frobenius Normal Forms 1641 Shuichi Moritsugu received % input: an integer c, the modulus m the B.S., M.S. and Ph.D. de- (0