Array Creation & Initialization
This article lists functions, specialized on initializing arrays from scratch.
Make sure to setup your code correctly for the snippets to compile!
Initialization with single value.
The following examples create arays and give all elements the same value:
These initialization functions are recommended for high performance in loops: first create an array of the required size and fill it with values afterwards.
Note, that the initialization functions ones(...), zeros(...), empty(...) all follow the same scheme: they provide non-generic overloads which allow to specify the dimension lenghts only and which all create arrays of element type double in StorageOrders.ColumnMajor.
Additionally, generic overloads exist, allowing to specify the element type as generic argument as well as the storage order as optional argument. When using these overloads, care must be taken that the correct overload was picked up by the compiler. It may help to name the last argument explicitly:
Note further, that for compatibility reasons the functions ones(...) and zeros(...) always create a matrix at least. This means that the array returned will have 2 or more dimensions. Providing a single dimension length parameter creates a square matrix. In order to create lower dimensional arrays (see: numpy array styles) use vector<T>() or array<T>().
Initialization with multiple values
Initializing arrays with multiple values is feasible for small numbers of elements.
But the vector() function is handy for larger arrays also. It prevents from additional allocations on the managed heap, and is recommended inside loops. In general, the values are first converted into an ILNumerics array (using vector()) and reshaped afterwards. Coming from 1D arrays, the reshape will always succeed without element copies. If required, the storage order can be determined during reshaping.
Casting from System.Array
Multidimensional .NET arrays of numeric element types convert implicitly to ILNumerics arrays. This is convenient in non-performance critical situations. Note, that the number of dimensions and the storage order is retained from the .NET array:
Note, that System.Array implicitly converts to any ILNumerics array type as long as both element types match. Therefore, such arrays can be given to functions expecting, let's say: InArray<T> parameters without the need for a local dummy variable Array<T>.
Casting from multidimensional System.Array T[,] preserves the storage order of the original array. Rows remain rows. Columns remain columns. (This is a breaking change compared to version 4, where a transposed version was created in this case.)
ILNumerics arrays store elements on the unmanaged heap. Thus, a copy is done when converting from managed arrays. Keep this fact in mind when maximum performance is required.
More initialization functions
For more details on every function of the