TheAlgorithms/C-Sharp · error · ArgumentException
Matrix of equation coefficients is not square shaped.
Error message
Matrix of equation coefficients is not square shaped.
What it means
LU.Eliminate(matrix, coefficients) solves a linear system A*x = b via LU decomposition and requires the coefficient matrix to be square (n x n). It throws ArgumentException 'Matrix of equation coefficients is not square shaped.' when matrix.GetLength(0) != matrix.GetLength(1), before delegating to Decompose.
Solutions
- Ensure matrix is n x n and pass the right-hand side as the separate double[] coefficients argument.
- If your data is an augmented n x (n+1) matrix, copy the first n columns into a square array and the last column into the coefficients vector.
- For non-square (over/underdetermined) systems use an appropriate solver (e.g. least squares), not LU.Eliminate.
Example fix
// before
lu.Eliminate(augmented, null); // augmented is n x (n+1): throws
// after
int n = augmented.GetLength(0);
var a = new double[n, n];
var b = new double[n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++) a[i, j] = augmented[i, j];
b[i] = augmented[i, n];
}
var x = lu.Eliminate(a, b); Defensive patterns
Strategy: validation
Validate before calling
if (matrix.GetLength(0) != matrix.GetLength(1))
throw new ArgumentException("Eliminate requires a square coefficient matrix");
if (coefficients == null || coefficients.Length != matrix.GetLength(0))
throw new ArgumentException("coefficients length must match matrix size"); Type guard
static bool IsSquareSystem(double[,] m, double[] b) =>
m != null && b != null && m.GetLength(0) == m.GetLength(1) && b.Length == m.GetLength(0); Try / catch
try
{
var x = LU.Eliminate(matrix, coefficients);
}
catch (ArgumentException ex) when (ex.Message.Contains("not square"))
{
// split augmented matrix or reject input
} Prevention
- Pass the RHS as a separate vector, never as an extra matrix column.
- Split augmented matrices before calling Eliminate.
- Use least-squares solvers for non-square systems instead of LU.
When it happens
Trigger: Calling Eliminate with a rectangular matrix (e.g. an n x (n+1) augmented matrix passed as the matrix argument instead of splitting the last column into coefficients); passing a matrix whose dimensions were misread from file input.
Common situations: Loading an augmented matrix from CSV and passing the whole thing as 'matrix'; building a least-squares/overdetermined system (more equations than unknowns) and trying to solve it with LU instead of least squares.
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.
- Only for num >= 0
- 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/f66977d3d0b39f28.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Numeric/Decomposition/LU.cs:77
}
}
return (L: lower, U: upper);
}
/// <summary>
/// Eliminates linear equations system represented as A*x=b, using LU-decomposition,
/// where A - matrix of equation coefficients, b - vector of absolute terms of equations.
/// </summary>
/// <param name="matrix">Matrix of equation coefficients.</param>
/// <param name="coefficients">Vector of absolute terms of equations.</param>
/// <returns>Vector-solution for linear equations system.</returns>
/// <exception cref="ArgumentException">Matrix of equation coefficients is not square shaped.</exception>
public static double[] Eliminate(double[,] matrix, double[] coefficients)
{
if (matrix.GetLength(0) != matrix.GetLength(1))
{
throw new ArgumentException("Matrix of equation coefficients is not square shaped.");
}
var pivot = matrix.GetLength(0);
var upperTransform = new double[pivot, 1]; // U * upperTransform = coefficients
var solution = new double[pivot]; // L * solution = upperTransform
(double[,] l, double[,] u) = Decompose(matrix);
for (var i = 0; i < pivot; i++)
{
double pivotPointSum = 0;
for (var j = 0; j < i; j++)
{
pivotPointSum += upperTransform[j, 0] * l[i, j];
}
upperTransform[i, 0] = (coefficients[i] - pivotPointSum) / l[i, i];
}View on GitHub (pinned to 96e2905cab)