Module_Statistics_IComparableExtension - vorcyc/Vorcyc.Mathematics GitHub Wiki
Location: Home / Statistics module / IComparableExtension
Vorcyc.Mathematics.Statistics.IComparableExtension is a class that provides extension methods to simplify comparison operations for IComparable<T> and IComparable, and supports parallel computation of maximum and minimum values.
All methods below are located in class: Vorcyc.Mathematics.Statistics.IComparableExtension
πContents
- π LessThan method
- π LessThanOrEqual method
- π GreaterThan method
- π GreaterThanOrEqual method
- π Equal method
- π NotEqual method
- π CompareMax method
- π CompareMin method
- π CompareMaxMin method
- π LocateMax method
- π LocateMin method
- π CompareMaxAsync method
- π LocateMaxAsync method
- π CompareMinAsync method
- π LocateMinAsync method
-
public static bool LessThan<T>(this T value1, T value2) where T : IComparable, IComparable<T>- Determines whether the first value is less than the second value.
- Parameters:
-
value1: The first value. -
value2: The second value.
-
- Returns:
trueif the first value is less than the second value; otherwise,false.
-
public static bool LessThanOrEqual<T>(this T value1, T value2) where T : IComparable, IComparable<T>- Determines whether the first value is less than or equal to the second value.
- Parameters:
-
value1: The first value. -
value2: The second value.
-
- Returns:
trueif the first value is less than or equal to the second value; otherwise,false.
-
public static bool GreaterThan<T>(this T value1, T value2) where T : IComparable, IComparable<T>- Determines whether the first value is greater than the second value.
- Parameters:
-
value1: The first value. -
value2: The second value.
-
- Returns:
trueif the first value is greater than the second value; otherwise,false.
-
public static bool GreaterThanOrEqual<T>(this T value1, T value2) where T : IComparable, IComparable<T>- Determines whether the first value is greater than or equal to the second value.
- Parameters:
-
value1: The first value. -
value2: The second value.
-
- Returns:
trueif the first value is greater than or equal to the second value; otherwise,false.
-
public static bool Equal<T>(this T value1, T value2) where T : IComparable, IComparable<T>- Determines whether the first value equals the second value.
- Parameters:
-
value1: The first value. -
value2: The second value.
-
- Returns:
trueif the first value equals the second value; otherwise,false.
-
public static bool NotEqual<T>(this T value1, T value2) where T : IComparable, IComparable<T>- Determines whether the first value does not equal the second value.
- Parameters:
-
value1: The first value. -
value2: The second value.
-
- Returns:
trueif the first value does not equal the second value; otherwise,false.
Returns the maximum of two values.
-
public static T CompareMax<T>(this T value1, T value2) where T : IComparable, IComparable<T>- Parameters:
-
value1: The first value. -
value2: The second value.
-
- Returns: The maximum of the two values.
- Parameters:
Returns the maximum value in an array.
-
public static T CompareMax<T>(this T[] values) where T : IComparable, IComparable<T>- Parameters:
-
values: The array of values.
-
- Returns: The maximum value in the array.
- Parameters:
Returns the maximum value in a specified range of an array.
-
public static T CompareMax<T>(this T[] values, int start, int length) where T : IComparable, IComparable<T>- Parameters:
-
values: The array of values. -
start: The starting index of the range. -
length: The length of the range.
-
- Returns: The maximum value in the specified range of the array.
- Parameters:
Returns the maximum value in a Span.
-
public static T CompareMax<T>(this Span<T> span) where T : IComparable, IComparable<T>- Parameters:
-
span: TheSpanof values.
-
- Returns: The maximum value in the
Span.
- Parameters:
Returns the minimum of two values.
-
public static T CompareMin<T>(this T value1, T value2) where T : IComparable, IComparable<T>- Parameters:
-
value1: The first value. -
value2: The second value.
-
- Returns: The minimum of the two values.
- Parameters:
Returns the minimum value in an array.
-
public static T CompareMin<T>(this T[] values) where T : IComparable, IComparable<T>- Parameters:
-
values: The array of values.
-
- Returns: The minimum value in the array.
- Parameters:
Returns the minimum value in a specified range of an array.
-
public static T CompareMin<T>(this T[] values, int start, int length) where T : IComparable, IComparable<T>- Parameters:
-
values: The array of values. -
start: The starting index of the range. -
length: The length of the range.
-
- Returns: The minimum value in the specified range of the array.
- Parameters:
Returns the minimum value in a Span.
-
public static T CompareMin<T>(this Span<T> span) where T : IComparable, IComparable<T>- Parameters:
-
span: TheSpanof values.
-
- Returns: The minimum value in the
Span.
- Parameters:
Returns the maximum and minimum values in an array.
-
public static (T max, T min) CompareMaxMin<T>(this T[] values) where T : IComparable, IComparable<T>- Parameters:
-
values: The array of values.
-
- Returns: A tuple containing the maximum and minimum values in the array.
- Parameters:
Returns the maximum and minimum values in a specified range of an array.
-
public static (T max, T min) CompareMaxMin<T>(this T[] values, int start, int length) where T : IComparable, IComparable<T>- Parameters:
-
values: The array of values. -
start: The starting index of the range. -
length: The length of the range.
-
- Returns: A tuple containing the maximum and minimum values in the specified range of the array.
- Parameters:
Returns the maximum and minimum values in a Span.
-
public static (T max, T min) CompareMaxMin<T>(this Span<T> span) where T : IComparable, IComparable<T>- Parameters:
-
span: TheSpanof values.
-
- Returns: A tuple containing the maximum and minimum values in the
Span.
- Parameters:
Returns the index and value of the maximum element in an array.
-
public static (int index, T value) LocateMax<T>(T[] values) where T : IComparable, IComparable<T>- Parameters:
-
values: The array of values.
-
- Returns: A tuple containing the index and value of the maximum element in the array.
- Parameters:
Returns the index and value of the maximum element in a specified range of an array.
-
public static (int index, T value) LocateMax<T>(T[] values, int start, int length) where T : IComparable, IComparable<T>- Parameters:
-
values: The array of values. -
start: The starting index of the range. -
length: The length of the range.
-
- Returns: A tuple containing the index and value of the maximum element in the specified range of the array.
- Parameters:
Returns the index and value of the maximum element in a Span.
-
public static (int index, T value) LocateMax<T>(Span<T> span) where T : IComparable, IComparable<T>- Parameters:
-
span: TheSpanof values.
-
- Returns: A tuple containing the index and value of the maximum element in the
Span.
- Parameters:
Returns the index and value of the minimum element in an array.
-
public static (int index, T value) LocateMin<T>(T[] values) where T : IComparable, IComparable<T>- Parameters:
-
values: The array of values.
-
- Returns: A tuple containing the index and value of the minimum element in the array.
- Parameters:
Returns the index and value of the minimum element in a specified range of an array.
-
public static (int index, T value) LocateMin<T>(T[] values, int start, int length) where T : IComparable, IComparable<T>- Parameters:
-
values: The array of values. -
start: The starting index of the range. -
length: The length of the range.
-
- Returns: A tuple containing the index and value of the minimum element in the specified range of the array.
- Parameters:
Returns the index and value of the minimum element in a Span.
-
public static (int index, T value) LocateMin<T>(Span<T> span) where T : IComparable, IComparable<T>- Parameters:
-
span: TheSpanof values.
-
- Returns: A tuple containing the index and value of the minimum element in the
Span.
- Parameters:
Returns the maximum value in a parallel sequence.
-
public static Task<TValue> CompareMaxAsync<TValue>(this TValue[] values, int? numberOfWorkers = null, bool useTPL = false) where TValue : IComparable, IComparable<TValue>- Parameters:
-
values: The array of values. -
numberOfWorkers: The number of worker tasks. If null, the number of tasks is determined by the environment. -
useTPL: If true, uses the Task Parallel Library (TPL) for parallel computation.
-
- Returns: A task representing the asynchronous operation. The task result contains the maximum value.
- Parameters:
Returns the maximum value in a specified range of a parallel sequence.
-
public static Task<TValue> CompareMaxAsync<TValue>(this TValue[] values, int start, int length, int? numberOfWorkers = null, bool useTPL = false) where TValue : IComparable, IComparable<TValue>- Parameters:
-
values: The array of values. -
start: The starting index of the range. -
length: The length of the range. -
numberOfWorkers: The number of worker tasks. If null, the number of tasks is determined by the environment. -
useTPL: If true, uses the Task Parallel Library (TPL) for parallel computation.
-
- Returns: A task representing the asynchronous operation. The task result contains the maximum value.
- Parameters:
Returns the index and value of the maximum element in a parallel sequence.
-
public static Task<(int, TValue)> LocateMaxAsync<TValue>(this TValue[] values, int? numberOfWorkers = null, bool useTPL = false) where TValue : IComparable, IComparable<TValue>- Parameters:
-
values: The array of values. -
numberOfWorkers: The number of worker tasks. If null, the number of tasks is determined by the environment. -
useTPL: If true, uses the Task Parallel Library (TPL) for parallel computation.
-
- Returns: A task representing the asynchronous operation. The task result contains the index and value of the maximum element.
- Parameters:
Returns the index and value of the maximum element in a specified range of a parallel sequence.
-
public static Task<(int, TValue)> LocateMaxAsync<TValue>(this TValue[] values, int start, int length, int? numberOfWorkers = null, bool useTPL = false) where TValue : IComparable, IComparable<TValue>- Parameters:
-
values: The array of values. -
start: The starting index of the range. -
length: The length of the range. -
numberOfWorkers: The number of worker tasks. If null, the number of tasks is determined by the environment. -
useTPL: If true, uses the Task Parallel Library (TPL) for parallel computation.
-
- Returns: A task representing the asynchronous operation. The task result contains the index and value of the maximum element.
- Parameters:
Returns the minimum value in a parallel sequence.
-
public static Task<TValue> CompareMinAsync<TValue>(this TValue[] values, int? numberOfWorkers = null, bool useTPL = false) where TValue : IComparable, IComparable<TValue>- Parameters:
-
values: The array of values. -
numberOfWorkers: The number of worker tasks. If null, the number of tasks is determined by the environment. -
useTPL: If true, uses the Task Parallel Library (TPL) for parallel computation.
-
- Returns: A task representing the asynchronous operation. The task result contains the minimum value.
- Parameters:
Returns the minimum value in a specified range of a parallel sequence.
-
public static Task<TValue> CompareMinAsync<TValue>(this TValue[] values, int start, int length, int? numberOfWorkers = null, bool useTPL = false) where TValue : IComparable, IComparable<TValue>- Parameters:
-
values: The array of values. -
start: The starting index of the range. -
length: The length of the range. -
numberOfWorkers: The number of worker tasks. If null, the number of tasks is determined by the environment. -
useTPL: If true, uses the Task Parallel Library (TPL) for parallel computation.
-
- Returns: A task representing the asynchronous operation. The task result contains the minimum value.
- Parameters:
Returns the index and value of the minimum element in a parallel sequence.
-
public static Task<(int, TValue)> LocateMinAsync<TValue>(this TValue[] values, int? numberOfWorkers = null, bool useTPL = false) where TValue : IComparable, IComparable<TValue>- Parameters:
-
values: The array of values. -
numberOfWorkers: The number of worker tasks. If null, the number of tasks is determined by the environment. -
useTPL: If true, uses the Task Parallel Library (TPL) for parallel computation.
-
- Returns: A task representing the asynchronous operation. The task result contains the index and value of the minimum element.
- Parameters:
Returns the index and value of the minimum element in a specified range of a parallel sequence.
-
public static Task<(int, TValue)> LocateMinAsync<TValue>(this TValue[] values, int start, int length, int? numberOfWorkers = null, bool useTPL = false) where TValue : IComparable, IComparable<TValue>- Parameters:
-
values: The array of values. -
start: The starting index of the range. -
length: The length of the range. -
numberOfWorkers: The number of worker tasks. If null, the number of tasks is determined by the environment. -
useTPL: If true, uses the Task Parallel Library (TPL) for parallel computation.
-
- Returns: A task representing the asynchronous operation. The task result contains the index and value of the minimum element.
- Parameters:
The following example uses several methods from the IComparableExtension class, with comments in the sample:
using System;
using System.Threading.Tasks;
using Vorcyc.Mathematics.Statistics;
public class IComparableExtensionExample
{
public static async Task Main()
{
// Define two values
int value1 = 5;
int value2 = 10;
// Compare the two values
Console.WriteLine($"Is {value1} less than {value2}? {value1.LessThan(value2)}");
Console.WriteLine($"Is {value1} less than or equal to {value2}? {value1.LessThanOrEqual(value2)}");
Console.WriteLine($"Is {value1} greater than {value2}? {value1.GreaterThan(value2)}");
Console.WriteLine($"Is {value1} greater than or equal to {value2}? {value1.GreaterThanOrEqual(value2)}");
Console.WriteLine($"Is {value1} equal to {value2}? {value1.Equal(value2)}");
Console.WriteLine($"Is {value1} not equal to {value2}? {value1.NotEqual(value2)}");
// Define an array
int[] values = { 1, 2, 3, 4, 5 };
// Get maximum and minimum in the array
int max = values.CompareMax();
int min = values.CompareMin();
Console.WriteLine($"Max value in array: {max}");
Console.WriteLine($"Min value in array: {min}");
// Get index and value of maximum and minimum in the array
var (maxIndex, maxValue) = values.LocateMax();
var (minIndex, minValue) = values.LocateMin();
Console.WriteLine($"Max value in array: {maxValue} at index {maxIndex}");
Console.WriteLine($"Min value in array: {minValue} at index {minIndex}");
// Compute maximum and minimum in the array in parallel
int parallelMax = await values.CompareMaxAsync();
int parallelMin = await values.CompareMinAsync();
Console.WriteLine($"Parallel max value in array: {parallelMax}");
Console.WriteLine($"Parallel min value in array: {parallelMin}");
// Compute index and value of maximum and minimum in the array in parallel
var (parallelMaxIndex, parallelMaxValue) = await values.LocateMaxAsync();
var (parallelMinIndex, parallelMinValue) = await values.LocateMinAsync();
Console.WriteLine($"Parallel max value in array: {parallelMaxValue} at index {parallelMaxIndex}");
Console.WriteLine($"Parallel min value in array: {parallelMinValue} at index {parallelMinIndex}");
}
}