Table of Contents

Class Sequences

Namespace
MathAssertions
Assembly
MathAssertions.dll

Sequence-property checks for ReadOnlySpan<T> of double: monotonicity, sortedness, boundedness, arithmetic and geometric progression, convergence, and length predicates. NaN-aware where it applies, allocation-free where it can be, callable from any test framework or production code.

public static class Sequences
Inheritance
Sequences
Inherited Members

Remarks

0.1.0 Cluster 3. Sits alongside MathTolerance in the same package; the MathAssertions.TUnit adapter delegates to these helpers from its [GenerateAssertion] extensions.

Methods

ConvergesTo(ReadOnlySpan<double>, double, double)

Returns true when the last value of the sequence is within tolerance of limit. The empty-span case returns false because no value has been observed.

public static bool ConvergesTo(ReadOnlySpan<double> values, double limit, double tolerance)

Parameters

values ReadOnlySpan<double>

Sequence to inspect.

limit double

Expected limit value.

tolerance double

Maximum allowed absolute difference between the last value and limit. Must be non-negative and not NaN.

Returns

bool

true if the last value is within tolerance of the limit.

Remarks

This is the practical convergence check used in test contexts: the sequence represents iterations of an algorithm and the last term is the latest estimate. For the formal Cauchy criterion (no presumed limit), see IsCauchyConvergent(ReadOnlySpan<double>, double).

Exceptions

ArgumentOutOfRangeException

tolerance is NaN or negative.

HasLength<T>(ReadOnlySpan<T>, int)

Returns true when the span has exactly expected elements.

public static bool HasLength<T>(ReadOnlySpan<T> values, int expected)

Parameters

values ReadOnlySpan<T>

Sequence to inspect.

expected int

Required length. Must be non-negative.

Returns

bool

true if values.Length == expected.

Type Parameters

T

Element type.

Remarks

Negative expected is rejected at entry. Length is non-negative, so a negative argument is always a caller bug rather than a useful "no value will match" sentinel; failing fast surfaces the typo at the call site.

Exceptions

ArgumentOutOfRangeException

expected is negative.

HasMinLength<T>(ReadOnlySpan<T>, int)

Returns true when the span has at least expected elements.

public static bool HasMinLength<T>(ReadOnlySpan<T> values, int expected)

Parameters

values ReadOnlySpan<T>

Sequence to inspect.

expected int

Minimum required length. Must be non-negative.

Returns

bool

true if values.Length >= expected.

Type Parameters

T

Element type.

Remarks

Negative expected is rejected at entry. Length is non-negative, so any negative threshold makes the result trivially true and would mask caller-side typos in the HasMinLength(values, -1) shape.

Exceptions

ArgumentOutOfRangeException

expected is negative.

IsArithmeticProgression(ReadOnlySpan<double>, double)

Returns true when adjacent differences are equal within tolerance; that is, the sequence is an arithmetic progression with a common difference. Empty and single-element spans are vacuously progressions.

public static bool IsArithmeticProgression(ReadOnlySpan<double> values, double tolerance)

Parameters

values ReadOnlySpan<double>

Sequence to inspect.

tolerance double

Maximum allowed deviation between adjacent differences. Must be non-negative and not NaN.

Returns

bool

true if the sequence has a (tolerance-stable) common difference.

Exceptions

ArgumentOutOfRangeException

tolerance is NaN or negative.

IsBounded(ReadOnlySpan<double>, double, double)

Returns true when every value is in the closed interval [min, max]. NaN values fail the bound check (no NaN is "within" any range). An empty span is vacuously bounded.

public static bool IsBounded(ReadOnlySpan<double> values, double min, double max)

Parameters

values ReadOnlySpan<double>

Sequence to inspect.

min double

Lower bound, inclusive. Must not be NaN.

max double

Upper bound, inclusive. Must not be NaN, and must satisfy max >= min.

Returns

bool

true if every value is in [min, max].

Remarks

NaN bounds are rejected at entry. IEEE 754 makes every comparison against NaN return false, which would let any sequence pass the bound check vacuously and silently invert the method's contract. Validation happens before the loop so the caller sees the failure even on empty input.

Exceptions

ArgumentOutOfRangeException

min or max is NaN.

ArgumentException

max is less than min.

IsCauchyConvergent(ReadOnlySpan<double>, double)

Returns true when the last two values of the sequence are within tolerance of each other. A single-step approximation of the Cauchy criterion: in the formal definition, the sequence is Cauchy when |a_m - a_n| is arbitrarily small for all sufficiently large m, n. The two-tail check is the practical proxy for "the iteration has stopped meaningfully changing"; sufficient for unit tests of convergent algorithms, not a substitute for analytic proof. Empty and single-element spans return true vacuously.

public static bool IsCauchyConvergent(ReadOnlySpan<double> values, double tolerance)

Parameters

values ReadOnlySpan<double>

Sequence to inspect.

tolerance double

Maximum allowed absolute difference between the last two values. Must be non-negative and not NaN.

Returns

bool

true if the last two values are within tolerance.

Exceptions

ArgumentOutOfRangeException

tolerance is NaN or negative.

IsGeometricProgression(ReadOnlySpan<double>, double)

Returns true when adjacent ratios are equal within tolerance; that is, the sequence is a geometric progression with a common ratio. Returns false when any divisor is zero (the ratio is undefined). Empty and single-element spans are vacuously progressions.

public static bool IsGeometricProgression(ReadOnlySpan<double> values, double tolerance)

Parameters

values ReadOnlySpan<double>

Sequence to inspect.

tolerance double

Maximum allowed deviation between adjacent ratios. Must be non-negative and not NaN.

Returns

bool

true if the sequence has a (tolerance-stable) common ratio.

Exceptions

ArgumentOutOfRangeException

tolerance is NaN or negative.

IsMonotonicallyDecreasing(ReadOnlySpan<double>)

Returns true when every value is less than or equal to the previous value (non-strict monotonicity).

public static bool IsMonotonicallyDecreasing(ReadOnlySpan<double> values)

Parameters

values ReadOnlySpan<double>

Sequence to inspect.

Returns

bool

true if non-increasing.

IsMonotonicallyIncreasing(ReadOnlySpan<double>)

Returns true when every value is greater than or equal to the previous value (non-strict monotonicity; adjacent equal values are allowed). Empty and single-element spans are vacuously monotonic.

public static bool IsMonotonicallyIncreasing(ReadOnlySpan<double> values)

Parameters

values ReadOnlySpan<double>

Sequence to inspect.

Returns

bool

true if non-decreasing.

IsSorted(ReadOnlySpan<double>)

Returns true when the sequence is sorted in ascending order. Convenience alias for IsMonotonicallyIncreasing(ReadOnlySpan<double>).

public static bool IsSorted(ReadOnlySpan<double> values)

Parameters

values ReadOnlySpan<double>

Sequence to inspect.

Returns

bool

true if sorted ascending.

IsStrictlyMonotonicallyDecreasing(ReadOnlySpan<double>)

Returns true when every value is strictly less than the previous value (adjacent equal values fail).

public static bool IsStrictlyMonotonicallyDecreasing(ReadOnlySpan<double> values)

Parameters

values ReadOnlySpan<double>

Sequence to inspect.

Returns

bool

true if strictly decreasing.

IsStrictlyMonotonicallyIncreasing(ReadOnlySpan<double>)

Returns true when every value is strictly greater than the previous value (adjacent equal values fail).

public static bool IsStrictlyMonotonicallyIncreasing(ReadOnlySpan<double> values)

Parameters

values ReadOnlySpan<double>

Sequence to inspect.

Returns

bool

true if strictly increasing.