VectorDotProd2d and VectorDotProd3d
in genMath.lib
Vector Functions Function List by Category Alphabetical Function
int VectorDotProd2d(
float
*VectorA, // pointer to array holding
vector A
float *VectorB, // pointer to array holding
vector B
float *Result) // pointer to
store result
int VectorDotProd3d(
float
*VectorA, // pointer to array holding
vector A
float
*VectorB, // pointer to array holding
vector B
float *Result) // pointer to
store result
Functions to compute the Dot Product of two 2-d or 3-d vectors:
2d
Dot
Product = a1 * b1 + a2 * b2
3d Dot Product = a1 * b1 + a2 * b2 + a3 * b3
Calls
to these routines will be faster than calls to genVectorDotProd,
for two reasons.
First, the dimension does not have to be passed as a parameter, and second, the
loops
in these routines are unrolled.
Note that by using multiple calls, these routines can be used to
compute dot products
are larger dimension vectors, if care is taken in the passing of the pointers.
The
return code is errno, defined in the C standard library (math.h). This is
an integer value
for math errors.
Sample Application:
/////////// splDP3d.c for VectorDotProd3d Library Function /////////// // // // // For this sample, 3-d vectors are used. Vectors are stored // // in arrays a and b, and the dot product is returned into // // a scalar, c. // // // // Use of VectorDotProd2d is similar. // // // ////////////////////////////////////////////////////////////////////// //////////////////////////// INCLUDES //////////////////////////////// #include "stdio.h" #include "genMath.h" /////////////////////////////// MAIN ///////////////////////////////// void main() { //local declarations //integers int retcode; // return code for call to VectorDotProd3d //floats float a[3]; // vector A float b[3]; // vector B float c; // dot product result //character strings char ch; //input string to exit //intialize the A and B vectors a[0] = (float)1.234; a[1] = (float)-.678; a[2] = (float)10.3; b[0] = (float)4.559; b[1] = (float)3.24; b[2] = (float)0.3422; //compute the dot product retcode = VectorDotProd3d(a,b,&c); //display the result printf("%s%f%s", "Dot Product = ", c,"\n\n"); //display closing message and wait for <enter> before exiting printf("Press <Enter> to end"); scanf("%c", &ch); } // End Main
/////////////////////////// End splDP3d.c ////////////////////////////
The reader may compute the dot product of A and B and compare to the output of the sample program.
Output from the test program splgenDP.c:
Dot Product = 6.953746