TheAlgorithms/C-Sharp · error · ArgumentException

The length of the start vector doesn't equal the size of…

Error message

The length of the start vector doesn't equal the size of the source matrix.

What it means

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.

Solutions

  1. Size the start vector to match the matrix: new double[source.GetLength(0)]
  2. Keep the start vector's construction tied to the same dimension variable as the matrix
  3. Validate startVector.Length == source.GetLength(0) before calling
  4. Catch ArgumentException and reinitialize the vector at the correct size

Example fix

// before
var m = new double[3,3];
PowerIteration.Dominant(m, new double[]{1,0}); // throws
// after
var m = new double[3,3];
var start = new double[m.GetLength(0)];
start[0] = 1;
PowerIteration.Dominant(m, start);
Defensive patterns

Strategy: validation

Validate before calling

if (startVector == null || startVector.Length != source.GetLength(0)) throw new ArgumentException("Start vector length must equal matrix size");

Type guard

bool StartVectorMatches(double[,] m, double[] v) => v != null && v.Length == m.GetLength(0);

Try / catch

try { var result = PowerIteration.Dominant(m, start); }
catch (ArgumentException) { start = new double[m.GetLength(0)]; start[0] = 1; result = PowerIteration.Dominant(m, start); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/d74f0c2f97eaaf13. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/LinearAlgebra/Eigenvalue/PowerIteration.cs:38

    /// <param name="source">Source square-shaped matrix.</param>
    /// <param name="startVector">Start vector.</param>
    /// <param name="error">Accuracy of the result.</param>
    /// <returns>Dominant eigenvalue and eigenvector pair.</returns>
    /// <exception cref="ArgumentException">The <paramref name="source" /> matrix is not square-shaped.</exception>
    /// <exception cref="ArgumentException">The length of the start vector doesn't equal the size of the source matrix.</exception>
    public static (double Eigenvalue, double[] Eigenvector) Dominant(
        double[,] source,
        double[] startVector,
        double error = 0.00001)
    {
        if (source.GetLength(0) != source.GetLength(1))
        {
            throw new ArgumentException("The source matrix is not square-shaped.");
        }

        if (source.GetLength(0) != startVector.Length)
        {
            throw new ArgumentException(
                "The length of the start vector doesn't equal the size of the source matrix.");
        }

        double eigenNorm;
        double[] previousEigenVector;
        double[] currentEigenVector = startVector;

        do
        {
            previousEigenVector = currentEigenVector;
            currentEigenVector = source.Multiply(
                    previousEigenVector.ToColumnVector())
                .ToRowVector();

            eigenNorm = currentEigenVector.Magnitude();
            currentEigenVector = currentEigenVector.Select(x => x / eigenNorm).ToArray();
        }
        while (Math.Abs(currentEigenVector.Dot(previousEigenVector)) < 1.0 - error);

View on GitHub (pinned to 96e2905cab)