{"record":{"id":"d74f0c2f97eaaf13","repo":"TheAlgorithms/C-Sharp","slug":"the-length-of-the-start-vector-doesn-t-equal-the-size-of-the","errorCode":null,"errorMessage":"The length of the start vector doesn't equal the size of the source matrix.","messagePattern":"The length of the start vector doesn't equal the size of the source matrix\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/LinearAlgebra/Eigenvalue/PowerIteration.cs","lineNumber":38,"sourceCode":"    /// <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();\n\n            eigenNorm = currentEigenVector.Magnitude();\n            currentEigenVector = currentEigenVector.Select(x => x / eigenNorm).ToArray();\n        }\n        while (Math.Abs(currentEigenVector.Dot(previousEigenVector)) < 1.0 - error);","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/LinearAlgebra/Eigenvalue/PowerIteration.cs#L20-L56","documentation":"PowerIteration.Dominant repeatedly multiplies the matrix by the start vector, which requires startVector.Length to equal the matrix's row/column count. When source.GetLength(0) != startVector.Length it throws ArgumentException, guarding against dimensionally invalid matrix-vector products.","triggerScenarios":"Calling Dominant(source, startVector) on a square matrix with a start vector whose length differs from the matrix size — e.g., a length-3 initial vector for a 2x2 matrix, or a zero-length array.","commonSituations":"Reusing a start vector from a differently sized matrix; building the start vector from data of the wrong dimension; forgetting to size the initial guess after changing matrix construction.","solutions":["Size the start vector to match the matrix: new double[source.GetLength(0)]","Keep the start vector's construction tied to the same dimension variable as the matrix","Validate startVector.Length == source.GetLength(0) before calling","Catch ArgumentException and reinitialize the vector at the correct size"],"exampleFix":"// before\nvar m = new double[3,3];\nPowerIteration.Dominant(m, new double[]{1,0}); // throws\n// after\nvar m = new double[3,3];\nvar start = new double[m.GetLength(0)];\nstart[0] = 1;\nPowerIteration.Dominant(m, start);","handlingStrategy":"validation","validationCode":"if (startVector == null || startVector.Length != source.GetLength(0)) throw new ArgumentException(\"Start vector length must equal matrix size\");","typeGuard":"bool StartVectorMatches(double[,] m, double[] v) => v != null && v.Length == m.GetLength(0);","tryCatchPattern":"try { var result = PowerIteration.Dominant(m, start); }\ncatch (ArgumentException) { start = new double[m.GetLength(0)]; start[0] = 1; result = PowerIteration.Dominant(m, start); }","preventionTips":["Derive the start vector length from source.GetLength(0), never a literal","Rebuild start vectors when matrix construction changes","Sanity-check matrix/vector dimensions together in a helper before eigen calls"],"tags":["csharp","linear-algebra","eigenvalue","vector"],"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"}