# Arithmetic Matrix Operations

 Home Matrix Home Arithmetic Matrix Routines Home Matrix Addition / Subtraction Matrix Diagonal Add / Subract Scalar Matrix Multiply / Divide by Scalar Matrix Multiplication ATB, ABT, ATA, AAT [A,B] and {A, B} Row / Column Transform Row Sum / Column Sum / Trace Zero Matrix / Identity Matrix Direct Sum Bilinear Function / Kronecker

## More Complex Matrix Multiplication

The real numbers form a subfield of the complex numbers consequently the complex numbers form a vector space over the real numbers, in fact a two-dimensional vector space with canonical basis vectors 1+i0 and 0+i1, there are, of course, many other pairs of vectors which constitute a basis. Using these two basis vectors one can define an embedding of the real numbers into the complex numbers by r → r+i0 and r → 0+ir, the first mapping says that one can regard a real number r as a complex number with real part r and imaginary part 0 while the second mapping says that one can regard a real number r as a complex number with real part 0 and imaginary part r. Therefore given a real matrix, one can regard the matrix as a complex matrix with imaginary part 0 or as a complex matrix with real part 0.

The function for multiplying a complex matrix and a real matrix is Multiply_CRMatrices( ) and the routine for multiplying a real matrix and a complex matrix is Multiply_RCMatrices( ). There are also special versions of these routines for the cases of the ubiquitous 2×2 and 3×3 matrices.

There is 1 version for Multiply_CRMatrices( ) and 1 version for Multiply_RCMatrices( ) both written in C. For the special 2×2 and 3×3 cases there is an additional version which is written as a macro.

The routine for multiplying a complex matrix and a real column vector is Multiply_CMatrix_by_RVector( ) and the routine for multiplying a real row vector by a complex matrix is Multiply_RVector_By_CMatrix( ). There is 1 version for Multiply_CMatrix_by_RVector( ) and 1 version for Multiply_RVector_By_CMatrix( ) both written in C.

### Function List

• void Multiply_CRMatrices( double complex *C, double complex *A, int nrows, int ncols, double *B, int mcols )

This routine calculates the complex matrix C = A B. The matrix, A should be declared in the calling routine as double complex [nrows][ncols], the matrix B should be declared as double [ncols][mcols] and the matrix C should be declared as double complex [nrows][mcols].

• void Multiply_CRMatrices_2x2( double complex *C, double complex *A, double *B )

This routine calculates the complex matrix C = A B for the special case in which all matrices, A, B, and C, are 2×2 matrices. The matrices, A and C, should be declared in the calling routine as double complex , the matrix B should be declared as double .

• void Multiply_CRMatrices_3x3( double complex *C, double complex *A, double *B )

This routine calculates the complex matrix C = A B for the special case in which all matrices, A, B, and C, are 3×3 matrices. The matrices, A and C, should be declared in the calling routine as double complex , the matrix B should be declared as double .

• void Multiply_RCMatrices( double complex *C, double *A, int nrows, int ncols, double complex *B, int mcols )

This routine calculates the complex matrix C = A B. The matrix, A should be declared in the calling routine as double [nrows][ncols], the matrix B should be declared as double complex [ncols][mcols] and the matrix C should be declared as double complex [nrows][mcols].

• void Multiply_RCMatrices_2x2( double complex *C, double *A, double complex *B )

This routine calculates the complex matrix C = A B for the special case in which all matrices, A, B, and C, are 2×2 matrices. The matrices, B and C, should be declared in the calling routine as double complex , the matrix A should be declared as double .

• void Multiply_RCMatrices_3x3( double complex *C, double *A, double complex *B )

This routine calculates the complex matrix C = A B for the special case in which all matrices, A, B, and C, are 3×3 matrices. The matrices, B and C, should be declared in the calling routine as double complex , the matrix A should be declared as double .

• void Multiply_CMatrices_by_RVector( double complex u[], double complex *A, int nrows, int ncols, double v[] )

This routine calculates the complex vector u = A v. The matrix, A, should be declared in the calling routine as double complex [nrows][ncols], the vector, v, should be declared as double [ncols] and the vector, u, should be declared as double complex [nrows]

• void Multiply_RVector_by_CMatrix( double complex *u, double *v, double complex *A, int nrows, int ncols )

This routine calculates the complex vector u = v A. The matrix, A, should be declared in the calling routine as double complex [nrows][ncols], the vector, v, should be declared as double [nrows] and the vector, u, should be declared as double complex [ncols].

#### C Test Code, Test Results, and Build Shell Script

• The file, testmulcrmatrices.c, contains a test program of Multiply_CRMatrices( ) in the file multiply_crmatrices.c. This test program requires the file multiply_crmatrices.c listed above.

• The file, MulCRMatricesTest.txt, contains the results of the test program testmulcrmatrices.c.

• The file, testmulcrmatrices.sh, contains the shell script used to compile, link, and execute the test program testmulcrmatrices.c.

• The file, testmulcrmatrices2x2.c, contains a test program of Multiply_CRMatrices_2x2( ) in the file multiply_crmatrices_2x2.c. This test program requires the file multiply_crmatrices_2x2.c listed above.

• The file, MulCRMatrices2x2Test.txt, contains the results of the test program testmulcrmatrices2x2.c.

• The file, testmulcrmatrices2x2.sh, contains the shell script used to compile, link, and execute the test program testmulcrmatrices2x2.c.

• The file, testmulcrmatrices2x2_h.c, contains a test program of Multiply_CRMatrices_2x2( ) in the file multiply_crmatrices_2x2.h. This test program requires the file multiply_crmatrices_2x2.h listed above.

• The file, MulCRMatrices2x2_hTest.txt, contains the results of the test program testmulcrmatrices2x2_h.c.

• The file, testmulcrmatrices2x2_h.sh, contains the shell script used to compile, link, and execute the test program testmulcrmatrices2x2_h.c.

• The file, testmulcrmatrices3x3.c, contains a test program of Multiply_CRMatrices_3x3( ) in the file multiply_crmatrices_3x3.c. This test program requires the file multiply_crmatrices_3x3.c listed above.

• The file, MulCRMatrices3x3Test.txt, contains the results of the test program testmulcrmatrices3x3.c.

• The file, testmulcrmatrices3x3.sh, contains the shell script used to compile, link, and execute the test program testmulcrmatrices3x3.c.

• The file, testmulcrmatrices3x3_h.c, contains a test program of Multiply_CRMatrices_3x3( ) in the file multiply_crmatrices_3x3.h. This test program requires the file multiply_crmatrices_3x3.h listed above.

• The file, MulCRMatrices3x3_hTest.txt, contains the results of the test program testmulcrmatrices3x3_h.c.

• The file, testmulcrmatrices3x3_h.sh, contains the shell script used to compile, link, and execute the test program testmulcrmatrices3x3_h.c.

• The file, testmulrcmatrices.c, contains a test program of Multiply_RCMatrices( ) in the file multiply_rcmatrices.c. This test program requires the file multiply_rcmatrices.c listed above.

• The file, MulRCMatricesTest.txt, contains the results of the test program testmulrcmatrices.c.

• The file, testmulrcmatrices.sh, contains the shell script used to compile, link, and execute the test program testmulrcmatrices.c.

• The file, testmulrcmatrices2x2.c, contains a test program of Multiply_RCMatrices_2x2( ) in the file multiply_rcmatrices_2x2.c. This test program requires the file multiply_rcmatrices_2x2.c listed above.

• The file, MulRCMatrices2x2Test.txt, contains the results of the test program testmulrcmatrices2x2.c.

• The file, testmulrcmatrices2x2.sh, contains the shell script used to compile, link, and execute the test program testmulrcmatrices2x2.c.

• The file, testmulrcmatrices2x2_h.c, contains a test program of Multiply_RCMatrices_2x2( ) in the file multiply_rcmatrices_2x2.h. This test program requires the file multiply_rcmatrices_2x2.h listed above.

• The file, MulRCMatrices2x2_hTest.txt, contains the results of the test program testmulrcmatrices2x2_h.c.

• The file, testmulrcmatrices2x2_h.sh, contains the shell script used to compile, link, and execute the test program testmulrcmatrices2x2_h.c.

• The file, testmulrcmatrices3x3.c, contains a test program of Multiply_RCMatrices_3x3( ) in the file multiply_rcmatrices_3x3.c. This test program requires the file multiply_rcmatrices_3x3.c listed above.

• The file, MulRCMatrices3x3Test.txt, contains the results of the test program testmulrcmatrices3x3.c.

• The file, testmulrcmatrices3x3.sh, contains the shell script used to compile, link, and execute the test program testmulrcmatrices3x3.c.

• The file, testmulrcmatrices3x3_h.c, contains a test program of Multiply_RCMatrices_3x3( ) in the file multiply_rcmatrices_3x3.h. This test program requires the file multiply_rcmatrices_3x3.h listed above.

• The file, MulRCMatrices3x3_hTest.txt, contains the results of the test program testmulrcmatrices3x3_h.c.

• The file, testmulrcmatrices3x3_h.sh, contains the shell script used to compile, link, and execute the test program testsubtractrcmatrices3x3_h.c.

• The file, testmulcmatrixbyrvector.c, contains a test program of Multiply_CMatrix_by_RVector( ) in the file multiply_cmatrix_by_rvector.c. This test program requires the file multiply_cmatrix_by_rvector.c listed above.

• The file, MulCMatrixByRVectorTest.txt, contains the results of the test program testmulcmatrixbyrvector.c.

• The file, testmulcmatrixbyrvector.sh, contains the shell script used to compile, link, and execute the test program testmulcmatrixbyrvector.c.

• The file, testmulrvectorbycmatrix.c, contains a test program of Multiply_RVector_by_CMatrix( ) in the file multiply_rvector_by_cmatrix.c. This test program requires the file multiply_rvector_by_cmatrix.c listed above.

• The file, MulRVectorByCMatrixTest.txt, contains the results of the test program testmulrvectorbycmatrix.c.

• The file, testmulrvectorbycmatrix.sh, contains the shell script used to compile, link, and execute the test program testmulrvectorbycmatrix.c.