c# - 在 C# 中使用 Math.Net 数值进行叉积

标签 c# numeric math.net mathnet-numerics

我有两个向量 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/

相关文章:

c# - 远程服务器返回 404

c# - 这种初始化方法有什么区别?

floating-point - IEEE 754 标准中哪里指定了 `Inf * 0` 的结果?

c# - 使用 MathNET 的置信区间

c# - 如何将 matrix<double> 转换为 Matrix<float>?

c# - 将EmguCV图像转换为系统绘图位图

c# - 拒绝访问用户文件夹

java - 如何在 Java 和 PostgreSQL 中处理 32 位数字?

perl - 包含字母字符的字符串如何在数值上等同于 Perl 中的数字?