Matrix<T>.diagonal constructor
Matrix<T>.diagonal ({})
Creates a new N x M
matrix where rows
is N
and columns
is M
. The
matrix is filled with diagonalValue
in the main diagonal and zeroes
otherwise.
A MatrixException object is thrown if rows
or columns
is set to 0.
Implementation
Matrix.diagonal({
required int rows,
required int columns,
required T defaultValue,
required T diagonalValue,
}) : 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.');
}
// Putting the given value in the diagonal.
for (var i = 0; i < rowCount; ++i) {
final pos = columnCount * i + i;
if (pos < _data.length && i < columnCount) {
_data[pos] = diagonalValue;
}
}
}