Arithmetic Matrix Operations



Matrix Diagonal Add / Subract Scalar

Adding / Subtracting a Scalar to / from the Diagonal of a Matrix

Occasionally one needs to add a scalar to or subtract a scalar from the diagonal of a matrix, usually a square matrix. The routines below perform that function. They are written for arbitrary real matrices declared as double [m][n] or for arbitrary complex matrices declared as double complex in the calling routine.

The function for adding a scalar to the diagonal of a real matrix is Add_Scalar_to_Diagonal( ) and the function for adding a scalar to the diagonal of a complex matrix is Add_Scalar_to_CDiagonal( ). Similarly the function for subtracting a scalar from the diagonal of a real matrix is Subtract_Scalar_from_Diagonal( ) and the function for subtracting a scalar from the diagonal of a complex matrix is Subtract_Scalar_from_Diagonal( ).

There are 2 versions for both Add_Scalar_to_Diagonal( ) and Subtract_Scalar_from_Diagonal( ), 1 version is written in C and 1 version written in NASM. There is 1 version for both Add_Scalar_to_CDiagonal( ) and Subtract_Scalar_from_CDiagonal( ) both routines are written in C.

Function List

  • void Add_Scalar_to_Diagonal(double *A, double x, int nrows, int ncols)

    Given the matrix A with nrows rows and ncols columns, this routine adds the scalar x to each of the diagonal elements of A. I.e. A[i][i] ← A[i][i] + x, i = 0, … , min(nrows, ncols).

  • void Add_Scalar_to_CDiagonal(double complex *A, double complex x, int nrows, int ncols)

    Given the complex matrix A with nrows rows and ncols columns, this routine adds the scalar x to each of the diagonal elements of A. I.e. A[i][i] ← A[i][i] + x, i = 0, … , min(nrows, ncols).

  • void Subtract_Scalar_from_Diagonal(double *A, double x, int nrows, int ncols)

    Given the matrix A with nrows rows and ncols columns, this routine subtracts the scalar x from each of the diagonal elements of A. I.e. A[i][i] ← A[i][i] - x, i = 0, … , min(nrows, ncols).

  • void Subtract_Scalar_from_CDiagonal(double complex *A, double complex x, int nrows, int ncols)

    Given the complex matrix A with nrows rows and ncols columns, this routine subtracts the scalar x from each of the diagonal elements of A. I.e. A[i][i] ← A[i][i] - x, i = 0, … , min(nrows, ncols).

C Source Code

NASM Source Code
For Linux gcc Users


C Test Code, Test Results, and Build Shell Script

  • The file, testaddtodiagonal.c, contains a test program of Add_Scalar_to_Diagonal( ) in the file add_scalar_to_diagonal.c. This test program requires the file add_scalar_to_diagonal.c listed above.

  • The file, AddToDiagonalTest.txt, contains the results of the test program testaddtodiagonal.c.

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


  • The file, testaddtocdiagonal.c, contains a test program of Add_Scalar_to_CDiagonal( ) in the file add_scalar_to_cdiagonal.c. This test program requires the file add_scalar_to_cdiagonal.c listed above.

  • The file, AddToCDiagonalTest.txt, contains the results of the test program testaddtocdiagonal.c.

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


  • The file, testsubfromdiagonal.c, contains a test program of Subtract_Scalar_from_Diagonal( ) in the file sub_scalar_from_diagonal.c. This test program requires the file sub_scalar_from_diagonal.c listed above.

  • The file, SubFromDiagonalTest.txt, contains the results of the test program testsubfromdiagonal.c.

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


  • The file, testsubfromcdiagonal.c, contains a test program of Subtract_Scalar_from_CDiagonal( ) in the file sub_scalar_from_cdiagonal.c. This test program requires the file sub_scalar_from_cdiagonal.c listed above.

  • The file, SubFromCDiagonalTest.txt, contains the results of the test program testsubfromcdiagonal.c.

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