## Matrix Multiplication.

For matrices whose matrix elements are members of a ring, the binary operations of the ring can be used to define matrix multiplication. Let (*R, 0, +, ·*) be a ring where

*R*is a non-empty set, + is the additive binary operator,

*0*is the additive identity and

*·*is the multiplicative binary operator. Given an

*m × n*matrix

**A**= (

*a*) and an

_{ij}*n × p*matrix

**B**= (

*b*), where

_{ij}*a*,

_{ij}*b*∈

_{ij}*R*, define the

*m × p*matrix

**C**=

**A B**by

**C**= (

*c*), where

_{ij}*c*= Σ

_{ij}^{n}

_{k=1}

*a*

_{ik}*b*.

_{kj}The functionality of matrix routines in this collection are routines which could be implemented for matrices defined over an arbitrary ring but the implementation presented here is for real matrices declared as

*double A[M][N]*or for dynamically defined matrices declared as

*double* A*and for complex matrices declared as

*double complex A[M][N]*or for dynamically defined complex matrices declared as

*double complex *A*.

The routine for multiplying two real matrices is

*Multiply_Matrices( )*and the routine for multiplying two complex matrices is

*Multiply_CMatrices( )*. The routine for multiplying a real matrix and a real column vector is

*Multiply_Matrix_by_Vector( )*and the routine for multiplying a real row vector by a real matrix is

*Multiply_Vector_by_Matrix( )*. The routine for multiplying a complex matrix and a complex column vector is

*Multiply_CMatrix_by_CVector( )*and the routine for multiplying a complex row vector by a complex matrix is

*Multiply_CVector_by_CMatrix( )*. There are special versions of these routines for the cases of the ubiquitous

*2×2*and

*3×3*matrices and for the

*2*-d and

*3*-d vectors.

Additionally there are functions for multiplying a row vector by a matrix by a column vector: For real vectors and a real matrix, the function

*Bilinear_Pairing()*returns the real number

*u*. Given real vector spaces

^{T}**A**v*and*

**U***, a pairing is a bilinear function*

**V***(,):*. The definition of a pairing for real vector spaces can be extended to that for complex vector spaces in several ways: Given complex vector spaces

**U×V**→**R***and*

**U***, a pairing is a bilinear function*

**V***(,):*or a pairing is a sesquilinear function

**U×V**→**C***(,):*. In the case of the sesquilinear definition, there is a choice to which of the two arguments one applies the complex conjugation. For complex vectors and a complex matrix, the function

**U×V**→**C***CBilinear_Pairing()*returns the complex number

*u*and the function

^{T}**A**v*Sesquilinear_Pairing()*returns the complex number

*u*, where † represents the transpose of the complex conjugate of the complex vector

^{†}**A**v*u*, i.e. the first argument of the sesquilinear pairing is conjugated.

Given a real vector space

*, a bilinear form is a bilinear function*

**V***(,):*. For real vectors and a real matrix, the function

**V×V**→**R***Bilinear_Form()*returns the real number

*u*. Given a complex vector space

^{T}**A**v*, a bilinear form is a bilinear function*

**V***(,):*or a sesquilinear form is a sesquilinear function

**V×V**→**C***(,):*. In the case of the sesquilinear definition, there is a choice to which of the two arguments one applies the complex conjugation. For complex vectors and a complex matrix, the function

**V×V**→**C***CBilinear_Form()*returns the complex number

*u*and the function

^{T}**A**v*Sesquilinear_Form()*returns the complex number

*u*, where † represents the transpose of the complex conjugate of the complex vector

^{†}**A**v*u*, i.e. the first argument of the sesquilinear form is conjugated.

Special versions for multiplying a matrix by a vector for the case in which the matrix is either symmetric, skew-symmetric, lower-triangular, or Hermitian and stored in lower triangular form can be found at More Matrix Multiplication For Matrices Stored in Lower Triangular Form.

Similarly special versions for multiplying a matrix by a vector for the case in which the matrix is either symmetric, skew-symmetric, lower-triangular, or Hermitian and stored in upper triangular form can be found at More Matrix Multiplication For Matrices Stored in Upper Triangular Form.

Additional routines for multiplying complex matrices in which at least one of the summands is a real matrix can be found at More Complex Matrix Multiplication.

### Function List

- void Multiply_Matrices( double *C, double *A, int nrows, int ncols, double *B, int

mcols )

This routine calculates the matrix*C = AB*, where*A*is an*nrows × ncols*matrix,*B*is an*ncols × mcols*matrix and*C*is an*nrows × mcols*matrix.

- void Multiply_Matrices_2x2( double *C, double *A, double *B )

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

- void Multiply_Matrices_3x3( double *C, double *A, double *B )

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

- void Multiply_Matrix_by_Vector( double u[ ], double *A, int nrows, int ncols, double

v[ ] )

Post multiply the*nrows × ncols*matrix*A*by the column vector*v*to calculate the column vector*u*, i.e.*u = A v*.

- void Multiply_Vector_by_Matrix( double u[ ], double v[ ], double *A, int nrows, int

ncols )

Calculate the row vector*u*by pre-multiplying the*nrows×ncols*matrix*A*by the row vector*v*, i.e.*u = v A*.

- double Bilinear_Pairing(double u[], double *A, double v[], int nrows, int ncols)

Calculate u^{T}Av, where u is nrows-dimensional column vector, v is an ncols-dimensional column vector and A is an nrows×ncols matrix.

- double Bilinear_Form(double u[], double *A, double v[], int n)

Calculate u^{T}Av, where u is n-dimensional column vector, v is an n-dimensional column vector and A is an n×n square matrix.

- void Multiply_CMatrices( double complex *C, double complex *A, int nrows, int ncols, double complex *B, int

mcols )

This routine calculates the complex matrix*C = AB*, where*A*is an*nrows × ncols*complex matrix,*B*is an*ncols × mcols*complex matrix and*C*is an*nrows × mcols*matrix.

- void Multiply_CMatrices_2x2( double complex *C, double complex *A, double complex *B )

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

- void Multiply_CMatrices_3x3( double complex *C, double complex *A, double complex *B )

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

- void Multiply_CMatrix_by_CVector( double complex u[ ], double complex *A, int nrows, int ncols, double complex v[ ] )

Post multiply the*nrows × ncols*complex matrix*A*by the complex column vector*v*to calculate the complex column vector*u*, i.e.*u = A v*.

- void Multiply_CVector_by_CMatrix( double complex u[ ], double complex v[ ], double complex *A, int nrows, int

ncols )

Calculate the complex row vector*u*by pre-multiplying the*nrows×ncols*complex matrix*A*by the complex row vector*v*, i.e.*u = v A*.

- double complex CBilinear_Pairing(double complex u[], double complex *A, double complex v[], int nrows, int ncols)

Calculate u^{T}Av, where u is nrows-dimensional complex column vector, v is an ncols-dimensional complex column vector and A is an nrows×ncols complex matrix.

- double complex CBilinear_Form(double complex u[], double complex *A, double complex v[], int n)

Calculate u^{T}Av, where u is n-dimensional complex column vector, v is an n-dimensional complex column vector and A is an n×n complex square matrix.

- double complex Sesquilinear_Pairing(double complex u[], double complex *A, double complex v[], int nrows, int ncols)

Calculate u^{†}Av, where u is nrows-dimensional complex column vector, v is an ncols-dimensional complex column vector and A is an nrows×ncols complex matrix.

- double complex Sesquilinear_Form(double complex u[], double complex *A, double complex v[], int n)

Calculate u^{†}Av, where u is n-dimensional complex column vector, v is an n-dimensional complex column vector and A is an n×n complex square matrix.

*C* Source Code

- The file, multiply_matrices.c, contains the version of Multiply_Matrices( ) written in
*C*.

- The file, multiply_matrices_2x2.c, contains the version of Multiply_Matrices_2x2( ) written in
*C*.

- The file, multiply_matrices_2x2.h, contains the version of Multiply_Matrices_2x2( ) written as a macro.

- The file, multiply_matrices_3x3.c
, contains the version of Multiply_Matrices_3x3( ) written in
*C*.

- The file, multiply_matrices_3x3.h
, contains the version of Multiply_Matrices_3x3( ) written as a macro.

- The file, multiply_matrix_by_vector.c, contains the version of Multiply_Matrix_by_Vector( ) written in
*C*.

- The file, multiply_vector_by_matrix.c, contains the version of Multiply_Vector_by_Matrix( ) written in
*C*.

- The file, bilinear_pairing.c, contains the version of Bilinear_Pairing( ) written in
*C*.

- The file, bilinear_form.c, contains the version of Bilinear_Form( ) written in
*C*.

- The file, multiply_cmatrices.c, contains the version of Multiply_CMatrices( ) written in
*C*.

- The file, multiply_cmatrices_2x2.c, contains the version of Multiply_CMatrices_2x2( ) written in
*C*.

- The file, multiply_cmatrices_2x2.h, contains the version of Multiply_CMatrices_2x2( ) written as a macro.

- The file, multiply_cmatrices_3x3.c
, contains the version of Multiply_CMatrices_3x3( ) written in
*C*.

- The file, multiply_cmatrices_3x3.h
, contains the version of Multiply_CMatrices_3x3( ) written as a macro.

- The file, multiply_cmatrix_by_cvector.c, contains the version of Multiply_CMatrix_by_CVector( ) written in
*C*.

- The file, multiply_cvector_by_cmatrix.c, contains the version of Multiply_CVector_by_CMatrix( ) written in
*C*.

- The file, cbilinear_pairing.c, contains the version of CBilinear_Pairing( ) written in
*C*.

- The file, cbilinear_form.c, contains the version of CBilinear_Form( ) written in
*C*.

- The file, sesquilinear_pairing.c, contains the version of Sesquilinear_Pairing( ) written in
*C*.

- The file, sesquilinear_form.c, contains the version of Sesquilinear_Form( ) written in
*C*.

*NASM* Source Code

For Linux gcc Users

- The file, multiply_matrices.asm, contains the version of Multiply_Matrices( ) written in
*NASM*.

- The file, multiply_matrices_2x2.asm, contains the version of Multiply_Matrices_2x2( ) written in
*NASM*.

- The file, multiply_matrices_3x3.asm, contains the version of

Multiply_Matrices_3x3( ) written in*NASM*.

- The file, multiply_matrix_by_vector.asm, contains the version of Multiply_Matrix_by_Vector written in
*NASM*.

- The file, multiply_vector_by_matrix.asm, contains the version of Multiply_Vector_by_Matrix written in
*NASM*.

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

- The file, testmulmatrices.c, contains a test program of Multiply_Matrices( ) in the file
*multiply_matrices.c*. This test program requires the file*multiply_matrices.c*listed above.

- The file, MulMatricesTest.txt, contains the results of the test program testmulmatrices.c.

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

- The file, testmulmatrices2x2.c, contains a test program of Multiply_Matrices_2x2( ) in the file
*multiply_matrices_2x2.c*. This test program requires the file*multiply_matrices_2x2.c*listed above.

- The file, MulMatrices2x2Test.txt, contains the results of the test program testmulmatrices2x2.c.

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

- The file, testmulmatrices2x2_h.c, contains a test program of Multiply_Matrices_2x2( ) in the file
*multiply_matrices_2x2.h*. This test program requires the file*multiply_matrices_2x2.h*listed above.

- The file, MulMatrices2x2_hTest.txt, contains the results of the test program testmulmatrices2x2_h.c.

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

- The file, testmulmatrices3x3.c, contains a test program of Multiply_Matrices_3x3( ) in the file
*multiply_matrices_3x3.c*. This test program requires the file*multiply_matrices_3x3.c*listed above.

- The file, MulMatrices3x3Test.txt, contains the results of the test program testmulmatrices3x3.c.

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

- The file, testmulmatrices3x3_h.c, contains a test program of Multiply_Matrices_3x3( ) in the file
*multiply_matrices_3x3.h*. This test program requires the file*multiply_matrices_3x3.h*listed above.

- The file, MulMatrices3x3_hTest.txt, contains the results of the test program testmulmatrices3x3_h.c.

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

- The file, testmulmatrixbyvector.c, contains a test program of Multiply_Matrix_by_Vector( ) in the file
*multiply_matrix_by_vector.c*. This test program requires the file*multiply_matrix_by_vector.c*listed above.

- The file, MulMatrixByVectorTest.txt, contains the results of the test program testmulmatrixbyvector.c.

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

- The file, testmulvectorbymatrix.c, contains a test program of Multiply_Vector_by_Matrix( ) in the file
*multiply_vector_by_matrix.c*. This test program requires the file*multiply_vector_by_matrix.c*listed above.

- The file, MulVectorByMatrixTest.txt, contains the results of the test program testmulvectorbymatrix.c.

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

- The file, testbilinearpairing.c, contains a test program of Bilinear_Pairing( ) in the file
*bilinear_pairing.c*. This test program requires the file*bilinear_pairing.c*listed above.

- The file, BilinearPairingTest.txt, contains the results of the test program testbilinearpairing.c.

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

- The file, testbilinearform.c, contains a test program of Bilinear_Form( ) in the file
*bilinear_form.c*. This test program requires the file*bilinear_form.c*listed above.

- The file, BilinearFormTest.txt, contains the results of the test program testbilinearform.c.

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

- The file, testmulcmatrices.c, contains a test program of Multiply_CMatrices( ) in the file
*multiply_cmatrices.c*. This test program requires the file*multiply_cmatrices.c*listed above.

- The file, MulCMatricesTest.txt, contains the results of the test program testmulcmatrices.c.

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

- The file, testmulcmatrices2x2.c, contains a test program of Multiply_CMatrices_2x2( ) in the file
*multiply_cmatrices_2x2.c*. This test program requires the file*multiply_cmatrices_2x2.c*listed above.

- The file, MulCMatrices2x2Test.txt, contains the results of the test program testmulcmatrices2x2.c.

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

- The file, testmulcmatrices2x2_h.c, contains a test program of Multiply_CMatrices_2x2( ) in the file
*multiply_cmatrices_2x2.h*. This test program requires the file*multiply_cmatrices_2x2.h*listed above.

- The file, MulCMatrices2x2_hTest.txt, contains the results of the test program testmulcmatrices2x2_h.c.

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

- The file, testmulcmatrices3x3.c, contains a test program of Multiply_CMatrices_3x3( ) in the file
*multiply_cmatrices_3x3.c*. This test program requires the file*multiply_matrices_3x3.c*listed above.

- The file, MulCMatrices3x3Test.txt, contains the results of the test program testmulcmatrices3x3.c.

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

- The file, testmulcmatrices3x3_h.c, contains a test program of Multiply_CMatrices_3x3( ) in the file
*multiply_cmatrices_3x3.h*. This test program requires the file*multiply_cmatrices_3x3.h*listed above.

- The file, MulCMatrices3x3_hTest.txt, contains the results of the test program testmulcmatrices3x3_h.c.

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

- The file, testmulcmatrixbycvector.c, contains a test program of Multiply_CMatrix_by_CVector( ) in the file
*multiply_cmatrix_by_cvector.c*. This test program requires the file*multiply_cmatrix_by_cvector.c*listed above.

- The file, MulCMatrixByCVectorTest.txt, contains the results of the test program testmulcmatrixbycvector.c.

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

- The file, testmulcvectorbycmatrix.c, contains a test program of Multiply_CVector_by_CMatrix( ) in the file
*multiply_cvector_by_cmatrix.c*. This test program requires the file*multiply_cvector_by_cmatrix.c*listed above.

- The file, MulCVectorByCMatrixTest.txt, contains the results of the test program testmulcvectorbycmatrix.c.

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

- The file, testcbilinearpairing.c, contains a test program of CBilinear_Pairing( ) in the file
*cbilinear_pairing.c*. This test program requires the file*cbilinear_pairing.c*listed above.

- The file, CBilinearPairingTest.txt, contains the results of the test program testcbilinearpairing.c.

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

- The file, testcbilinearform.c, contains a test program of CBilinear_Form( ) in the file
*cbilinear_form.c*. This test program requires the file*cbilinear_form.c*listed above.

- The file, CBilinearFormTest.txt, contains the results of the test program testcbilinearform.c.

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

- The file, testsesquilinearpairing.c, contains a test program of Sesquilinear_Pairing( ) in the file
*sesquilinear_pairing.c*. This test program requires the file*sesquilinear_pairing.c*listed above.

- The file, SesquilinearPairingTest.txt, contains the results of the test program testsesquilinearpairing.c.

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

- The file, testsesquilinearform.c, contains a test program of Sesquilinear_Form( ) in the file
*sesquilinear_form.c*. This test program requires the file*sesquilinear_form.c*listed above.

- The file, SesquilinearFormTest.txt, contains the results of the test program testsesquilinearform.c.

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