Position item in an array larger than 3D

General FreeBASIC programming questions.
dodicat
Posts: 5984
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Position item in an array larger than 3D

Postby dodicat » May 03, 2012 22:52

Thanks fxm, the loop is cleaner.
I can generalize getting the dimensions of arrays.
(not too messy, but each macro call must have a different dummy).
Perhaps you could try similar for your offset, and perhaps use GET_DIMENSION to save passing 3 parameters?
Just a thought!
viz:

Code: Select all

 

#macro GET_DIMENSION(_array,_datatype,_dimension,_temp)
function _temp(array() as _datatype) as integer
 Dim _d As Integer
    Asm
        mov esi, [ebp+8]
        mov eax, [esi+16]
        mov [_d], eax
    End Asm
return _d
end function
_dimension= _temp(_array())
    #endmacro
   
'===============================================
 dim as integer DIMENSION   
dim as double a(1 to 5,3 to 9,8 to 10)

GET_DIMENSION(a,double,DIMENSION,dummy1)
print DIMENSION
print

dim as byte b(1 to 5,4 to 9,3 to 7,2 to 6)
GET_DIMENSION(b,byte,DIMENSION,dummy2)
print dimension
print

type vector
    as single x,y
end type

dim as vector v(1 to 4,2 to 9)
GET_DIMENSION(v,vector,DIMENSION,dummy3)
print dimension
sleep
 
fxm
Posts: 9253
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Position item in an array larger than 3D

Postby fxm » May 04, 2012 15:53

Richard wrote:I assumed the array already existed and Ircvs wanted to use pointers or assembly code for access.

About this, a short example to enlighten how to access to array elements, but from pointer index operator.
3D array:
- classic access by array indexes
- access by pointer operator with 1 index
- access by pointer operator with 2 indexes
- access by pointer operator with 3 indexes

Code: Select all

#Macro Print_all_elements_with_comment(VARijk, COMMENT)
  Print "Print from " & #VARijk;
  If COMMENT <> "" then
    Print " " & COMMENT;
  End If
  Print
  For j As Integer = 0 To Uj
    For i As Integer = 0 To Ui
      For k As Integer = 0 To Uk
        Print Using "####"; VARijk;
      Next k
      Print "    ";
    Next i
    Print
  Next j
  Print
#Endmacro

Dim Ui As Integer = 2
Dim Uj As Integer = 3
Dim Uk As Integer = 4

Dim array(Ui, Uj, Uk) As Integer
  For i As Integer = 0 To Ui
    For j As Integer = 0 To Uj
      For k As Integer = 0 To Uk
        array(i, j, k) = i * 100 + j * 10 + k
      Next k
    Next j
  Next i



Print_all_elements_with_comment(array(i,j,k), "directly with array indexes")



Dim p As Integer Ptr
  p = @array(0, 0, 0)
Print_all_elements_with_comment(p[(i*(Uj+1)+j)*(Uk+1)+k], "by pointer + 1 index, with p=@array(0,0,0)")



Dim pp As Integer Ptr Ptr = Callocate(Ui + 1, Sizeof(Integer Ptr))
  For i As Integer = 0 To Ui
    pp[i] = @array(i, 0, 0)
  Next i
Print_all_elements_with_comment(pp[i][j*(Uk+1)+k], "by pointer + 2 indexes, with pp[i]=@array(i,0,0)")
 


Dim ppp As Integer Ptr Ptr Ptr = Callocate(Ui + 1, Sizeof(Integer Ptr Ptr))
  For i As Integer = 0 To Ui
    ppp[i] = Callocate(Uj + 1, Sizeof(Integer Ptr))
    For j As Integer = 0 To Uj
      ppp[i][j] = @array(i, j, 0)
    Next j
  Next i
Print_all_elements_with_comment(ppp[i][j][k], "by pointer + 3 indexes, with ppp[i][j]=@array(i,j,0)")

Sleep

Deallocate(p)

For i As Integer = 0 To Ui
  Deallocate(pp[i])
Next i
Deallocate(pp)

For i As Integer = 0 To Ui
  For j As Integer = 0 To Uj
    Deallocate(ppp[i][j])
  Next j
  Deallocate(ppp[i])
Next i
Deallocate(ppp)

Code: Select all

Print from array(i,j,k) directly with array indexes
   0   1   2   3   4     100 101 102 103 104     200 201 202 203 204
  10  11  12  13  14     110 111 112 113 114     210 211 212 213 214
  20  21  22  23  24     120 121 122 123 124     220 221 222 223 224
  30  31  32  33  34     130 131 132 133 134     230 231 232 233 234

Print from p[(i*(Uj+1)+j)*(Uk+1)+k] by pointer + 1 index, with p=@array(0,0,0)
   0   1   2   3   4     100 101 102 103 104     200 201 202 203 204
  10  11  12  13  14     110 111 112 113 114     210 211 212 213 214
  20  21  22  23  24     120 121 122 123 124     220 221 222 223 224
  30  31  32  33  34     130 131 132 133 134     230 231 232 233 234

Print from pp[i][j*(Uk+1)+k] by pointer + 2 indexes, with pp[i]=@array(i,0,0)
   0   1   2   3   4     100 101 102 103 104     200 201 202 203 204
  10  11  12  13  14     110 111 112 113 114     210 211 212 213 214
  20  21  22  23  24     120 121 122 123 124     220 221 222 223 224
  30  31  32  33  34     130 131 132 133 134     230 231 232 233 234

Print from ppp[i][j][k] by pointer + 3 indexes, with ppp[i][j]=@array(i,j,0)
   0   1   2   3   4     100 101 102 103 104     200 201 202 203 204
  10  11  12  13  14     110 111 112 113 114     210 211 212 213 214
  20  21  22  23  24     120 121 122 123 124     220 221 222 223 224
  30  31  32  33  34     130 131 132 133 134     230 231 232 233 234
dodicat
Posts: 5984
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Position item in an array larger than 3D

Postby dodicat » May 04, 2012 21:36

Hi Ircvs~fxm
If you are filling an array anyway, then surely the most simple way to allocate values to a pointer to that array is by a counter?
A simple counter can do the same as all the methods previously put forward in posts here. I have added four lines to fxm's post to include a counter.
All that is required is a reference array of integer (to count) running parallel to the array, a shadow, if you like, and a counter in the inner loop where the values are passed to the array.

Code: Select all

#Macro Print_all_elements_with_comment(VARijk, COMMENT)
  Print "Print from " & #VARijk;
  If COMMENT <> "" then
    Print " " & COMMENT;
  End If
  Print
  For j As Integer = 0 To Uj
    For i As Integer = 0 To Ui
      For k As Integer = 0 To Uk
        Print Using "####"; VARijk;
      Next k
      Print "    ";
    Next i
    Print
  Next j
  Print
#Endmacro

Dim Ui As Integer = 2
Dim Uj As Integer = 3
Dim Uk As Integer = 4

dim as integer counter
Dim array(Ui, Uj, Uk) As Integer
dim reference(Ui,Uj,Uk) as integer    '==============1)
  For i As Integer = 0 To Ui
    For j As Integer = 0 To Uj
      For k As Integer = 0 To Uk
        array(i, j, k) = i * 100 + j * 10 + k
        reference(i,j,k)=counter      '======================2)
        counter=counter+1             '==================3)
      Next k
    Next j
  Next i



Print_all_elements_with_comment(array(i,j,k), "directly with array indexes")



Dim p As Integer Ptr
  p = @array(0, 0, 0)
Print_all_elements_with_comment(p[(i*(Uj+1)+j)*(Uk+1)+k], "by pointer + 1 index, with p=@array(0,0,0)")



Dim pp As Integer Ptr Ptr = Callocate(Ui + 1, Sizeof(Integer Ptr))
  For i As Integer = 0 To Ui
    pp[i] = @array(i, 0, 0)
  Next i
Print_all_elements_with_comment(pp[i][j*(Uk+1)+k], "by pointer + 2 indexes, with pp[i]=@array(i,0,0)")
 


Dim ppp As Integer Ptr Ptr Ptr = Callocate(Ui + 1, Sizeof(Integer Ptr Ptr))
  For i As Integer = 0 To Ui
    ppp[i] = Callocate(Uj + 1, Sizeof(Integer Ptr))
    For j As Integer = 0 To Uj
      ppp[i][j] = @array(i, j, 0)
    Next j
  Next i
Print_all_elements_with_comment(ppp[i][j][k], "by pointer + 3 indexes, with ppp[i][j]=@array(i,j,0)")

print

Print_all_elements_with_comment(p[reference(i,j,k)]," by simple counter") '===============4)


Sleep

Deallocate(p)

For i As Integer = 0 To Ui
  Deallocate(pp[i])
Next i
Deallocate(pp)

For i As Integer = 0 To Ui
  For j As Integer = 0 To Uj
    Deallocate(ppp[i][j])
  Next j
  Deallocate(ppp[i])
Next i
Deallocate(ppp)
 
fxm
Posts: 9253
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Position item in an array larger than 3D

Postby fxm » May 04, 2012 22:21

Yes In this particular case, because the array elements are all initialized and in the exact order corresponding to their memory mapping (from the lower address to the higher address).

In case of any change of the initialization of the array elements, this solution is not obviously applicable.
For example by changing the order of loops on i and j indexes:

Code: Select all

#Macro Print_all_elements_with_comment(VARijk, COMMENT)
  Print "Print from " & #VARijk;
  If COMMENT <> "" then
    Print " " & COMMENT;
  End If
  Print
  For j As Integer = 0 To Uj
    For i As Integer = 0 To Ui
      For k As Integer = 0 To Uk
        Print Using "####"; VARijk;
      Next k
      Print "    ";
    Next i
    Print
  Next j
  Print
#Endmacro

Dim Ui As Integer = 2
Dim Uj As Integer = 3
Dim Uk As Integer = 4

dim as integer counter
Dim array(Ui, Uj, Uk) As Integer
dim reference(Ui,Uj,Uk) as integer    '==============1)
    For j As Integer = 0 To Uj
  For i As Integer = 0 To Ui
      For k As Integer = 0 To Uk
        array(i, j, k) = i * 100 + j * 10 + k
        reference(i,j,k)=counter      '======================2)
        counter=counter+1             '==================3)
      Next k
  Next i
    Next j



Print_all_elements_with_comment(array(i,j,k), "directly with array indexes")



Dim p As Integer Ptr
  p = @array(0, 0, 0)
Print_all_elements_with_comment(p[(i*(Uj+1)+j)*(Uk+1)+k], "by pointer + 1 index, with p=@array(0,0,0)")



Dim pp As Integer Ptr Ptr = Callocate(Ui + 1, Sizeof(Integer Ptr))
  For i As Integer = 0 To Ui
    pp[i] = @array(i, 0, 0)
  Next i
Print_all_elements_with_comment(pp[i][j*(Uk+1)+k], "by pointer + 2 indexes, with pp[i]=@array(i,0,0)")
 


Dim ppp As Integer Ptr Ptr Ptr = Callocate(Ui + 1, Sizeof(Integer Ptr Ptr))
  For i As Integer = 0 To Ui
    ppp[i] = Callocate(Uj + 1, Sizeof(Integer Ptr))
    For j As Integer = 0 To Uj
      ppp[i][j] = @array(i, j, 0)
    Next j
  Next i
Print_all_elements_with_comment(ppp[i][j][k], "by pointer + 3 indexes, with ppp[i][j]=@array(i,j,0)")

print

Print_all_elements_with_comment(p[reference(i,j,k)]," by simple counter") '===============4)


Sleep

Deallocate(p)

For i As Integer = 0 To Ui
  Deallocate(pp[i])
Next i
Deallocate(pp)

For i As Integer = 0 To Ui
  For j As Integer = 0 To Uj
    Deallocate(ppp[i][j])
  Next j
  Deallocate(ppp[i])
Next i
Deallocate(ppp)
 
Richard
Posts: 2958
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Position item in an array larger than 3D

Postby Richard » May 05, 2012 0:09

Is a counter really needed when pointers are available?

Code: Select all

'=======================================================================
' labling many dimensioned arrays
'=======================================================================
Dim As integer i, j, k, a(1 To 2, 4 To 6, 0 To 3)
Dim As Integer Ptr ba = @a(1, 4, 0)     ' base address of array
For i = 1 To 2
    For j = 4 To 6
        For k = 0 To 3
            a(i, j, k) = @a(i, j, k) - ba
            print using " ###  ###  ###  #####"; i; j; k; a(i, j, k)
        Next k
    Next j
Next i
'=======================================================================
Sleep
'=======================================================================
fxm
Posts: 9253
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Position item in an array larger than 3D

Postby fxm » May 05, 2012 5:11

Anyway, this above counting method for determine the offset of an element of an array has already been proposed earlier by me at viewtopic.php?p=173654#p173654.
lrcvs
Posts: 569
Joined: Mar 06, 2008 19:27
Location: Spain

Re: Position item in an array larger than 3D

Postby lrcvs » May 05, 2012 8:35

I understand means well.

But I do not understand is the concept of fourth, fifth, sixth, .... dimension.

For example: Z = [5 4 3 2]

mathematically speaking it would be 5 * 4 * 3 * 2 = 120 elements.

But 5 * 4 = X * Y = level 1 = 20 elements in each level.

20 * 3 = 20 * Z is the number of 3D or level 3 = 60 items.

So far, so good.

Now we have in the 4D = 2.

I understand that at level 3, the last layer, and each element has an extension of 2 more items of the fourth dimension.

So I think 20 elements of the last level * 2 extensions more = 40 elements.

I still think if 3D = 60 items + 40 items in the fouth dimension = 100 items.

No item that is the 120 mathematically speaking.

What is your concept of the following dimensions to 3D?

(the fourth, fifth, sixth ... dimension)

Your opinion?
fxm
Posts: 9253
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Position item in an array larger than 3D

Postby fxm » May 05, 2012 9:34

lrcvs wrote:I understand means well.
But I do not understand is the concept of fourth, fifth, sixth, .... dimension.

For example: Z = [5 4 3 2]
mathematically speaking it would be 5 * 4 * 3 * 2 = 120 elements.
But 5 * 4 = X * Y = level 1 = 20 elements in each level.
20 * 3 = 20 * Z is the number of 3D or level 3 = 60 items.
So far, so good.

Now we have in the 4D = 2.
I understand that at level 3, the last layer, and each element has an extension of 2 more items of the fourth dimension.
So I think 20 elements of the last level * 2 extensions more = 40 elements.
I still think if 3D = 60 items + 40 items in the fouth dimension = 100 items.
No item that is the 120 mathematically speaking.

What is your concept of the following dimensions to 3D?
(the fourth, fifth, sixth ... dimension)
Your opinion?

Z = [2]
One block of 2 elements
[2]

Z = [3 2]
One block of (3 blocks of 2 elements)
([2][2][2])

Z = [4 3 2]
One block of (4 blocks of (3 blocks of 2 elements))
{ ([2][2][2]) ([2][2][2]) ([2][2][2]) ([2][2][2]) }

Z = [5 4 3 2]
One block of (5 blocks of (4 blocks of (3 blocks of 2 elements)))
| { ([2][2][2]) ([2][2][2]) ([2][2][2]) ([2][2][2]) } { ([2][2][2]) ([2][2][2]) ([2][2][2]) ([2][2][2]) } { ([2][2][2]) ([2][2][2]) ([2][2][2]) ([2][2][2]) } { ([2][2][2]) ([2][2][2]) ([2][2][2]) ([2][2][2]) } { ([2][2][2]) ([2][2][2]) ([2][2][2]) ([2][2][2]) } |

And so on...
Richard
Posts: 2958
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Position item in an array larger than 3D

Postby Richard » May 05, 2012 10:16

@ Ircvs.

I consider the 1'st dimension to be a simple list.
The 2'nd dimension is a table with rows and columns on a single sheet of paper.
The 3'rd dimension is a stack of sheets, maybe bound as a book.
The 4'th dimensions is a row of books.
The 5'th dimension is the shelves of a bookshelf.
The 6'th is a row of book shelves.
The 7'th is one floor of the library.
The 8'th is the library building.
The 9'th is the libraries in the region.
The 10'th is all the libraries in the country.
The 11'th is all the libraries in all the countries on the Earth.
The 12'th is planets.
The 13'th solar systems
The 14'th galaxies......

The size of the array will be the product of the number of elements in every dimension.
For 20 dimensions with a minimum of two elements each, say 0 and 1, we have 2^20 elements total = 1 million.
lrcvs
Posts: 569
Joined: Mar 06, 2008 19:27
Location: Spain

Re: Position item in an array larger than 3D

Postby lrcvs » May 05, 2012 12:42

Hey, hey, hey, STOP.

I have found my error in 4D.

It is a error of conception dimendional.

Well, let's let W = (5 * 4 * 3 * 2) = 120 elements, Ok!
w = (x * y * z * "D")
x = 1D
y = x * y = 2D
z = x * y * z = 3D
D = number of times that repeats the block x, y, z

Let's see:
w = (5 * 4 * 3 * 2) = 120
We see that "2" is the number of times
Now comes my error of conception,
I have assumed that 4D was an extension of 3D, that was my error of conception.

As we see from my calculation / deduction wrong in my post:
:::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::
We have now in the 4D = 2.
I Understand That at level 3, the last layer,
and has an element Each extension of 2 more items of the fourth dimension.
So I think 20 elements of the last level * 2 = 40 elements more extensions.
I still think if 3D = 60 items + 40 items in the fouth dimension = 100 items.
That item is not the 120 Mathematically speaking.

This is a ERROR!!!
:::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::

... Then the fourth dimension (4D) are the times you repeat the block: x, y, z.

Now, by extension ...
we know what they mean the fifth, sixth, seventh, eighth,..... dimensions:

An example:
w = (2 * 2 * 2 * 2 * 2) = (a b c d e)
a * b * c = 2 * 2 * 2 = 8 = 3D starting block

8 * d (d as we have said is the number of times to repeat xyz) then 8 * 2 = 16 elements to the fourth dimension

we ...

16 * e ("e" also indicates the number of times to repeat the previous block, xyz + d)
16 * 2 = 32 total elements in W (2 * 2 * 2 * 2 * 2)


I believe that I have understood the concept of multidimensional array.

Thank you all for your interest in explaining the concepts of multidimensional array.
lrcvs
Posts: 569
Joined: Mar 06, 2008 19:27
Location: Spain

Re: Position item in an array larger than 3D

Postby lrcvs » May 05, 2012 14:44

Code: Select all


Going back to question the origin of this topic:

 "Calculation of the position of an element within an array multidemensional"

 Once I understood the concept of multidimensional array and the dimensions, the solution is relatively simple.

 1 º) We calculate the position of the item you want to search in the first block 3D.

 2 º) Multiply the number of dimensions - 1, the total of the items of the main block and
 add it to the position of the element in the first block.

 Example:

 Starting block (Array) W = (3 * 3 * 3 * 3) = 81 elements.

 Position the item to find "X" = (3 * 1 * 3 * 3)

 W = (Row Col Level Dimension) = (R C L D)

 Element to look for (f g h i)

 f = row, col = g, h = level, i = dimension

 1 °) We seek the position of the element "X" in the first 3D block

 ((f-1) * c) + ((f * c) * h - 1) + g
 ((3 - 1) * 3) + ((3 * 3) * (3 - 1)) + 1
 6 + 18 + 1 = 25 = position of the first block dento 3D, Ok

 2 º) Now multiply the first block * the number of dimensions - 1
 (3 * 3 * 3) * (3 - 1)
 27 * 2 = 54

 3 º) Now add 18 to the starting position and ...
 we have found the final position of the element "X" (3 1 3 3) = 54 + 25 = "79" position = "x" element

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Another example:

 w = (7 * 6 * 5 * 4 * 3) = 2520 elements

 As discussed in the previous post, the first block (xyz) is: 7 * 6 * 5
 the dimensions would be: 4 * 3
 then 4 * 3 means we have to multiply the first block (xyz) * 4
 and then multiply by 3 = ((x * y * z) * 4) * 3

 The element to look for "X" is at position "X" (6 * 5 * 4 * 3 * 2)

 In that position is "X" in the array "W"?


 W = (7 6 5 4 3) = (R C L D1 D2)

 X = (6 5 4 3 2) = (J K L M N)


 1 °) We seek the position of the element "X" in the first 3D block

 ((j-1) * c) + ((r * c) * l - 1) + k

 ((6 - 1) * 6) + ((7 * 6) * (4 - 1)) + 5
 30 + 126 + 5 = 161 Position on the starting block

 2 º) Multiply the number of dimensions (D1) - 1, the total of the items of the main block and
 add it to the position of the element in the first block and then the result is multiplied by "D2"

 (7 * 6 * 5) * 3 = 630

 Now add 630 + 161 = 791 = "1D"

 3 º) Multiply 791 * "D2" = 791 * 3 = 2373 = position of the element "X" in the array "w" of 2520 items
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

I think we have the method, now makes possible is a formula to simplify the calculations.

 regards
fxm
Posts: 9253
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Position item in an array larger than 3D

Postby fxm » May 05, 2012 16:20

Sorry, but I do not agree with your offset formulas!

- Offset formula for your 4D array element and verification:

Code: Select all

Dim Shared As Integer U4,L4,U3,L3,U2,L2,U1,L1  'dim array(L4 To U4,L3 To U3,L2 To U2,L1 To U1)
Dim Shared As Integer N4,N3,N2,N1              'array(N4,N3,N2,N1)

Function offset4() As Integer
  Return (((N4-L4)*(U3-L3+1)+N3-L3)*(U2-L2+1)+N2-L2)*(U1-L1+1)+N1-L1
End Function



U4 = 3 : L4 = 1
U3 = 3 : L3 = 1
U2 = 3 : L2 = 1
U1 = 3 : L1 = 1

N4 = 3
N3 = 1
N2 = 3
N1 = 3

Print Offset4()
Print
Dim As Integer array(L4 To U4,L3 To U3,L2 To U2,L1 To U1)
Print "verification"
Print @array(N4,N3,N2,N1)-@array(L4,L3,L2,L1)

Sleep

Code: Select all

 62

verification
62

- Offset formula for your 5D array element and verification:

Code: Select all

Dim Shared As Integer U5,L5,U4,L4,U3,L3,U2,L2,U1,L1  'dim array(L5 To U5,L4 To U4,L3 To U3,L2 To U2,L1 To U1)
Dim Shared As Integer N5,N4,N3,N2,N1                 'array(N5,N4,N3,N2,N1)

Function offset5() As Integer
  Return ((((N5-L5)*(U4-L4+1)+N4-L4)*(U3-L3+1)+N3-L3)*(U2-L2+1)+N2-L2)*(U1-L1+1)+N1-L1
End Function



U5 = 7 : L5 = 1
U4 = 6 : L4 = 1
U3 = 5 : L3 = 1
U2 = 4 : L2 = 1
U1 = 3 : L1 = 1

N5 = 6
N4 = 5
N3 = 4
N2 = 3
N1 = 2

Print Offset5()
Print
Dim As Integer array(L5 To U5,L4 To U4,L3 To U3,L2 To U2,L1 To U1)
Print "verification"
Print @array(N5,N4,N3,N2,N1)-@array(L5,L4,L3,L2,L1)

Sleep

Code: Select all

 2083

verification
2083

and so on...
lrcvs
Posts: 569
Joined: Mar 06, 2008 19:27
Location: Spain

Re: Position item in an array larger than 3D

Postby lrcvs » May 05, 2012 17:42

@ fxm:

I think, unless planning mistake that my example # 1, this right!

Let's do as they would our ancestors:

We have 3 blocks of 3 * 3 * 3 = (3 3 3 3) dimensions... and each block contains 27 items, ok!

Now consider that there are 2 blocks that do not use it, and each having 27 elements, then ... 27 * 2 = 54 elements.

Now calculate the position 3 * 1 * 3 in the free block, this means that the searched item is found in row 3, column 1 and level 3.

1 2 3 block 1, level 1
4 5 6
7 8 9

10 11 12 block 1, level 2
13 14 15
16 17 18

19 20 21 block 1, level 3
22 23 24
25 <<<< position row = 3, col = 1, level = 3


While the position of the searched item is 25, Ok!

Then ... 54 + 25 = 79

Then 79, is the position (3 1 3 3) of the element "X" within the array Z (3 3 3 3)

I think it's fine.!

Also, I think the example # 2.
fxm
Posts: 9253
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Position item in an array larger than 3D

Postby fxm » May 05, 2012 18:44

No, I disagree!
The aim is to find a formula or a counting method which verifies the compiler mapping.

My two formulas (for 4D and 5D) verify the array mapping of compiler.
I think that you are not coherent with the compiler array storing in memory.
The array elements are stored in memory by increasing indexes from the right to the left (as naturally increasing numbers formed with the indexes taken in the same order)


IMHO (with your notations):
Array(B,L,R,C)
C: column
R: raw
L: level
B: block

Example:
Array(3,3,3,3)
element(3,1,3,3)

We have 3 blocks of 3 * 3 * 3 = (3 3 3 3) dimensions... and each block (L,R,C) contains 27 items, ok!
Now consider that there are 2 blocks that do not use it, and each having 27 elements, then ... 27 * 2 = 54 elements.
Now calculate the position 1 * 3 * 3 (L,R,C) in the free block, this means that the searched item is found in column 3, row 3 and level 1.

1 2 3 block 1, level 1
4 5 6
7 8 9<<<< position col = 3, row = 3, level = 1

10 11 12 block 1, level 2
13 14 15
16 17 18

19 20 21 block 1, level 3
22 23 24
25 26 27

While the position of the searched item is 9, Ok!
Then ... 54 + 9 = 63

Then 63, is the position (3 1 3 3) of the element "X" within the array Z (3 3 3 3) => offset = 63-1 = 62
lrcvs
Posts: 569
Joined: Mar 06, 2008 19:27
Location: Spain

Re: Position item in an array larger than 3D

Postby lrcvs » May 05, 2012 19:18

@ fxm:

1 º) The objective is to find the formula / mathematical method to find the position of an element in a multidimensional array, 2D,3D,4D...

2 º) The result of a program must match a model.

Here is an array of 4D = W (3 3 3 3)

The question is: Find where position of item is the (3 1 3 3) of array W.

Well, my method of counting or formula are safe.

I think your concept is a error with what each element within an array.

examples:
z (10) = all rows or all columns
z (10,10) = 10 rows x 10 columns "always" = 1 rows x 2 cols
z (10,10,10) = 10 rows x 10 columns x 10 levels "always" = 1 rows x 2 cols x 3 levels
z (10, 10, 10, X) = 10 rows x 10 columns x 10 level "X" dimensions "always" = 1 rows x 2 cols x 3 levels x 4 dimensions


Then (3 1 3 3) = (a b c d)
a = 3 = rows
b = 1 = columns
c = 3 = levels
d = 3 = dimensions

The element is in the third block (3 d), level (3 c), column (1 b) and row (3 a).

I think you, are wrong concept.!

Always read first rows (a), after columns (b) and then levels (c) and finally the dimensions (d).

My formula or method and example are correct.!

Return to “General”

Who is online

Users browsing this forum: Google [Bot] and 4 guests