{"record":{"id":"f66977d3d0b39f28","repo":"TheAlgorithms/C-Sharp","slug":"matrix-of-equation-coefficients-is-not-square-shaped","errorCode":null,"errorMessage":"Matrix of equation coefficients is not square shaped.","messagePattern":"Matrix of equation coefficients is not square shaped\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Numeric/Decomposition/LU.cs","lineNumber":77,"sourceCode":"            }\n        }\n\n        return (L: lower, U: upper);\n    }\n\n    /// <summary>\n    ///     Eliminates linear equations system represented as A*x=b, using LU-decomposition,\n    ///     where A - matrix of equation coefficients, b - vector of absolute terms of equations.\n    /// </summary>\n    /// <param name=\"matrix\">Matrix of equation coefficients.</param>\n    /// <param name=\"coefficients\">Vector of absolute terms of equations.</param>\n    /// <returns>Vector-solution for linear equations system.</returns>\n    /// <exception cref=\"ArgumentException\">Matrix of equation coefficients is not square shaped.</exception>\n    public static double[] Eliminate(double[,] matrix, double[] coefficients)\n    {\n        if (matrix.GetLength(0) != matrix.GetLength(1))\n        {\n            throw new ArgumentException(\"Matrix of equation coefficients is not square shaped.\");\n        }\n\n        var pivot = matrix.GetLength(0);\n        var upperTransform = new double[pivot, 1]; // U * upperTransform = coefficients\n        var solution = new double[pivot]; // L * solution = upperTransform\n        (double[,] l, double[,] u) = Decompose(matrix);\n\n        for (var i = 0; i < pivot; i++)\n        {\n            double pivotPointSum = 0;\n\n            for (var j = 0; j < i; j++)\n            {\n                pivotPointSum += upperTransform[j, 0] * l[i, j];\n            }\n\n            upperTransform[i, 0] = (coefficients[i] - pivotPointSum) / l[i, i];\n        }","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Numeric/Decomposition/LU.cs#L59-L95","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nlu.Eliminate(augmented, null); // augmented is n x (n+1): throws\n// after\nint n = augmented.GetLength(0);\nvar a = new double[n, n];\nvar b = new double[n];\nfor (int i = 0; i < n; i++)\n{\n    for (int j = 0; j < n; j++) a[i, j] = augmented[i, j];\n    b[i] = augmented[i, n];\n}\nvar x = lu.Eliminate(a, b);","handlingStrategy":"validation","validationCode":"if (matrix.GetLength(0) != matrix.GetLength(1))\n    throw new ArgumentException(\"Eliminate requires a square coefficient matrix\");\nif (coefficients == null || coefficients.Length != matrix.GetLength(0))\n    throw new ArgumentException(\"coefficients length must match matrix size\");","typeGuard":"static bool IsSquareSystem(double[,] m, double[] b) =>\n    m != null && b != null && m.GetLength(0) == m.GetLength(1) && b.Length == m.GetLength(0);","tryCatchPattern":"try\n{\n    var x = LU.Eliminate(matrix, coefficients);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"not square\"))\n{\n    // split augmented matrix or reject input\n}","preventionTips":["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."],"tags":["matrix","linear-algebra","argument-validation","csharp"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}