I am trying to perform a Calculus Cross Product calculation using Linq and trying to figure out the pattern for the below code:
static void Main(string[] args)
{
double[] a = { 1, -1, -1 };
double[] b = {.5,1,.5};
var cross = from x in a
from y in b
select new {x,y};
List<double> LeftSide = new List<double>();
foreach (var c in cross) {
Console.WriteLine("x = " + c.x + " y = " + c.y);
double res = c.x * c.y;
Console.WriteLine("");
LeftSide.Add(res);
}
double i = LeftSide[5] - LeftSide[7];
double j = LeftSide[2] - LeftSide[6];
double k = LeftSide[1] - LeftSide[3];
Console.WriteLine("("+ i + "i) - (" + j + "j) +(" + k + "k)" );
Console.ReadLine();
}
Once I cross join the a and b, I need to perform the following calculations:
double i = LeftSide[5] - LeftSide[7];
double j = LeftSide[2] - LeftSide[6];
double k = LeftSide[1] - LeftSide[3];
This works and I get the desired output, put I know it can be written more efficiently. I am looking for any suggestions, to point me in the right direction.
Note: This is not a homework question, but is related to Calculus III Cross Products. I am a CS Major
You are making this way, way, way too complicated. The cross product of vectors (a0, a1, a2)
and (b0, b1, b2)
is (a1 * b2 - a2 * b1, a2 * b0 - a0 * b2, a0 * b1 - a1 * b0)
. So just compute that:
double[] a = { 1.0, -1.0, -1.0 };
double[] b = { 0.5, 1.0, 0.5 };
double[] cross =
{
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0]
};
And you're done in a single statement. There's no need to involve LINQ.