5

I want to access the elements of a System.Numerics.Vector<T> in C#. I'm following the official documentation: https://learn.microsoft.com/en-us/dotnet/api/system.numerics.vector-1?view=netcore-2.2

I'm able to create different vectors with different datatypes. For example: var test = new Vector<double>(new double[] { 1.0, 2.0, 1.0 });

But now I have the problem, that I'm unable to call test.Count; it is not possible to call Count on an instance of type System.Numerics.Vector<T>.

I can access single elements with the access operator [], but I dont know how many elements are in the vector.

According to the documentation, there should be the public property:

public static int Count { get; }

But I can not call in on my instance of System.Numerics.Vector<T>. Instead I can call it only in a static manner like following:

Vector<double>.Count

This is equal to 2.

I can also call:

Vector<Int32>.Count

returning: 4 and

Vector<Int16>.Count

returning 8.

And now I'm really a bit confused, about how to use this static property. At first, I thought, that this property would return the number of elements stored in the vector (as stated in the documentation). Second, I thought, that this property returns the size of the vector in memory, but this number increases from double to Int32 to Int16.

Interestingly I can not call this static property from my instance created by:

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

I can not call test.Count!

Do you know how to access the elements of System.Numerics.Vector<T>?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Marcel Gangwisch
  • 8,856
  • 4
  • 23
  • 32
  • 1
    Generally you could use the iterator with forall to access the elements. That, of course, does not make the method description of the getterless confusing… – dom Jan 30 '19 at 14:30
  • The correct one is "foreach" of course, like: foreach(double d in test) – dom Jan 30 '19 at 14:36
  • This is not working since Vector is not enumerable: `foreach statement cannot operate on variables of type Vector` – Marcel Gangwisch Jan 30 '19 at 14:40

2 Answers2

7

There is no way to do that. Vector<T> is of a fixed size, since it's trying to optimize for hardware acceleration. The docs state:

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.

Reading the source at https://source.dot.net/#System.Private.CoreLib/shared/System/Numerics/Vector.cs

Shows that it will throw if less data is passed in that's required and will only take up to Count of items passed in.

halfer
  • 19,824
  • 17
  • 99
  • 186
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • You are right! I it is also possible to access only two double elements in my case. However I'm able to write: `var test2 = new Vector(new double[] { 1.0, 2.0, 1.0, 3.0, 5.0, 10.0, 11.6, 20 });` and the code is compiling and also executed. Nevertheless only the first two numbers are stored. – Marcel Gangwisch Jan 30 '19 at 14:49
  • So do you know another way, another math library, to make vector calculations based on vectors of arbitrary length? – Marcel Gangwisch Jan 30 '19 at 15:47
0

Vector.Count property only shows who much elements of concrete type it can fit. And that why it's value only increase from double to short int. You can only fit 16 byte in there, therefore 2 double var, 4 int var, 8 short int var and so on.

No Name
  • 21
  • 3