**5.6****
****Functions of Vectors** (.NET, C#, CSharp, VB, Visual Basic, F#)

**NMath** provides a variety
of functions that take vectors as arguments.

Class **NMathFunctions**
provides static methods for rounding a vector's elements:

● Round() rounds each element of a given vector to the specified number of decimal places.

● Ceil() applies the ceiling rounding function to each element of a given vector.

● Floor() applies the floor rounding function to each element of a given vector.

For instance, this code converts a vector of dollar amounts to Euros, then rounds to two decimal places:

Code Example – C# vector

var v = new DoubleVector( "$4.30 $0.08 ($5.87)", NumberStyles.Number | NumberStyles.AllowCurrencySymbol | NumberStyles.AllowParentheses ); v = v * 0.9289; // exchange rate v = NMathFunctions.Round( v, 2 );

Code Example – VB vector

Dim V As New DoubleVector("$4.30 $0.08 ($5.87)", NumberStyles.Number Or NumberStyles.AllowCurrencySymbol Or NumberStyles.AllowParentheses) V = V * 0.9289 ' exchange rate V = NMathFunctions.Round(V, 2)

**Sums, Differences, and Products**

Class **NMathFunctions**
provides static methods to calculate sums, differences, and products
of vector elements:

● Sum() returns the sum of the elements in a given vector.

● AbsSum() returns the sum of the absolute value of the elements in a given vector. (For complex vectors, this function calculates the sum of the L1 norms of the vector's elements.)

● CumulativeSum() returns a vector containing the cumulative sum of the elements in a given vector, such that u[i] = v[0] + v[1] + ... v[i].

● NaNSum() returns the sum of the elements in a given vector, ignoring values that are Not-a-Number (NaN). NaN functions are available for real-value vectors only, not complex number vectors.

● Delta() returns a vector containing the differences between successive elements in a given vector, such that:

u[0] = v[0] u[i] = v[i] - v[i-1]

● Product() returns the product of the elements in a given vector.

● CumulativeProduct() returns a vector containing the cumulative product of the elements in a given vector.

● Dot() returns the vector dot, or inner, product d of two vectors, v and w, where

d = v[0]*w[0] + v[1]*w[1]...

● OuterProduct() creates a matrix containing the outer product of two vectors.

● Cross() computes the cross product of two vectors. The vectors must have at least length three, and elements beyond three are ignored for purposes of computing the cross product.

For example:

Code Example – C# vector

var v = new FloatVector( "[1 2 3 4 5 6]" ); var u = new FloatVector( v.Length, 1, 1 ); float dp = NMathFunctions.Dot( v, u );

Code Example – VB vector

Dim V As New FloatVector("[1 2 3 4 5 6]") Dim U As New FloatVector(V.Length, 1, 1) Dim DP As Single = NMathFunctions.Dot(V, U)

Class **NMathFunctions**
provides static min/max finding methods that return the integer index
of the element that meets the appropriate criterion:

● MaxIndex() returns the index of the element with the greatest value.

● MinIndex() returns the index of the element with the smallest value.

● MaxAbsIndex() returns the index of the element with the greatest absolute value.

● MinAbsIndex() returns the index of the element with the smallest absolute value.

Min/max value methods MaxValue(),
MinValue(), MaxAbsValue(),
and MinAbsValue() return the value of the
element that meets the appropriate criterion. The returned type depends
on the type of the vector. For instance, the MaxValue()
method that accepts a **DoubleVector**
returns a double.

NaNMax(), NaNMin(), NaNMaxIndex(), and NaNMinIndex() ignore values that are Not-a-Number (NaN). NaN functions are available for real-value vectors only, not complex number vectors.

The static Mean()
method on **NMathFunctions** returns
the mean of a given vector's elements. Median()
returns the median. If the length of the vector is even, the middle two
elements are averaged. Median() is available
for real-value vectors only, not complex number vectors, because there
is no standard ordering for complex numbers.

Variance() returns the biased variance of the elements. For instance:

Code Example – C# vector

var v = new DoubleVector( "[1 2 3 4 5 6]" ); double mean = NMathFunctions.Mean( v ); double variance = NMathFunctions.Variance( v );

Code Example – VB vector

Dim V As New DoubleVector("[1 2 3 4 5 6]") Dim Mean As Double = NMathFunctions.Mean(V) Dim Variance As Double = NMathFunctions.Variance(V)

SumOfSquares() returns the sum of the squared deviations from the mean of the elements of a given vector.

NaNMean(), NanMedian(), NaNVariance(), and NanSumOfSquares() ignore values that are Not-A-Number (NaN). NaNCount() returns the number of NaN values in a vector. NaN functions are available for real-value vectors only, not complex vectors.

**NMath** extends standard
trigonometric functions Acos(), Asin(), Atan(), Cos(), Cosh(), Sin(), Sinh(), Tan(), and Tanh()
to take vector arguments. Class **NMathFunctions**
provides these functions as static methods. For example, this code construct
a vector whose contents are the cosines of another vector:

Code Example – C# vector

var v = new FloatVector( 10, 0, 2 ); FloatVector cosv = NMathFunctions.Cos( v );

Code Example – VB vector

Dim V As New FloatVector(10, 0, 2) Dim Cosv As FloatVector = NMathFunctions.Cos(V)

The static Atan2() method takes two vectors and applies the two-argument arc tangent function to successive pairs of elements.

**NMath**
extends standard transcendental functions Exp()
and Log(), Log10()
to take vector arguments. Class **NMathFunctions**
provides these functions as static methods; each takes a single vector
as an argument and return a vector as a result. For instance, this code
creates a vector whose elements are the log of another vector's elements:

Code Example – C# vector

var v = new DoubleVector( 10, 0, 5 ); DoubleVector log = NMathFunctions.Log( v );

Code Example – VB vector

Dim V As New DoubleVector(10, 0, 5) Dim Log As DoubleVector = NMathFunctions.Log(V)

Class **NMathFunctions**
also provides the exponential function Pow()
to raise each element of a vector to a real exponent:

Code Example – C# vector

var v = new DoubleVector( 100, 0, 1 ); FloatVector vCubed = NMathFunctions.Pow( v, 3 );

Code Example – VB vector

Dim V As New DoubleVector(100, 0, 1) Dim VCubed As FloatVector = NMathFunctions.Pow(V, 3)

**Absolute Value and Square Root**

The static Abs()
function on class **NMathFunctions**
applies the absolute value function to each element of a given vector:

Code Example – C# vector

var v = new DoubleVector ( 10, 0, -1 ); DoubleVector abs = NMathFunctions.Abs( v );

Code Example – VB vector

Dim V As New DoubleVector(10, 0, -1) Dim ABS As DoubleVector = NMathFunctions.Abs(V)

**NMath**
also extends the standard Sqrt() function
to take a vector argument. Thus, this code creates a vector whose elements
are the square root of another vector's elements:

Code Example – C# vector

var v = new DoubleVector( 10, 0, 5 ); DoubleVector sqrt = NMathFunctions.Sqrt( v );

Code Example – VB vector

Dim V As New DoubleVector(10, 0, 5) Dim SQRT As DoubleVector = NMathFunctions.Sqrt(V)

The static Sort()
method on class **NMathFunctions**
sorts the elements of a given vector in ascending order using the quicksort
algorithm and returns a new vector containing the result:

Code Example – C# vector

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

Code Example – VB vector

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

**NOTE—****This
method is only available for FloatVector and DoubleVector, since there
is no standard ordering for complex numbers.**

Any NaN values in the vector are placed at the end of the ordered vector. To order the elements in descending order, Reverse() the returned vector:

Code Example – C# vector

v = NMathFunctions.Sort( v ).Reverse();

Code Example – VB vector

V = NMathFunctions.Sort(V).Reverse()

Static methods Real()
and Imag() on class **NMathFunctions**
return the real and imaginary part of a vector's elements. If the elements
of the given vector are real, Real() simply
returns the given vector and Imag() returns
a vector of the same length containing all zeros.

Static methods Arg() and
Conj() on class **NMathFunctions**
return the arguments (or phases) and complex conjugates of a vector's
elements. If the elements of the given vector are real, both methods
simply return the given vector.