Shop OBEX P1 Docs P2 Docs Learn Events
Multi dimensional arrays in spin — Parallax Forums

Multi dimensional arrays in spin

John_BJohn_B Posts: 16
edited 2009-07-29 17:27 in Propeller 1
Hi,
Is it possible to have an array of arrays in spin?

Say 3 sensors with 5 parameters each.
I have tried creating a var:

long sensor[noparse][[/noparse]3][noparse][[/noparse]5]

and accessing with:

sensor[noparse][[/noparse]ch1][noparse][[/noparse]param1] := 40

with no luck.

If its possible what is the correct syntax; any examples?
If its not possible what is the best way of creating and accessing multi dimensional arrays in spin.

Cheers
John

Comments

  • Bill HenningBill Henning Posts: 6,445
    edited 2009-07-28 16:36
    bst has multi-dimensional arrays

    see http://forums.parallax.com/showthread.php?p=754811
    John_B said...
    Hi,
    Is it possible to have an array of arrays in spin?



    Say 3 sensors with 5 parameters each.

    I have tried creating a var:



    long sensor



    and accessing with:



    sensor[noparse][[/noparse]ch1][noparse][[/noparse]param1] := 40



    with no luck.



    If its possible what is the correct syntax; any examples?

    If its not possible what is the best way of creating and accessing multi dimensional arrays in spin.



    Cheers

    John
    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.mikronauts.com - my site 6.250MHz custom Crystals for running Propellers at 100MHz
    Las - Large model assembler for the Propeller Largos - a feature full nano operating system for the Propeller
    Morpheus & Mem+ Advanced dual Propeller SBC with XMM and 256 Color VGA
    Please use mikronauts _at_ gmail _dot_ com to contact me off-forum, my PM is almost totally full
  • KyeKye Posts: 2,200
    edited 2009-07-28 17:03
    Make a singletone array and acess it like a multi dimensional arrary.

    address := (row * ARRAY_COLUMN_LENGTH) + column

    Bascially each row is worth X number of columns.

    Simple.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • localrogerlocalroger Posts: 3,452
    edited 2009-07-28 19:04
    The C-like bracket notation adds, but does not multiply by row size. You should be able to something like sensor[noparse][[/noparse]ch1*numchans][noparse][[/noparse]param] := 40.
  • AleAle Posts: 2,363
    edited 2009-07-28 20:40
    The homespun compiler supports multidimentional arrays. Not officially supported by Parallax but it is an alternative.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Visit some of my articles at Propeller Wiki:
    MATH on the propeller propeller.wikispaces.com/MATH
    pPropQL: propeller.wikispaces.com/pPropQL
    pPropQL020: propeller.wikispaces.com/pPropQL020
    OMU for the pPropQL/020 propeller.wikispaces.com/OMU
  • cessnapilotcessnapilot Posts: 182
    edited 2009-07-28 21:11
    Hi John_B,

    Your code trials are· very similar to my first lines in SPIN.

    An array of arrays can be regarded as a matrix, or name it whatever you like. I·cut and paste·some·comment part of an obex object,

    http://obex.parallax.com/objects/317/·

    that might help you in the·indexing and addressing of 2-dimensional arrays.

    '
    "In the worlds of math, engineering and physics, it's the matrix that·
    separates the men from the boys, the· women from the girls."··········
    ·············································· (Jack W. Crenshaw).····
    ······································································
    ·A matrix is an array of numbers organized in rows and columns. We····
    usually give the row number first, then the column. So a [noparse][[/noparse]3-by-4]·····
    matrix has twelve numbers arranged in three rows where each row has a·
    length of four························································
    ······································································
    ························ +··············· ·+····························
    ························ | 1· 2· 3·· 4··· |····························
    ························ | 2· 3· 4·· 5··· |····························
    ························ | 3· 4· 5· 6.28 |····························
    ························ +················ +····························
    ······································································
    ·Since computer RAM is organized sequentially, as we access it with a·
    single number that we call address, we have to find a way to map the··
    two dimensions of the matrix onto the one-dimensional, sequential·····
    memory. In Propeller SPIN that is rather easy since we can use arrays.
    For the previous matrix we can declare an array of LONGs, e.g.········
    ······································································
    ························· VAR·· LONG mA[noparse][[/noparse]12]···························
    ······································································
    that is large enough to contain the "three times four" 32-bit IEEE 754
    float numbers of the· matrix. In SPIN language the indexing starts with
    zero, so the first row, first column element of this matrix is placed·
    in mA[noparse][[/noparse]0]. The second row, fourth column element is placed in mA[noparse][[/noparse]7]. The
    general convention that I used with the "FPU_Matrix_Driver.spin" object
    is that the ith row, jth column element is accessed at the index······
    ······································································
    ························ "mA[noparse][[/noparse]i,j]" = mA[noparse][[/noparse]index]························
    ······································································
    where·································································
    ······································································
    ···················· index = (i-1)*(#col) + (j-1)·····················
    ······································································
    and #col = 4 in this example. There are the 'Matrix_Put' and the······
    'Matrix_Get' procedures in the driver to aid the access to the elements
    of a matrix. In this example the second row, fourth column element of·
    mA can be set to 5.0 using············································
    ······································································
    ·········· OBJNAME.Matrix_Put(@mA, 5.0, 2, 4, #row, #col)·············

    Like in the previous example, the bunch of data in matrices is accessed
    by the driver using the starting HUB memory address of the data. For··
    example, after you declared mB and mC matrices to be the same [noparse][[/noparse]3-by-4]
    size as mA····························································
    ······································································
    ························· VAR·· LONG mB[noparse][[/noparse]12]···························
    ························· VAR·· LONG mC[noparse][[/noparse]12]···························
    ······································································
    you can add mB to mC and store the result in mA with the following····
    single procedure call·················································
    ······································································
    ·OBJNAME.Matrix_Add(@mA, @mB, @mC, 3, 4)···· (meaning mA := mB + mC)··
    ······································································
    You can't multiply mB with mC, of course, but you can multiply mB with
    the transpose of mC. To obtain this transpose use·····················
    ······································································
    ·OBJNAME.Matrix_Transpose(@mCT, @mC, 3, 4)·· (meaning mCT := Tr. of mC)
    ······································································
    mCT is a [noparse][[/noparse]4-by-3] matrix, which can be now multiplied from the left···
    with mB as····························································
    ······································································
    ·OBJNAME.Matrix_Multiply(@mD,@mB,@mCT,3,4,3) (meaning mD := mB * mCT)·
    ······································································
    where the result mD is a [noparse][[/noparse]3-by-3] matrix. This matrix algebra coding··
    convention can yield compact and easy to debug code. The following 8··
    lines of SPIN code (OBJNAME here is FPUMAT) were taken from the·······
    'FPU_ExtendedKF.spin' application and calculate the Kalman gain matrix
    from five other matrices (A, P, C, CT, Sz) at a snap··················
    ······································································
    ······ (··· Formula: K = A * P * CT * Inv[noparse][[/noparse]C * P * CT + Sz]·· )········
    ············································································
    ···· FPUMAT.Matrix_Transpose(@mCT, @mC, _R, _N)·······················
    ···· FPUMAT.Matrix_Multiply(@mAP, @mA, @mP, _N, _N, _N)···············
    ···· FPUMAT.Matrix_Multiply(@mAPCT, @mAP, @mCT, _N, _N, _R)···········
    ···· FPUMAT.Matrix_Multiply(@mCP, @mC, @mP, _R, _N, _N)···············
    ···· FPUMAT.Matrix_Multiply(@mCPCT, @mCP, @mCT, _R, _N, _R)···········
    ···· FPUMAT.Matrix_Add(@mCPCTSz, @mCPCT, @mSz, _R, _R)················
    ···· FPUMAT.Matrix_Invert(@mCPCTSzInv, @mCPCTSz, _R)···················
    ···· FPUMAT.Matrix_Multiply(@mK, @mAPCT, @mCPCTSzInv, _N, _R, _R)
    '
    ····

    ·Never mind floating point, this indexing and addressing works with LONGs, too.

    ·
    And a good advice. When your SPIN program uses 2-dimensional
    arrays of the same size very intesively, prepare an index array vector, e.g.

    VAR LONG row_base[noparse][[/noparse]#row]

    REPEAT i FROM 1 TO #row
    · row_base[noparse][[/noparse]i] := (i - 1) * #col

    and in the numerous calculation of 2-dimensinal indices you can use this vector to
    access data at matrix[noparse][[/noparse]i,j]

    data_index := row_base[noparse][[/noparse]i] + (j-1)

    avoiding a multiplication in each turn. That may speed up the code noticably.


    Cheers,

    Istvan·········

    Post Edited (cessnapilot) : 7/29/2009 5:23:01 AM GMT
  • BradCBradC Posts: 2,601
    edited 2009-07-29 00:33
    Bill Henning said...
    bst has multi-dimensional arrays

    see http://forums.parallax.com/showthread.php?p=754811

    No it doesn't. mpark built multi-dimension arrays into homespun. I have not replicated this feature.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <This space for rent>
  • John_BJohn_B Posts: 16
    edited 2009-07-29 11:11
    Thanks to everyone for all the info.
    John
  • Bill HenningBill Henning Posts: 6,445
    edited 2009-07-29 17:27
    My bad, I mixed up which spin compiler had that feature!
    BradC said...
    Bill Henning said...
    bst has multi-dimensional arrays

    see http://forums.parallax.com/showthread.php?p=754811

    No it doesn't. mpark built multi-dimension arrays into homespun. I have not replicated this feature.
    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.mikronauts.com - my site 6.250MHz custom Crystals for running Propellers at 100MHz
    Las - Large model assembler for the Propeller Largos - a feature full nano operating system for the Propeller
    Morpheus & Mem+ Advanced dual Propeller SBC with XMM and 256 Color VGA
    Please use mikronauts _at_ gmail _dot_ com to contact me off-forum, my PM is almost totally full
Sign In or Register to comment.