hadamardProduct static method

Matrix hadamardProduct(
  1. Matrix a,
  2. Matrix b
)

Return Hadamard product of a and b,

Example:

final a = Matrix.fromLists([[1,2], [2,1]]); // [[1, 2]
                                            // [2, 1]]
final b = Matrix.fromLists([[1,2], [3,4]]); // [[1, 2]
                                            // [3, 4]]
final matrix = MatrixOperation.hadamardProduct(a, b); // or a%b
print(matrix);
// Output:
// matrix 2тип2
// [[1.0, 4.0]
// [6.0, 4.0]]

Throw an exception if dimension's condition not met

Implementation

static Matrix hadamardProduct(Matrix a, Matrix b) {
  if (a.n == b.n && a.m == b.m) {
    var resultMatrix = Matrix.zero(n: a.n, m: b.m);
    for (int i = 0; i < resultMatrix.n; i += 1) {
      for (int j = 0; j < resultMatrix.m; j += 1) {
        resultMatrix[i][j] = a[i][j] * b[i][j];
      }
    }
    return resultMatrix;
  } else {
    throw Exception(
        'Dimensions error: dim(A) != dim(B)  |(${a.n}, ${a.m}) != (${b.n},${b.m})|');
  }
}