# General Purpose Matrix Operations

 Home Matrix Home General Purpose Matrix Routines Home Copy Matrices / Vectors Get Row / Column of a Matrix Set Row / Column of a Matrix Get / Set the Diagonal of a Matrix Set Matrix Diagonal Elements to a Scalar Get / Set a Submatrix Fill Matrix with Scalar Join Matrices Interchange Rows / Columns Transpose Matrix

## Get / Set the Diagonal of a Matrix.

Given an m×n matrix A = (aij) the diagonal of A is the array d = (di) where di = aii for
i = 0, … , min(m,n).

The routines for getting the diagonal of real matrix of type double and setting the diagonal of a real matrix of type double are called Get_Diagonal() and Set_Diagonal() respectively and the routines for getting the diagonal of complex matrix of type double complex and setting the diagonal of a complex matrix of type double complex are called Get_CDiagonal() and Set_CDiagonal() respectively.

There are 2 versions of the Get_Diagonal() routine and 2 versions of the Set_Diagonal() routine. For both the Get_Diagonal() and Set_Diagonal() routines 1 version is written in C and 1 version written in NASM assembly language.

There is 1 version of the Get_CDiagonal() routine and 1 version of the Set_CDiagonal() routine, both written in C.

### Function List

• void Get_Diagonal(double v[ ], double *A, int nrows, int ncols)

Given the matrix A with nrows rows and ncols columns, this routine copies the diagonal elements of A, A[i][i], to the destination vector v, i.e. v[i] = A[i] [i]
for i = 0, … , min(nrows,ncols). The destination vector v should be dimensioned in the calling routine at least as large as min (nrows, ncols).

• void Set_Diagonal(double *A, double v[ ], int nrows, int ncols)

Given the vector v of dimension at least min (nrows, ncols), this routine copies min (nrows, ncols) elements of v to the diagonal elements of the destination matrix A, i.e. A[i] [i]=v[i] for i = 0, … , min(nrows,ncols). The destination matrix A should be dimensioned in the calling routine as nrows × ncols.

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

Given the complex matrix A with nrows rows and ncols columns, this routine copies the diagonal elements of A, A[i][i], to the complex destination vector v, i.e. v[i] = A[i] [i]
for i = 0, … , min(nrows,ncols). The destination vector v should be dimensioned in the calling routine at least as large as min (nrows, ncols).

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

Given the complex vector v of dimension at least min (nrows, ncols), this routine copies min (nrows, ncols) elements of v to the diagonal elements of the complex destination matrix A, i.e. A[i][i]=v[i] for i = 0, … , min(nrows,ncols). The destination matrix A should be dimensioned in the calling routine as nrows × ncols.

#### C Source Code

• The file, get_diagonal.c, contains the version of Get_Diagonal() written in C.

• The file, set_diagonal.c, contains the version of Set_Diagonal() written in C.

• The file, get_cdiagonal.c, contains the version of Get_CDiagonal() written in C.

• The file, set_cdiagonal.c, contains the version of Set_CDiagonal() written in C.

#### NASM Source CodeFor Linux gcc Users

• The file, get_diagonal.asm, contains the version of Get_Diagonal() written in NASM.

• The file, set_diagonal.asm, contains the version of Set_Diagonal() written in NASM.

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

• The file, testgetdiagonal.c, contains a test program of Get_Diagonal( ) in the file get_diagonal.c. This test program requires the file get_diagonal.c listed above.

• The file, GetDiagonalTest.txt, contains the results of the test program testgetdiagonal.c.c.

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

• The file, testsetdiagonal.c, contains a test program of Set_Diagonal( ) in the file set_diagonal.c. This test program requires the file set_diagonal.c listed above.

• The file, SetDiagonalTest.txt, contains the results of the test program testsetdiagonal.c.

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

• The file, testgetcdiagonal.c, contains a test program of Get_CDiagonal( ) in the file get_cdiagonal.c. This test program requires the file get_cdiagonal.c listed above.

• The file, GetCDiagonalTest.txt, contains the results of the test program testgetcdiagonal.c.

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

• The file, testsetcdiagonal.c, contains a test program of Set_CDiagonal( ) in the file set_cdiagonal.c. This test program requires the file set_cdiagonal.c listed above.

• The file, SetCDiagonalTest.txt, contains the results of the test program testsetcdiagonal.c.

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