C ++ - Definition of 2d matrices of type std :: array

I want to define two 2d matrices: f and f_transpose for the type: std::array <std::array <float, 3>, dim> and std::array <std::array <float, dim>, 3> . The value of dim is 23 .

I want the elements f[0][0] , f[1][1] , f[2][2] , f_transpose[0][0] , f_transpose[1][1] and f_transpose[2][2] were 1 , and the remaining elements were 0 .

These arrays are global variables, and I tried the following:

 static array <array <float, 3>, dim> f = {{{{1}}, {{0, 1}}, {{0, 0, 1}}}}; static array <array <float, dim>, 3> f_transpose = {{{{1, 0, 0}}, {{0, 1, 0}}, {{0, 0, 1}}}}; 

This gives me 1 in the necessary places, but some of the values ​​that should be 0 are 1 or -1 . I understand that everything that is not defined will be considered 0 , but it is clear that not what is happening. How do I solve this problem?

Edit:

I had to remove the github link provided here earlier. But for the answer (and comment) to make sense, I added the appropriate function below:

 void print_ffamily(){ cout << "F Matrix" << endl; for (int i = 0; i < 3; i++){ for (int j = 0; j < dim; j++){ printf("%0.2f ", f[i][j]); } cout << endl; } cout << "F transposed Matrix" << endl; for (int i = 0; i < dim; i++){ for (int j = 0; j < 3; j++){ printf("%0.2f ", f_transpose[i][j]); } cout << endl; } } 
+5
source share
1 answer

I am sure both are correct.

For both of you, you started list-initialization , which is aggregate initialization in this case.

For f_transpose you started the initializer for each of the 3 submatrices, the missing elements of each array are value-initialized, which means zero initialization for the float .
For f you specify initializers for 3 arrays, for their elements it is also applied above. The remaining arrays are indirectly (via value initialization, <C ++ 11) or directly (β‰₯ C ++ 11), an aggregate is initialized, the value of which is initialized and, therefore, zero initializes all their elements, because the initializer is empty.

Are you checking the content correctly?

+2
source

Source: https://habr.com/ru/post/1265850/


All Articles