eigenvalues method

  1. @override
List<Complex> eigenvalues()
override

Returns the eigenvalues associated to this matrix.

Eigenvalues can only be computed if the matrix is square, meaning that it must have the same number of columns and rows.

Implementation

@override
List<Complex> eigenvalues() {
  if (!isSquareMatrix) {
    throw const MatrixException(
      'Eigenvalues can be computed on square matrices only!',
    );
  }

  // From now on, we're sure that the matrix is square. If it's 1x1 or 2x2,
  // computing the roots of the characteristic polynomial is faster and more
  // precise.
  if (rowCount == 1 || rowCount == 2) {
    return characteristicPolynomial().solutions();
  }

  // For 3x3 matrices and bigger, use the "eigendecomposition" algorithm.
  final eigenDecomposition = EigendecompositionComplex(
    matrix: this,
  );

  // The 'D' matrix contains real and complex coefficients of the eigenvalues
  // so we can ignore the other 2.
  final decomposition = eigenDecomposition.decompose()[1];
  final eigenvalues = <Complex>[];

  for (var i = 0; i < decomposition.rowCount; ++i) {
    eigenvalues.add(decomposition(i, i));
  }

  return eigenvalues;
}