Table of Contents

Class MathFunctions

Namespace
VirtualLabAPI.Core.Numerics
Assembly
VirtualLabAPI.dll

This class contains various mathematical functions and constants. Further methods can be found in the classes MathSupport and CenterSpace.NMath.Core.SpecialFunctions.

public static class MathFunctions
Inheritance
MathFunctions
Inherited Members

Fields

HalfPi

Constant value of \(\frac{\pi}{2}\).

public const double HalfPi = 1.5707963267948966

Field Value

double

TwoPi

Constant value of \(2 \cdot \pi\).

public const double TwoPi = 6.283185307179586

Field Value

double

Methods

Abs(double)

Returns the absolute value of the double number.

public static double Abs(this double number)

Parameters

number double

The double number.

Returns

double

The absolute value of the double number.

CeilingWithTolerance(double)

Method for a ceiling (rounding up) operation considering a tolerance. Thus the value 3 + epsilon (determined via IsEqualToZero(double, double) will return 3 rather than 4.

public static int CeilingWithTolerance(double valueToCeil)

Parameters

valueToCeil double

Value to be rounded up.

Returns

int

Rounded value.

ChangeSign(double, double)

Changes the sign of the number. So for example you can change -4.5 to +4.5 and vice versa.

public static double ChangeSign(double x, double sign)

Parameters

x double

The original number.

sign double

The sign the number should have.

Returns

double

The original number if the sign of the original number and the given sign are equal. Else the original number multiplied with -1.

ChangeSign(int, double)

Changes the sign of the number. So for example you can change -4 to +4 and vice versa.

public static int ChangeSign(int x, double sign)

Parameters

x int

The original number.

sign double

The sign the number should have.

Returns

int

The original number if the sign of the original number and the given sign are equal. Else the original number multiplied with -1.

Cos(double)

Special cosine function as the Cos(double) function has problems with \(\cos\left(\frac{\pi}{2} + n ⋅ \pi\right)\).

public static double Cos(double d)

Parameters

d double

The argument of the cosine.

Returns

double

The cosine of d.

DegreeToRadian(double)

Convert degrees to radians.

public static double DegreeToRadian(double degree)

Parameters

degree double

The angle in degrees.

Returns

double

The angle in radians.

DrawAllSubsetsOfGivenSizeFromSetOfIndices(int, int)

Method for creating all possible subsets of a given size out of a set of indices.

public static List<int[]> DrawAllSubsetsOfGivenSizeFromSetOfIndices(int subsetSize, int populationSize)

Parameters

subsetSize int

Size of the subsets to draw.

populationSize int

Size of the (zero based) index set. So the highest index is populationSize - 1.

Returns

List<int[]>

List of all subsets of given size.

Factorial(int)

Calculates the factorial \(n!\).

public static double Factorial(int n)

Parameters

n int

Order of factorial to calculate: \(n\).

Returns

double

\(n!\)

FloorWithTolerance(double)

Performs floor rounding whereas a tolerance is applied so that e.g. 9.999999999999 is rounded to 10 instead of 9.

public static int FloorWithTolerance(double valueToRound)

Parameters

valueToRound double

The value to round.

Returns

int

The rounded value.

GCD(params double[])

Greatest Common Divisor (GCD) of a double array. Note that only a precision of 10 digits is used to avoid problems with numerical noise. So the GCD of 500e-9 and 600e-9 + 1e-18 is 100e-9.

public static double GCD(params double[] a)

Parameters

a double[]

The doubles for which the GCD is calculated.

Returns

double

The GCD

GCD(int, int)

Calculates the Greatest Common Divisor by using Euclid's algorithm.

public static int GCD(int m, int n)

Parameters

m int

First integer number.

n int

Second integer number.

Returns

int

Greatest common divisor of m and n.

GCD(ulong, ulong)

Returns the greatest common divisor (GCD) of two integer values. The method bases on the recursive Euclidean algorithm.

public static ulong GCD(ulong a, ulong b)

Parameters

a ulong

The first integer.

b ulong

The second integer.

Returns

ulong

The greatest common divisor of the two integer values.

GCD(ulong[])

Returns the greatest common divisor (GCD) for an array of integer values. The method bases on the recursive Euclidean algorithm.

public static ulong GCD(ulong[] a)

Parameters

a ulong[]

Array of values for which the GCD is calculated. The integer values must have the sat type ulong (= unsigned long).

Returns

ulong

The greatest common divisor of all values in the given array.

GetDBValue(double)

Calculates corresponding value in decibel (dB) of a given number \(x\): \(dB = \log_{10}(x) \cdot 10\).

public static double GetDBValue(double number)

Parameters

number double

Number \(x\) to calculate the dB value from.

Returns

double

Result of operation.

GetQuantizationLevel(double, double, double, double)

Gets the quantization level on which a certain value lies.

public static int GetQuantizationLevel(double value, double minimumQuantizedValue, double maximumQuantizedValue, double quantizationDifference)

Parameters

value double

The value to quantize.

minimumQuantizedValue double

The value corresponding to the lowest quantization level.

maximumQuantizedValue double

The value corresponding to the highest quantization level.

quantizationDifference double

The difference between two consecutive quantization levels.

Returns

int

Index of the quantization level closest to the given value.

GetQuantizedValue(double, double, double, double)

Gets the quantized value.

public static double GetQuantizedValue(double value, double minimumQuantizedValue, double maximumQuantizedValue, double quantizationDifference)

Parameters

value double

The value to quantize.

minimumQuantizedValue double

The value corresponding to the lowest quantization level.

maximumQuantizedValue double

The value corresponding to the highest quantization level.

quantizationDifference double

The difference between two consecutive quantization levels.

Returns

double

The quantized value.

Hypot(double, double)

Calculates the length \(c\) of the hypotenuse of a right-angled triangle from the lengths \(a\) and \(b\) of the two other sides

public static double Hypot(double a, double b)

Parameters

a double

Length of the first cathetus.

b double

Length of the second cathetus.

Returns

double

The length of the hypotenuse \(c = \sqrt{a^2 + b^2}\).

IndexOfTriangleNumber(int)

The ith triangle number is i * (i+1) / 2 which we have solved for i to return the double index of the matching triangle number. For example this method returns 2.0 for 3 because 3 is the second triangle number. It returns 2.4 for 4 which means that 4 is above the second and below the third triangle number.

public static double IndexOfTriangleNumber(int number)

Parameters

number int

The number for which the index of the matching triangle number is determined.

Returns

double

The index of the matching triangle number. If you want to obtain the largest triangle number less than or equal the given number use a floor rounding on the result. If you want to obtain the smallest triangle number greater than or equal the given number use a ceiling rounding on the result.

Exceptions

ArgumentException

Number must be larger or equal than zero.

InitQuantizationMinMaxRiser(double, double, int, out double, out double, out double)

Initializes a "Min-Max-Riser" quantization.

public static void InitQuantizationMinMaxRiser(double minimum, double maximum, int numberOfQuantizationLevels, out double minimumQuantizedValue, out double maximumQuantizedValue, out double quantizationDifference)

Parameters

minimum double

The minimum of the value range to quantize.

maximum double

The maximum of the value range to quantize.

numberOfQuantizationLevels int

The number of quantization levels.

minimumQuantizedValue double

Out parameter to return the minimum quantized value.

maximumQuantizedValue double

Out parameter to return the maximum quantized value.

quantizationDifference double

Out parameter to return the difference between two consecutive quantization levels.

InitQuantizationMinMaxTread(double, double, int, out double, out double, out double)

Initializes a "Min-Max-Tread" quantization.

public static void InitQuantizationMinMaxTread(double minimum, double maximum, int numberOfQuantizationLevels, out double minimumQuantizedValue, out double maximumQuantizedValue, out double quantizationDifference)

Parameters

minimum double

The minimum of the value range to quantize.

maximum double

The maximum of the value range to quantize.

numberOfQuantizationLevels int

The number of quantization levels.

minimumQuantizedValue double

Out parameter to return the minimum quantized value.

maximumQuantizedValue double

Out parameter to return the maximum quantized value.

quantizationDifference double

Out parameter to return the difference between two consecutive quantization levels.

IsEven(long)

Checks if an integer \(n\) number is even.

public static bool IsEven(long number)

Parameters

number long

Integer number \(n\) to test.

Returns

bool

Result of operation.

IsFinite(double)

Determines whether the given value is finite, i.e. neither NaN ("not a number") nor infinity.

public static bool IsFinite(double val)

Parameters

val double

Value for which it is checked whether it is finite.

Returns

bool

true if the given value in double precision id finite.

IsOdd(long)

Checks if an integer \(n\) number is odd.

public static bool IsOdd(long number)

Parameters

number long

Integer number \(n\) to test.

Returns

bool

Result of operation.

LCM(params double[])

Returns the least common multiple [means: the relation d1i1 = d2i2 = ..., with d as the given double-numbers and i as integers must be fulfilled] (LCM, in German: kgV) of N doubles. The method bases on the prim-factor-decomposition. the signs are not taken into account. Note that only a precision of 10 digits is used to avoid problems with numerical noise. So the LCM of 500e-9 and 600e-9 + 1e-18 is 3000e-9.

public static double LCM(params double[] a)

Parameters

a double[]

List of doubles.

Returns

double

The least common multiple of the given numbers.

Max(params double[])

Determines the maximum from an arbitrary number of doubles. Note that this method is two times slower than nested maximum determination (e.g. Math.Max(a, Math.Max(b, c))).

public static double Max(params double[] values)

Parameters

values double[]

The values for which the maximum shall be determined, either given as individual double values or as array of doubles.

Returns

double

The maximum of all values.

MaxAbs(double, double)

Returns the number with the higher absolute value. If both numbers have the same absolute value then the first number is returned.

public static double MaxAbs(double a, double b)

Parameters

a double

First number.

b double

Second number.

Returns

double

Result of operation.

Min(params double[])

Determines the minimum from an arbitrary number of doubles. Note that this method is two times slower than nested minimum determination (e.g. Math.Min(a, Math.Min(b, c))).

public static double Min(params double[] values)

Parameters

values double[]

The values for which the minimum shall be determined, either given as individual double values or as array of doubles.

Returns

double

The minimum of all values.

Min(Vector, Vector)

Returns the component-wise minimum of two Vector objects.

public static Vector Min(Vector a, Vector b)

Parameters

a Vector

The first vector.

b Vector

The second vector.

Returns

Vector

Result of the operation as new Vector.

Min(VectorD, VectorD)

Returns the component-wise minimum of two VectorD objects.

public static VectorD Min(VectorD a, VectorD b)

Parameters

a VectorD

The first vector.

b VectorD

The second vector.

Returns

VectorD

Result of the operation as new VectorD.

Modulo(double, double)

public static double Modulo(double input, double divisor)

Parameters

input double
divisor double

Returns

double

NumericDerivative(Func<double, double>, double, double)

Calculates the numerical derivative of a function.

public static double NumericDerivative(Func<double, double> function, double position, double epsilon = 1E-10)

Parameters

function Func<double, double>

The function 𝑦 = f(𝑥).

position double

The position 𝑥 at which the derivative is calculated.

epsilon double

The numerical derivative is calculated by calculating the function values at two positions 𝑥 + ε and 𝑥 - ε.

Returns

double

The numerical derivative of the function at the given position.

PhaseDiff(double, double)

Calculates the absolute difference between two phase values which are given in radians. Because of mathematical properties of the unit circle result is always within the interval \([0, \pi)\).

public static double PhaseDiff(double phase1, double phase2)

Parameters

phase1 double

First phase value.

phase2 double

Second phase value.

Returns

double

Result of operation

QuantizePoint(Complex, int, int)

Quantizes a value in the amplitude range \(0 \ldots 1\) and the phase range \(-\pi \ldots \pi\). The phase levels are calculated with the following formula: \(\frac{\pi}{2} - k \cdot \frac{\pi}{n}\). \(n\) is the number of phase levels, \(k = 0, \ldots, n-1\).

public static Complex QuantizePoint(Complex point, int amplitudeLevels, int phaseLevels)

Parameters

point Complex

The complex value to quantize.

amplitudeLevels int

Number of amplitude levels. Zero means no quantization.

phaseLevels int

Number of phase levels. Zero means no quantization.

Returns

Complex

The quantized complex value.

QuantizePoint(Complex, int, int, double)

Quantizes a value in the amplitude range \(0 \ldots 1\) and the phase range \(-\pi \ldots \pi\). The phase levels are calculated with the following formula: \(\frac{\pi}{2} - k \cdot \frac{\pi}{n}\). \(n\) is the number of phase levels, \(k = 0, \ldots, n-1\).

public static Complex QuantizePoint(Complex point, int amplitudeLevels, int phaseLevels, double factor)

Parameters

point Complex

The complex value to quantize.

amplitudeLevels int

Number of amplitude levels. Zero means no quantization.

phaseLevels int

Number of phase levels. Zero means no quantization.

factor double

The projection strength \(\lambda\). \(\lambda = 1\) means hard quantization and \(\lambda = 0\) means no quantization. Values of \(\lambda\) between 0 and 1 lead to soft quantization. The used formulas can be found in the quantization chapter of the manual.

Returns

Complex

The quantized complex value.

RadianToDegree(double)

Convert degrees to radians.

public static double RadianToDegree(double radian)

Parameters

radian double

The angle in radians.

Returns

double

The angle in degrees.

ReducedPhase(double)

Reduces a phase value to the interval \([-\pi, \pi)\). The approximation error of fraction for \(2 \pi\) is 5.2e-22.

public static double ReducedPhase(double phaseVal)

Parameters

phaseVal double

The given phase value.

Returns

double

The normalized phase value.

RombergIntegral(RealFunctionWithSingleArgument, double, double, int)

Computes the definite integral of the real function \(f\) in the range from xmin to xmax. Romberg integration with \(2^{(n-1)+1}\) equidistant function evaluations is used to this end.

public static double RombergIntegral(RealFunctionWithSingleArgument f, double xmin, double xmax, int n)

Parameters

f RealFunctionWithSingleArgument

Delegate of the real function \(f\) to integrate.

xmin double

Lower border of integration interval.

xmax double

Upper border of integration interval.

n int

Order of the method. 1 means trapezoidal, 2 means Simpson rule.

Returns

double

Approximate integral value.

RoundDigits(double, int?)

Rounds the given value to the specified number of significant digits (12345.6 → 12340) with at most the specified number of decimal places (0.00123456 → 0.0012).

public static double RoundDigits(this double valueToRound, int? numberOfDigits = null)

Parameters

valueToRound double

The value to round.

numberOfDigits int?

The number of digits. If not specified, the number of digits specified in the Global Options is used.

Returns

double

The rounded value.

RoundSignificantDigits(double, int)

Rounds the given floating point number to the specified number of significant digits.

public static double RoundSignificantDigits(double valueToRound, int digits)

Parameters

valueToRound double

The value (in double precision) to round.

digits int

The number of digits of the mantissa after rounding. The value must be between 0 and 15. All other values will cause an ArgumentException. A value of 0 means no rounding.

Returns

double

The rounded value.

RoundToInt(double)

This function rounds to closest integer. The difference to Math.Round is that n + 0.5 is always rounded to n + 1.

[Obsolete("Use MathSupport.RoundToInt instead.")]
public static int RoundToInt(double valueToRound)

Parameters

valueToRound double

Value to be rounded.

Returns

int

Result of operation.

RoundToLong(double)

This function rounds to closest long value. The difference to Math.Round is that n + 0.5 is always rounded to n + 1.

public static long RoundToLong(double valueToRound)

Parameters

valueToRound double

Value to be rounded.

Returns

long

Result of operation.

RoundToMultiple(double, double)

Rounds the given valueToRound to the nearest multiple of divisor.

public static double RoundToMultiple(double valueToRound, double divisor)

Parameters

valueToRound double

The value to round.

divisor double

The divisor defining the step size used for rounding.

Returns

double

The rounded value. If the valueToRound is either NaN ("not a number"), infinity or has an absolute value larger than long.MaxValue times the divisor, then the original valueToRound is returned. If the divisor is either NaN, infinity or zero, then NaN is returned.

RoundToVector(VectorD)

This function rounds the given double vector to the closest integer vector. The difference to Math.Round is that n + 0.5 is always rounded to n + 1.

public static Vector RoundToVector(VectorD val)

Parameters

val VectorD

The VectorD object to round

Returns

Vector

The rounded Vector.

RoundWithDownwardTolerance(double)

Method that considers numerical errors with rounding in the following way: 3.5 - Globals.Epsilon will be rounded to 4 and -1.5 - Globals.Epsilon will be rounded to -1

public static int RoundWithDownwardTolerance(double valueToRound)

Parameters

valueToRound double

Value to be rounded

Returns

int

Rounded value.

Sin(double)

Special sine function as the Sin(double) function has problems with \(\sin\left(n ⋅ \pi\right)\).

public static double Sin(double d)

Parameters

d double

The argument of the sine.

Returns

double

The sine of d.

Sinc(double)

Calculates the sinc-function for value \(x\). Sinc is defined as \(\operatorname{sinc}(x) = \begin{cases} 1 & x = 0 \\ \sin(x) / x & \ \ \text{else} \end{cases}\) .

public static double Sinc(double x)

Parameters

x double

Double value \(x\) for which the sinc is calculated.

Returns

double

Result of operation.

SolveQuadraticEquation(double, double, double, out double, out double)

Function solving the general quadratic equation \(0 = a_2 \cdot x^2 + a_1 \cdot x + a_0\) .

public static int SolveQuadraticEquation(double a2, double a1, double a0, out double x1, out double x2)

Parameters

a2 double

Coefficient of quadratic term.

a1 double

Coefficient of linear term.

a0 double

Coefficient of constant term.

x1 double

Out parameter giving the solution if only one solution exists or the smaller of the two solutions.

x2 double

Out parameter giving the larger solution in case of two solutions.

Returns

int

The number of solutions. Either -1, 0, 1, or 2. Two equal values in case of zero discriminant. -1 indicates that any x is a solution.

Swap(ref double, ref double)

Swaps the values of two double variables \(a\) and \(b\).

public static void Swap(ref double a, ref double b)

Parameters

a double

The first value.

b double

The second value.

Swap<T>(ref T, ref T)

Generic function swapping two objects which must be value types (e.g. int, double, ...).

public static void Swap<T>(ref T a, ref T b)

Parameters

a T

The first object.

b T

The second object.

Type Parameters

T

The type of the objects to swap.

SymDefSolve(double[,], ref double[])

Solves the linear system matr * x = vect by using the Cholesky decomposition of matr.

public static void SymDefSolve(double[,] matr, ref double[] vect)

Parameters

matr double[,]

A two dimensional coefficients matrix. The matrix must be symmetric and positive definite.

vect double[]

In the function call this parameter must contain the function values vector of the equation system. After the evaluation of the function this parameter contains the solution vector x.

Tan(double)

Special tangent function as the Tan(double) function has problems with \(\tan\left(n ⋅\frac{\pi}{2}\right)\).

public static double Tan(double d)

Parameters

d double

The argument of the tangent.

Returns

double

The tangent of d.

TriangleNumber(int)

Calculates the ith triangle number which is i * (i+1) / 2

public static int TriangleNumber(int index)

Parameters

index int

The index of the triangle number to calculate.

Returns

int

The ith triangle number