Multidimensional arrays structure in memory [duplicate] - c++

This question already has answers here:
Multi-dimensional array initialization
(3 answers)
Closed 7 years ago.
I've seen that these types of array are sometimes seen as a matrix. I had a teacher who said that this was an easy way to look at it, and that the real way it looks is in a linear form. For example:
int a[2][3][2] = {0, 1, 2, 3 , 4, 5, 6, 7 , 8, 9, 10, 11};
How would you represent this in a linear way?

As far as memory layout is concerned,
int a[2][3][2] = { { { 0, 1 }, { 2, 3 }, { 4, 5 } },
{ { 6, 7 }, { 8, 9 }, { 10, 11 } } };
is the same as:
int b[12] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
and a[i][j][k] is equivalent to b[i * 6 + j * 2 + k].

For your three-dimensional example, you can use an array b of size 2*3*2 = 12 and access the former element a[i][j][k] via b[k + 2 * ( j + 3* i)].
Alternatively, any other rearrangement will do as well, for example b[i + 2 * ( j + 3* k)]. The optimal choice depends on how you mainly want to traverse the array.
In general, one can translate any array
a[0 ... N_1-1][0 ... N_2-1] ... [0 ... N_k-1]
into a linear array b of size N_1 * N_2 * ... * N_k. In practice, the only thing you have to change is the index function: so when you want to access element a[i_1][i_2] ... [i_k] in your original array, you use the following instead
b[i_k + N_k * ( i_{k-1} + N_{k-1} * ( ... N_2 * i_1) ...))]

An array is a number of elements, stored consecutively in memory.
A 2D array is an array of arrays.
As such, a 2D array is a number of arrays, stored consecutively in memory. It follows that all the elements of the 2D array are stored consecutively in memory.
In your concrete example, the compiler will allocate memory in this structure:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
------- ------- ------- ------- ------- ---------
line 0 line 1 line 2 line 3 line 4 line 5
----------------------- -------------------------
block 0 block 1
-------------------------------------------------
3D array

Related

Pointer to portions of array

I have an object of std::vector<std::array<double, 16>>
vector entry Data
[0] - 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[1] - 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[2] - 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[...] - 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
This is intended to represent a 4x4 matrix in ravel format.
To not duplicate information I would like to create a pointer to extract a 3x3 from the above structure:
I have mathematical operations for the 3x3 structure (std::array<double, 9>)
someStructure: pointing to data elements [0, 1, 2, 4, 5, 6, 8, 9 10]
The end goal is do: std::array<double, 9> tst = someStructure[0] + someStructure[1];
Is this doable?
Best Regards
The 3x3 part is not contiguous, hence a pointer alone wont help here.
You can write a view_as_3x3 that allows you to access elements of the submatrix of the 4x4 as if it was contiguous:
struct view_as_3x3 {
double& operator[](size_t index) {
static const size_t mapping[] = {0, 1, 2, 4, 5, 6, 8, 9, 10};
return parent[mapping[index]];
}
std::array<double, 16>& parent;
};
Such that for example
for (size_t = 0; i< 9; ++i) std::cout << " " << view_as_3x3{orignal_matrix}[i];
is printing the 9 elements of the 3x3 sub-matrix of the original 4x4 original_matrix.
Then you could more easily apply your 3x3 algorithms to the 3x3 submatrix of a 4x4 matrix. You just need to replace the std::array<double, 9> with some generic T. For example change
double sum_of_elements(const std::array<double, 9>& arr) {
double res = 0;
for (int i=0;i <9; ++i) res += arr[i];
return res;
}
To:
template <typename T>
double sum_of_elements(const T& arr) {
double res = 0;
for (int i=0;i <9; ++i) res += arr[i];
return res;
}
The calls are then
std::array<double, 16> matrix4x4;
sum_of_elements(view_as_3x3{matrix4x4});
// or
std::array<double, 9> matrix3x3;
sum_of_elements(matrix3x3);
It would be nicer to use iterators instead of indices, however, writing the view with custom iterators requires considerable amount of boilerplate. On the other hand, I would not suggest to use naked std::arrays in the first place, but rather some my_4x4matrix that holds the array as member and provides iterators and more convenience methods.

C++ Sort vector by index

I need to sort a std::vector by index. Let me explain it with an example:
Imagine I have a std::vector of 12 positions (but can be 18 for example) filled with some values (it doesn't have to be sorted):
Vector Index: 0 1 2 3 4 5 6 7 8 9 10 11
Vector Values: 3 0 2 3 2 0 1 2 2 4 5 3
I want to sort it every 3 index. This means: the first 3 [0-2] stay, then I need to have [6-8] and then the others. So it will end up like this (new index 3 has the value of previous idx 6):
Vector Index: 0 1 2 3 4 5 6 7 8 9 10 11
Vector Values: 3 0 2 1 2 2 3 2 0 4 5 3
I'm trying to make it in one line using std::sort + lambda but I can't get it. Also discovered the std::partition() function and tried to use it but the result was really bad hehe
Found also this similar question which orders by odd and even index but can't figure out how to make it in my case or even if it is possible: Sort vector by even and odd index
Thank you so much!
Note 0: No, my vector is not always sorted. It was just an example. I've changed the values
Note 1: I know it sound strange... think it like hte vecotr positions are like: yes yes yes no no no yes yes yes no no no yes yes yes... so the 'yes' positions will go in the same order but before the 'no' positions
Note 2: If there isn't a way with lambda then I thought making it with a loop and auxiliar vars but it's more ugly I think.
Note 3: Another example:
Vector Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Vector Values: 3 0 2 3 2 0 1 2 2 4 5 3 2 3 0 0 2 1
Sorted Values: 3 0 2 1 2 2 2 3 0 3 2 0 4 5 3 0 2 1
The final Vector Values is sorted (in term of old index): 0 1 2 6 7 8 12 13 14 3 4 5 9 10 11 15 16 17
You can imagine those index in 2 colums, so I want first the Left ones and then the Right one:
0 1 2 3 4 5
6 7 8 9 10 11
12 13 14 15 16 17
You don't want std::sort, you want std::rotate.
std::vector<int> v = {20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31};
auto b = std::next(std::begin(v), 3); // skip first three elements
auto const re = std::end(v); // keep track of the actual end
auto e = std::next(b, 6); // the end of our current block
while(e < re) {
auto mid = std::next(b, 3);
std::rotate(b, mid, e);
b = e;
std::advance(e, 6);
}
// print the results
std::copy(std::begin(v), std::end(v), std::ostream_iterator<int>(std::cout, " "));
This code assumes you always do two groups of 3 for each rotation, but you could obviously work with whichever arbitrary ranges you wanted.
The output looks like what you'd want:
20 21 22 26 27 28 23 24 25 29 30 31
Update: #Blastfurnace pointed out that std::swap_ranges would work as well. The rotate call can be replaced with the following line:
std::swap_ranges(b, mid, mid); // passing mid twice on purpose
With the range-v3 library, you can write this quite conveniently, and it's very readable. Assuming your original vector is called input:
namespace rs = ranges;
namespace rv = ranges::views;
// input [3, 0, 2, 3, 2, 0, 1, 2, 2, 4, 5, 3, 2, 3, 0, 0, 2, 1]
auto by_3s = input | rv::chunk(3); // [[3, 0, 2], [3, 2, 0], [1, 2, 2], [4, 5, 3], [2, 3, 0], [0, 2, 1]]
auto result = rv::concat(by_3s | rv::stride(2), // [[3, 0, 2], [1, 2, 2], [2, 3, 0]]
by_3s | rv::drop(1) | rv::stride(2)) // [[3, 2, 0], [4, 5, 3], [0, 2, 1]]
| rv::join
| rs::to<std::vector<int>>; // [3, 0, 2, 1, 2, 2, 2, 3, 0, 3, 2, 0, 4, 5, 3, 0, 2, 1]
Here's a demo.

Extracting Columns From a Vector Matrix to store in an Array

Problem Explanation
Edit: Solution is added to the bottom in case anyone needs it
Let's Say I have a matrix with following data
1 3 2 1 7 9 5 8
9 1 3 8 6 9 4 1
3 2 4 0 4 5 7 4
3 5 6 4 6 5 7 4
I want to define a size_of_chunks variable that will store the number of chunks for example if I set size_of_chunks=2 then the matrix will become
Chunk1 | Chunk2 | Chunk3 | Chunk4
-------|---------|--------|-------
1 3 | 2 1 | 7 9 | 5 8
9 1 | 3 8 | 6 9 | 4 1
3 2 | 4 0 | 4 5 | 7 4
3 5 | 6 4 | 6 5 | 7 4
I want to take one of these chunks and use push_back to a std::vector<int>temp(row*col);
Chunk1 |
---------|
Start-> 1 3 |
9 1 |
3 2 |
End-> 3 5 |
so the temp will look like the following:
temp = {1, 3, 9, 1, 3, 2, 3, 5}
Later on when I did the same steps to chunk 2 the temp will look like this:
temp = {1, 3, 9, 1, 3, 2, 3, 5, 2, 1, 3, 8, 4, 0, 6, 4}
Code
int row = 4;
int col = 8;
int size_of_chunk = 2;
std::vector<int> A(row*col);
std::vector<int> temp(row*col);
// Here a Call for the Function to Fill A with Data
for(int r=0;r<row;r++){
for(int c=0;c<col;c+=size_of_chunk){
for(int i=0;i<c;i++){
temp.push_back(A[r*col+i]);
}
}
}
It's giving me values that doesn't match to end results how should I approach this problem ?
Thank you!
Solution
for(int c=0;c<col;c+=size_of_chunk){
for(int r=0;r<row;r++){
for(int i=0;i<size_of_chunk;i++){
temp.push_back(A[r*col+i+c]);
}
}
}
Don't specify a size for the vector in the constructor and use push_back, because that way you end up with a vector twice as big as it should be. Do one or the other.
In this case presumably it should be
std::vector<int> temp;
That is, start the vector at size zero and increase it's size using push_back.

Row-wise Element Indexing in PyTorch for C++

I am using the C++ frontend for PyTorch and am struggling with a relatively basic indexing problem.
I have an 8 by 6 Tensor such as the one below:
[ Variable[CUDAFloatType]{8,6} ]
0 1 2 3 4 5
0 1.7107e-14 4.0448e-17 4.9708e-06 1.1664e-08 9.9999e-01 2.1857e-20
1 1.8288e-14 5.9356e-17 5.3042e-06 1.2369e-08 9.9999e-01 2.4799e-20
2 2.6828e-04 9.0390e-18 1.7517e-02 1.0529e-03 9.8116e-01 6.7854e-26
3 5.7521e-10 3.1037e-11 1.5021e-03 1.2304e-06 9.9850e-01 1.4888e-17
4 1.7811e-13 1.8383e-15 1.6733e-05 3.8466e-08 9.9998e-01 5.2815e-20
5 9.6191e-06 2.6217e-23 3.1345e-02 2.3024e-04 9.6842e-01 2.9435e-34
6 2.2653e-04 8.4642e-18 1.6085e-02 9.7405e-04 9.8271e-01 6.3059e-26
7 3.8951e-14 2.9903e-16 8.3518e-06 1.7974e-08 9.9999e-01 3.6993e-20
I have another Tensor with just 8 elements in it such as:
[ Variable[CUDALongType]{8} ]
0
3
4
4
4
4
4
4
I would like to index the rows of my first tensor using the second to produce:
0
0 1.7107e-14
1 1.2369e-08
2 9.8116e-01
3 9.9850e-01
4 9.9998e-01
5 9.6842e-01
6 9.8271e-01
7 9.9999e-01
I have tried a few different approaches including index_select but it seems to produce an output that has the same dimensions as the input (8x6).
In Python I think I could index with Python's built-in indexing as discussed here: https://github.com/pytorch/pytorch/issues/1080
Unfortunately, in C++ I can only index a Tensor with a scalar (zero-dimensional Tensor) so I don't think that approach works for me here.
How can I achieve my desired result without resorting to loops?
It turns out you can do this in a couple different ways. One with gather and one with index. From the PyTorch discussions where I asked the same question:
Using torch::gather
auto x = torch::randn({8, 6});
int64_t idx_data[8] = { 0, 3, 4, 4, 4, 4, 4, 4 };
auto idx = x.type().toScalarType(torch::kLong).tensorFromBlob(idx_data, 8);
auto result = x.gather(1, idx.unsqueeze(1));
Using the C++ specific torch::index
auto x = torch::randn({8, 6});
int64_t idx_data[8] = { 0, 3, 4, 4, 4, 4, 4, 4 };
auto idx = x.type().toScalarType(torch::kLong).tensorFromBlob(idx_data, 8);
auto rows = torch::arange(0, x.size(0), torch::kLong);
auto result = x.index({rows, idx});

Longest sub-sequence the elements of which make up a set of increasing integers

Find the length of the longest continuous sub-sequence of an array the elements of which make up a set of continuous increasing integers.
The input file consists of the number n(the number of elements in the array) followed by n integers.
example input - 10 1 6 4 5 2 3 8 10 7 7
example output - 6(1 6 4 5 2 3 since they make the set 1 2 3 4 5 6).
I was able to write an algorithm that satisfies 0<n<5000 but in order to get 100 points the algorithm had to work for 0<=n<=50000.
How about something like this? Arrange the array elements in descending order, each coupled with its index-range as a local maximum (for example, A[0] = 10 would be the maximum for array indexes, [0, 10], while A[3] = 4 would be the local maximum for array indexes, [3,3]. Now traverse this list and find the longest, continuously descending sequence where the index-ranges are all contained in the starting range.
10 1 6 4 5 2 3 8 10 7 7
=> 10, [ 0,10]
8, [ 1, 7]
7, [ 9,10]
6, [ 1, 6] <--
5, [ 3, 6] | ranges
4, [ 3, 3] | all
3, [ 5, 6] | contained
2, [ 5, 5] | in [1,6]
1, [ 1, 1] <--