16

I am starting to use the Numpy and really like it's array handling capabilities. Is there some library that I can use in C# that provides similar capabilities with arrays. The features I would like most are:

  • Creating one array from another
  • Easy/trival iteration of arrays of n dimension
  • Slicing of arrays
Patrick
  • 17,669
  • 6
  • 70
  • 85
JerryKur
  • 7,279
  • 6
  • 24
  • 33

2 Answers2

8

NumPY has been ported to .NET via IronPython.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Icemanind
  • 47,519
  • 50
  • 171
  • 296
1

I don't think you need a library. I think LINQ does all you mention quite well.

Creating one array from another

int[,] parts = new int[2,3];

int[] flatArray = parts.ToArray();
// Copying the array with the same dimensions can easily be put into an extension 
// method if you need it, nothing to grab a library for ...

Easy iteration

int[,] parts = new int[2,3];

foreach(var item in parts)
    Console.WriteLine(item);

Slicing of arrays

int[] arr = new int[] { 2,3,4,5,6 };
int[] slice = arr.Skip(2).Take(2).ToArray();

// Multidimensional slice 
int[,] parts = new int[2,3];
int[] slice = arr.Cast<int>().Skip(2).Take(2).ToArray();

The awkward .Cast<int> in the last example is due to the quirk that multidimensional arrays in C# are only IEnumerable and not IEnumerable<T>.

Community
  • 1
  • 1
driis
  • 161,458
  • 45
  • 265
  • 341
  • 21
    LINQ is lacking numerical features, only works well with 1 dimension and it'll be significantly slower than a good numerics lib. – CodesInChaos Apr 12 '13 at 17:00
  • 1
    Well, the question is about array manipulation, it doesn't say whether performance or numerical computation is a factor. – driis Apr 12 '13 at 17:02
  • 1
    At least "array slicing" and "n dimensions" were mentioned and are lacking in LINQ. – CodesInChaos Apr 12 '13 at 17:03
  • 4
    An array slice points into the original array. You're producing copies of the slice. – CodesInChaos Apr 12 '13 at 18:07
  • @driis of course you need a library! Things like matrix addition, subtraction, dot products, cross products and determinants need not be implemented from scratch! – user32882 Mar 29 '23 at 15:31