Matrix<T> constructor

Matrix<T>({
  1. required int rows,
  2. required int columns,
  3. required T defaultValue,
  4. required T identityOneValue,
  5. bool identity = false,
})

Creates a new N x M matrix where rows is N and columns is M. The matrix is filled with zeroes.

If identity is set to true (by default it's false) then the matrix is initialized with zeroes and the diagonal is filled with ones.

A MatrixException object is thrown if rows or columns is set to 0.

Implementation

Matrix({
  required int rows,
  required int columns,
  required T defaultValue,
  required T identityOneValue,
  bool identity = false,
})  : rowCount = rows,
      columnCount = columns,
      _data = List<T>.filled(rows * columns, defaultValue) {
  // Making sure the user entered valid dimensions for the matrix
  if ((rows == 0) || (columns == 0)) {
    throw const MatrixException('The rows or column count cannot be zero.');
  }

  // The identity matrix has 1 in the diagonal
  if (identity) {
    if (rows != columns) {
      throw const MatrixException('The identity matrix must be square.');
    }

    for (var i = 0; i < rowCount; ++i) {
      _data[columnCount * i + i] = identityOneValue;
    }
  }
}