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 all zeroes and the diagonal is filled with ones.

Implementation

Matrix({
  required int rows,
  required int columns,
  required T defaultValue,
  required T identityOneValue,
  bool identity = false,
})  : rowCount = rows,
      columnCount = columns {
  // 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.');
  }

  // Creating a new FIXED length list
  _data = List<T>.filled(rows * columns, defaultValue);

  // Exposing data to the outside in read-only mode
  flattenData = UnmodifiableListView<T>(_data);

  // 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;
    }
  }
}