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.
If you have been working with C# in Visual Studio, you are propably very familiar with the following array creation expressions:
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.
Creating large arrays in this way 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.
After creating an array you might want to access its subarrays, in order to monitor or manipulate. 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 array A will be used in the following to demonstrate different methods.
Multiple strings each referencing a different dimension need to be seperated by a comma. While references for multiple dimensions that are combined in one string have to be seperated by a semicolon. Both methods are equivalent.
If you want to yield the last element of a specified dimension, you may use the string "end".
Instead of strings, you may also use constants to access individual array elements:
In some cases, you might want to access different array elements inside a loop. To automate this process, you can use variables:
Here, the built-in variable "end" is also available yielding the last element of a specified dimension. end is a property of the static ILMath class. You can derive your class from ILMath or utilize a using static ILMath; directive in you code file in order to access all members of ILMath without any prefix.
Besides, it is also possible to mix the methods presented above:
To increase efficiency, you can access several elements at the same time. For demonstration purposes we will introduce a larger array:
Just like before, the strings referencing different dimensions are seperated by a comma. Referenced single elements of the same dimension are enclosed in one string and seperated by commas again.
Note, how we used the string ":" to yield a complete column.
To access more than one element using constants, we have to fall back on the method r(). r() specifies a range, thus you need to provide an initial and end value.
To use variables, you need to define them initially. To yield various elements, you can define arbitrary arrays of the type integer:
Of course, you can mix the presented methods again to yield multi-dimensional subarrays:
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. But be aware: This array must contain the same number of elements as the subarray:
Throughout your code, you might not only want to manipulate existing elements but expand your array. For this purpose, you only have to address an element that does not exist yet and assign a value. Other elements that had to be created in this process, are assigned a value of 0.
Removing elements that are no longer needed, can be removed by using the following expression
or alternatively by assigning the value null to a specified subarray:
If you are looking for more details about this topic, check out the following pages:
- Array creation
- Other built-in functions to create arrays: column(), row(), ones(), zeros(), empty(), eye()
- Array manipulation