minor method

  1. @override
RealMatrix minor(
  1. int row,
  2. int col
)
override

A minor of a matrix A is the determinant of some smaller square matrix, cut down from A by removing one or more of its rows and columns.

This function only computes the first minor, which is the minor obtained by only removing 1 row and 1 column from the source matrix. This is often useful to calculate cofactors.

A MatrixException object is thrown in a few cases:

  • row or col is smaller than zero;
  • row or col are higher than, respectively, the total row or column count;
  • row or col is 1.

Implementation

@override
RealMatrix minor(int row, int col) {
  if (row < 0 || col < 0) {
    throw const MatrixException('The arguments must be positive!');
  }

  if (row > rowCount || col > columnCount) {
    throw const MatrixException('The given (row; col) pair is invalid.');
  }

  if (rowCount == 1 || columnCount == 1) {
    throw const MatrixException(
      'Cannot compute minors when "rowCount" or "columnCount" is 1.',
    );
  }

  final source = List<List<double>>.generate(
    rowCount - 1,
    (_) => List<double>.generate(columnCount - 1, (_) => 0.0),
  );

  for (var i = 0; i < rowCount; ++i) {
    for (var j = 0; i != row && j < columnCount; ++j) {
      if (j != col) {
        final minorRow = i < row ? i : i - 1;
        final minorCol = j < col ? j : j - 1;

        source[minorRow][minorCol] = this(i, j);
      }
    }
  }

  return RealMatrix.fromData(
    rows: rowCount - 1,
    columns: columnCount - 1,
    data: source,
  );
}