Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross Product using Math.Net Numerics with C#

I have two vectors MathNet.Numerics.LinearAlgebra.Generic.Vector<double>, like the following:

Vector<double> v1 = new DenseVector(new double[] { 1, 2, 3 });     
Vector<double> v2 = new DenseVector(new double[] { 3, 2, 1 });

I basicly want to CrossProduct them, however couldn't find an official function. I know cross product is a very easy function which I can write myself, but I want to use the API's function.

Both of the below works for me: (Couldn't find such functions in the API.)

Vector<double> result = v1.CrossProduct(v2);
Vector<double> result = Vector.CrossProduct(v1,v2);

I found this, however couldn't find the function when I tried to write it: API Reference

like image 943
Sait Avatar asked Aug 01 '12 12:08

Sait


2 Answers

Sample method to do the cross-product of a 3 element vector.

    using DLA = MathNet.Numerics.LinearAlgebra.Double;

    public static DLA.Vector Cross(DLA.Vector left, DLA.Vector right)
    {
        if ((left.Count != 3 || right.Count != 3))
        {
            string message = "Vectors must have a length of 3.";
            throw new Exception(message);
        }
        DLA.Vector result = new DLA.DenseVector(3);
        result[0] = left[1] * right[2] - left[2] * right[1];
        result[1] = -left[0] * right[2] + left[2] * right[0];
        result[2] = left[0] * right[1] - left[1] * right[0];

        return result;
    }
like image 156
denver Avatar answered Sep 22 '22 00:09

denver


You are accessing the API documentation for Math.NET Iridium, which is a discontinued project. The intention was that the Iridium code base should be integrated into Math.NET Numerics, but it seems that the CrossProduct functionality has not been transferred yet, as can be seen in these two discussion threads on the Math.NET Numerics Codeplex site.

If you want to use Math.NET Iridium, where the CrossProduct method is surely available, you can download the most recent source code from here.

like image 44
Anders Gustafsson Avatar answered Sep 20 '22 00:09

Anders Gustafsson