c# - 如何在 C# 中获取 System.Numerics.Vector 的元素?

标签 c# vector count system.numerics

我想访问 System.Numerics.Vector<T> 的元素在 C# 中。 我正在关注官方文档:https://learn.microsoft.com/en-us/dotnet/api/system.numerics.vector-1?view=netcore-2.2

我能够创建具有不同数据类型的不同向量。 例如:var test = new Vector<double>(new double[] { 1.0, 2.0, 1.0 });

但现在我遇到了问题,我无法调用 test.Count;无法在 System.Numerics.Vector<T> 类型的实例上调用 Count .

我可以使用 access operator [] 访问单个元素, 但我不知道向量中有多少个元素。

根据文档,应该有公共(public)属性(property):

public static int Count { get; }

但我无法调用我的 System.Numerics.Vector<T> 实例.相反,我只能以静态方式调用它,如下所示:

Vector<double>.Count

这等于 2。

我也可以调用:

Vector<Int32>.Count

返回:4 和

Vector<Int16>.Count

返回 8.

现在我真的有点困惑,关于如何使用这个静态属性。起初,我以为这个属性会返回存储在向量中的元素数(如文档中所述)。其次,我想,这个属性返回内存中向量的大小,但这个数字从 double 增加到 Int32 到 Int16。

有趣的是,我不能从我创建的实例中调用这个静态属性:

var test = new Vector<double>(new double[] { 1.0, 2.0, 1.0 });

我不能调用test.Count !

你知道如何访问System.Numerics.Vector<T>的元素吗? ?

最佳答案

没有办法做到这一点。 Vector<T>是固定大小的,因为它试图优化硬件加速。文档状态:

The count of a Vector<T> instance is fixed, but its upper limit is CPU-register dependent. It is intended to be used as a building block for vectorizing large algorithms.

https://source.dot.net/#System.Private.CoreLib/shared/System/Numerics/Vector.cs 阅读源代码

表明如果传入的数据较少,它就会抛出所需的数据,并且最多只占用 Count。传入的项目数。

关于c# - 如何在 C# 中获取 System.Numerics.Vector 的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54442779/

相关文章:

c# - 在 C# 中将数组作为参数传递

javascript - 如何在 Notes 实体字段上应用 REQUIRED FIELD 验证?

c# - 套接字编程或 REST 服务

c++ - 由于运算符重载,我可以用 '=' 初始化 std::vector 吗?

c# - 发布 Web 应用程序时 AfterPublish 脚本不运行

c++ - 二进制 '==' : no operator found error for vector C++

c++ - C++ 中的协变返回类型到底是什么?

具有多个Where子句的SQL计数函数

mysql - 计算mysql中的列项目

mysql - 计数考虑 3 列的总和