TheAlgorithms/C-Sharp · error · ArgumentException
Only for num >= 0
Error message
Only for num >= 0
What it means
GaussJordanElimination.Solve(matrix) solves a linear system via Gauss-Jordan elimination on an augmented n x (n+1) matrix. Before solving it calls CanMatrixBeUsed, which requires the matrix to be non-empty and shaped n x (n+1); otherwise it throws ArgumentException 'Please use a n*(n+1) matrix with Length > 0.'
Solutions
- Build the augmented matrix: for a system A*x = b, create an n x (n+1) array where column n holds b.
- Check the matrix is non-empty (GetLength(0) > 0 and GetLength(1) == GetLength(0) + 1) before calling Solve.
- Inspect CanMatrixBeUsed's shape rules and validate input where it is constructed, e.g. after parsing.
Example fix
// before
var solver = new GaussJordanElimination();
solver.Solve(a); // a is n x n: throws
// after
int n = a.GetLength(0);
var augmented = new double[n, n + 1];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++) augmented[i, j] = a[i, j];
augmented[i, n] = b[i];
}
solver.Solve(augmented); Defensive patterns
Strategy: validation
Validate before calling
bool usable = matrix != null
&& matrix.GetLength(0) > 0
&& matrix.GetLength(1) == matrix.GetLength(0) + 1;
if (!usable)
throw new ArgumentException("Solve requires a non-empty n x (n+1) augmented matrix"); Type guard
static bool IsAugmentedMatrix(double[,] m) =>
m != null && m.GetLength(0) > 0 && m.GetLength(1) == m.GetLength(0) + 1; Try / catch
try
{
var ok = solver.Solve(matrix);
}
catch (ArgumentException ex) when (ex.Message.Contains("n*(n+1)"))
{
// rebuild augmented matrix or reject input
} Prevention
- Always append the constants column to form the n x (n+1) augmented matrix.
- Validate shape right after parsing/loading the system.
- Reject empty matrices before calling Solve.
When it happens
Trigger: Passing a non-augmented n x n coefficient matrix instead of n x (n+1); passing an empty 0-length array (RowCount becomes 0); passing a rectangular matrix whose column count is not rows + 1.
Common situations: Forgetting to append the constants column (b) to the coefficient matrix; loading a system from CSV without the RHS column; constructing the array with new double[0,0] when the input was empty or parsing failed.
Understand the failure class
Background: Tensor shape mismatch errors ("must have shape", "expected shape ... got ..."): when tensor dimensions disagree with what an op or layer was told to expect — this error's family across 6 libraries.
Related errors
- Source matrix is not square shaped.
- Matrix of equation coefficients is not square shaped.
- The source matrix is not square-shaped.
- Invalid parameter settings for Ascon Hash
- Cash flows list cannot be empty
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/3f1781f38996e10a.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Numeric/Factorial.cs:22
/// The factorial of a positive integer n, denoted by n!,
/// is the product of all positive integers less than or equal to n.
/// </summary>
public static class Factorial
{
/// <summary>
/// Calculates factorial of a integer number.
/// </summary>
/// <param name="inputNum">Integer Input number.</param>
/// <returns>Factorial of integer input number.</returns>
public static BigInteger Calculate(int inputNum)
{
// Convert integer input to BigInteger
BigInteger num = new BigInteger(inputNum);
// Don't calculate factorial if input is a negative number.
if (BigInteger.Compare(num, BigInteger.Zero) < 0)
{
throw new ArgumentException("Only for num >= 0");
}
// Factorial of numbers greater than 0.
BigInteger result = BigInteger.One;
for (BigInteger i = BigInteger.One; BigInteger.Compare(i, num) <= 0; i = BigInteger.Add(i, BigInteger.One))
{
result = BigInteger.Multiply(result, i);
}
return result;
}
}
View on GitHub (pinned to 96e2905cab)