performFunction method

Matrix performFunction(
  1. dynamic function(
    1. double
    )
)

Perform a function on the matrix

  • function The function to perform
  • Returns The new matrix
  • Example
Matrix matrix = Matrix(2, 2);
Matrix newMatrix = matrix.performFunction((a) => a * 2);
  • Note The function should take a double as input and return a double

Implementation

Matrix performFunction(Function(double) function) {
  Matrix newMatrix = Matrix(_row, _col);
  for (int i = 0; i < _row; i++) {
    for (int j = 0; j < _col; j++) {
      double result = function(getAt(i, j));
      newMatrix.setAt(i, j, value: result);
    }
  }
  return newMatrix;
}