max method

dynamic max({
  1. int? axis,
})

Returns the largest numeric value in the matrix along the specified axis.

If axis is null, the largest value in the matrix is returned. If axis is 0, a list of the largest values in each column is returned. If axis is 1, a list of the largest values in each row is returned.

Example:

var matrix = Matrix.fromList([[2, 3], [1, 4]]);

var maxValue = matrix.max();
print(maxValue); // Output: 4

var rowMaxValues = matrix.max(axis: 1);
print(rowMaxValues); // Output: [3, 4]

var colMaxValues = matrix.max(axis: 0);
print(colMaxValues); // Output: [2, 4]

Throws Exception if the matrix is empty. Throws ArgumentError if axis is not null, 0, or 1. Throws ArgumentError if the matrix contains non-numeric values.

Implementation

dynamic max({int? axis}) {
  int rows = rowCount;
  int cols = columnCount;
  if (rows == 0 || cols == 0) {
    throw Exception("Matrix is empty");
  }

  if (axis != null && axis != 0 && axis != 1) {
    throw ArgumentError(
        "Axis must be 0 (for columns), 1 (for rows), or null (for total min)");
  }

  switch (axis) {
    case 0:
      return List<dynamic>.generate(_data[0].length,
          (i) => _data.map((row) => row[i]).reduce(math.max));
    case 1:
      return _data.map((row) => row.reduce(math.max)).toList();
    default:
      dynamic maxValue = _data[0][0];

      for (var row in _data) {
        dynamic rowMax = row.reduce(math.max);
        if (rowMax > maxValue) {
          maxValue = rowMax;
        }
      }

      return maxValue;
  }
}