isIdentity method

  1. @override
bool isIdentity()
override

The identity matrix is a square matrix with ones on the main diagonal and zeros elsewhere. It is denoted by In, or simply by I.

A MatrixException object is thrown if the matrix isn't square.

Implementation

@override
bool isIdentity() {
  if (!isSquareMatrix) {
    throw const MatrixException('The matrix is not square!');
  }

  for (var i = 0; i < rowCount; i++) {
    for (var j = 0; j < columnCount; j++) {
      if ((i != j) && (this(i, j) != 0)) {
        return false;
      }

      if ((i == j) && (this(i, j) != 1)) {
        return false;
      }
    }
  }

  return true;
}