Table of Contents

Class MathUtil

Namespace
Hi.Geom
Assembly
HiGeom.dll

Math Utility.

public static class MathUtil
Inheritance
MathUtil
Inherited Members

Fields

sqrt2

square root of 2.

public const double sqrt2 = 1.4142135623730951

Field Value

double

sqrt3

square root of 3.

public const double sqrt3 = 1.7320508075688772

Field Value

double

Methods

AbsDiffAngle(double, double)

Calculates the absolute difference between two angles in radians

public static double AbsDiffAngle(double angleA_rad, double angleB_rad)

Parameters

angleA_rad double

First angle in radians

angleB_rad double

Second angle in radians

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

primaryValue double

primary value

alteringValue double

the candidate value

Returns

double

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 T

The source value to check.

isApplyingAlternateFunc Func<T, bool>

Function that determines if the alternative should be applied.

alternative T

The 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 double

Value at (0,0) on normalized bilinear coordinate

v01 double

Value at (0,1) on normalized bilinear coordinate

v10 double

Value at (1,0) on normalized bilinear coordinate

v11 double

Value at (1,1) on normalized bilinear coordinate

u double

Ratio along v00 and v10, etc.

v double

Ratio 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 T

value at (0,0) on normalized bilinear coordinate

v01 T

value at (0,1) on normalized bilinear coordinate

v10 T

value at (1,0) on normalized bilinear coordinate

v11 T

value at (1,1) on normalized bilinear coordinate

u double

ratio along v00 and v10, etc.

v double

ratio 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 T

value at (0,0) on normalized bilinear coordinate

v01 T

value at (0,1) on normalized bilinear coordinate

v10 T

value at (1,0) on normalized bilinear coordinate

v11 T

value at (1,1) on normalized bilinear coordinate

u double

ratio along v00 and v10, etc.

v double

ratio 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 int

The 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 double

The 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 double

Value 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 double

Value 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 double

Value 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 double

Value 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 double

Value 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 double

Value 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 double

Value 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 double

value

lowerBound double

lower bound, inclusive

upperBound double

upper 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 double

The value to cycle.

lowerBound double

The lower bound of the range.

upperBound double

The upper bound of the range.

isBothBoundInclusive bool

If 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 double

target angle

anchor_rad double

anchor angle

isBothBoundInclusive bool

if 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 double

value

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 int

Output 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

src double[]

The source array to be modified.

scale double

The scalar value to divide by.

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 double

The 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 double

The 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

src double[]

The first array.

src2 double[]

The second array.

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 double

The sum of the geometric series.

powIndex double

The power index in the series.

convergenceLimit double

The 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

src double[]

The source array.

scale double

The scalar value to divide by.

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

a double[]

The first array.

b double[]

The second array.

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 TimeSpan

The beginning TimeSpan

end TimeSpan

The ending TimeSpan

pos TimeSpan

The 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 T

range begin

end T

range end

pos T

key 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 T

The beginning value

end T

The ending value

pos T

The 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

src double[]

The source array.

s double

The scalar value to multiply by.

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

a double[]

The first array.

b double[]

The second array to subtract.

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 int

The 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 TimeSpan

The first TimeSpan value

b TimeSpan

The second TimeSpan value

ratio double

The 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 T
b T
ratio 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 T

a

b T

b

ratio double

ratio

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 T

The first value

b T

The second value

pos double

The 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 TItem

The first item

b TItem

The second item

key double

The 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 T

The first value

b T

The second value

ratio double

The 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 double

The 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 TItem

The first item

b TItem

The second item

key TKey

The 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

src double[,]

The source 2x2 matrix

dst double[,]

The destination matrix to store the inverse

Inverse3d(double[,], double[,])

Calculates the inverse of a 3x3 matrix

public static void Inverse3d(double[,] src, double[,] dst)

Parameters

src double[,]

The source 3x3 matrix

dst double[,]

The destination matrix to store the inverse

IsFinite(double)

Is v neither NaN nor infinity.

public static bool IsFinite(double v)

Parameters

v double

value

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

src double[]

The source array to be modified.

scale double

The scalar value to multiply by.

Returns

double[]

The modified source array.

NoChanged(double)

Do nothing.

public static double NoChanged(double src)

Parameters

src double

src

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 double

The 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 int

The 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 double

The 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 int

The 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

matrixA double[,]

The first matrix.

matrixB double[,]

The second matrix.

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 double

constant term.

c1 double

coefficient of power of 1 of x.

c2 double

coefficient of power of 2 of x.

c3 double

coefficient 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 double

coefficient of constant of x.

b double

coefficient of power of 1 of x.

a double

coefficient of power of 2 of x.

x0 double

lower root

x1 double

higher 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

src IList<double>
avg double

Returns

double

STD

Square(double)

Calculates the square of a double value.

public static double Square(this double src)

Parameters

src double

The 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 int

The 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 double

radian

Returns

double

degree

ToRad(double)

Get radian from degree.

public static double ToRad(double deg)

Parameters

deg double

degree

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 string

The 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

src double[]

The source double array

format string

The format string to use for each double value

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

src double

src

format string

format

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.