TheAlgorithms/C-Sharp · error · ArgumentException

The source matrix is not square-shaped.

Error message

The source matrix is not square-shaped.

What it means

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.

Solutions

  1. Ensure the input matrix is square before calling Dominant
  2. Compute the appropriate square matrix (e.g., covariance A^T*A) from non-square data
  3. Validate dimensions at load time with source.GetLength(0) == source.GetLength(1)
  4. Catch ArgumentException and reject the non-square matrix with a clear message

Example fix

// before
double[,] m = {{1,2,3},{4,5,6}};
PowerIteration.Dominant(m, new double[2]); // throws
// after
double[,] m = {{1,2},{4,5}};
PowerIteration.Dominant(m, new double[]{1,0});
Defensive patterns

Strategy: validation

Validate before calling

if (source == null || source.GetLength(0) != source.GetLength(1)) throw new ArgumentException("Dominant requires a square matrix");

Type guard

bool IsSquare(double[,] m) => m.GetLength(0) == m.GetLength(1);

Try / catch

try { var (value, vector) = PowerIteration.Dominant(m, start); }
catch (ArgumentException ex) { throw new InvalidDataException("Expected square matrix", ex); }

Prevention

When it happens

Trigger: Calling Dominant(source, startVector) with a rectangular double[,] (rows != columns), e.g., a data matrix passed where a covariance/adjacency matrix was expected.

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

Related errors


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

Appendix: source

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

    ///     </item>
    ///     <item>
    ///         <description>The <paramref name="source" /> matrix must be square-shaped.</description>
    ///     </item>
    /// </list>
    /// <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();

View on GitHub (pinned to 96e2905cab)