Provides extended mathematical operations including number theory, signal processing, and functional programming utilities.
public static class MathEx
int Ceiling(int x, int period)Summary: Rounds a value up to the nearest multiple of a specified period.
Parameters:
x- The value to round.
period- The period (interval) to round to.Returns: The smallest multiple of
periodthat is greater than or equal tox.
Func<int, int> Ceiling(Func<int, int> f, int period)Summary: Rounds a value up to the nearest multiple of a specified period.
Parameters:
x- The value to round.
period- The period (interval) to round to.Returns: The smallest multiple of
periodthat is greater than or equal tox.
Func<int, int> Compose(Func<int, int> f, Func<int, int> g)Summary: Creates a new function that represents the composition of two functions.
Parameters:
f- The outer function.
g- The inner function.Returns: A function that returns
f(g(x))for any inputx.
int Floor(int x, int period)Summary: Rounds a value down to the nearest multiple of a specified period.
Parameters:
x- The value to round.
period- The period (interval) to round to.Returns: The largest multiple of
periodthat is less than or equal tox.
Func<int, int> Floor(Func<int, int> f, int period)Summary: Rounds a value down to the nearest multiple of a specified period.
Parameters:
x- The value to round.
period- The period (interval) to round to.Returns: The largest multiple of
periodthat is less than or equal tox.
IEnumerable<int> GetDivisorsLessThanOrEqual(int value, int max)Summary: Gets divisors for
valuethat is less than or equal to the specifiedmaxvalue.Parameters:
value- The value to get divisors of.
max- The maximum divisor threshold.
int GreatestCommonDivisor(int v1, int v2)Summary: Calculates the greatest common divisor (GCD) of two integers using Euclid's algorithm.
Parameters:
v1- The first integer value.
v2- The second integer value.Returns: The greatest common divisor of
v1andv2.
double GreatestCommonDivisor(double v1, double v2)Summary: Calculates the greatest common divisor (GCD) of two integers using Euclid's algorithm.
Parameters:
v1- The first integer value.
v2- The second integer value.Returns: The greatest common divisor of
v1andv2.
int Hysteron(ref int state, int x, int width = 1, int height = 1)Summary: Implements a hysteresis (hysteron) operator where the output depends on both current input and previous state.
Parameters:
state- The current state of the operator, which is updated based on the input.
x- The input value.
width- The upper threshold. When input reaches or exceeds this value, state becomes . Default is 1.
height- The maximum output value. Default is 1.Returns: The updated state value.
Remarks: This function maintains state between calls, implementing memory-like behavior common in control systems.
Func<int, int> Modulate(Func<int, int> carrier, Func<int, int> cellFunction, int period)Summary: Creates a modulated function by combining a carrier function with a cell function.
Parameters:
carrier- The carrier function that provides the base signal.
cellFunction- The cell function that modulates the carrier within each period.
period- The modulation period.Returns: A function representing the modulated signal.
Remarks: This implements a form of amplitude modulation where the carrier is sampled at period boundaries and interpolated using the cell function.
Func<int, int> Multiply(Func<int, int> f, Func<int, int> g)Summary: Creates a new function that multiplies the results of two functions pointwise.
Parameters:
f- The first function.
g- The second function.Returns: A function that returns
f(x) * g(x)for any inputx.
Func<int, int> Periodic(Func<int, int> f, int period)Summary: Creates a periodic version of a function by applying sawtooth wrapping to its input.
Parameters:
f- The function to make periodic.
period- The period of repetition.Returns: A function that repeats
feveryperiodunits.
int Rect(int x, int width = 1, int height = 1)Summary: Implements a rectangular (pulse) function that returns a specified height within a defined width, otherwise 0.
Parameters:
x- The input value.
width- The width of the rectangular pulse. Default is 1.
height- The height of the rectangular pulse. Default is 1.Returns:
heightifxis within [0,width); otherwise, 0.
int SawTooth(int x, int period)Summary: Generates a sawtooth wave pattern with a specified period.
Parameters:
x- The input value.
period- The period of the sawtooth wave.Returns: A value in the range [0,
period) that repeats in a sawtooth pattern.
int Step(int x)Summary: Implements a step function (Heaviside step function) that returns 0 for negative values and 1 for non-negative values.
Parameters:
x- The input value.Returns: 0 if
xis negative; otherwise, 1.