Arithmetic Matrix Operations



Matrix Addition / Subtraction

Matrix Addition / Subtraction

The functionality of matrix routines in this collection are routines which could be implemented for matrices defined over an arbitrary group. The implementation presented here is for real matrices declared as double A[M][N] or for dynamically defined real 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 function for adding two real matrices is Add_Matrices( ) and the function for adding two complex matrices is Add_CMatrices( ). There are special versions of these routines for the cases of the ubiquitous 2×2 and 3×3 matrices. Inversely the function for subtracting a real matrix from another real matrix is Subtract_Matrices( ) and the function for subtracting a complex matrix from another complex matrix is Subtract_CMatrices( ). There are also special versions of these routines for the cases of the ubiquitous 2×2 and 3×3 matrices.

There are 2 versions for both Add_Matrices( ) and Subtract_Matrices( ), 1 version is written in C and 1 version written in NASM. There is one version for both Add_CMatrices( ) and Subtract_Matrices( ) both written in C. For the special 2×2 and 3×3 cases there is an additional version which is written as a macro.

Additional routines for adding and subtracting complex matrices in which at least one of the summands is a real matrix can be found at More Complex Matrix Addition / Subtraction.

Function List

  • void Add_Matrices( double *C, double *A, double *B, int nrows, int ncols )

    This routine calculates the matrix C = A + B. All the matrices, A, B, and C, should be declared in the calling routine as double [nrows][ncols].

  • void Add_Matrices_2x2( double *C, double *A, double *B )

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

  • void Add_Matrices_3x3( double *C, double *A, double *B )

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

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

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

  • void Add_CMatrices_2x2( double complex *C, double complex *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, A, B, and C, should be declared in the calling routine as double complex [2][2].

  • void Add_CMatrices_3x3( double complex *C, double complex *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, A, B, and C, should be declared in the calling routine as double complex [3][3].

  • void Subtract_Matrices( double *C, double *A, double *B, int nrows, int ncols )

    This routine calculates the matrix C = A - B. All the matrices, A, B, and C, should be declared in the calling routine as double [nrows][ncols].

  • void Subtract_Matrices_2x2( double *C, double *A, double *B )

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

  • void Subtract_Matrices_3x3( double *C, double *A, double *B )

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

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

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

  • void Subtract_CMatrices_2x2( double complex *C, double complex *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, A, B, and C, should be declared in the calling routine as double complex [2][2].

  • void Subtract_CMatrices_3x3( double complex *C, double complex *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 A, B, and C should be declared in the calling routine as double complex [3][3].

C Source Code

NASM Source Code
For Linux gcc Users


C Test Code, Test Results, and Build Shell Script

  • The file, testaddmatrices.c, contains a test program of Add_Matrices( ) in the file add_matrices.c. This test program requires the file add_matrices.c listed above.

  • The file, AddMatricesTest.txt, contains the results of the test program testaddmatrices.c.

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


  • The file, testaddmatrices2x2.c, contains a test program of Add_Matrices_2x2( ) in the file add_matrices_2x2.c. This test program requires the file add_matrices_2x2.c listed above.

  • The file, AddMatrices2x2Test.txt, contains the results of the test program testaddmatrices2x2.c.

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


  • The file, testaddmatrices2x2_h.c, contains a test program of Add_Matrices_2x2( ) in the file add_matrices_2x2.h. This test program requires the file add_matrices_2x2.h listed above.

  • The file, AddMatrices2x2_hTest.txt, contains the results of the test program testaddmatrices2x2_h.c.

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


  • The file, testaddmatrices3x3.c, contains a test program of Add_Matrices_3x3( ) in the file add_matrices_3x3.c. This test program requires the file add_matrices_3x3.c listed above.

  • The file, AddMatrices3x3Test.txt, contains the results of the test program testaddmatrices3x3.c.

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


  • The file, testaddmatrices3x3_h.c, contains a test program of Add_Matrices_3x3( ) in the file add_matrices_3x3.h. This test program requires the file add_matrices_3x3.h listed above.

  • The file, AddMatrices3x3_hTest.txt, contains the results of the test program testaddmatrices3x3_h.c.

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


  • The file, testaddcmatrices.c, contains a test program of Add_CMatrices( ) in the file add_cmatrices.c. This test program requires the file add_cmatrices.c listed above.

  • The file, AddCMatricesTest.txt, contains the results of the test program testaddcmatrices.c.

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


  • The file, testaddcmatrices2x2.c, contains a test program of Add_CMatrices_2x2( ) in the file add_cmatrices_2x2.c. This test program requires the file add_cmatrices_2x2.c listed above.

  • The file, AddCMatrices2x2Test.txt, contains the results of the test program testaddcmatrices2x2.c.

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


  • The file, testaddcmatrices2x2_h.c, contains a test program of Add_CMatrices_2x2( ) in the file add_cmatrices_2x2.h. This test program requires the file add_cmatrices_2x2.h listed above.

  • The file, AddCMatrices2x2_hTest.txt, contains the results of the test program testaddcmatrices2x2_h.c.

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


  • The file, testaddcmatrices3x3.c, contains a test program of Add_CMatrices_3x3( ) in the file add_cmatrices_3x3.c. This test program requires the file add_cmatrices_3x3.c listed above.

  • The file, AddCMatrices3x3Test.txt, contains the results of the test program testaddcmatrices3x3.c.

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


  • The file, testaddcmatrices3x3_h.c, contains a test program of Add_CMatrices_3x3( ) in the file add_cmatrices_3x3.h. This test program requires the file add_cmatrices_3x3.h listed above.

  • The file, AddCMatrices3x3_hTest.txt, contains the results of the test program testaddcmatrices3x3_h.c.

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


  • The file, testsubtractmatrices.c, contains a test program of Subtract_Matrices( ) in the file subtract_matrices.c. This test program requires the file subtract_matrices.c listed above.

  • The file, SubMatricesTest.txt, contains the results of the test program testsubtractmatrices.c.

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


  • The file, testsubtractmatrices2x2.c, contains a test program of Subtract_Matrices_2x2( ) in the file subtract_matrices_2x2.c. This test program requires the file subtract_matrices_2x2.c listed above.

  • The file, SubMatrices2x2Test.txt, contains the results of the test program testsubtractmatrices2x2.c.

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


  • The file, testsubtractmatrices2x2_h.c, contains a test program of Subtract_Matrices_2x2( ) in the file subtract_matrices_2x2.h. This test program requires the file subtract_matrices_2x2.h listed above.

  • The file, SubMatrices2x2_hTest.txt, contains the results of the test program testsubtractmatrices2x2_h.c.

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


  • The file, testsubtractmatrices3x3.c, contains a test program of Subtract_Matrices_3x3( ) in the file subtract_matrices_3x3.c. This test program requires the file subtract_matrices_3x3.c listed above.

  • The file, SubMatrices3x3Test.txt, contains the results of the test program testsubtractmatrices3x3.c.

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


  • The file, testsubtractmatrices3x3_h.c, contains a test program of Subtract_Matrices_3x3( ) in the file subtract_matrices_3x3.h. This test program requires the file subtract_matrices_3x3.h listed above.

  • The file, SubMatrices3x3_hTest.txt, contains the results of the test program testsubtractmatrices3x3_h.c.

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


  • The file, testsubtractcmatrices.c, contains a test program of Subtract_CMatrices( ) in the file subtract_cmatrices.c. This test program requires the file subtract_cmatrices.c listed above.

  • The file, SubCMatricesTest.txt, contains the results of the test program testsubtractcmatrices.c.

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


  • The file, testsubtractcmatrices2x2.c, contains a test program of Subtract_CMatrices_2x2( ) in the file subtract_cmatrices_2x2.c. This test program requires the file subtract_cmatrices_2x2.c listed above.

  • The file, SubCMatrices2x2Test.txt, contains the results of the test program testsubtractcmatrices2x2.c.

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


  • The file, testsubtractcmatrices2x2_h.c, contains a test program of Subtract_CMatrices_2x2( ) in the file subtract_cmatrices_2x2.h. This test program requires the file subtract_cmatrices_2x2.h listed above.

  • The file, SubCMatrices2x2_hTest.txt, contains the results of the test program testsubtractcmatrices2x2_h.c.

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


  • The file, testsubtractcmatrices3x3.c, contains a test program of Subtract_CMatrices_3x3( ) in the file subtract_cmatrices_3x3.c. This test program requires the file subtract_cmatrices_3x3.c listed above.

  • The file, SubCMatrices3x3Test.txt, contains the results of the test program testsubtractcmatrices3x3.c.

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


  • The file, testsubtractcmatrices3x3_h.c, contains a test program of Subtract_CMatrices_3x3( ) in the file subtract_cmatrices_3x3.h. This test program requires the file subtract_cmatrices_3x3.h listed above.

  • The file, SubCMatrices3x3_hTest.txt, contains the results of the test program testsubtractcmatrices3x3_h.c.

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