# 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

## Copy Matrices / Vectors

A copy of an m×n matrix B = (bij) is an m×n matrix A = (aij) such that aij = bij for all i,j. Similarly a copy of an n-dimensional vector v = (vi) is an n-dimensional vector w = (wi) such that wi = vi for all i.

The function for copying a real matrix with type double is Copy_Matrix() and the function for copying a complex matrix with type double complex is Copy_CMatrix(). Similarly the function for copying a real vector with type double is Copy_Vector() and the function for copying a complex vector with type double complex is Copy_CVector().

There are 4 versions of the Copy_Matrix() routine, 3 versions written in C and 1 version written in NASM assembly language. Likewise there are 4 versions of the Copy_Vector() routine, 3 versions written in C, and 1 written in NASM assembly language. For both the Copy_Matrix() and Copy_Vector() routines of the 3 versions written in C, one is a macro which calls the C library function memcpy(), one is a C function which calls the C library function memcpy(), and one is a function which does not call memcpy().

There are 3 versions of the Copy_CMatrix() routine, all versions written in C. Likewise there are 3 versions of the Copy_CVector() routine, all versions written in C. For both the Copy_CMatrix() and Copy_CVector() routines of the 3 versions written in C, one is a macro which calls the C library function memcpy(), one is a C function which calls the C library function memcpy(), and one is a function which does not call memcpy().

When you build a library, only one version of each of the functions should be included unless you change the names.

### Function List

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

Given the nrows × ncols matrix B, this routine copies the elements of B to A, i.e.
A = B. In the calling routine the matrix A should be dimensioned the same as that of the matrix B.

• void Copy_Vector(double *d, double *s, int n)

Given the n dimensional source vector s, this routine copies the elements of s to the destination vector d, i.e. d = s. The destination vector d should be dimensioned in the calling routine at least as large as n.

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

Given the nrows × ncols complex matrix B, this routine copies the elements of B to the complex matrix A, i.e. A = B.
In the calling routine the matrix A should be dimensioned the same as that of the matrix B.

• void Copy_CVector(double complex *d, double complex *s, int n)

Given the n dimensional complex source vector s, this routine copies the elements of s to the complex destination vector d, i.e. d = s. The destination vector d should be dimensioned in the calling routine at least as large as n.

#### C Source Code

• The file, copy_matrix.c, contains the version of Copy_Matrix() written in C using the C library function memcpy.

• The file, copy_matrix.h, contains the version of Copy_Matrix() written as a macro.

• The file, ccopy_matrix.c, contains the version of Copy_Matrix() written in C not using the C library function memcpy.

• The file, copy_vector.c, contains the version of Copy_Vector() written in C using the C library function memcpy.

• The file, copy_vector.h, contains the version of Copy_Vector() written as a macro.

• The file, ccopy_vector.c, contains the version of Copy_Vector() written in C not using the C library function memcpy.

• The file, copy_cmatrix.c, contains the version of Copy_CMatrix() written in C using the C library function memcpy.

• The file, copy_cmatrix.h, contains the version of Copy_CMatrix() written as a macro.

• The file, ccopy_cmatrix.c, contains the version of Copy_CMatrix() written in C not using the C library function memcpy.

• The file, copy_cvector.c, contains the version of Copy_CVector() written in C using the C library function memcpy.

• The file, copy_cvector.h, contains the version of Copy_CVector() written as a macro.

• The file, ccopy_cvector.c, contains the version of Copy_CVector() written in C not using the C library function memcpy.

#### NASM Source CodeFor Linux gcc Users

• The file, copy_matrix.asm, contains the version of Copy_Matrix() written in NASM assembly language.

• The file, copy_vector.asm, contains the version of Copy_Vector() written in NASM assembly language.

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

• The file, testcopymatrix.c, contains a test program of Copy_Matrix( ) in the file copy_matrix.c. This test program requires the file copy_matrix.c listed above.

• The file, CopyMatrixTest.txt, contains the results of the test program testcopymatrix.c.

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

• The file, testccopymatrix.c, contains a test program of Copy_Matrix( ) in the file ccopy_matrix.c. This test program requires the file ccopy_matrix.c listed above.

• The file, CCopyMatrixTest.txt, contains the results of the test program testccopymatrix.c.

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

• The file, testcopymatrix_h.c, contains a test program of Copy_Matrix( ) in the file copy_matrix.h. This test program requires the file copy_matrix.h listed above.

• The file, CopyMatrix_hTest.txt, contains the results of the test program testcopymatrix_h.c.

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

• The file, testcopyvector.c, contains a test program of Copy_Vector( ) in the file copy_vector.c. This test program requires the file copy_vector.c listed above.

• The file, CopyVectorTest.txt, contains the results of the test program testcopyvector.c.

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

• The file, testccopyvector.c, contains a test program of Copy_Vector( ) in the file ccopy_vector.c. This test program requires the file ccopy_vector.c listed above.

• The file, CCopyVectorTest.txt, contains the results of the test program testccopyvector.c.

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

• The file, testcopyvector_h.c, contains a test program of Copy_Vector( ) in the file copy_vector.h. This test program requires the file copy_vector.h listed above.

• The file, CopyVector_hTest.txt, contains the results of the test program testcopyvector_h.c.

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

• The file, testcopycmatrix.c, contains a test program of Copy_CMatrix( ) in the file copy_cmatrix.c. This test program requires the file copy_cmatrix.c listed above.

• The file, CopyCMatrixTest.txt, contains the results of the test program testcopycmatrix.c.

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

• The file, testccopycmatrix.c, contains a test program of Copy_CMatrix( ) in the file ccopy_cmatrix.c. This test program requires the file ccopy_cmatrix.c listed above.

• The file, CCopyCMatrixTest.txt, contains the results of the test program testccopycmatrix.c.

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

• The file, testcopycmatrix_h.c, contains a test program of Copy_CMatrix( ) in the file copy_cmatrix.h. This test program requires the file copy_cmatrix.h listed above.

• The file, CopyCMatrix_hTest.txt, contains the results of the test program testcopycmatrix_h.c.

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

• The file, testcopycvector.c, contains a test program of Copy_CVector( ) in the file copy_cvector.c. This test program requires the file copy_cvector.c listed above.

• The file, CopyCVectorTest.txt, contains the results of the test program testcopycvector.c.

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

• The file, testccopycvector.c, contains a test program of Copy_CVector( ) in the file ccopy_cvector.c. This test program requires the file ccopy_cvector.c listed above.

• The file, CCopyCVectorTest.txt, contains the results of the test program testccopycvector.c.

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

• The file, testcopycvector_h.c, contains a test program of Copy_CVector( ) in the file copy_cvector.h. This test program requires the file copy_cvector.h listed above.

• The file, CopyCVector_hTest.txt, contains the results of the test program testcopycvector_h.c.

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