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
valuesReadOnlySpan<double>Sequence to inspect.
limitdoubleExpected limit value.
tolerancedoubleMaximum allowed absolute difference between the last value and
limit. Must be non-negative and not NaN.
Returns
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
toleranceis 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
valuesReadOnlySpan<T>Sequence to inspect.
expectedintRequired length. Must be non-negative.
Returns
Type Parameters
TElement 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
expectedis 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
valuesReadOnlySpan<T>Sequence to inspect.
expectedintMinimum required length. Must be non-negative.
Returns
Type Parameters
TElement 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
expectedis 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
valuesReadOnlySpan<double>Sequence to inspect.
tolerancedoubleMaximum allowed deviation between adjacent differences. Must be non-negative and not NaN.
Returns
Exceptions
- ArgumentOutOfRangeException
toleranceis NaN or negative.
IsBounded(ReadOnlySpan<double>, double, double)
Returns true when every value is in the closed interval
[. NaN values fail the bound
check (no NaN is "within" any range). An empty span is vacuously bounded.min, max]
public static bool IsBounded(ReadOnlySpan<double> values, double min, double max)
Parameters
valuesReadOnlySpan<double>Sequence to inspect.
mindoubleLower bound, inclusive. Must not be NaN.
maxdoubleUpper bound, inclusive. Must not be NaN, and must satisfy
max >= min.
Returns
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
minormaxis NaN.- ArgumentException
maxis less thanmin.
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
valuesReadOnlySpan<double>Sequence to inspect.
tolerancedoubleMaximum allowed absolute difference between the last two values. Must be non-negative and not NaN.
Returns
Exceptions
- ArgumentOutOfRangeException
toleranceis 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
valuesReadOnlySpan<double>Sequence to inspect.
tolerancedoubleMaximum allowed deviation between adjacent ratios. Must be non-negative and not NaN.
Returns
Exceptions
- ArgumentOutOfRangeException
toleranceis 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
valuesReadOnlySpan<double>Sequence to inspect.
Returns
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
valuesReadOnlySpan<double>Sequence to inspect.
Returns
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
valuesReadOnlySpan<double>Sequence to inspect.
Returns
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
valuesReadOnlySpan<double>Sequence to inspect.
Returns
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
valuesReadOnlySpan<double>Sequence to inspect.