## Vector Addition / Subtraction

The functionality of vector routines in this collection are routines which could be implemented for vectors defined over an arbitrary group but the implementation presented here is for real vectors declared as*double v[N]*or for dynamically defined vectors declared as

*double* v*and for complex vectors declared as

*double complex v[N]*or for dynamically defined complex vectors declared as

*double complex *v*.

The function for adding two real vectors is

*Add_Vectors( )*and the function for adding two complex vectors is

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

*2*-d and

*3*-d vectors. Inversely the function for subtracting two vectors is

*Subtract_Vectors( )*and the function for subtracting a complex vector from another complex vector is

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

*2*-d and

*3*-d vectors.

There are 2 versions for both

*Add_Vectors( )*and

*Subtract_Vectors( )*, 1 version is written in

*C*and 1 version written in

*NASM*. There is one version for both

*Add_CVectors( )*and

*Subtract_Vectors( )*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 vectors in which at least one of the summands is a real vector can be found at More Complex Vector Addition / Subtraction.

By abuse of language, a complex vector in which all of its components are real and is stored as an array

*double [n]*is referred to as a real vector. Similarly a complex vector in which all of its components are purely imaginary and is stored as an array

*double [n]*is called an imaginary vector.

### Function List

- void Add_Vectors( double w[ ], double u[ ], double v[ ], int n )

This routine calculates the vector*w = u + v*. All vectors,*u, v,*and*w*, should be declared in the calling routine as*double [n]*.

- void Add_Vectors_2d( double w[ ], double u[ ], double v[ ] )

This routine calculates the vector*w = u + v*for the special case in which all vectors,*u, v,*and*w*, are*2d*vectors. The vectors,*u, v,*and*w*, should be declared in the calling routine as*double [2]*.

- void Add_Vectors_3d( double w[ ], double u[ ], double v[] )

This routine calculates the vector*w = u + v*x for the special case in which all vectors,*u, v,*and*w*, are*3*d vectors. The vectors,*u, v,*and*w*, should be declared in the calling routine as*double [3]*.

- void Subtract_Vectors( double w[ ], double u[ ], double v[ ], int n )

This routine calculates the vector*w = u - v*. All vectors,*u, v,*and*w*, should be declared in the calling routine as*double [n]*.

- void Subtract_Vectors_2d( double w[ ], double u[ ], double v[ ] )

This routine calculates the vector*w = u - v*for the special case in which all vectors,*u, v,*and*w*, are*2d*vectors. The vectors,*u, v,*and*w*, should be declared in the calling routine as*double [2]*.

- void Subtract_Vectors_3d( double w[ ], double u[ ], double v[ ] )

This routine calculates the vector*w = u - v*for the special case in which all vectors,*u, v,*and*w*, are*3d*vectors. The vectors,*u, v,*and*w*, should be declared in the calling routine as*double [3]*.

- void Add_CVectors( double complex w[ ], double complex u[ ], double complex v[ ], int n )

This routine calculates the vector*w = u + v*. All vectors,*u, v,*and*w*, should be declared in the calling routine as*double complex [n]*.

- void Add_CVectors_2d( double complex w[ ], double complex u[ ], double complex v[ ] )

This routine calculates the vector*w = u + v*for the special case in which all vectors,*u, v,*and*w*, are*2d*vectors. The vectors,*u, v,*and*w*, should be declared in the calling routine as*double complex [2]*.

- void Add_CVectors_3d( double complex w[ ], double complex u[ ], double complex v[] )

This routine calculates the vector*w = u + v*x for the special case in which all vectors,*u, v,*and*w*, are*3*d vectors. The vectors,*u, v,*and*w*, should be declared in the calling routine as*double complex [3]*.

- void Subtract_CVectors( double complex w[ ], double complex u[ ], double complex v[ ], int n )

This routine calculates the vector*w = u - v*. All vectors,*u, v,*and*w*, should be declared in the calling routine as*double complex [n]*.

- void Subtract_CVectors_2d( double complex w[ ], double complex u[ ], double complex v[ ] )

This routine calculates the vector*w = u - v*for the special case in which all vectors,*u, v,*and*w*, are*2d*vectors. The vectors,*u, v,*and*w*, should be declared in the calling routine as*double complex [2]*.

- void Subtract_CVectors_3d( double complex w[ ], double complex u[ ], double complex v[ ] )

This routine calculates the vector*w = u - v*for the special case in which all vectors,*u, v,*and*w*, are*3d*vectors. The vectors,*u, v,*and*w*, should be declared in the calling routine as*double complex [3]*.

*C* Source Code

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

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

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

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

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

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

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

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

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

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

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

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

*C* Source Code - Macros

As a rule, the macros presented here are faster and require less memory than either the *C*version or the

*NASM*version of the corresponding routines. The major draw back is that when calling these macros DO NOT use expressions for the arguments. I.e. do not call MyMacro(&A[2], n+m) but rather set variables

*double * pA*,

*int k*then set

*pA = A + 2; k = n+m;*and then call the macro MyMacro(pA,k). It would be easy to change the macro to allow such expressions but in general not all versions of

*C*have a

*typeof*"keyword" which would facilitate the changes and moreover make the macro run slower. For

*C*compilers which have the

*inline*keyword, a better alternative would be to make the corresponding

*C*functions inline.

- The file, add_vectors_2d.h, contains the version of Add_Vectors_2d( ) written as a macro. This macro is also suitable for adding complex vectors and for adding real vectors with complex vectors.

- The file, add_vectors_3d.h, contains the version of Add_Vectors_3d( ) written as a macro. This macro is also suitable for adding complex vectors and for adding real vectors with complex vectors.

- The file, subtract_vectors_2d.h, contains the version of Subtract_Vectors_2d( ) written as a macro. This macro is also suitable for subtracting complex vectors, subtracting a real vector from a complex vector and subtracting a complex vector from a real vector.

- The file, subtract_vectors_3d.h, contains the version of Subtract_Vectors_3d( ) written as a macro. This macro is also suitable for subtracting complex vectors, subtracting a real vector from a complex vector and subtracting a complex vector from a real vector.

*NASM* Source Code

For Linux gcc Users

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

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

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

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

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

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

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

- The file, testaddvectors.c, contains a test program of Add_Vectors( ) in the file
*add_vectors.c*. This test program requires the file*add_vectors.c*listed above.

- The file, AddVectorsTest.txt, contains the results of the test program testaddvectors.c.

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

- The file, testaddvectors2d.c, contains a test program of Add_Vectors_2d( ) in the file
*add_vectors_2d.c*. This test program requires the file*add_vectors_2d.c*listed above.

- The file, AddVectors2dTest.txt, contains the results of the test program testaddvectors2d.c.

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

- The file, testaddvectors2d_h.c, contains a test program of the macro Add_Vectors_2d( ) in the file
*add_vectors_2d.h*for the case in which the arguments are the same as in*add_vectors_2d.c*. This test program requires the file*add_vectors_2d.h*listed above.

- The file, AddVectors2d_hTest.txt, contains the results of the test program testaddvectors2d_h.c.

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

- The file, testaddvectors3d.c, contains a test program of Add_Vectors_3d( ) in the file
*add_vectors_3d.c*. This test program requires the file*add_vectors_3d.c*listed above.

- The file, AddVectors3dTest.txt, contains the results of the test program testaddvectors3d.c.

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

- The file, testaddvectors3d_h.c, contains a test program of the macro Add_Vectors_3d( ) in the file
*add_vectors_3d.h*for the case in which the arguments are the same as in*add_vectors_3d.c*. This test program requires the file*add_vectors_3d.h*listed above.

- The file, AddVectors3d_hTest.txt, contains the results of the test program testaddvectors3d_h.c.

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

- The file, testaddcvectors.c, contains a test program of Add_CVectors( ) in the file
*add_cvectors.c*. This test program requires the file*add_cvectors.c*listed above.

- The file, AddCVectorsTest.txt, contains the results of the test program testaddcvectors.c.

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

- The file, testaddcvectors2d.c, contains a test program of Add_CVectors_2d( ) in the file
*add_cvectors_2d.c*. This test program requires the file*add_cvectors_2d.c*listed above.

- The file, AddCVectors2dTest.txt, contains the results of the test program testaddcvectors2d.c.

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

- The file, testaddcvectors2d_h.c, contains a test program of the macro Add_Vectors_2d( ) in the file
*add_vectors_2d.h*for the case in which the arguments are the same as in*add_cvectors_2d.c*. This test program requires the file*add_vectors_2d.h*listed above.

- The file, AddCVectors2d_hTest.txt, contains the results of the test program testaddcvectors2d_h.c.

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

- The file, testaddcvectors3d.c, contains a test program of Add_CVectors_3d( ) in the file
*add_cvectors_3d.c*. This test program requires the file*add_cvectors_3d.c*listed above.

- The file, AddCVectors3dTest.txt, contains the results of the test program testaddcvectors3d.c.

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

- The file, testaddcvectors3d_h.c, contains a test program of the macro Add_Vectors_3d( ) in the file
*add_vectors_3d.h*for the case in which the arguments are the same as in*add_cvectors_3d.c*. This test program requires the file*add_vectors_3d.h*listed above.

- The file, AddCVectors3d_hTest.txt, contains the results of the test program testaddcvectors3d_h.c.

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

- The file, testsubvectors.c, contains a test program of Subtract_Vectors( ) in the file
*subtract_vectors.c*. This test program requires the file*subtract_vectors.c*listed above.

- The file, SubtractVectorsTest.txt, contains the results of the test program testsubtractmatrices.c.

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

- The file, testsubvectors2d.c, contains a test program of Subtract_Vectors_2d( ) in the file
*subtract_vectors_2d.c*. This test program requires the file*subtract_vectors_2d.c*listed above.

- The file, SubtractVectors2dTest.txt, contains the results of the test program testsubtractvectors2d.c.

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

- The file, testsubvectors2d_h.c, contains a test program of the macro Subtract_Vectors_2d( ) in the file
*subtract_vectors_2d.h*for the case in which the arguments are the same as in*subtract_vectors_2d.c*. This test program requires the file*subtract_vectors_2d.h*listed above.

- The file, SubtractVectors2d_hTest.txt, contains the results of the test program testsubtractvectors2d_h.c.

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

- The file, testsubvectors3d.c, contains a test program of Subtract_Vectors_3d( ) in the file
*subtract_vectors_3d.c*. This test program requires the file*subtract_vectors_3d.c*listed above.

- The file, SubtractVectors3dTest.txt, contains the results of the test program testsubtractvectors3d.c.

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

- The file, testsubvectors3d_h.c, contains a test program of the macro Subtract_Vectors_3d( ) in the file
*subtract_vectors_3d.h*for the case in which the arguments are the same as in*subtract_vectors_3d.c*. This test program requires the file*subtract_vectors_3d.h*listed above.

- The file, SubtractVectors3d_hTest.txt, contains the results of the test program testsubtractvectors3d_h.c.

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

- The file, testsubcvectors.c, contains a test program of Subtract_CVectors( ) in the file
*subtract_cvectors.c*. This test program requires the file*subtract_cvectors.c*listed above.

- The file, SubtractCVectorsTest.txt, contains the results of the test program testsubcvectors.c.

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

- The file, testsubcvectors2d.c, contains a test program of Subtract_CVectors_2d( ) in the file
*subtract_cvectors_2d.c*. This test program requires the file*subtract_cvectors_2d.c*listed above.

- The file, SubtractCVectors2dTest.txt, contains the results of the test program testsubcvectors2d.c.

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

- The file, testsubcvectors2d_h.c, contains a test program of the macro Subtract_Vectors_2d( ) in the file
*subtract_vectors_2d.h*for the case in which the arguments are the same as in*subtract_cvectors_2d.c*. This test program requires the file*subtract_vectors_2d.h*listed above.

- The file, SubtractCVectors2d_hTest.txt, contains the results of the test program testsubcvectors2d_h.c.

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

- The file, testsubcvectors3d.c, contains a test program of Subtract_CVectors_3d( ) in the file
*subtract_cvectors_3d.c*. This test program requires the file*subtract_cvectors_3d.c*listed above.

- The file, SubtractCVectors3dTest.txt, contains the results of the test program testsubcvectors3d.c.

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

- The file, testsubcvectors3d_h.c, contains a test program of the macro Subtract_Vectors_3d( ) in the file
*subtract_vectors_3d.h*for the case in which the arguments are the same as in*subtract_cvectors_3d.c*. This test program requires the file*subtract_vectors_3d.h*listed above.

- The file, SubtractCVectors3d_hTest.txt, contains the results of the test program testsubcvectors3d_h.c.

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