{"record":{"id":"3743ae0bab6e7978","repo":"TheAlgorithms/C-Sharp","slug":"the-source-matrix-is-not-square-shaped","errorCode":null,"errorMessage":"The source matrix is not square-shaped.","messagePattern":"The source matrix is not square-shaped\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/LinearAlgebra/Eigenvalue/PowerIteration.cs","lineNumber":33,"sourceCode":"    ///     </item>\n    ///     <item>\n    ///         <description>The <paramref name=\"source\" /> matrix must be square-shaped.</description>\n    ///     </item>\n    /// </list>\n    /// <param name=\"source\">Source square-shaped matrix.</param>\n    /// <param name=\"startVector\">Start vector.</param>\n    /// <param name=\"error\">Accuracy of the result.</param>\n    /// <returns>Dominant eigenvalue and eigenvector pair.</returns>\n    /// <exception cref=\"ArgumentException\">The <paramref name=\"source\" /> matrix is not square-shaped.</exception>\n    /// <exception cref=\"ArgumentException\">The length of the start vector doesn't equal the size of the source matrix.</exception>\n    public static (double Eigenvalue, double[] Eigenvector) Dominant(\n        double[,] source,\n        double[] startVector,\n        double error = 0.00001)\n    {\n        if (source.GetLength(0) != source.GetLength(1))\n        {\n            throw new ArgumentException(\"The source matrix is not square-shaped.\");\n        }\n\n        if (source.GetLength(0) != startVector.Length)\n        {\n            throw new ArgumentException(\n                \"The length of the start vector doesn't equal the size of the source matrix.\");\n        }\n\n        double eigenNorm;\n        double[] previousEigenVector;\n        double[] currentEigenVector = startVector;\n\n        do\n        {\n            previousEigenVector = currentEigenVector;\n            currentEigenVector = source.Multiply(\n                    previousEigenVector.ToColumnVector())\n                .ToRowVector();","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/LinearAlgebra/Eigenvalue/PowerIteration.cs#L15-L51","documentation":"PowerIteration.Dominant computes the dominant eigenvalue/eigenvector, an operation defined only for square matrices. If source.GetLength(0) != source.GetLength(1) it throws ArgumentException, since the iteration relies on multiplying a matrix by a vector of matching size.","triggerScenarios":"Calling Dominant(source, startVector) with a rectangular double[,] (rows != columns), e.g., a data matrix passed where a covariance/adjacency matrix was expected.","commonSituations":"Passing raw feature matrices (n x m) to eigen-decomposition instead of a derived square matrix; typos in matrix construction; reading a matrix from CSV with unequal rows/columns.","solutions":["Ensure the input matrix is square before calling Dominant","Compute the appropriate square matrix (e.g., covariance A^T*A) from non-square data","Validate dimensions at load time with source.GetLength(0) == source.GetLength(1)","Catch ArgumentException and reject the non-square matrix with a clear message"],"exampleFix":"// before\ndouble[,] m = {{1,2,3},{4,5,6}};\nPowerIteration.Dominant(m, new double[2]); // throws\n// after\ndouble[,] m = {{1,2},{4,5}};\nPowerIteration.Dominant(m, new double[]{1,0});","handlingStrategy":"validation","validationCode":"if (source == null || source.GetLength(0) != source.GetLength(1)) throw new ArgumentException(\"Dominant requires a square matrix\");","typeGuard":"bool IsSquare(double[,] m) => m.GetLength(0) == m.GetLength(1);","tryCatchPattern":"try { var (value, vector) = PowerIteration.Dominant(m, start); }\ncatch (ArgumentException ex) { throw new InvalidDataException(\"Expected square matrix\", ex); }","preventionTips":["Only pass adjacency/covariance matrices (square by construction) to eigen routines","Validate matrix shape right after loading from file/CSV","Keep raw rectangular data separate from square operator matrices"],"tags":["csharp","linear-algebra","eigenvalue","matrix"],"backgroundTag":"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"}