NMath provides a variety of functions that take vectors as arguments.
Class NMathFunctions provides static methods for rounding a vector's elements:
For instance, this code converts a vector of dollar amounts to Euros, then rounds to two decimal places:
DoubleVector 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 );
Class NMathFunctions provides static methods to calculate sums, differences, and products of vector elements:
u[0] = v[0] u[i] = v[i] - v[i-1]
d = v[0]*w[0] + v[1]w[1]...
FloatVector v = new FloatVector( "[1 2 3 4 5 6]" ); FloatVector u = new FloatVector( v.Length, 1, 1 ); float dp = 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:
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:
DoubleVector v = new DoubleVector( "[1 2 3 4 5 6]" ); double mean = NMathFunctions.Mean( v ); double variance = 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:
FloatVector v = new FloatVector( 10, 0, 2 ); FloatVector cosv = 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:
DoubleVector v = new DoubleVector( 10, 0, 5 ); DoubleVector log = NMathFunctions.Log( v );
Class NMathFunctions also provides the exponential function Pow() to raise each element of a vector to a real exponent:
FloatVector v = new DoubleVector( 100, 0, 1 ); FloatVector vCubed = NMathFunctions.Pow( v, 3 );
The static Abs() function on class NMathFunctions applies the absolute value function to each element of a given vector:
DoubleVector v = new DoubleVector ( 10, 0, -1 ); DoubleVector abs = 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:
DoubleVector v = new DoubleVector( 10, 0, 5 ); DoubleVector sqrt = 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:
double[] dblArray = { 1.12, -2.0, 3.88, 1.2, 15.345 };
DoubleVector v = 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:
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.
TOC | Previous | Next | Index