Note that there are some explanatory texts on larger screens.

plurals
  1. POarray: convert index of one dimensional array to a vector index of a multidimensional array
    text
    copied!<p>It will be a long question, please, take a deep breath before reading.</p> <p>I want to understand what would be the fastest algorithm to convert index of one dimensional array to a vector index of a multidimensional array.</p> <p>Let's proceed with an example to understand why do I need it:</p> <blockquote> <p>I have a 2 dimensional array: Array[i1][i2]</p> <p>i1 runs from i1_b=0 to i1_e=2</p> <p>i2 runs from i2_b=0 to i2_e=1</p> </blockquote> <p>So this array is outputted into the file line by line:</p> <blockquote> <p>Array[0][0]</p> <p>Array[0][1]</p> <p>Array[0][2]</p> <p>Array[1][0]</p> <p>Array[1][1]</p> <p>Array[1][2]</p> </blockquote> <p>Now I read the file line by line and index k is the number of the line being read last. </p> <blockquote> <p>I read the first line which is Array[0][0] and k=0</p> <p>I read the second line which is Array[0][1] and k=1</p> <p>...</p> </blockquote> <p>One can notice that k will run from k_b=0 to k_e=5 and </p> <blockquote> <p>k=0 will correspond to i1=0, i2=0</p> <p>k=1 will correspond to i1=0, i2=1</p> <p>...</p> </blockquote> <p>Problem: So my problem is how to convert k into i1 and i2 the fastest way possible? (I don't need it while reading the file, but later in my program)</p> <p>In this example, one of the solutions would be</p> <blockquote> <p>i1=k/(i1_e - i1_b + 1);</p> <p>i2=k%(i1_e - i1_b + 1);</p> </blockquote> <p>Question 1: Is it the fastest possible solution in term of cycles and computer time?</p> <p>OK. Question 2: How can we generalize this algorithm to multidimensional arrays?</p> <blockquote> <p>Array[i1][i2][i3][i4]</p> <p>i1=k/(i1_e - i1_b + 1);</p> <p>i2=k%(i1_e - i1_b + 1);</p> <p>i3=i2/(i1_e - i1_b + 1);</p> <p>i4=i2%(i1_e - i1_b + 1);</p> </blockquote> <p>Question 3: Is it the fastest way to do it?</p> <p>Question 4: related question would be what is the latency for modular division, integer division, adding integers and multiplying integers? If these numbers depend on the architecture, please, also let me know.</p> <p>Thanks in advance!</p> <p>P.S. It may be easier for someone to think about this problem as the fastest algorithm to convert seconds into days-hours-minutes-seconds.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload