# NMath User's Guide

5.2 Creating Vectors (.NET, C#, CSharp, VB, Visual Basic, F#)

This section describes how to create instances of the vector classes.

Creating Vectors from Numeric Values

You can construct vector objects from numeric values in a variety of ways. All such constructors create a new view of a new data block.

A single passed, non-negative integer creates a vector of that length, with all values initialized to zero. For instance, this creates a vector of floating point values with 10 elements:

Code Example – C# vector

```var v = new FloatVector( 10 );
```

Code Example – VB vector

```Dim V As New FloatVector(10)
```

Another constructor enables you to set the initial value of all elements in the vector:

Code Example – C# vector

```var v = new DoubleVector( 10, 2.0 );
```
```// v[i]==2 for all i

```

```var u =
```
```  new FloatComplexVector( 10, new FloatComplex( 1.0, -2.0 ) );
```
```// u[j] == 1 - 2i for all j
```

Code Example – VB vector

```Dim V As New DoubleVector(10, 2.0)
```
```' V(i)=2 for all i

```

```Dim U As New FloatComplexVector(10, New FloatComplex(1.0, -2.0))
```
```' U(j) = 1 - 2i for all j
```

Similarly, the vector classes provide a constructor that lets you set the length, the value of the first element, and an amount to increment each successive element in the vector. The ith element of the vector thus has the value initialValue + i * increment. For example, this creates the vector [1, 3, 5, 7, 9]:

Code Example – C# vector

```var v = new FloatVector( 5, 1, 2 );
```

Code Example – VB vector

```Dim V As New FloatVector(5, 1, 2)
```

You can also create a vector from an array of values:

Code Example – C# vector

```double[] dblArray = {1.12, -2.0, 3.88, 1.2, 15.345};
```
```var v = new DoubleVector( dblArray );
```

Code Example – VB vector

```Dim DblArray() As Double = {1.12, -2.0, 3.88, 1.2, 15.345}
```
```Dim V As New DoubleVector(DblArray)
```

Or a comma-delimited list:

Code Example – C# vector

```var v = new FloatVector( 3.5, -6.7, 0.0, 3.11, 8.90, 5.0 );
```

Code Example – VB vector

```Dim V As New FloatVector(3.5, -6.7, 0.0, 3.11, 8.9, 5.0)
```

Complex vector types can also be created from polar coordinates:

Code Example – C# complex vector from polar coordinates

```var magnitudes = new FloatVector( 1, 2, 3, 6 );
```
```var angles = new FloatVector( 1, 2, 3, -3 );
```
```var v = FloatComplexVector.FromPolar( magnitudes, angles );
```

Code Example – VB complex vector from polar coordinates

```Dim Magnitudes As New FloatVector( 1, 2, 3, 6 )
```
```Dim Angles as New FloatVector( 1, 2, 3, -3 )
```
```Dim V = FloatComplexVector.FromPolar( magnitudes, angles )
```

Lastly, you can use a random number generator to fill a vector with random values. See Chapter 9 for more information.

Creating Vectors from Strings

You can also construct vectors from a string representation of the form [ v1 v2 v3 ... ]. The brackets are optional, and extra whitespace is ignored. Again, these constructors create a new view of a new data block.

For instance:

Code Example – C# vector

```string s = "4.3 -232   5.344 23.4   -32.43      ";
```
```var v = new DoubleVector( s );

```

```s = "[ (4.3,3.5) (23.4,-234.3) (-21.2,0) ]";
```
```var u = new DoubleComplexVector( s );
```

Code Example – VB vector

```Dim S As String = "4.3 -232   5.344 23.4   -32.43      "
```
```Dim V As New DoubleVector(S)

```

```S = "[ (4.3,3.5) (23.4,-234.3) (-21.2,0) ]"
```
```Dim U As New DoubleComplexVector(S)
```

An optional second parameter to the constructor accepts values from the System.Globalization.NumberStyles enumeration. These styles are used by the Parse() methods of the numeric base types. For example:

Code Example – C# vector

```using System.Globalization;

```

```string s = "\$4.52 \$4.32 \$4.56 \$9.94 (\$0.04) (\$5.00)";
```
```var v = new FloatVector( s,
```
```     NumberStyles.AllowCurrencySymbol |
```
```     NumberStyles.AllowDecimalPoint |
```
```     NumberStyles.AllowParentheses );
```

Code Example – VB vector

```Imports System.Globalization

```

```Dim S As String = "\$4.52 \$4.32 \$4.56 \$9.94 (\$0.04) (\$5.00)"
```
```Dim V As New FloatVector(s,
```
```  NumberStyles.AllowCurrencySymbol Or
```
```  NumberStyles.AllowDecimalPoint Or
```
```  NumberStyles.AllowParentheses)
```

NOTE—Whitespace, even if set as a group separator, is interpreted as a data separator. Also note that currency representation is based on locale information in System.Glo­balization.CultureInfo, unless you override that information.

Finally, you can construct a vector from a given text reader. Just position the text reader at the start of a valid text representation of a vector. In this case, the brackets are required, since the text reader reads the stream until a closing bracket is encountered. For instance:

Code Example – C# vector

```var reader = new StreamReader( "data.txt" );
```
```// ... read until start of vector
```
```var v = new DoubleVector( reader );
```

Code Example – VB vector

```Dim Reader As New StreamReader("data.txt")
```
```' ... read until start of vector
```
```Dim V As New DoubleVector(Reader)
```

Again, an optional second parameter accept values from the System.Globalization.NumberStyles enumeration.

Instead of using a constructor, you can also create a vector from a string representation using the static Parse() method. The vector classes provide overloads of the Parse() method that accept a string, a string plus number styles, a text reader, and a text reader plus number styles.

Thus:

Code Example – C# vector

```string s = "\$4.52 \$4.32 \$4.56 \$9.94 (\$0.04) (\$5.00)";
```
```FloatVector v = FloatVector.Parse( s,
```
```  NumberStyles.AllowCurrencySymbol |
```
```  NumberStyles.AllowDecimalPoint |
```
```  NumberStyles.AllowParentheses );
```

Code Example – VB vector

```Dim S As String = "\$4.52 \$4.32 \$4.56 \$9.94 (\$0.04) (\$5.00)"
```
```Dim V As FloatVector = FloatVector.Parse(s,
```
```  NumberStyles.AllowCurrencySymbol Or
```
```  NumberStyles.AllowDecimalPoint Or
```
```  NumberStyles.AllowParentheses)
```

Conversely, the overridden ToString() member function returns a string representation of a vector of the form [ v1 v2 v3 ... ]. A variant of the ToString() method also accepts a standard .NET numeric format string. For example, the format string "C" indicates currency notion:

Code Example – C# vector

```var v = new DoubleVector( "[ 1.12 8.95 3.95 4.60 ]" );
```
```Console.WriteLine( v.ToString( "C" ) );
```

Code Example – VB vector

```Dim V As New DoubleVector("[ 1.12 8.95 3.95 4.60 ]")
```
```Console.WriteLine(V.ToString("C"))
```

The Write() member function writes a text representation of a vector to a given text writer. Again, a numeric format string is an optional second parameter.

You can create a vector object from an ADO.NET object such as a DataTable, an array of DataRow objects, a DataRowCollection, or a DataView­­. See Chapter 52 for more information.

Implicit Conversion

The implicit conversion operators for the vector classes are shown in Figure 2. An arrow indicates implicit promotion.

Figure 2 – Implicit conversion for vectors

Copying Vectors

The vector classes provide three copy methods:

Clone() returns a deep copy of a vector. Data is copied, so each vector references different data.

ShallowCopy() returns a shallow copy of a vector. Data is not copied. Both vectors reference the same data.

DeepenThisCopy() copies the data viewed by a vector to new data block. This guarantees that there is only one reference to the underlying data, and that this data is in contiguous storage.

For example:

Code Example – C# vector

```var v = new DoubleVector( "[1 2 3 4 5]" );
```
```DoubleVector u = v.ShallowCopy();

```

```u[0] = 0;   // v[0] == u[0]
```
```u.DeepenThisCopy();
```
```u[1] = 0;   // v[1] != u[1]
```

Code Example – VB vector

```Dim V As New DoubleVector("[1 2 3 4 5]")
```
```Dim U As DoubleVector = V.ShallowCopy()

```

```U(0) = 0   ' V(0) = U(0)
```
```U.DeepenThisCopy()
```
```U(1) = 0   ' V(1) <> U(1)
```

New Vector Views

A common method of creating vectors in NMath is to create a new vector view of data already referenced by another object. This is achieved using Slice and Range objects, as described in Section 4.2. Here's an example using a Slice object to create a new view of a vector's data:

Code Example – C# vector

```var v = new DoubleVector( 10, 1, 1 );
```
```// v = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

```

```var first3Elements = new Slice( 0, 3 );
```
```DoubleVector u = v[first3Elements];
```

Code Example – VB vector

```Dim V As New DoubleVector(10, 1, 1)
```
```' v = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

```

```Dim First3Elements As New Slice(0, 3)
```
```Dim U As DoubleVector = v(First3Elements)
```

Notice that the vector indexer is overloaded to accept indexing objects, and return a new view of the indexed data.

Vector u behaves exactly like a vector constructed with 3 elements whose values are 1, 2, 3. That is:

Code Example – C# vector

```u[0] == 1; // true
```
```u[1] == 2; // true
```
```u[2] == 3; // true
```
```u[3]; //Index out of bounds exception!
```

Code Example – VB vector

```U(0) = 1 ' true
```
```U(1) = 2 ' true
```
```U(2) = 3 ' true
```
```U(3) 'Index out of bounds exception!
```

The difference between u and a newly constructed vector becomes clear when a value in u is changed. This changes the corresponding value in v, since they both reference the same data.

Code Example – C# vector

```u[2] = 99;
```
```v[2] == 99; // true!
```

Code Example – VB vector

```U(2) = 99
```
```V(2) = 99 ' true!
```

Here's another example using a Range object:

Code Example – C# vector

```var v = new DoubleVector( "[1 2 3 4 5 6]" );
```
```DoubleVector everyOther = v[new Range( 0,Position.End,2 )];
```

Code Example – VB vector

```Dim V As New DoubleVector("[1 2 3 4 5 6]")
```
```Dim EveryOther As DoubleVector = V(New Range(0, Position.End, 2))
```

Methods such as Row(), Column(), Diagonal(), and Slice() on the matrix classes also create vector views. See Chapter 6 for more information.

Top

Top