Position item in an array larger than 3D

General FreeBASIC programming questions.
fxm
Posts: 8957
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Position item in an array larger than 3D

Postby fxm » May 05, 2012 19:35

lrcvs wrote: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.!
It is your method but FreeBASIC does not work as that.
Extract of documentation about DIM
Multidimensional arrays ... are stored in this definite order: values differing only in the last index are contiguous (row-major order).

Compare the result of your formula/method relating to the array memory mapping by the FreeBASIC compiler.
Your result is not coherent with the FreeBASIC code execution, mine yes!

Code: Select all

Dim As Integer array(1 To 3, 1 To 3, 1 To 3, 1 To 3)
Print @array(3, 1, 3, 3) - @array(1, 1, 1, 1)
Sleep

Code: Select all

62
fxm
Posts: 8957
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Position item in an array larger than 3D

Postby fxm » May 05, 2012 20:18

Mapping of 'Dim As Integer array(1 To 3, 1 To 3, 1 To 3, 1 To 3)' in memory.
The value of each element is initialized to the number corresponding to the concatenation of its 4 indexes (in decimal base).

Code: Select all

Dim As Integer array(1 To 3, 1 To 3, 1 To 3, 1 To 3)
Dim As Integer Ptr p = @array(1, 1, 1, 1)

For I As Integer = 1 To 3
  For J As Integer = 1 To 3
    For K As Integer = 1 To 3
      For L As Integer = 1 To 3
        array(I, J, K, L) = I * 1000 + J * 100 + K * 10 + L
      Next L
    Next K
  Next J
Next I

For I As Integer = 0 to 3 * 3 * 3 * 3 - 1
  If I Mod 10 = 0 Then
    Print
  End If
  Print p[I];
Next I
Print
Sleep

Code: Select all

1111 1112 1113 1121 1122 1123 1131 1132 1133 1211
1212 1213 1221 1222 1223 1231 1232 1233 1311 1312
1313 1321 1322 1323 1331 1332 1333 2111 2112 2113
2121 2122 2123 2131 2132 2133 2211 2212 2213 2221
2222 2223 2231 2232 2233 2311 2312 2313 2321 2322
2323 2331 2332 2333 3111 3112 3113 3121 3122 3123
3131 3132 3133 3211 3212 3213 3221 3222 3223 3231
3232 3233 3311 3312 3313 3321 3322 3323 3331 3332
3333
81 elements
element '3133' is at position 63 => offset = 62
fxm
Posts: 8957
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Position item in an array larger than 3D

Postby fxm » May 05, 2012 21:11

Mapping of 'array(1 To 7, 1 To 6, 1 To 5, 1 To 4, 1 To 3)' in memory.
The value of each element is initialized to the number corresponding to the concatenation of its 5 indexes (in decimal base).

Code: Select all

Dim As Integer array(1 To 7, 1 To 6, 1 To 5, 1 To 4, 1 To 3)
Dim As Integer Ptr p = @array(1, 1, 1, 1, 1)

For I As Integer = 1 To 7
  For J As Integer = 1 To 6
    For K As Integer = 1 To 5
      For L As Integer = 1 To 4
        For M As Integer = 1 To 3
          array(I, J, K, L, M) = I * 10000 + J * 1000 + K * 100 + L * 10 + M
        Next M
      Next L
    Next K
  Next J
Next I

For I As Integer = 0 to 7 * 6 * 5 * 4 * 3 - 1
  If I mod 10 = 0 Then
    If I Mod 200 = 0 and I > 0 Then
      Print
      Print "Any key to continue..."
      Sleep
    End If
    Print
    Print Using "(####)"; I;
  End If
  Print p[I];
Next I
Print
Print "End"
Sleep

Code: Select all

(1990) 64142 64143 64211 64212 64213 64221 64222 64223 64231 64232
Any key to continue...

(2000) 64233 64241 64242 64243 64311 64312 64313 64321 64322 64323
(2010) 64331 64332 64333 64341 64342 64343 64411 64412 64413 64421
(2020) 64422 64423 64431 64432 64433 64441 64442 64443 64511 64512
(2030) 64513 64521 64522 64523 64531 64532 64533 64541 64542 64543
(2040) 65111 65112 65113 65121 65122 65123 65131 65132 65133 65141
(2050) 65142 65143 65211 65212 65213 65221 65222 65223 65231 65232
(2060) 65233 65241 65242 65243 65311 65312 65313 65321 65322 65323
(2070) 65331 65332 65333 65341 65342 65343 65411 65412 65413 65421
(2080) 65422 65423 65431 65432 65433 65441 65442 65443 65511 65512
(2090) 65513 65521 65522 65523 65531 65532 65533 65541 65542 65543
(2100) 66111 66112 66113 66121 66122 66123 66131 66132 66133 66141
(2110) 66142 66143 66211 66212 66213 66221 66222 66223 66231 66232
(2120) 66233 66241 66242 66243 66311 66312 66313 66321 66322 66323
(2130) 66331 66332 66333 66341 66342 66343 66411 66412 66413 66421
(2140) 66422 66423 66431 66432 66433 66441 66442 66443 66511 66512
(2150) 66513 66521 66522 66523 66531 66532 66533 66541 66542 66543
(2160) 71111 71112 71113 71121 71122 71123 71131 71132 71133 71141
(2170) 71142 71143 71211 71212 71213 71221 71222 71223 71231 71232
(2180) 71233 71241 71242 71243 71311 71312 71313 71321 71322 71323
(2190) 71331 71332 71333 71341 71342 71343 71411 71412 71413 71421
Any key to continue...
We can verify that element '65432' is at offset 2083
dodicat
Posts: 5757
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Position item in an array larger than 3D

Postby dodicat » May 05, 2012 21:30

Not withstanding bloomers and in lieu of everything, you now have another formula Ircvs.

Code: Select all

 

#define offset(array,position,from,lowerbounds) @array position -@array lowerbounds

Dim As Integer array(1 To 3, 1 To 3, 1 To 3, 1 To 3)
print offset(array,(3,1,3,3),from,(1,1,1,1))
sleep

 


Also, I prefer to visualize array dimensions as a homing in process.
Eventually, like a function, returning one value.
assign the value in the inner loop.
EXAMPLE:
array(earth,boisphere,anamalia,mammalia,rodentia,muriodea,murinae,mus,house mouse) = cheese.
lrcvs
Posts: 567
Joined: Mar 06, 2008 19:27
Location: Spain

Re: Position item in an array larger than 3D

Postby lrcvs » May 05, 2012 22:03

Friends, here my program and formula
:::::::::::::::::::::::::::::::::::::::::::::::


Code: Select all

Dim As Integer a,b,c,d,j,k,l,m,n,p,r,s,t,u, x

Cls
'create array 4D
j = 3
k = 3
l = 3
m = 3

Dim w (j,k,l,m) As Integer

'create array 1D
Dim v (3*3*3*3) As Integer

'position to look of "X"
a = 3
b = 1
c = 3
d = 3

p = 0
For r = 1 To j
   For s = 1 To k
      For t = 1 To l
         For u = 1 To m
            p = p + 1
         w (r,s,t,u) = p 'into W 4D array
         v (p) = p ' into v 1D array         
         Next
      Next
   Next
Next

'here show the item "x" to look

p = 1
For r = 1 To j
   For s = 1 To k
      For t = 1 To l
         For u = 1 To m
           If r = d  And s = c And t = a And u = b Then Print "Position 4D = ";w (d,c,a,u)
         p = p + 1   
         Next u
      Next t
   Next s
Next r

' calculate position with formula (((a - 1) * c) + ((f * c) * (c - 1)) + b) + ((j*k*l) *(m - 1))
x = (((a - 1) * k) + ((j * k) * (c - 1)) + b) + ((j*k*l)*(m - 1))
Print "Position of X with formula = ";x
Print "Position 1D = ";v (x)

Sleep
End

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Image in Screen

Position 4D = 79
Position of X with formula = 79
Position 1D = 79
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Greetings
dodicat
Posts: 5757
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Position item in an array larger than 3D

Postby dodicat » May 05, 2012 22:42

Your formula is nearly correct, but I've corrected it.
Also, position4d is surely 3,1,3,3.
So I've made it that.
Also you must include p at value 0, so I've started to increment p AFTER it has passed through 0.
Also, to be strictly correct, w should be 1 to 3, not 0 to 3, but I've left that as 0 is not included in the loops.
I've changed c to b in your formula and added 1 to compensate for the altered p increment.

Code: Select all

Dim As Integer a,b,c,d,j,k,l,m,n,p,r,s,t,u, x

Cls
'create array 4D
j = 3
k = 3
l = 3
m = 3

Dim w (j,k,l,m) As Integer

'create array 1D
Dim v (3*3*3*3) As Integer

'position to look of "X"
a = 3
b = 1
c = 3
d = 3

p = 0
For r = 1 To j
For s = 1 To k
For t = 1 To l
For u = 1 To m
'p = p + 1
w (r,s,t,u) = p 'into W 4D array
v (p) = p ' into v 1D array
p=p+1

Next
Next
Next
Next

'here show the item "x" to look

p = 1
For r = 1 To j
For s = 1 To k
For t = 1 To l
For u = 1 To m
If r = a And s = b And t = c And u = d Then Print "Position 4D = ";w (r,s,t,u)
p = p + 1
Next u
Next t
Next s
Next r

' calculate position with formula (((a - 1) * c) + ((f * c) * (c - 1)) + b) + ((j*k*l) *(m - 1))
x = (((a - 1) * k) + ((j * k) * (b - 1)) + b) + ((j*k*l)*(m - 1))+1
Print "Position of X with formula = ";x
Print "Position 1D = ";v (x)

Sleep
End
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'Image in Screen

'Position 4D = 62
'Position of X with formula = 62
'Position 1D = 62
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

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

Re: Position item in an array larger than 3D

Postby fxm » May 05, 2012 22:44

Yes, but I thought that the aim of this topic was to take as exact model the FreeBASIC compiler mapping in memory for multidimensional array!
Your model is other (indexes not taken in the same order for memory mapping).

Modifying 2 lines in your program, we recover the FreeBASIC compiler mapping:

Code: Select all

Dim As Integer a,b,c,d,j,k,l,m,n,p,r,s,t,u, x

Cls
'create array 4D
j = 3
k = 3
l = 3
m = 3

Dim w (j,k,l,m) As Integer

'create array 1D
Dim v (3*3*3*3) As Integer

'position to look of "X"
a = 3
b = 1
c = 3
d = 3

p = 0
For r = 1 To j
For s = 1 To k
For t = 1 To l
For u = 1 To m
p = p + 1
w (r,s,t,u) = p 'into W 4D array
v (p) = p ' into v 1D array
Next
Next
Next
Next

'here show the item "x" to look

p = 1
For r = 1 To j
For s = 1 To k
For t = 1 To l
For u = 1 To m
'If r = d And s = c And t = a And u = b Then Print "Position 4D = ";w (d,c,a,u)
If r = a And s = b And t = c And u = d Then Print "Position 4D = ";w (r,s,t,u) 'compatibility with FreeBASIC compiler
p = p + 1
Next u
Next t
Next s
Next r

' calculate position with formula (((a - 1) * c) + ((f * c) * (c - 1)) + b) + ((j*k*l) *(m - 1))
'x = (((a - 1) * k) + ((j * k) * (c - 1)) + b) + ((j*k*l)*(m - 1))
x = (((c - 1) * k) + ((j * k) * (b - 1)) + d) + ((a - 1)*(k*l*m)) 'compatibility with FreeBASIC compiler
Print "Position of X with formula = ";x
Print "Position 1D = ";v (x)

Sleep
End
You toke the order d, c, a, b
when FreeBASIC compiler takes the order a, b, c, d

Remark:
It would more beautiful to write the formula with this terms order + error typo:
' calculate position with formula (((f * c) * (c - 1)) + (c * (a - 1)) + b) + ((j*k*l) *(m - 1))
'x = (((j * k) * (c - 1)) + (k * (a - 1)) + b) + ((j*k*l)*(d - 1))
x = (((j * k) * (b - 1)) + (k * (c - 1)) + d) + ((a - 1)*(k*l*m)) 'compatibility with FreeBASIC compiler

{Edit]
Correction of typo in the last formula.
Last edited by fxm on May 06, 2012 5:04, edited 2 times in total.
dodicat
Posts: 5757
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Position item in an array larger than 3D

Postby dodicat » May 06, 2012 0:37

I't getting late now.
I assume that the freebasic mapping is from 0 to total number of elements-1, as used by the array pointer.
If it is then here would be my formula.

Code: Select all

dim as integer n,o,p,q
n=1
o=1
p=1
q=1
dim as integer j,k,l,m
j=3
k=3
l=3
m=3
dim as integer a,b,c,d
a=3
b=1
c=3
d=3
dim as integer array(n To j, o To k, p To l, q To m)

dim as integer X= (m-q+1)*((l-p+1)*((k-o+1)*(a-n)+(b-o))+(c-p))+(d-q)
print X
sleep

 

If not, then as I said, it's getting late.
lrcvs
Posts: 567
Joined: Mar 06, 2008 19:27
Location: Spain

Re: Position item in an array larger than 3D

Postby lrcvs » May 06, 2012 4:29

Well, now there are multiple questions:

So that applications can use?

As a mathematical function, you can apply to any programming language and mathematical, for doing so, make it known!

The first advantage I see is that with this formula / method, we save our beloved loops "For / next", "do / until", "while / do" ...., in any process where we have to find an element within an array.

Second advantage is time saved in the process of searching for an element in any array, then make a function that works directly with this mathematical process.!

As noted, a humble seed (question) can be born a great tree (applications).

Now you, are the technicians / specialists, develop it and make it known, for without your help this would not have been possible.!

My knowledge is very limited in this field.!

You can call it formula / method "XKrouhn" is one of my alias on the network.

Now I retire to continue experimenting / playing with new ideas.

Notify what ye do.!

Greetings from Spain.

My sentence: "Here a friend"
fxm
Posts: 8957
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Position item in an array larger than 3D

Postby fxm » May 06, 2012 5:58

I remind you my two mathematical functions (compatible with FreeBASIC) already mentioned in a previous post:


For 4D array
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


For 5D array
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
fxm
Posts: 8957
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Position item in an array larger than 3D

Postby fxm » May 06, 2012 6:08

Then the next mathematical function for 6D array (always compatibility verified with FreeBASIC)
Dim Shared As Integer U6,L6,U5,L5,U4,L4,U3,L3,U2,L2,U1,L1 'dim array(L6 To U6,L5 To U5,L4 To U4,L3 To U3,L2 To U2,L1 To U1)
Dim Shared As Integer N6,N5,N4,N3,N2,N1 'array(N6,N5,N4,N3,N2,N1)

Function offset6() As Integer
Return (((((N6-L6)*(U5-L5+1)+N5-L5)*(U4-L4+1)+N4-L4)*(U3-L3+1)+N3-L3)*(U2-L2+1)+N2-L2)*(U1-L1+1)+N1-L1
End Function
'verified with: @array(N6,N5,N4,N3,N2,N1)-@array(L6,L5,L4,L3,L2,L1)


At the end, the mathematical function for 7D array (always compatibility verified with FreeBASIC)
Dim Shared As Integer U7,L7,U6,L6,U5,L5,U4,L4,U3,L3,U2,L2,U1,L1 'dim array(L7 To U7,L6 To U6,L5 To U5,L4 To U4,L3 To U3,L2 To U2,L1 To U1)
Dim Shared As Integer N7,N6,N5,N4,N3,N2,N1 'array(N7,N6,N5,N4,N3,N2,N1)

Function offset7() As Integer
Return ((((((N7-L7)*(U6-L6+1)+N6-L6)*(U5-L5+1)+N5-L5)*(U4-L4+1)+N4-L4)*(U3-L3+1)+N3-L3)*(U2-L2+1)+N2-L2)*(U1-L1+1)+N1-L1
End Function
'verified with: @array(N7,N6,N5,N4,N3,N2,N1)-@array(L7,L6,L5,L4,L3,L2,L1)


and so on...
Richard
Posts: 2926
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Position item in an array larger than 3D

Postby Richard » May 06, 2012 12:52

FB uses a faster solution by pre-computing the spans and a lower bound correction that can be combined with the array base address.
This makes the address equation simpler to write, and faster to evaluate.

Code: Select all

'=======================================================================
' Faster and Simpler, this example is for a 4D array
'=======================================================================
Dim Shared As Integer L4=1, L3=2, L2=1, L1=0    ' lower bounds
Dim Shared As Integer u4=2, u3=3, u2=2, u1=1    ' upper bounds

Dim Shared As Integer s4, s3, s2, s1 ' dimension spans, sn = un - Ln + 1
Dim Shared As Integer k = 0 ' lower bounds adjustment, to be precomputed

'-----------------------------------------------------------------------
' this is the simpler and easily expanded position equation
Function dim_4(_
    Byval i4 As Integer, Byval i3 As Integer,_
    Byval i2 As Integer, Byval i1 As Integer) As Integer
    Return ((i4  *s3+i3)  *s2+i2)  *s1+i1  +k
End Function

'-----------------------------------------------------------------------
' precompute once the array descriptor parameters
s4 = u4 - L4 + 1
s3 = u3 - L3 + 1
s2 = u2 - L2 + 1
s1 = u1 - L1 + 1
k = - dim_4( L4, L3, L2, L1 )
Print "k ="; k

'-----------------------------------------------------------------------
' test the code
Dim As Integer a( L4 To u4, L3 To u3, L2 To u2, L1 To u1 )  ' an array
Dim As Integer i4, i3, i2, i1                   ' array indexes
Dim As Integer Ptr ba = @a( L4, L3, L2, L1 )    ' base address
Print "ba ="; ba
Print

Dim As String f = "###  ###  ###  ###   ########   ######## "
Print             " i4   i3   i2   i1    FBarray    dim_4() "

For i4 = L4 To u4
    For i3 = L3 To u3
        For i2 = L2 To u2
            For i1 = L1 To u1
                Print Using f; i4; i3; i2; i1; @a(i4, i3, i2, i1)-ba; dim_4(i4, i3, i2, i1)
            Next i1
        Next i2
    Next i3
Next i4

'=======================================================================
Sleep
'=======================================================================

And here is a trivial equation generator for any number of dimensions...

Code: Select all

Dim As Integer n, i
For n = 1 To 10
    Dim As String eqn = "i" + Str(n)
    For i = n-1 To 1 Step -1
        eqn = "(" + eqn + "*s" + Str(i) + "+i" + Str(i) + ")"
    Next i
    Print "Return " + eqn + "+k"
Next n

This is the example output...
Return i1+k
Return (i2*s1+i1)+k
Return ((i3*s2+i2)*s1+i1)+k
Return (((i4*s3+i3)*s2+i2)*s1+i1)+k
Return ((((i5*s4+i4)*s3+i3)*s2+i2)*s1+i1)+k
Return (((((i6*s5+i5)*s4+i4)*s3+i3)*s2+i2)*s1+i1)+k
Return ((((((i7*s6+i6)*s5+i5)*s4+i4)*s3+i3)*s2+i2)*s1+i1)+k
Return (((((((i8*s7+i7)*s6+i6)*s5+i5)*s4+i4)*s3+i3)*s2+i2)*s1+i1)+k
Return ((((((((i9*s8+i8)*s7+i7)*s6+i6)*s5+i5)*s4+i4)*s3+i3)*s2+i2)*s1+i1)+k
Return (((((((((i10*s9+i9)*s8+i8)*s7+i7)*s6+i6)*s5+i5)*s4+i4)*s3+i3)*s2+i2)*s1+i1)+k
lrcvs
Posts: 567
Joined: Mar 06, 2008 19:27
Location: Spain

Re: Position item in an array larger than 3D

Postby lrcvs » May 06, 2012 17:19

Sorry I can not continue with this subject, because as I said in my previous post, "my skills are limited in this field"

Greetings and thanks to all.!!!
fxm
Posts: 8957
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Position item in an array larger than 3D

Postby fxm » May 06, 2012 19:06

To me also conclude, I applied the improvement method of Richard to my 'Offset formula for your 4D array element and verification' previous code at my post viewtopic.php?p=173831#p173831.

Obviously, I found the same formula.
The variable 'k', that Richard calls 'lower bounds adjustment', represents the offset of the reel or virtual element (0,0,0,0), which is valid or not valid depending on the array bounds.

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)
Dim Shared As Integer S4,S3,S2,S1              'size of each dimension
Dim Shared As Integer K                        'offset of pseudo array(0,0,0,0)

Function offset4() As Integer
  Return ((N4*S3+N3)*S2+N2)*S1+N1+K
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

S4 = U4-L4+1
S3 = U3-L3+1
S2 = U2-L2+1
S1 = U1-L1+1
K = -(((L4*S3+L3)*S2+L2)*S1+L1)

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
dodicat
Posts: 5757
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Position item in an array larger than 3D

Postby dodicat » May 06, 2012 21:31

I'll conclude also, it has been an interesting topic.
But I'll go out with a blaze, all you guys have been beautifying the final equation, my final macros are all but beautifull maths.
file1
This writes the bas file for the macros up to degree 20:

Code: Select all

dim as integer dimensions=20

open "offset.bas" for output as #1

print #1,"#define ub ubound"
print #1,"#define lb lbound"
print #1,"#define _r(arr,n) (ub(arr,n)-lb(arr,n)+1)"

print #1,"#define d1(a,n1) (n1-lb(a,1))"
print #1,"#define d2(a,n1,n2) (_r(a,2)*d1(a,n1)+(n2-lb(a,2)))"
dim as string temp,params,_r,endbit
for z as integer=3 to dimensions
    temp=temp+"n"+str(z)+","
    params=str(z)+"(a,n1,n2,"+temp
    params=rtrim(params,",")+")"
    _r=" (_r(a,"+str(z)+")*d"+str(z-1)+"(a,"
    endbit="+(n"+str(z)+"-lb(a,"+str(z)+")))"
    print #1,"#define d"+params+_r+mid(params,5,len(params)-8)+")"+endbit
next z

close #1

This is an example, the 20 dimensions are in recursive macros, the example is degree 8.
(I get an error of too many dimensions from the compiler if I go higher than eight)

Code: Select all

#define ub ubound
#define lb lbound
#define _r(arr,n) (ub(arr,n)-lb(arr,n)+1)
#define d1(a,n1) (n1-lb(a,1))
#define d2(a,n1,n2) (_r(a,2)*d1(a,n1)+(n2-lb(a,2)))
#define d3(a,n1,n2,n3) (_r(a,3)*d2(a,n1,n2)+(n3-lb(a,3)))
#define d4(a,n1,n2,n3,n4) (_r(a,4)*d3(a,n1,n2,n3)+(n4-lb(a,4)))
#define d5(a,n1,n2,n3,n4,n5) (_r(a,5)*d4(a,n1,n2,n3,n4)+(n5-lb(a,5)))
#define d6(a,n1,n2,n3,n4,n5,n6) (_r(a,6)*d5(a,n1,n2,n3,n4,n5)+(n6-lb(a,6)))
#define d7(a,n1,n2,n3,n4,n5,n6,n7) (_r(a,7)*d6(a,n1,n2,n3,n4,n5,n6)+(n7-lb(a,7)))
#define d8(a,n1,n2,n3,n4,n5,n6,n7,n8) (_r(a,8)*d7(a,n1,n2,n3,n4,n5,n6,n7)+(n8-lb(a,8)))
#define d9(a,n1,n2,n3,n4,n5,n6,n7,n8,n9) (_r(a,9)*d8(a,n1,n2,n3,n4,n5,n6,n7,n8)+(n9-lb(a,9)))
#define d10(a,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10) (_r(a,10)*d9(a,,n1,n2,n3,n4,n5,n6,n7,n8,n9,)+(n10-lb(a,10)))
#define d11(a,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11) (_r(a,11)*d10(a,,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,)+(n11-lb(a,11)))
#define d12(a,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12) (_r(a,12)*d11(a,,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,)+(n12-lb(a,12)))
#define d13(a,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13) (_r(a,13)*d12(a,,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,)+(n13-lb(a,13)))
#define d14(a,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14) (_r(a,14)*d13(a,,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,)+(n14-lb(a,14)))
#define d15(a,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,n15) (_r(a,15)*d14(a,,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,)+(n15-lb(a,15)))
#define d16(a,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,n15,n16) (_r(a,16)*d15(a,,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,n15,)+(n16-lb(a,16)))
#define d17(a,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,n15,n16,n17) (_r(a,17)*d16(a,,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,n15,n16,)+(n17-lb(a,17)))
#define d18(a,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,n15,n16,n17,n18) (_r(a,18)*d17(a,,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,n15,n16,n17,)+(n18-lb(a,18)))
#define d19(a,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,n15,n16,n17,n18,n19) (_r(a,19)*d18(a,,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,n15,n16,n17,n18,)+(n19-lb(a,19)))
#define d20(a,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,n15,n16,n17,n18,n19,n20) (_r(a,20)*d19(a,,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,n15,n16,n17,n18,n19,)+(n20-lb(a,20)))

dim shared as byte a(1 to 6,3 to 7,4 to 8,2 to 6,3 to 7,4 to 8,2 to 5,4 to 9)
print  d8(a,2,4,5,3,5,5,5,6)
print    @a(2,4,5,3,5,5,5,6)-@a(1,3,4,2,3,4,2,4)
sleep 

Return to “General”

Who is online

Users browsing this forum: No registered users and 4 guests