elementwise_product

Calculates the elementwise product / Hadamard product. Multiplies every element of \(\boldsymbol{A}\) with the respective element of \(\boldsymbol{B}\) if dimensions of \(\boldsymbol{A}\), \(\boldsymbol{B}\) and \(\boldsymbol{C}\) are equal.

\[\boldsymbol{C}=\boldsymbol{A} \odot \boldsymbol{B}\]
\[\begin{split}\begin{bmatrix} a_{11} & a_{12} & a_{1n} \\ a_{21} & a_{22} & a_{2n} \\ a_{m1} & a_{m2} & a_{mn} \end{bmatrix} \begin{bmatrix} b_{11} & b_{12} & b_{1n} \\ b_{21} & b_{22} & b_{2n} \\ b_{m1} & b_{m2} & b_{mn} \end{bmatrix} = \begin{bmatrix} a_{11} \cdot b_{11} & a_{12} \cdot b_{12} & a_{1n} \cdot b_{1n} \\ a_{21} \cdot b_{21} & a_{22} \cdot b_{22} & a_{2n} \cdot b_{2n} \\ a_{m1} \cdot b_{m1} & a_{m2} \cdot b_{m2} & a_{mn} \cdot b_{mn} \end{bmatrix}\end{split}\]
void uz_matrix_elementwise_product(uz_matrix_t const *const A, uz_matrix_t const *const B, uz_matrix_t *const C_out)

Calculates the elementwise product C_out= A .* B of all elements of the matrix A and B (also called Hadamard-Product)

Parameters:
  • A – Pointer to a uz_matrix_t instance

  • B – Pointer to a uz_matrix_t instance

  • C_out – Result of the elementwise product is written to C_out

Column vector Example

\[\begin{split}\begin{bmatrix} 1 \\ 2 \\ 3 \end{bmatrix} \odot \begin{bmatrix} 1 \\ 2 \\ 3 \end{bmatrix} = \begin{bmatrix} 1 \\ 4 \\ 9 \end{bmatrix}\end{split}\]
 1
 2void test_uz_matrix_vector_elementwise_multiplication_3_times_1(void){
 3    uint32_t rows=3;
 4    uint32_t columns=1;
 5    float A_data[3]={1,2,3};
 6    float B_data[3]={1,2,3};
 7    float C_data[3]={134513};
 8    uz_matrix_t* A=init_array_test_helper(A_data,UZ_MATRIX_SIZE(A_data),rows, columns );
 9    uz_matrix_t* B=init_array_test_helper(B_data,UZ_MATRIX_SIZE(B_data),rows, columns );
10    uz_matrix_t* C=init_array_test_helper(C_data,UZ_MATRIX_SIZE(C_data),rows, columns );
11    // C=A .* B
12    uz_matrix_elementwise_product(A,B, C);
13    TEST_ASSERT_EQUAL_FLOAT(1,uz_matrix_get_element_zero_based(C,0,0));
14    TEST_ASSERT_EQUAL_FLOAT(4,uz_matrix_get_element_zero_based(C,1,0));
15    TEST_ASSERT_EQUAL_FLOAT(9,uz_matrix_get_element_zero_based(C,2,0));

Row vector Example

\[\begin{bmatrix} 1 & 2 & 3 & 4 \end{bmatrix} \odot \begin{bmatrix} 1 & 2 & 3 & 4 \end{bmatrix} = \begin{bmatrix} 1 & 4 & 9 & 16 \end{bmatrix}\]
 1
 2void test_uz_matrix_matrix_elementwise_multiplication_1_times_4(void){
 3    uint32_t rows=1;
 4    uint32_t columns=4;
 5    float A_data[4]={1,2,3,4};
 6    float B_data[4]={1,2,3,4};
 7    float C_data[4]={21345};
 8    uz_matrix_t* A=init_array_test_helper(A_data,UZ_MATRIX_SIZE(A_data),rows, columns );
 9    uz_matrix_t* B=init_array_test_helper(B_data,UZ_MATRIX_SIZE(B_data),rows, columns );
10    uz_matrix_t* C=init_array_test_helper(C_data,UZ_MATRIX_SIZE(C_data),rows, columns );
11    // C=A .* B
12    uz_matrix_elementwise_product(A,B, C);
13    TEST_ASSERT_EQUAL_FLOAT(1, uz_matrix_get_element_zero_based(C,0,0) );
14    TEST_ASSERT_EQUAL_FLOAT(4, uz_matrix_get_element_zero_based(C,0,1) );
15    TEST_ASSERT_EQUAL_FLOAT(9, uz_matrix_get_element_zero_based(C,0,2) );
16    TEST_ASSERT_EQUAL_FLOAT(16,uz_matrix_get_element_zero_based(C,0,3) );

Matrix Example

\[\begin{split}\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \odot \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} = \begin{bmatrix} 1 & 4 \\ 9 & 16 \end{bmatrix}\end{split}\]
 1
 2void test_uz_matrix_matrix_elementwise_multiplication_2_times_2(void){
 3    uint32_t rows=2;
 4    uint32_t columns=2;
 5    // | 1 2 | .* | 1 2 | = | 1 4  |
 6    // | 3 4 |    | 3 4 |   | 9 16 |
 7    float A_data[4]={1,2,3,4};
 8    float B_data[4]={1,2,3,4};
 9    float C_data[4]={63};
10    uz_matrix_t* A=init_array_test_helper(A_data,UZ_MATRIX_SIZE(A_data),rows, columns );
11    uz_matrix_t* B=init_array_test_helper(B_data,UZ_MATRIX_SIZE(B_data),rows, columns );
12    uz_matrix_t* C=init_array_test_helper(C_data,UZ_MATRIX_SIZE(C_data),rows, columns );
13    // C=A .* B
14    uz_matrix_elementwise_product(A,B, C);
15    TEST_ASSERT_EQUAL_FLOAT(1, uz_matrix_get_element_zero_based(C,0,0));
16    TEST_ASSERT_EQUAL_FLOAT(4, uz_matrix_get_element_zero_based(C,0,1));
17    TEST_ASSERT_EQUAL_FLOAT(9, uz_matrix_get_element_zero_based(C,1,0));
18    TEST_ASSERT_EQUAL_FLOAT(16,uz_matrix_get_element_zero_based(C,1,1));