Class MathUtil
Math Utility.
public static class MathUtil
- Inheritance
-
MathUtil
- Inherited Members
Fields
sqrt2
square root of 2.
public const double sqrt2 = 1.4142135623730951
Field Value
sqrt3
square root of 3.
public const double sqrt3 = 1.7320508075688772
Field Value
Methods
AbsDiffAngle(double, double)
Calculates the absolute difference between two angles in radians
public static double AbsDiffAngle(double angleA_rad, double angleB_rad)
Parameters
Returns
- double
The absolute difference between the two angles
Add(double[], double[])
Adds the elements of the second array to the corresponding elements of the first array in-place.
public static double[] Add(this double[] src, double[] src2)
Parameters
srcdouble[]The source array to which elements will be added.
src2double[]The array containing elements to add.
Returns
- double[]
The modified source array.
AlterIfNan(double, double)
public static double AlterIfNan(double primaryValue, double alteringValue)
Parameters
Returns
ApplyAlterIf<T>(T, Func<T, bool>, T)
Applies an alternative value if the source value meets a specified condition.
public static T ApplyAlterIf<T>(T src, Func<T, bool> isApplyingAlternateFunc, T alternative)
Parameters
srcTThe source value to check.
isApplyingAlternateFuncFunc<T, bool>Function that determines if the alternative should be applied.
alternativeTThe alternative value to use if the condition is met.
Returns
- T
Either the original value or the alternative value based on the condition.
Type Parameters
TThe type of the values.
Average(IEnumerable<Vec3d>)
Average.
public static Vec3d Average(this IEnumerable<Vec3d> src)
Parameters
srcIEnumerable<Vec3d>src
Returns
- Vec3d
Average
BilinearInterpolate(double, double, double, double, double, double)
Performs bilinear interpolation between four double values
public static double BilinearInterpolate(double v00, double v01, double v10, double v11, double u, double v)
Parameters
v00doubleValue at (0,0) on normalized bilinear coordinate
v01doubleValue at (0,1) on normalized bilinear coordinate
v10doubleValue at (1,0) on normalized bilinear coordinate
v11doubleValue at (1,1) on normalized bilinear coordinate
udoubleRatio along v00 and v10, etc.
vdoubleRatio along v00 and v01, etc.
Returns
- double
The interpolated value
BilinearInterpolate<T>(T, T, T, T, double, double)
Bilinear interpolate.
public static T BilinearInterpolate<T>(T v00, T v01, T v10, T v11, double u, double v) where T : IAdditionOperators<T, T, T>, IMultiplyOperators<T, double, T>
Parameters
v00Tvalue at (0,0) on normalized bilinear coordinate
v01Tvalue at (0,1) on normalized bilinear coordinate
v10Tvalue at (1,0) on normalized bilinear coordinate
v11Tvalue at (1,1) on normalized bilinear coordinate
udoubleratio along v00 and v10, etc.
vdoubleratio along v00 and v01, etc.
Returns
- T
interpolated value
Type Parameters
Tvalue type.
BilinearInterpolate<T>(T, T, T, T, double, double, Func<T, double, T>, Func<T, T, T>)
Bilinear interpolate.
public static T BilinearInterpolate<T>(T v00, T v01, T v10, T v11, double u, double v, Func<T, double, T> scalingFunc, Func<T, T, T> addingFunc)
Parameters
v00Tvalue at (0,0) on normalized bilinear coordinate
v01Tvalue at (0,1) on normalized bilinear coordinate
v10Tvalue at (1,0) on normalized bilinear coordinate
v11Tvalue at (1,1) on normalized bilinear coordinate
udoubleratio along v00 and v10, etc.
vdoubleratio along v00 and v01, etc.
scalingFuncFunc<T, double, T>scaling function
addingFuncFunc<T, T, T>adding function
Returns
- T
interpolated value
Type Parameters
Tvalue type.
BinaryDividentSequence(int)
Generates a value in a binary divident sequence based on the seed.
public static double BinaryDividentSequence(int seed)
Parameters
seedintThe seed value for the sequence.
Returns
- double
The value in the binary divident sequence.
Cbrt(double)
Calculates the cube root of a number.
public static double Cbrt(double v)
Parameters
vdoubleThe value to calculate the cube root of.
Returns
- double
The cube root of the specified value.
Convert_inchdmin_To_mmds(double)
Converts inches per minute to millimeters per second.
public static double Convert_inchdmin_To_mmds(double inchdmin)
Parameters
inchdmindoubleValue in inches per minute.
Returns
- double
Value in millimeters per second.
Convert_mdmin_To_mmds(double)
Converts meters per minute to millimeters per second.
public static double Convert_mdmin_To_mmds(double mdmin)
Parameters
mdmindoubleValue in meters per minute.
Returns
- double
Value in millimeters per second.
Convert_mmdmin_To_mmds(double)
Converts millimeters per minute to millimeters per second.
public static double Convert_mmdmin_To_mmds(double mmdmin)
Parameters
mmdmindoubleValue in millimeters per minute.
Returns
- double
Value in millimeters per second.
Convert_mmds_To_mdmin(double)
Converts millimeters per second to meters per minute.
public static double Convert_mmds_To_mdmin(double mmds)
Parameters
mmdsdoubleValue in millimeters per second.
Returns
- double
Value in meters per minute.
Convert_mmds_To_mmdmin(double)
Converts millimeters per second to millimeters per minute.
public static double Convert_mmds_To_mmdmin(double mmds)
Parameters
mmdsdoubleValue in millimeters per second.
Returns
- double
Value in millimeters per minute.
Convert_radds_To_rpm(double)
Converts radians per second to revolutions per minute (RPM).
public static double Convert_radds_To_rpm(double radds)
Parameters
raddsdoubleValue in radians per second.
Returns
- double
Value in revolutions per minute.
Convert_rpm_To_radds(double)
Converts revolutions per minute (RPM) to radians per second.
public static double Convert_rpm_To_radds(double rpm)
Parameters
rpmdoubleValue in revolutions per minute.
Returns
- double
Value in radians per second.
Cycle(double, double, double)
If the value is within lowerBound and upperBound,
return value.
Otherwise, perform looped value according to the direction from lower to upper.
ex. bound=(0,1), value=3.1, than return 0.1.
public static double Cycle(double value, double lowerBound, double upperBound)
Parameters
valuedoublevalue
lowerBounddoublelower bound, inclusive
upperBounddoubleupper bound, exclusive
Returns
- double
cycled value
Cycle(double, double, double, bool)
Cycles a value to be within the specified range, with configurable bound inclusivity.
public static double Cycle(double value, double lowerBound, double upperBound, bool isBothBoundInclusive)
Parameters
valuedoubleThe value to cycle.
lowerBounddoubleThe lower bound of the range.
upperBounddoubleThe upper bound of the range.
isBothBoundInclusiveboolIf true, both bounds are inclusive; otherwise, only the lower bound is inclusive.
Returns
- double
The cycled value within the specified range.
Cycle2Pi_rad(double, double, bool)
Get value by the cycle range transformation.
The cycle is from anchor_rad-pi to anchor_rad+pi.
public static double Cycle2Pi_rad(double target_rad, double anchor_rad = 0, bool isBothBoundInclusive = false)
Parameters
target_raddoubletarget angle
anchor_raddoubleanchor angle
isBothBoundInclusiveboolif true, both bounds are inclusive; otherwise, only the lower bound is inclusive
Returns
- double
adjusted target angle
CycleUnit(double)
Get the value locates on 0(inclusive) ~ 1(exclusive). The source code:
return v - Math.Floor(v);
public static double CycleUnit(double v)
Parameters
vdoublevalue
Returns
- double
0 (inclusive) ~1 (exclusive)
Decompose(double[,], out int[], out int)
Performs LU decomposition with partial pivoting on a matrix.
public static double[,] Decompose(double[,] matrix, out int[] perm, out int toggle)
Parameters
matrixdouble[,]The matrix to decompose.
permint[]Output parameter that holds row permutations.
toggleintOutput parameter that is +1 or -1 depending on whether the number of row exchanges is even or odd.
Returns
- double[,]
The LU decomposition of the matrix.
Exceptions
- Exception
Thrown when attempting to decompose a non-square matrix.
Div(double[], double)
Divides each element of the array by a scalar value in-place.
public static double[] Div(this double[] src, double scale)
Parameters
Returns
- double[]
The modified source array.
Erf(double)
Calculates the error function (erf) for the specified value.
public static double Erf(double x)
Parameters
xdoubleThe value to calculate the error function for
Returns
- double
The error function value
Erfc(double)
Calculates the complementary error function (erfc) for the specified value.
public static double Erfc(double x)
Parameters
xdoubleThe value to calculate the complementary error function for
Returns
- double
The complementary error function value
GetAbs(double[])
Creates a new array containing the absolute values of the elements in the source array.
public static double[] GetAbs(this double[] src)
Parameters
srcdouble[]The source array.
Returns
- double[]
A new array containing the absolute values of the elements in the source array.
GetAdd(double[], double[])
Creates a new array by adding corresponding elements of two arrays.
public static double[] GetAdd(this double[] src, double[] src2)
Parameters
Returns
- double[]
A new array containing the sum of corresponding elements.
GetCommonRatioFromGeometricSeries(double, double, double)
Calculates the common ratio from a geometric series sum.
public static double GetCommonRatioFromGeometricSeries(double geometricSeriesSum, double powIndex, double convergenceLimit = 0.001)
Parameters
geometricSeriesSumdoubleThe sum of the geometric series.
powIndexdoubleThe power index in the series.
convergenceLimitdoubleThe convergence limit for the calculation.
Returns
- double
The common ratio of the geometric series.
GetDiv(double[], double)
Creates a new array by dividing each element of the source array by a scalar value.
public static double[] GetDiv(this double[] src, double scale)
Parameters
Returns
- double[]
A new array with each element divided by the scalar value.
GetDot(double[], double[])
Creates a new array by multiplying corresponding elements of two arrays.
public static double[] GetDot(this double[] a, double[] b)
Parameters
Returns
- double[]
A new array containing the product of corresponding elements.
GetInterpolationRatio(TimeSpan, TimeSpan, TimeSpan)
Gets the interpolation ratio between two TimeSpan values
public static double GetInterpolationRatio(TimeSpan begin, TimeSpan end, TimeSpan pos)
Parameters
beginTimeSpanThe beginning TimeSpan
endTimeSpanThe ending TimeSpan
posTimeSpanThe position TimeSpan to calculate the ratio for
Returns
- double
The interpolation ratio: (pos - begin) / (end - begin)
GetInterpolationRatio<T>(T, T, T)
Get position ratio.
(pos - begin) / (end - begin) .
public static double GetInterpolationRatio<T>(T begin, T end, T pos) where T : ISubtractionOperators<T, T, T>, IDivisionOperators<T, T, double>
Parameters
beginTrange begin
endTrange end
posTkey position
Returns
- double
position ratio
Type Parameters
T
GetInterpolationRatio<T>(T, T, T, Func<T, T, T>, Func<T, T, double>)
Gets the interpolation ratio between two values using custom subtraction and division functions
public static double GetInterpolationRatio<T>(T begin, T end, T pos, Func<T, T, T> minusFunc, Func<T, T, double> divFunc)
Parameters
beginTThe beginning value
endTThe ending value
posTThe position value to calculate the ratio for
minusFuncFunc<T, T, T>The function to use for subtraction
divFuncFunc<T, T, double>The function to use for division
Returns
- double
The interpolation ratio: (pos - begin) / (end - begin)
Type Parameters
TThe type of the values
GetMul(double[], double)
Creates a new array by multiplying each element of the source array by a scalar value.
public static double[] GetMul(this double[] src, double s)
Parameters
Returns
- double[]
A new array with each element multiplied by the scalar value.
GetSub(double[], double[])
Creates a new array by subtracting corresponding elements of the second array from the first array.
public static double[] GetSub(this double[] a, double[] b)
Parameters
Returns
- double[]
A new array containing the difference of corresponding elements.
Idt(int)
Creates an identity matrix of the specified size.
public static double[,] Idt(int n)
Parameters
nintThe size of the square identity matrix.
Returns
- double[,]
An n x n identity matrix.
Interpolate(TimeSpan, TimeSpan, double)
Interpolates between two TimeSpan values using the specified ratio
public static TimeSpan Interpolate(TimeSpan a, TimeSpan b, double ratio)
Parameters
aTimeSpanThe first TimeSpan value
bTimeSpanThe second TimeSpan value
ratiodoubleThe interpolation ratio (0.0 to 1.0)
Returns
- TimeSpan
The interpolated TimeSpan value: a * (1 - ratio) + b * ratio
InterpolateWithinBoundary<T>(T, T, double)
If ratio smaller or equal 0, return a.
If ratio larger or equal 1, return b.
Otherwise, interpolate by ratio.
public static T InterpolateWithinBoundary<T>(T a, T b, double ratio) where T : IEqualityOperators<T, T, bool>, IAdditionOperators<T, T, T>, IMultiplyOperators<T, double, T>
Parameters
aTbTratiodouble
Returns
- T
Type Parameters
T
Interpolate<T>(T, T, double)
Interpolate from a to b with ratio alpha:(1-alpha).
public static T Interpolate<T>(T a, T b, double ratio) where T : IEqualityOperators<T, T, bool>, IAdditionOperators<T, T, T>, IMultiplyOperators<T, double, T>
Parameters
aTa
bTb
ratiodoubleratio
Returns
- T
a * (1 - ratio) + b * ratio
Type Parameters
T
Interpolate<T>(T, T, double, Func<T, double>)
Interpolates between two values based on a position using a position function.
public static T Interpolate<T>(T a, T b, double pos, Func<T, double> posFunc) where T : INumber<T>, IMultiplyOperators<T, double, T>
Parameters
aTThe first value
bTThe second value
posdoubleThe position to interpolate at
posFuncFunc<T, double>Function to extract a position from a value
Returns
- T
The interpolated value
Type Parameters
TThe type of the values
Interpolate<TItem>(TItem, TItem, double, Func<TItem, double>, Func<TItem, TItem, TItem>, Func<TItem, double, TItem>)
Interpolates between two items based on a key value using custom functions.
public static TItem Interpolate<TItem>(TItem a, TItem b, double key, Func<TItem, double> keyFunc, Func<TItem, TItem, TItem> addingFunc, Func<TItem, double, TItem> scalingFunc)
Parameters
aTItemThe first item
bTItemThe second item
keydoubleThe key value to interpolate at (0.0 to 1.0)
keyFuncFunc<TItem, double>Function to extract a double key from an item
addingFuncFunc<TItem, TItem, TItem>Function to add two items
scalingFuncFunc<TItem, double, TItem>Function to scale an item by a double
Returns
- TItem
The interpolated item
Type Parameters
TItemThe type of the items
Interpolate<T>(T, T, double, Func<T, T, T>, Func<T, double, T>)
Interpolates between two values using custom addition and scaling functions
public static T Interpolate<T>(T a, T b, double ratio, Func<T, T, T> addingFunc, Func<T, double, T> scalingFunc)
Parameters
aTThe first value
bTThe second value
ratiodoubleThe interpolation ratio (0.0 to 1.0)
addingFuncFunc<T, T, T>The function to use for addition
scalingFuncFunc<T, double, T>The function to use for scaling
Returns
- T
The interpolated value: a * (1 - ratio) + b * ratio
Type Parameters
TThe type of the values
Interpolate<T>(T[], T[], double)
Interpolates between two arrays of values using the specified ratio
public static T[] Interpolate<T>(T[] a, T[] b, double ratio) where T : INumber<T>, IMultiplyOperators<T, double, T>
Parameters
aT[]The first array
bT[]The second array
ratiodoubleThe interpolation ratio (0.0 to 1.0)
Returns
- T[]
A new array containing the interpolated values
Type Parameters
TThe type of the array elements
Interpolate<TKey, TItem>(TItem, TItem, TKey, Func<TItem, TKey>, Func<TKey, TKey, TKey>, Func<TKey, TKey, double>, Func<TItem, TItem, TItem>, Func<TItem, double, TItem>)
Interpolates between two items using custom key extraction, key operations, and item operations
public static TItem Interpolate<TKey, TItem>(TItem a, TItem b, TKey key, Func<TItem, TKey> keyFunc, Func<TKey, TKey, TKey> keyMinusFunc, Func<TKey, TKey, double> keyDivFunc, Func<TItem, TItem, TItem> itemAddingFunc, Func<TItem, double, TItem> itemScalingFunc)
Parameters
aTItemThe first item
bTItemThe second item
keyTKeyThe key value to interpolate at
keyFuncFunc<TItem, TKey>Function to extract a key from an item
keyMinusFuncFunc<TKey, TKey, TKey>Function to subtract keys
keyDivFuncFunc<TKey, TKey, double>Function to divide keys
itemAddingFuncFunc<TItem, TItem, TItem>Function to add items
itemScalingFuncFunc<TItem, double, TItem>Function to scale items
Returns
- TItem
The interpolated item
Type Parameters
TKeyThe type of the key used for interpolation
TItemThe type of the items being interpolated
Inverse(double[,])
Computes the inverse of a matrix.
public static double[,] Inverse(double[,] matrix)
Parameters
matrixdouble[,]The matrix to invert.
Returns
- double[,]
The inverse of the matrix.
Exceptions
- Exception
Thrown when the matrix cannot be inverted.
Inverse2d(double[,], double[,])
Calculates the inverse of a 2x2 matrix
public static void Inverse2d(double[,] src, double[,] dst)
Parameters
Inverse3d(double[,], double[,])
Calculates the inverse of a 3x3 matrix
public static void Inverse3d(double[,] src, double[,] dst)
Parameters
IsFinite(double)
Is v neither NaN nor infinity.
public static bool IsFinite(double v)
Parameters
vdoublevalue
Returns
- bool
Is
vneither NaN nor infinity.
Mul(double[], double)
Multiplies each element of the array by a scalar value in-place.
public static double[] Mul(this double[] src, double scale)
Parameters
Returns
- double[]
The modified source array.
NoChanged(double)
Do nothing.
public static double NoChanged(double src)
Parameters
srcdoublesrc
Returns
- double
src
Norm2(double[])
Calculates the Euclidean norm (L2 norm) of a vector.
public static double Norm2(this double[] src)
Parameters
srcdouble[]The source vector as an array of doubles.
Returns
- double
The Euclidean norm of the vector.
Normalized(double[])
Creates a new array by normalizing the source array to have a unit norm.
public static double[] Normalized(this double[] src)
Parameters
srcdouble[]The source array.
Returns
- double[]
A new array with the same direction as the source array but with unit norm.
Pow3(double)
Calculates the cube (power of 3) of a double value.
public static double Pow3(this double src)
Parameters
srcdoubleThe source double value
Returns
- double
The cube of the source value
Pow3(int)
Calculates the cube (power of 3) of an integer value.
public static int Pow3(this int src)
Parameters
srcintThe source integer value
Returns
- int
The cube of the source value
Pow4(double)
Calculates the fourth power of a double value.
public static double Pow4(this double src)
Parameters
srcdoubleThe source double value
Returns
- double
The fourth power of the source value
Pow4(int)
Calculates the fourth power of an integer value.
public static int Pow4(this int src)
Parameters
srcintThe source integer value
Returns
- int
The fourth power of the source value
Product(double[,], double[,])
Multiplies two matrices.
public static double[,] Product(double[,] matrixA, double[,] matrixB)
Parameters
Returns
- double[,]
The product of the two matrices.
Exceptions
- Exception
Thrown when the matrices are not conformable for multiplication.
SolveCubic(double, double, double, double)
public static Complex[] SolveCubic(double c0, double c1, double c2, double c3)
Parameters
c0doubleconstant term.
c1doublecoefficient of power of 1 of x.
c2doublecoefficient of power of 2 of x.
c3doublecoefficient of power of 3 of x.
Returns
- Complex[]
SolveQuadratic(double, double, double, out double, out double)
Solve quadratic equation.
public static double SolveQuadratic(double c, double b, double a, out double x0, out double x1)
Parameters
cdoublecoefficient of constant of x.
bdoublecoefficient of power of 1 of x.
adoublecoefficient of power of 2 of x.
x0doublelower root
x1doublehigher root
Returns
- double
determinent
SqrtVariance(IList<double>, out double)
Standard deviation with n denominator (instead of n-1).
public static double SqrtVariance(IList<double> src, out double avg)
Parameters
Returns
- double
STD
Square(double)
Calculates the square of a double value.
public static double Square(this double src)
Parameters
srcdoubleThe source double value
Returns
- double
The square of the source value
Square(int)
Calculates the square of an integer value.
public static int Square(this int src)
Parameters
srcintThe source integer value
Returns
- int
The square of the source value
Sum(IEnumerable<Vec3d>)
Sum.
public static Vec3d Sum(this IEnumerable<Vec3d> src)
Parameters
srcIEnumerable<Vec3d>src
Returns
- Vec3d
Sum
ToDeg(double)
Get degree from radian.
public static double ToDeg(double rad)
Parameters
raddoubleradian
Returns
- double
degree
ToRad(double)
Get radian from degree.
public static double ToRad(double deg)
Parameters
degdoubledegree
Returns
- double
radian
ToString(double[,], string)
Converts a 2D double array to a string representation using the specified format
public static string ToString(this double[,] src, string format)
Parameters
srcdouble[,]The source 2D double array
formatstringThe format string to use for each double value
Returns
- string
A string representation of the 2D array
ToString(double[], string)
Converts a double array to a string representation using the specified format
public static string ToString(this double[] src, string format)
Parameters
Returns
- string
A string representation of the array
ToStringWithoutCultureNum(double, string)
To string function. The special number is formatted by XmlConvert.
public static string ToStringWithoutCultureNum(this double src, string format)
Parameters
Returns
- string
string
Transpose(double[,])
Transposes a 2D matrix represented as a 2D array.
public static double[,] Transpose(double[,] src)
Parameters
srcdouble[,]The source matrix to transpose.
Returns
- double[,]
A new matrix that is the transpose of the source matrix.