How is one size size of an array measured at runtime?

I am working on a function to determine the distribution entropy. He uses copula if they are familiar with this. I need to sum the values ​​in an array based on which "care".

Example: consider the following example ...

  Dimension 0 (across)
 _ _ _ _ _ _ _ _ _ _ _ _ _ _
 | _ 0 _ | _ 0 _ | _ 0 _ | _ 2 _ |  Dimension 1
 | _ 1 _ | _ 0 _ | _ 2 _ | _ 0 _ |  (down)
 | _ 0 _ | _ 3 _ | _ 0 _ | _ 6 _ |
 | _ 0 _ | _ 0 _ | _ 0 _ | _ 0 _ |

 I "care about" dimension 0 only, and "don't care" about the rest (dim 1).
 Summing this array with the above specifications will
 "collapse" the "stacks" of dimension 1 down to a single 4 x 1 array:

 _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
 | _ 1 _ | _ 3 _ | _ 2 _ | _ 8 _ |

 This can then be summed, or have any operation performed.

I need to do this with an array of "n", which can be 20. In addition, I need to be able to do this, take care of some dimensions and collapse the rest. It’s especially hard for me because I can’t imagine 20 dimensions: p. If anyone could help me create c / C ++ code to collapse / summarize, I would be very grateful.

Update:

Just got home. Here is some information to answer your questions:

  • Sorry to discard the changes, I was hoping that when I click on the roll-back, it will show me the changes so that I can see what I messed up, a bit like Wikipedia. This was not the way I found out.
  • @jeff - What doesn't make sense? I use this great service for (as I think) a legitimate reason. I want to practice my hobby, and that's it, since I'm in high school. Many of my posts consider implementing a genetic algorithm (this is a message, sparsearray, array ranking, pointer manipulation).
  • I use a sparse representation of the array, since it is possible to exceed the number of molecules in the universe using a traditional (dense) array. At the moment, the implementation of sparsearray alone does not really matter, since I am working on making it work with a standard array before moving on to a sparse representation. For those who have not seen my previous questions, I use the binary search tree as a structure to contain sparse points of the array, and the “driver” function to traverse the tree as needed, returning all the functions that are designed to work. This is flexible, so I can host many different ways to access the array.
  • The structure is a hypercube, and the number of measurements is indicated at runtime, as well as the length of each measurement (which are all the same, since this is a hypercube).

Thanks everyone for your imput.

+4
source share
10 answers

This may have applications. Lets say you implemented a 2D game Conway Life (which defines a 2D plane, 1 for live, 0 for dead), and you saved a Game history for each iteration (which then defines a 3D cube). If you wanted to know how many bacteria were alive in history, you would use the above algorithm. You can use the same algorithm for the 3D version (and 4D, 5D, etc.) of the Game of Life Grid.

I would say that this was a recursion question, I am not a C programmer yet, but I know that this is possible in C. In python

def iter_arr(array): sum = 0 for i in array: if type(i) == type(list()): sum = sum + iter_arr(i) else: sum = sum + i return sum 
  • Iterate over each item in an array
  • If the element is another array, call the function again
  • If the item is not an array, add it to the sum
  • Refundable amount

Then you apply this to each element in the “caring” dimension.

This is easier in python due to duck input, though ...

+2
source

@Jeff

I really think this is an interesting question. I'm not sure how useful this is, but this is the right question.

@Ed

Can you provide a little more information on this? You said that the size of the array is dynamic, but also the number of dynamic elements?

EDITOR: I’ll try to answer the question anyway. I can’t give you the code from the top of my head (it will take some time to get this right without the compiler here on this PC), but I can point you in the right direction ...

As an example, you can use 8 dimensions (0-7) with indices from 0 to 3. You only care about 1,2 and 6. This means that you have two arrays. First, array_care[4][4][4] for 1,2 and 6. array_care[4][4][4] will contain the final result.

Next, we want to iterate in a very specific way. We have an input[4][4][4][4][4][4][4][4] array for analysis, and we take care of sizes 1, 2 and 6.

We need to define some temporary indexes:

 int dim[8] = {0,0,0,0,0,0,0,0}; 

We also need to keep the order in which we want to increase the indices:

 int increase_index_order[8] = {7,5,4,3,0,6,2,1}; int i = 0; 

This order is important to fulfill your requests.

Define a completion flag:

 bool terminate=false; 

Now we can create our loop:

 while (terminate) { array_care[dim[1]][dim[2]][dim[6]] += input[dim[0]][dim[1]][dim[2]][dim[3]][dim[4]][dim[5]][dim[6]][dim[7]]; while ((dim[increase_index_order[i]] = 3) && (i < 8)) { dim[increase_index_order[i]]=0; i++; } if (i < 8) { dim[increase_index_order[i]]++; i=0; } else { terminate=true; } } 

This should work on 8 dimensions, taking care of 3 dimensions. It takes a little longer to make it dynamic, and I don't have time. Hope this helps. I apologize, but have not yet studied the code. :(

+2
source

This view is much simpler if you use STL containers, or perhaps Boost.MultiArray . But if you have to use an array:

 #include <iostream> #include <boost/foreach.hpp> #include <vector> int sum(int x) { return x; } template <class T, unsigned N> int sum(const T (&x)[N]) { int r = 0; for(int i = 0; i < N; ++i) { r += sum(x[i]); } return r; } template <class T, unsigned N> std::vector<int> reduce(const T (&x)[N]) { std::vector<int> result; for(int i = 0; i < N; ++i) { result.push_back(sum(x[i])); } return result; } int main() { int x[][2][2] = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } }; BOOST_FOREACH(int v, reduce(x)) { std::cout<<v<<"\n"; } } 
+2
source

Actually, by comparing the quantities that you have already summed, so the measurement does not matter for your example. Did I miss or do something?

0
source

I think the best that can be done here would be one of two things:

  • To rethink the design, if it is too complicated, find a less complicated way.
  • Stop trying to visualize this.: P Just store the appropriate sizes that you need to summarize, and then do them one at a time. Once you have the basic code, look at improving the efficiency of your algorithm.
0
source

I ask you to distinguish, there is ALWAYS another way.

And if you really cannot reorganize, then you need to break the problem down into smaller parts. As I said, set which measurements you need to summarize, and then click them one at a time.

Also stop changing changes, they fix your spelling mistakes, they try to help you;)

0
source

When you say you don't know how many dimensions exist, how exactly do you define data structures?

At some point, someone needs to create this array, and for this they need to know the size of the array. You can force the creator to pass this data along with the array.

If the question is not to define such a data structure ...

0
source

You do this in c / C ++ ... so you have an array of an array of the array ... you do not need to visualize 20 dimensions, since this is not how the data is laid out in memory, for two-dimensional:

 [1] --> [1,2,3,4,5,6,...] [2] --> [1,2,3,4,5,6,...] [3] --> [1,2,3,4,5,6,...] [4] --> [1,2,3,4,5,6,...] [5] --> [1,2,3,4,5,6,...] . . . . . . 

So why can't you iterate over the first, summing up its contents? If you are trying to find size, then sizeof(array)/sizeof(int) is a risky approach. You need to know the dimension in order to be able to process this data, and set the memory up so that you know the recursion depth for the summation. Here is some kind of pseudo code of what you should do,

 sum( n_matrix, depth ) running_total = 0 if depth = 0 then foreach element in the array running_total += elm else foreach element in the array running_total += sum( elm , depth-1 ) return running_total 
0
source
 x = number_of_dimensions; while (x > 1) { switch (x) { case 20: reduce20DimensionArray(); x--; break; case 19: ..... } } 

(Sorry, could not resist.)

0
source

If I understand correctly, you want to summarize all the values ​​in the cross section defined in each "hopper" along 1 dimension. I suggest creating a 1D array for your destination, and then scroll through each element in your array by adding a value to the destination with the index of the dimension of interest.

If you use an arbitrary number of dimensions, you should have a way to address the elements (I would be interested in how you implement this). Your implementation of this will affect how you set the target index. But the obvious way would be with if checked in iteration loops.

0
source

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


All Articles