Question

I'm trying to get the dot product of two matrices, or vectors. I am using the Accord.net framework but I can't seem to find anything in the documentation that shows how to do this.

Here's an example:

double[] vector1 = { 1, 2, 3 };
double[] vector2 = { 3, 4, 5 };

Now I need to multiply them like so:

(1 * 3) + (2 * 4) + (3 * 5)

I assume this is possible, I just can't find the documentation that shows the method used for this.

Was it helpful?

Solution

Shouldn't the following code work?

vector1.InnerProduct(vector2);

Documentation url: http://accord-framework.net/docs/html/M_Accord_Math_Matrix_InnerProduct.htm

OTHER TIPS

Here is answer.

double[] vector1 = { 1, 2, 3 };
double[] vector2 = { 3, 4, 5 };
double result = Matrix.Dot(vector1, vector2);

You can use LINQ's Zip() function like this:

using System.Linq;

double[] vector1 = { 1, 2, 3 };
double[] vector2 = { 3, 4, 5 };

IEnumerable<double> dotProducts = vector1.Zip(vector2, (a, b) => a * b);

Zip() operates on two members at the same location (or index). The delegate passed to Zip() denotes the function used to generate a zipped single value from the members at the same index in two series.

Iterating over the result will give you the following:

foreach (double dp in dotProducts)
{
   Console.WriteLine(dp);
}
/* Output:
 * 3
 * 8
 * 15
 */

Instead of storing the values in an array you could store them in a Vector like :

Vector v1 = new Vector(1, 2, 3);
Vector v2 = new Vector(3, 4, 5);
Double crossProduct  = Vector.CrossProduct(vector1, vector2);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top