Computing Basics
Before we really get into the depths of ILNumerics computing engine, we should look into some of the basics. One of those very powerful basics are arrays that you can not only create but also access and manipulate in many different ways. We will deal with the following topics:
Some chapters will include interactive web components showing examples. This is where you can also write your own code. Learn more about interactive components here.
Array Creation
If you have been working with C# in Visual Studio, you are propably very familiar with the following array creation expressions:
.NET arrays are stored in row major order in memory. Thus, the new ILNumerics array has RowMajor storage order, too.
In ILNumerics any array expression may be assigned to multidimensional generic array classes. Note that you can not use the implicit type var, but have to specify the array type explicitly as displayed in the code above. This buys you efficient automatic memory management for your computing arrays.
Creating large arrays via .NET arrays is very cumbersome and quickly becomes unclear. Thus, ILNumerics does not only support those expressions but offers alternatives to achieve the same in a more efficient way.
Let's say you would like to generate a regularly spaced vector, then you can use the method below:
All you need to do is provide an initial value, a final value and a number of elements. But if it is more important to you to determine a specific increment, then the following method might be better for you:
The first expression yields a vector with an increment of 1, while the second expression results in a vector with an increment of 0.8.
To create arrays of arbitrary dimensions, you can use:
If you are looking to create an array of arbitrary size with an initial value and an increment of one, you just have to provide the dimensions. However, you may also provide the initial value and the increment.
Another method to create multidimensional arrays is array():
In the first expression a column vector is created from the provided elements. The second array is generated by providing one value and the dimensions. As a result all elements exhibit the same value. Learn more array creation methods, like zeros(), and ones() here.
Access
After creating an array you might want to access its subarrays, in order to monitor or manipulate them. Those subarrays can either be single elements or a whole range of elements as shown in the following:
The code displays how square brackets and either strings, constants or variables are used to yield individual subarrays. The position of elements to select from A is specified as its 0-based index in the corresponding dimension.
Note the difference: Matlab & Co. use 1- based indexing. ILNumerics and numpy use 0-based indexing.The array A will be used in the following to demonstrate different methods.
Single Elements
Index specifiers for individual dimensions need to be separated by a comma. It is considered good coding style to provide a single specifier per dimension only and to separate them by commas ',':
If you want to yield the last element of a specified dimension, you may use the special specifier ILNumerics.Globals.end. Import the static Globals class into the scope of your code to get shorter expressions:
In some cases, you might want to access different array elements inside a loop. To automate this process, you can use variables:
Range of elements
To increase efficiency, you can access several elements at the same time. For demonstration purposes we will introduce a larger array:
To access more than one element using constants, we can use the method r() or slice(). r() specifies a range in the same way Matlab® and similar frameworks do. slice() corresponds to the numpy way of slicing.To use variables, you need to define them initially. To yield various elements, you can define arbitrary arrays of integer elements:
Of course, you can mix the presented methods again to yield multi-dimensional subarrays:
Subarray creation is a very flexible and useful way of creating arrays. It can become quite complex, too. To learn about all options start reading here.
Manipulation
In most cases we don't just want to look at array elements but we want to modify them.
Modifying a single element is quite easy. You just have to assign an arbitrary scalar to the element you want to change.
If you want to manipulate various elements, you have to assign an array. This array may contain the same number of elements as the subarray:
More
If you are looking for more details about this topic, check out the following pages: