elementWise method
applies the given binary function element-wise on this matrix and the given matrix.
other
: The matrix to element-wise apply the function with this matrix.
f
: The binary function to apply element-wise on the matrices.
Returns a new matrix containing the result of the element-wise function application.
Example:
var matrixA = Matrix([[1, 2], [3, 4]]);
var matrixB = Matrix([[2, 3], [4, 5]]);
var result = matrixA.elementWise(matrixB, (a, b) => a * b);
print(result);
// Output:
// 2 6
// 12 20
Implementation
Matrix elementWise(Matrix other, dynamic Function(dynamic, dynamic) f) {
if (rowCount != other.rowCount || columnCount != other.columnCount) {
throw Exception(
"Matrices must have the same shape for element-wise operation");
}
List<List<dynamic>> newData = List.generate(rowCount,
(i) => List.generate(columnCount, (j) => f(this[i][j], other[i][j])));
return Matrix(newData);
}