我有两个向量 MathNet.Numerics.LinearAlgebra.Generic.Vector<double>
,如下所示:
Vector<double> v1 = new DenseVector(new double[] { 1, 2, 3 });
Vector<double> v2 = new DenseVector(new double[] { 3, 2, 1 });
我基本上想对它们进行 CrossProduct,但是找不到官方函数。我知道叉积是一个非常简单的函数,我可以自己编写,但我想使用 API 的函数。
以下两个对我都有效:(在 API 中找不到这样的函数。)
Vector<double> result = v1.CrossProduct(v2);
Vector<double> result = Vector.CrossProduct(v1,v2);
我找到了这个,但是当我尝试编写它时找不到函数:API Reference
最佳答案
对 3 元素向量进行叉积的示例方法。
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;
}
关于c# - 在 C# 中使用 Math.Net 数值进行叉积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11759720/