Gapotchenko.FX.Math.Intervals 2024.1.3

Prefix Reserved
dotnet add package Gapotchenko.FX.Math.Intervals --version 2024.1.3                
NuGet\Install-Package Gapotchenko.FX.Math.Intervals -Version 2024.1.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Gapotchenko.FX.Math.Intervals" Version="2024.1.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Gapotchenko.FX.Math.Intervals --version 2024.1.3                
#r "nuget: Gapotchenko.FX.Math.Intervals, 2024.1.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Gapotchenko.FX.Math.Intervals as a Cake Addin
#addin nuget:?package=Gapotchenko.FX.Math.Intervals&version=2024.1.3

// Install Gapotchenko.FX.Math.Intervals as a Cake Tool
#tool nuget:?package=Gapotchenko.FX.Math.Intervals&version=2024.1.3                

Overview

The module provides data structures and primitives for working with intervals. Interval is a mathematical structure that defines a range of values in a unified and formalized way.

Interval<T>

Interval<T> type represents a continuous range of values. For example, a human age interval for teenagers can be defined as:

using Gapotchenko.FX.Math.Intervals;

// Define an inclusive interval of years ranging from 13 to 19.
// Inclusive interval has both its ends included in the interval.
// The formal interval representation is [13,19],
// where '[' denotes the start of the interval (inclusive),
// and ']' denotes the end of the interval (inclusive).
var teenagers = Interval.Inclusive(13, 19);

Given that range, it is now possible to do various operations:

Console.Write("Enter your age: ");
var age = int.Parse(Console.ReadLine());

// Everyone of an age between 13 and 19 years (inclusive) is a teenager.
// Using the interval notation, this can be stated as: age ∈ [13,19],
// where '∈' symbol denotes the "is an element of" operation.
if (teenagers.Contains(age))
    Console.WriteLine("Congrats, you are a teenager.");
else
    Console.WriteLine("Congrats, you are not a teenager.");

In this simple example, the value is just a number but it can be any comparable type. For example, it can be a System.Version, a System.DateTime, etc.

Interval Operations

The real power of intervals comes when you need to perform certain operations on them.

Overlap Detection

Interval<T>.Overlaps function returns a Boolean value indicating whether a specified interval overlaps with another:

var teenagers = Interval.Inclusive(13, 19);

// Adults are people of 18 years or older
// (the exact age of adulthood depends on a jurisdiction but we use 18 for simplicity).
// Using the interval notation, this is [18,∞),
// where ')' denotes the end of the interval (non-inclusive),
var adults = Interval.FromInclusive(18);

Console.Write(
    "Can teenagers be adults? The answer is {0}."
    teenagers.Overlaps(adults) ? "yes" : "no");

The snippet produces the following output:

Can teenagers be adults? The answer is yes.

Intersection

The intersection of two intervals returns an interval which has a range shared by both of them:

var teenagers = Interval.Inclusive(13, 19);
var adults = Interval.FromInclusive(18);

Console.WriteLine(
    "Adult teenagers have an age of {0}",
    teenagers.Intersect(adults));

The snippet produces the following output:

Adult teenagers have an age of [18,19].

Union

The union of two continuous intervals has the range that covers both of them:

var teenagers = Interval.Inclusive(13, 19);
var adults = Interval.FromInclusive(18);

Console.WriteLine(
    "Adults and teenagers have an age of {0}",
    teenagers.Union(adults));

The snippet produces the following output:

Adults and teenagers have an age of [13,inf).

Note the [13,inf) interval string in the output above. This is the ASCII variant of a formal [13,∞) notation. The ASCII notation is produced by Interval<T>.ToString() method by default. If you want the formal Unicode notation, you can pass U format specifier as in Interval<T>.ToString("U") method call:

Console.WriteLine(
    "Adults and teenagers have an age of {0:U}",
    teenagers.Union(adults));

which produces the output using Unicode mathematical symbols:

Adults and teenagers have an age of [13,∞).

Interval Construction

To define an interval, you can use a set of predefined methods provided by the static Interval type:

// [10,20]
interval = Interval.Inclusive(10, 20);

// (10,20)
interval = Interval.Exclusive(10, 20);

// [10,20)
interval = Interval.InclusiveExclusive(10, 20);

// (10,20]
interval = Interval.ExclusiveInclusive(10, 20);

// [10,∞)
interval = Interval.FromInclusive(10);

// (10,∞)
interval = Interval.FromExclusive(10);

// (-∞,10]
interval = Interval.ToInclusive(10);

// (-∞,10)
interval = Interval.ToExclusive(10);

Or you can explicitly construct an interval by using an Interval<T> constructor and the notion of boundaries:

// [10,20)
interval = new Interval<int>(IntervalBoundary.Inclusive(10), IntervalBoundary.Exclusive(20));

// (10,∞)
interval = new Interval<int>(IntervalBoundary.Exclusive(10), IntervalBoundary.PositiveInfinity<int>());

Special Intervals

There are a few special intervals readily available for use:

// An empty interval ∅
interval = Interval.Empty<T>();

// An infinite interval (-∞,∞)
interval = Interval.Infinite<T>();

// A degenerate interval [x;x]
interval = Interval.Degenerate(x);

ValueInterval<T>

ValueInterval<T> type provides a similar functionality to Interval<T> but it is a structure in terms of .NET type system, while Interval<T> is a class. The difference is that ValueInterval<T> can be allocated on stack without involving expensive GC memory operations, also it has tinier memory footprint.

All in all, ValueInterval<T> is the preferred interval type to use. Being totally transparent and interchangeable with Interval<T>, it comes with certain restrictions. For example, ValueInterval<T> cannot use a custom System.IComparer<T>, and thus it requires T type to always implement System.IComparable<T> interface. This is not an obstacle for most specializing types, but this is a formal restriction that may affect your choice in favor of Interval<T>.

Another scenario where you may prefer Interval<T> type better is when you need to pass it as a reference to many places in code. This may save some CPU time and memory in cases where T type is sufficiently large because passing the interval by reference avoids copying.

Commonly Used Types

  • Gapotchenko.FX.Math.Intervals.Interval<T>
  • Gapotchenko.FX.Math.Intervals.ValueInterval<T>

Other Modules

Let's continue with a look at some other modules provided by Gapotchenko.FX:

Or look at the full list of modules.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 is compatible.  netcoreapp2.2 was computed.  netcoreapp3.0 is compatible.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 is compatible.  net472 is compatible.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Gapotchenko.FX.Math.Intervals:

Package Downloads
Gapotchenko.FX.Math.Metrics

Provides math metrics algorithms.

Gapotchenko.FX.Profiles.Math

Represents the Math profile of Gapotchenko.FX.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2024.1.3 146 11/10/2024