CMAT is a matrix calculator program, written in C.

Calculations can be performed on matrices with complex rational coefficients using exact arithmetic routines, as well as on matrices with elements mod p. There is also a MSDOS version which runs on 386/486 machines.

The MSDOS version is available by anonymous ftp from Mathematics Archives.

The MSDOS and SUN versions are available at
People using the SUN version have to create a .cmatrc file in the working directory, consisting of the two lines

(Also see cmat_bugfix.html for bug reports.)

There are three calculator programs within CMAT: CMATR, CMATCR and CMATM. CMATR works over the rationals, CMATCR works the field of complex rationals and CMATM works over the field of p elements, where p is any prime less than 216 = 65536.

(In the student computing labs in rooms 4.20/5.19, click on one of the icons CMATR, CMATCR, CMATM.)

The programs use multiple precision arithmetic routines based on those in [Fla][342-357,175-185]. (See documentation.)

Up to M0 (=30) objects of each type can be created and stored for use in future sessions.

(rational) numbers: R[0],...,R[M0-1];
(rational) matrices: RM[0],...,RM[M0-1];
polynomials (rational): PR[0],...,PR[M0-1];
polynomial (rational) matrices: PRM[0],...,PRM[M0-1];
(complex rational) numbers CR[0],...,CR[M0-1];
(complex rational) matrices CRM[0],...,CRM[M0-1];
polynomials (complex rational): PCR[0],...,PCR[M0-1];
polynomial (complex rational) matrices: PCRM[0],...,PCRM[M0-1];
(mod p) matrices: mM[0],...,mM[M0-1];
polynomials (mod p): Pm[0],...,Pm[M0-1];
polynomial (mod p) matrices: PmM[0],...,PmM[M0-1].
The program can handle integers of arbitrary size. Matrix size is restricted only by the time taken to create the matrix in question and to compute with it. Execution times are much less when calculating with matrices mod p.


Conjugation, addition, subtraction, multiplication, exponentiation, ratio and inverse. Also the m-th root of rational can be computed to a given number of decimal places.


Addition, subtraction, multiplication, scalar-multiplication, exponentiation, derivative, evaluation, quotient, remainder, greatest common divisor, expressing the gcd as a linear combination of the polynomials involved, square-free decomposition. The resultant of two polynomials over Z[x] and the discriminant of a polynomial over Z[x] can be found. There is modular exponentiation for polynomials mod p.

The Berlekamp matrix of a squarefree polynomial mod p. (Our Berlekamp matrix is the transpose of the usual Berlekamp matrix.) Factorization of polynomials with rational, complex rational or mod p coefficients into irreducibles. Finding an irreducible polynomial of given degree mod p. Finding the nth cyclotomic polynomial. (mod p)


  1. Addition, negation, complex-conjugate transpose, multiplication by a scalar, matrix multiplication, exponentiation, Kronecker product, evaluating matrix polynomials at a square matrix.
  2. The standard elementary row and column operations can be performed on all types of matrices.
  3. The entries of a stored matrix can be changed, either individually, or by row or column. A single row or column can be deleted.
  4. A single row or column vector can be selected from a stored matrix. More generally, a submatrix of a stored matrix can be formed by selecting a subfamily of rows or columns, respectively.
  5. Two matrices can be joined by rows or columns. The partitioned matrix [A|I] can be formed from A. A matrix can be unwrapped into a column vector (a process useful for finding the minimum polynomial of a matrix).
  6. Forming the matrix A-tI, for use in eigenvector calculations.
  7. Creating special matrices such as the identity matrix, elementary Jordan matrices and companion matrices as well as band matrices.
  8. Creating matrices of scalars whose (i-j)th element is given by simple arithmetical expressions such as i + j - i^2 + 3*j for the numerators and denominators, may be created wholly within CMAT.
  9. Finding the inverse, adjoint, determinant, characteristic polynomial and minimum polynomial of a matrix of scalars, P-1AP. The Cholesky decomposition of a positive definite matrix is found: A=L*DL, where L is upper unit triangular and D is a diagonal matrix. The output has the diagonal elements of D on its diagonal and its above diagonal elements are those of L.
    The inverse of a matrix M of integers mod m can be found, when m > 1 is a positive integer which is coprime to the determinant of M.
  10. Finding the reduced row-echelon form, solving linear equations, finding bases for the null-space N(A) and column-space C(A) of a matrix A. These procedures are very useful in algorithms for finding the Jordan canonical form, or more generally, the rational canonical form of a square matrix. (See [paper].)
  11. Calculating dot product, length and using the Gram-Schmidt process to find an orthogonal basis for the column-space of a matrix. Finding the cross-product of the row vectors of an (n - 1) x n matrix.
  12. Finding the determinant and adjoint of a matrix with polynomial entries.
  13. Finding the Smith canonical form of a matrix B with polynomial entries. (e.g. when applied to the matrix B = xI - A, where A is a matrix with rational or complex rational entries, this gives the similarity invariants of A, in particular we get the minimum polynomial of A as the invariant of highest degree.)
    We also get polynomial matrices P and Q, whose determinants are constant polynomials, with the property that PBQ is in Smith canonical form.
  14. Elementary row/column operations for matrices with polynomial entries.
  15. Printing matrices of rationals to a file or the screen, accurate to a given number of decimals.
  16. Sending data to the line-printer.
  17. A file of r (<= M0-j) prepared matrices of the same type (rational, floating point, complex rational or modular) can be entered into array positions j,...j+r-1.


After entering cmat, the user can now descend through various levels by selecting from the displayed screen options. To ascend through the various levels, type q when this is an option. When leaving the program, the user is given the option of saving data for reuse in future sessions.


To enter a rational number and store the number as R[0] for example, select the appropriate menu and type n 0.

NOTE: Use CONTROL-H, not the backspace key, to backspace over characters.

Integers are entered as usual, while non-integer rationals are entered with a slash as integer/positive integer: e.g. 1/2, while complex numbers with non-zero imaginary part must end with an i: e.g. 1/2i represents (1/2)i. No brackets are to be used when entering numbers. Rational numbers are outputted in 'lowest terms' with positive denominator.

Some latitude is allowed as regards spacing. For example, the expressions 1-i, 1- i, 1 - i represent the same complex rational, while 1 -i represents the two numbers 1 and -i.

Matrices are entered row by row, the end of a row being marked by a carriage return. Spaces separate entries of a row. Having entered matrices RM[0] and RM[1], the product RM[0] * RM[1] is sent to RM[2] (say) or RM[0], RM[1]) by selecting the multiplication option t from the menu below and typing t 0 1 2.

Rational Matrix Arithmetic
a j k l : RM[j] + RM[k] -> RM[l]
t j k l : RM[j] * RM[k] -> RM[l]
s j k l : RM[j] - RM[k] -> RM[l]
m j k   :       -RM[j]  -> RM[k]
f j k l :  R[j](RM[k])  -> RM[l] (scalar multiple)
* j k   :     (RM[j])*  -> RM[k] (transpose)
e n j k : RM[j] ^  n    -> RM[k] (exponentiation)
z j k l : RM[j] - R[k]I -> RM[l]
p       : print numbers and matrices
x       : to enter data
q       : to exit
To discontinue entering a polynomial or matrix from the keyboard, type q or any non-alphanumeric character when entering a coefficient. A default polynomial or matrix is then stored.

To calculate the 10th power of RM[0], select the exponentiation option e from the above menu, typing e 10 0 3 to send the output to RM[3]. The situation is somewhat simpler with modular arithmetic calculations. Here no numbers are stored and non-negative numbers less than the relevant modulus are entered directly, instead of being stored. For example to multiply the matrix mM[0] by 5 (mod 7) and to store the result as mM[1], select the appropriate menu with the menu line

f,j k l p :j * (mM[k]) -> mM[l](mod p)(scalar multiple)
and type f 5 0 1 7.

It is important to remember that when using the modular arithmetic section of CMAT, operations should be carried out only on stored objects having the same modulus.

To terminate input prematurely after entering the first letter of a menu option, type q followed by RETURN.

Finally, to input a file of r (<= M0-j) prepared matrices of the same type (rational, floating point, complex rational or modular) into array positions j,...j+r-1, the first line of the file should contain the number of matrices; the next line consists of the row and column number of the first matrix, followed by the respective rows of the matrix. For example, the following file represents two matrices of rationals, a 3 x 3 matrix followed by a 2 x 2 matrix:

/* rational matrix file */
3 3
2/3 5 -7/8
1/2 12 -5
7 6 4
2 2
1 0
0 1

Comments on algorithms used in CMAT



I am indebted over the years for help in fixing bugs and improving program design generously provided by Michael Forbes (in the early years), Peter Adams and Sean Vickery.

WWW address:

Last modified 1st August 1997