uz_matrix_sum#

Calculates \(\boldsymbol{C}=\boldsymbol{A} + \boldsymbol{B}\) if dimensions of \(\boldsymbol{A}\), \(\boldsymbol{B}\) and \(\boldsymbol{C}\) are equal.

Warning

Do not call this function with A and B having the same pointer to prevent aliasing issues.

void uz_matrix_sum(uz_matrix_t const *const A, uz_matrix_t const *const B, uz_matrix_t *const C_out)#

Calculates C_out = A + B.

Parameters:
  • A – Pointer to a uz_matrix_t instance

  • B – Pointer to a uz_matrix_t instance

  • C_out – Sum of A and B is written to C_out

Example#

\[\begin{split}\begin{bmatrix} 1 & 2\\ 3 & 4 \end{bmatrix} + \begin{bmatrix} 1 & 2\\ 3 & 4 \end{bmatrix} = \begin{bmatrix} 2 & 4\\ 6 & 8 \end{bmatrix}\end{split}\]
 1#ifdef TEST
 2
 3#include "unity.h"
 4#include "test_assert_with_exception.h"
 5#include "uz_matrix.h"
 6#include "uz_matrix_init_helper.h"
 7
 8void setUp(void)
 9{
10}
11
12void tearDown(void)
13{
14}
15
16void test_uz_matrix_matrix_add_column_vector(void){
17    uint32_t rows=1;
18    uint32_t columns=4;
19    float A_data[4]={1,2,3,4};
20    float B_data[4]={1,2,3,4};
21    float C_data[4]={113};
22    uz_matrix_t* A=init_array_test_helper(A_data,UZ_MATRIX_SIZE(A_data),rows, columns );
23    uz_matrix_t* B=init_array_test_helper(B_data,UZ_MATRIX_SIZE(B_data),rows, columns );
24    uz_matrix_t* C=init_array_test_helper(C_data,UZ_MATRIX_SIZE(C_data),rows, columns );
25    // C=A + B
26    uz_matrix_sum(A,B, C);
27    TEST_ASSERT_EQUAL_FLOAT(2,uz_matrix_get_element_zero_based(C,0,0));
28    TEST_ASSERT_EQUAL_FLOAT(4,uz_matrix_get_element_zero_based(C,0,1));
29    TEST_ASSERT_EQUAL_FLOAT(6,uz_matrix_get_element_zero_based(C,0,2));
30    TEST_ASSERT_EQUAL_FLOAT(8,uz_matrix_get_element_zero_based(C,0,3));
31}
32
33void test_uz_matrix_matrix_add_row_vector(void){
34    uint32_t rows=4;
35    uint32_t columns=1;
36    float A_data[4]={1,2,3,4};
37    float B_data[4]={1,2,3,4};
38    float C_data[4]={1523};
39    uz_matrix_t* A=init_array_test_helper(A_data,UZ_MATRIX_SIZE(A_data),rows, columns );
40    uz_matrix_t* B=init_array_test_helper(B_data,UZ_MATRIX_SIZE(B_data),rows, columns );
41    uz_matrix_t* C=init_array_test_helper(C_data,UZ_MATRIX_SIZE(C_data),rows, columns );
42    // C=A + B
43    uz_matrix_sum(A,B, C);
44    TEST_ASSERT_EQUAL_FLOAT(2,uz_matrix_get_element_zero_based(C,0,0) );
45    TEST_ASSERT_EQUAL_FLOAT(4,uz_matrix_get_element_zero_based(C,1,0) );
46    TEST_ASSERT_EQUAL_FLOAT(6,uz_matrix_get_element_zero_based(C,2,0) );
47    TEST_ASSERT_EQUAL_FLOAT(8,uz_matrix_get_element_zero_based(C,3,0) );
48}
49
50void test_uz_matrix_matrix_add_2_times2(void){
51    uint32_t rows=2;
52    uint32_t columns=2;
53    float A_data[4]={1,2,3,4};
54    float B_data[4]={1,2,3,4};
55    float C_data[4]={4286};
56    uz_matrix_t* A=init_array_test_helper(A_data,UZ_MATRIX_SIZE(A_data),rows, columns );
57    uz_matrix_t* B=init_array_test_helper(B_data,UZ_MATRIX_SIZE(B_data),rows, columns );
58    uz_matrix_t* C=init_array_test_helper(C_data,UZ_MATRIX_SIZE(C_data),rows, columns );
59    // C=A + B
60    uz_matrix_sum(A,B, C);
61    TEST_ASSERT_EQUAL_FLOAT(2,uz_matrix_get_element_zero_based(C,0,0) );
62    TEST_ASSERT_EQUAL_FLOAT(4,uz_matrix_get_element_zero_based(C,0,1) );
63    TEST_ASSERT_EQUAL_FLOAT(6,uz_matrix_get_element_zero_based(C,1,0) );
64    TEST_ASSERT_EQUAL_FLOAT(8,uz_matrix_get_element_zero_based(C,1,1) );
65}
66
67#endif // TEST