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
src
double[]The source array to which elements will be added.
src2
double[]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
src
TThe source value to check.
isApplyingAlternateFunc
Func<T, bool>Function that determines if the alternative should be applied.
alternative
TThe 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
T
The type of the values.
Average(IEnumerable<Vec3d>)
Average.
public static Vec3d Average(this IEnumerable<Vec3d> src)
Parameters
src
IEnumerable<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
v00
doubleValue at (0,0) on normalized bilinear coordinate
v01
doubleValue at (0,1) on normalized bilinear coordinate
v10
doubleValue at (1,0) on normalized bilinear coordinate
v11
doubleValue at (1,1) on normalized bilinear coordinate
u
doubleRatio along v00 and v10, etc.
v
doubleRatio 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
v00
Tvalue at (0,0) on normalized bilinear coordinate
v01
Tvalue at (0,1) on normalized bilinear coordinate
v10
Tvalue at (1,0) on normalized bilinear coordinate
v11
Tvalue at (1,1) on normalized bilinear coordinate
u
doubleratio along v00 and v10, etc.
v
doubleratio along v00 and v01, etc.
Returns
- T
interpolated value
Type Parameters
T
value 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
v00
Tvalue at (0,0) on normalized bilinear coordinate
v01
Tvalue at (0,1) on normalized bilinear coordinate
v10
Tvalue at (1,0) on normalized bilinear coordinate
v11
Tvalue at (1,1) on normalized bilinear coordinate
u
doubleratio along v00 and v10, etc.
v
doubleratio along v00 and v01, etc.
scalingFunc
Func<T, double, T>scaling function
addingFunc
Func<T, T, T>adding function
Returns
- T
interpolated value
Type Parameters
T
value type.
BinaryDividentSequence(int)
Generates a value in a binary divident sequence based on the seed.
public static double BinaryDividentSequence(int seed)
Parameters
seed
intThe 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
v
doubleThe 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
inchdmin
doubleValue 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
mdmin
doubleValue 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
mmdmin
doubleValue 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
mmds
doubleValue 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
mmds
doubleValue 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
radds
doubleValue 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
rpm
doubleValue 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
value
doublevalue
lowerBound
doublelower bound, inclusive
upperBound
doubleupper 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
value
doubleThe value to cycle.
lowerBound
doubleThe lower bound of the range.
upperBound
doubleThe upper bound of the range.
isBothBoundInclusive
boolIf 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_rad
doubletarget angle
anchor_rad
doubleanchor angle
isBothBoundInclusive
boolif 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
v
doublevalue
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
matrix
double[,]The matrix to decompose.
perm
int[]Output parameter that holds row permutations.
toggle
intOutput 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
x
doubleThe 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
x
doubleThe 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
src
double[]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
geometricSeriesSum
doubleThe sum of the geometric series.
powIndex
doubleThe power index in the series.
convergenceLimit
doubleThe 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
begin
TimeSpanThe beginning TimeSpan
end
TimeSpanThe ending TimeSpan
pos
TimeSpanThe 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
begin
Trange begin
end
Trange end
pos
Tkey 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
begin
TThe beginning value
end
TThe ending value
pos
TThe position value to calculate the ratio for
minusFunc
Func<T, T, T>The function to use for subtraction
divFunc
Func<T, T, double>The function to use for division
Returns
- double
The interpolation ratio: (pos - begin) / (end - begin)
Type Parameters
T
The 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
n
intThe 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
a
TimeSpanThe first TimeSpan value
b
TimeSpanThe second TimeSpan value
ratio
doubleThe 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
a
Tb
Tratio
double
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
a
Ta
b
Tb
ratio
doubleratio
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
a
TThe first value
b
TThe second value
pos
doubleThe position to interpolate at
posFunc
Func<T, double>Function to extract a position from a value
Returns
- T
The interpolated value
Type Parameters
T
The 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
a
TItemThe first item
b
TItemThe second item
key
doubleThe key value to interpolate at (0.0 to 1.0)
keyFunc
Func<TItem, double>Function to extract a double key from an item
addingFunc
Func<TItem, TItem, TItem>Function to add two items
scalingFunc
Func<TItem, double, TItem>Function to scale an item by a double
Returns
- TItem
The interpolated item
Type Parameters
TItem
The 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
a
TThe first value
b
TThe second value
ratio
doubleThe interpolation ratio (0.0 to 1.0)
addingFunc
Func<T, T, T>The function to use for addition
scalingFunc
Func<T, double, T>The function to use for scaling
Returns
- T
The interpolated value: a * (1 - ratio) + b * ratio
Type Parameters
T
The 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
a
T[]The first array
b
T[]The second array
ratio
doubleThe interpolation ratio (0.0 to 1.0)
Returns
- T[]
A new array containing the interpolated values
Type Parameters
T
The 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
a
TItemThe first item
b
TItemThe second item
key
TKeyThe key value to interpolate at
keyFunc
Func<TItem, TKey>Function to extract a key from an item
keyMinusFunc
Func<TKey, TKey, TKey>Function to subtract keys
keyDivFunc
Func<TKey, TKey, double>Function to divide keys
itemAddingFunc
Func<TItem, TItem, TItem>Function to add items
itemScalingFunc
Func<TItem, double, TItem>Function to scale items
Returns
- TItem
The interpolated item
Type Parameters
TKey
The type of the key used for interpolation
TItem
The type of the items being interpolated
Inverse(double[,])
Computes the inverse of a matrix.
public static double[,] Inverse(double[,] matrix)
Parameters
matrix
double[,]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
v
doublevalue
Returns
- bool
Is
v
neither 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
src
doublesrc
Returns
- double
src
Norm2(double[])
Calculates the Euclidean norm (L2 norm) of a vector.
public static double Norm2(this double[] src)
Parameters
src
double[]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
src
double[]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
src
doubleThe 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
src
intThe 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
src
doubleThe 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
src
intThe 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
c0
doubleconstant term.
c1
doublecoefficient of power of 1 of x.
c2
doublecoefficient of power of 2 of x.
c3
doublecoefficient 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
c
doublecoefficient of constant of x.
b
doublecoefficient of power of 1 of x.
a
doublecoefficient of power of 2 of x.
x0
doublelower root
x1
doublehigher 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
src
doubleThe 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
src
intThe source integer value
Returns
- int
The square of the source value
Sum(IEnumerable<Vec3d>)
Sum.
public static Vec3d Sum(this IEnumerable<Vec3d> src)
Parameters
src
IEnumerable<Vec3d>src
Returns
- Vec3d
Sum
ToDeg(double)
Get degree from radian.
public static double ToDeg(double rad)
Parameters
rad
doubleradian
Returns
- double
degree
ToRad(double)
Get radian from degree.
public static double ToRad(double deg)
Parameters
deg
doubledegree
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
src
double[,]The source 2D double array
format
stringThe 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
src
double[,]The source matrix to transpose.
Returns
- double[,]
A new matrix that is the transpose of the source matrix.