ILNumerics - Technical Computing Tools

High Performance Framework for Visualization

and Computing in Industry and Science

 

tgt

Creating ILArray<T> by Casting

Implicit and explicit casting operators exist for the conversion of ILNumerics arrays to system types.

Scalars

A singleton System type T can always be converted to a scalar ILNumerics array of matching element type - implicitly. Therefore, system types can directly get assigned to array variables and given as parameter for functions, expecting an ILNumerics array:

The other way around must be explicitly casted, since arrays are not always scalar:

An exception will be thrown when attempting to cast a nonscalar ILArray to a single system type element.

Vectors

One dimensional System.Array T[] convert implicitly to ILNumerics arrays. This will result in a column vector:

The cast uses the original System.Array as internal memory for the resulting array. The reverse direction is not supported. See Importing and Exporting Elements from Arrays for options on system type interaction.

Multidimensional Case

Multidimensional System.Array implicitly cast to ILNumerics arrays of the same rank, shape and element type:

The inner-most dimension of the source corresponds to the columns of the resulting array. Therefore, when converting a matrix from a System.Array that way one will observe a transposed matrix in the resulting array! Or more general: the resulting array will expose the same element ordering in the internal storage as the source but its dimensions will be flipped.

The conversion from System.Array to ILArray<T> is the most general case supported by ILNumerics. It includes the conversion from arbitrary System.Arrays, including object[] and object[,] ... However, the elements stored in the source array must be either of:

 

* Scalar System.Value types: double, float, int, uint, long, ulong, short, ushort, byte, sbyte.

* Scalar ILArray<T> type, where T is one of the above System.Value types.

Creating arrays this way involves a shallow copy of the source elements into new memory. The original System.Array will not get referenced afterwards from ILNumerics.

ILNumerics arrays created from empty System.Array by implicit casts will retain the shape of the source.

A Note about .NET Jagged Arrays

ILNumerics supports the concept of rectilinear arrays. In this context, that means along each specified (arbitrary) dimensions the number of the elements must be the same. Since .NET jagged arrays does not ensuring that, that is why ILNumerics does not support the implicit conversion from such arrays.

 

Note about memory ownership

ILNumerics is greedy in taking ownership of any source array provided. However, this works out for single-dimensional T[], where T is a simple, numeric primitive value type only.

Example: assigning a System.Array of element type double (Double in Visual Basic) to a local variable of type ILArray<double>

Example: providing System.Array as argument to a function which expects an parameter of type ILInArray<T>.

 

See also: Implicit conversion operator in the class reference