Module_Statistics_IComparableExtension - vorcyc/Vorcyc.Mathematics GitHub Wiki

Location: Home / Statistics module / IComparableExtension

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


Vorcyc.Mathematics.Statistics.IComparableExtension class

Methods

1. LessThan 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: true if the first value is less than the second value; otherwise, false.

2. LessThanOrEqual method

  • 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: true if the first value is less than or equal to the second value; otherwise, false.

3. GreaterThan method

  • 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: true if the first value is greater than the second value; otherwise, false.

4. GreaterThanOrEqual method

  • 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: true if the first value is greater than or equal to the second value; otherwise, false.

5. Equal method

  • 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: true if the first value equals the second value; otherwise, false.

6. NotEqual method

  • 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: true if the first value does not equal the second value; otherwise, false.

7. CompareMax method

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.

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.

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.

Returns the maximum value in a Span.

  • public static T CompareMax<T>(this Span<T> span) where T : IComparable, IComparable<T>
    • Parameters:
      • span: The Span of values.
    • Returns: The maximum value in the Span.

8. CompareMin method

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.

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.

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.

Returns the minimum value in a Span.

  • public static T CompareMin<T>(this Span<T> span) where T : IComparable, IComparable<T>
    • Parameters:
      • span: The Span of values.
    • Returns: The minimum value in the Span.

9. CompareMaxMin method

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.

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.

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: The Span of values.
    • Returns: A tuple containing the maximum and minimum values in the Span.

10. LocateMax method

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.

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.

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: The Span of values.
    • Returns: A tuple containing the index and value of the maximum element in the Span.

11. LocateMin method

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.

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.

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: The Span of values.
    • Returns: A tuple containing the index and value of the minimum element in the Span.

12. CompareMaxAsync method

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.

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.

13. LocateMaxAsync method

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.

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.

14. CompareMinAsync method

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.

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.

15. LocateMinAsync method

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.

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.

Code example

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}");
    }
}
⚠️ **GitHub.com Fallback** ⚠️