# NMath User's Guide

4.2 Slices and Ranges (.NET, C#, CSharp, VB, Visual Basic, F#)

The most common means of obtaining a different view of a specific block of data in NMath is by using and indexing objects. These classes simply provide a way to specify a subset on non-negative integers with constant spacing, which you can then use as an indexing object into matrices and vectors. (See Chapter 5 and Chapter 6 for more information.)

Creating Slices and Ranges

The difference between a Slice and a Range is only in how you specify the integer subset. You construct a Slice object by specifying:

a beginning index

the total number of indices

a step increment, or stride

For example, to create a slice for the indices { 2, 4, 6, 8, 10 }, specify a start of 2, 5 total elements, and a stride of 2, like so:

Code Example – C# slice

```var s = new Slice( 2, 5, 2 );
```

Code Example – VB slice

```Dim S As New Slice(2, 5, 2)
```

You construct a Range object by specifying:

a beginning index

an ending index

a stride

Thus, to create a range for the indices { 2, 4, 6, 8, 10 }, specify a starting point of 2, a stopping point of 10, and a stride of 2:

Code Example – C# range

```var r = new Range( 2, 10, 2 );
```

Code Example – VB range

```Dim R As New Range(2, 10, 2)
```

Creating Abstract Subsets

Suppose you want to address the elements in a vector v from the third element to the last. You could do this by creating a Range like so:

Code Example – C# range

```var r = new Range( 2, v.Length - 1, 1 );
```

Code Example – VB range

```Dim R As New Range(2, v.Length - 1, 1)
```

but this is rather cumbersome. As a convenience, therefore, NMath provides the Position enumeration which lists different view positions of underlying data. You can use values in the Position enumeration in conjunction with ranges and slices to create abstract subsets. The precise meaning of an abstract subset is only determined when an indexing object is applied to a particular matrix or vector. The enumerated values are:

Start indicates the starting position.

MidPoint indicates the midpoint position, rounded down for data structures with an even number of elements.

End indicates the ending position.

For instance, this code creates two ranges that could be used to specify the odd and even elements of a vector:

Code Example – C# range

```var evenElements = new Range( Position.Start, Position.End, 2 );
var oddElements = new Range( 1, Position.End, 2 );
```

Code Example – VB range

```Dim EvenElements As New Range(Position.Start, Position.End, 2)
Dim OddElements As New Range(1, Position.End, 2)
```

The static All property on Slice and Range returns a new object indexing all elements:

Code Example – C# slice

```Slice allElements = Slice.All;
```

Code Example – VB range

```Dim AllElements As Slice = Slice.All
```

Modifying Ranges and Slices

You can modify an existing Slice or Range object using the Set() member function. For example:

Code Example – C# range

```var r = new Range( Position.Start, Position.End, 2 );
r.Set( Position.Start, Position.MidPoint, 1 );
```

Code Example – VB range

```Dim R As New Range(Position.Start, Position.End, 2)
R.Set(Position.Start, Position.MidPoint, 1)

```

Top

Top